/******************************************************************** * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * * Copyright (C) Genome Research Limited * ********************************************************************/ package uk.ac.sanger.artemis.components.filetree; import uk.ac.sanger.artemis.components.SwingWorker; import uk.ac.sanger.artemis.components.database.DatabaseEntrySource; import uk.ac.sanger.artemis.components.database.DatabaseJPanel; import uk.ac.sanger.artemis.j2ssh.SshLogin; import uk.ac.sanger.artemis.j2ssh.SshFileManager; import uk.ac.sanger.artemis.util.StringVector; import uk.ac.sanger.artemis.Options; import javax.swing.table.TableColumn; import javax.swing.*; import java.io.File; import java.io.FileFilter; import java.awt.event.*; import java.awt.*; import java.util.Properties; import java.util.Enumeration; import java.util.Vector; import javax.swing.border.Border; public class LocalAndRemoteFileManager extends JFrame { /***/ private static final long serialVersionUID = 1L; private JScrollPane remoteTree; private SshJTreeTable sshtree; private JSplitPane treePane = null; private DatabaseEntrySource entry_source; public static JCheckBoxMenuItem lazyLoad = new JCheckBoxMenuItem("Lazy load feature data", false); private static JCheckBoxMenuItem automaticHistory = new JCheckBoxMenuItem("Automatic History Annotation", false); public static JCheckBoxMenuItem domainLoad = new JCheckBoxMenuItem("Display protein domains", false); public LocalAndRemoteFileManager(JFrame frame) { this(frame,getArtemisFilter()); } /** * * File Manager Frame * @param frame parent frame * @param filter file name filter * */ public LocalAndRemoteFileManager(JFrame frame, FileFilter filter) { super(); final JPanel localPanel = new JPanel(new BorderLayout()); final SshLogin ssh_login = new SshLogin(); JTreeTable ftree = new JTreeTable(new FileSystemModel(getLocalDirectories(), filter, this)); JScrollPane localTree = new JScrollPane(ftree); localTree.getViewport().setBackground(Color.white); localPanel.add(localTree,BorderLayout.CENTER); final JLabel local_status_line = getStatusLabel("LOCAL"); localPanel.add(local_status_line,BorderLayout.NORTH); final JPanel remotePanel = new JPanel(new BorderLayout()); // final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); final Dimension panelSize = new Dimension((int)(screen.getWidth()/3), (int)(screen.getHeight()/4)); String remote_name = ""; final JLabel remote_status_line = getStatusLabel(""); if(FileList.ssh_client == null) // if no connection etablished yet { final Box bdown = Box.createVerticalBox(); JButton connect = new JButton("Connect"); connect.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { login(remotePanel, bdown, ssh_login, panelSize, local_status_line, remote_status_line); } }); bdown.add(ssh_login.getLogin()); // listen to passwd field for return press JPasswordField pwf = ssh_login.getJPasswordField(); pwf.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { login(remotePanel, bdown, ssh_login, panelSize, local_status_line, remote_status_line); } }); bdown.add(connect); /*int ypos = panelSize.height-connect.getPreferredSize().height; if(ypos>0) bdown.add(Box.createVerticalStrut(ypos/2));*/ bdown.add(Box.createVerticalGlue()); remotePanel.add(bdown, BorderLayout.SOUTH); remotePanel.setPreferredSize(panelSize); } else { FileList flist = new FileList(); setRemoteTree(flist, sshtree, remoteTree, remotePanel, panelSize, remote_status_line); } remote_status_line.setText("REMOTE "+remote_name); remotePanel.add(remote_status_line,BorderLayout.NORTH); treePane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, localPanel,remotePanel); treePane.setOneTouchExpandable(true); JPanel pane = (JPanel)getContentPane(); pane.setLayout(new BorderLayout()); DbConnectionThread dbthread = null; if(System.getProperty("chado") != null) { setTitle("Database and File Manager"); entry_source = new DatabaseEntrySource(); boolean promptUser = true; if(System.getProperty("read_only") != null) { promptUser = false; entry_source.setReadOnly(true); } if(!entry_source.setLocation(promptUser)) return; JLabel label = new JLabel(" Database Loading..."); JPanel dbPane = new JPanel(); dbPane.add(label); dbPane.setBackground(Color.white); dbPane.setPreferredSize(panelSize); JSplitPane mainSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, dbPane, treePane); dbthread = new DbConnectionThread(mainSplit, panelSize, entry_source, dbPane); dbthread.start(); treePane.setDividerLocation((int)(screen.getHeight()/4)); mainSplit.setOneTouchExpandable(true); mainSplit.setDividerLocation((int)(screen.getHeight()/4)); pane.add(mainSplit, BorderLayout.CENTER); } else { setTitle("File Manager"); pane.add(treePane, BorderLayout.CENTER); treePane.setDividerLocation((int)(screen.getHeight()/4)); } setJMenuBar(makeMenuBar(pane,ftree,sshtree,localPanel, remotePanel,treePane,panelSize,dbthread)); localPanel.add(getFileFileterComboBox(ftree), BorderLayout.SOUTH); localTree.setPreferredSize(panelSize); // Set the column width int width = panelSize.width; setColumnWidth(ftree, width); pack(); int yloc = (int)((screen.getHeight()-getHeight())/2); setLocation(0,yloc); setVisible(true); } private void login(JPanel remotePanel, Box bdown, SshLogin ssh_login, Dimension panelSize, JLabel local_status_line, JLabel remote_status_line) { setCursor(new Cursor(Cursor.WAIT_CURSOR)); final SshFileManager ssh_fm; try { ssh_fm = new SshFileManager(ssh_login); } catch(NullPointerException npe) { setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); JOptionPane.showMessageDialog(LocalAndRemoteFileManager.this, "Check login details and try again.", "Failed Login", JOptionPane.ERROR_MESSAGE); return; } FileList flist = new FileList(ssh_fm); remotePanel.remove(bdown); int divider_loc = treePane.getDividerLocation(); setRemoteTree(flist, sshtree, remoteTree, remotePanel, panelSize, remote_status_line); if(treePane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) treePane.setBottomComponent(remotePanel); else treePane.setRightComponent(remotePanel); treePane.setDividerLocation(divider_loc); setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } private void setRemoteTree(final FileList flist, SshJTreeTable sshtree, JScrollPane remoteTree, JPanel remotePanel, final Dimension panelSize, final JLabel remote_status_line) { sshtree = new SshJTreeTable(new FileSystemModel( getRemoteDirectories(flist.pwd()), LocalAndRemoteFileManager.this), LocalAndRemoteFileManager.this); remoteTree = new JScrollPane(sshtree); remoteTree.setPreferredSize(panelSize); remoteTree.getViewport().setBackground(Color.white); remotePanel.add(remoteTree,BorderLayout.CENTER); String remote_name = SshLogin.getHostname(); if(!SshLogin.getPort().equals("")) remote_name = remote_name + ":" + SshLogin.getPort(); remote_status_line.setText("REMOTE "+remote_name); setColumnWidth(sshtree, panelSize.width); } private void setColumnWidth(JTable table, int width) { TableColumn col0 = table.getColumnModel().getColumn(0); col0.setPreferredWidth( (int)(width*0.60) ); TableColumn col1 = table.getColumnModel().getColumn(1); col1.setPreferredWidth( (int)(width*0.12) ); TableColumn col2 = table.getColumnModel().getColumn(2); col2.setPreferredWidth( (int)(width*0.28) ); } /** * * Create a status JLabel with bevelled border * */ private JLabel getStatusLabel(String status) { final JLabel status_line = new JLabel(status); Border loweredbevel = BorderFactory.createLoweredBevelBorder(); Border raisedbevel = BorderFactory.createRaisedBevelBorder(); Border compound = BorderFactory.createCompoundBorder(raisedbevel,loweredbevel); status_line.setBorder(compound); final FontMetrics fm = this.getFontMetrics(status_line.getFont()); final int font_height = fm.getHeight()+10; status_line.setMinimumSize(new Dimension(100, font_height)); status_line.setPreferredSize(new Dimension(100, font_height)); return status_line; } /** * * Look in j2ssh.properties for local directories. * */ private File[] getLocalDirectories() { final Properties settings = SshLogin.getProperties(); Enumeration enum_prop = settings.propertyNames(); Vector dirs = new Vector(); dirs.add(new File(System.getProperty("user.home"))); dirs.add(new File(System.getProperty("user.dir"))); while(enum_prop.hasMoreElements()) { final String property = (String)enum_prop.nextElement(); File f = new File(settings.getProperty(property)); if(property.startsWith("localdir") && f.exists()) dirs.add(f); } File fdirs[] = new File[dirs.size()]; for(int i=0; i