/* File Name: FilePanel.java */ package explorer.client.gui; import java.io.*; import java.net.*; import javax.swing.*; import javax.swing.table.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import javax.swing.tree.*; import java.util.Date; import explorer.*; import explorer.client.CrossXClient; import explorer.client.CrossXClientFactory; /** * FilePanel is used to display the files and their attributes * in a table structure. They implement methods to Copy a file, Move a file * Delete a file, Rename a file,Set Properties for a file */ public class FilePanel extends JPanel implements ActionListener { private JTable table; MyTableModel tableModel; CrossXClientFactory client; JScrollPane listContainer; ClientOpenFrame gui; JPopupMenu popup; MouseListener popupListener; static ImageIcon driveIcon; static ImageIcon systemIcon; static ImageIcon folderIcon; static ImageIcon fileIcon; static { try { java.net.URL url= ClassLoader.getSystemResource("resources/drive.gif"); driveIcon = new ImageIcon(url,"drive"); url= ClassLoader.getSystemResource("resources/mycom.gif"); systemIcon = new ImageIcon(url,"computer"); url= ClassLoader.getSystemResource("resources/closedfolder.gif"); folderIcon = new ImageIcon(url,"folder"); url= ClassLoader.getSystemResource("resources/file.gif"); fileIcon = new ImageIcon(url,"file"); } catch(Exception e) { e.printStackTrace(); } } /** * Used to initialize the componenets of the File Panel */ public FilePanel(ClientOpenFrame gui) { super(); this.gui=gui; client = gui.getClient(); tableModel = new MyTableModel(0); table=new JTable(tableModel); table.setShowVerticalLines(false); table.setShowHorizontalLines(false); setLayout(new GridLayout(1,0)); listContainer = new JScrollPane(table); listContainer.setMinimumSize(new Dimension(300, 50)); add(listContainer); popup=new JPopupMenu(); JMenuItem openmnu=new JMenuItem("Open"); openmnu.addActionListener(this); JMenuItem copyTomnu=new JMenuItem("Copy To"); copyTomnu.addActionListener(this); JMenuItem moveTomnu=new JMenuItem("Move To"); moveTomnu.addActionListener(this); JMenuItem delmnu=new JMenuItem("Delete"); delmnu.addActionListener(this); JMenuItem renamemnu=new JMenuItem("Rename"); renamemnu.addActionListener(this); JMenuItem propmnu=new JMenuItem("Properties"); propmnu.addActionListener(this); popup.add(openmnu); popup.addSeparator(); popup.add(copyTomnu); popup.add(moveTomnu); popup.addSeparator(); popup.add(delmnu); popup.add(renamemnu); popup.addSeparator(); popup.add(propmnu); popupListener = new PopupListener(); table.addMouseListener(popupListener); popup.setLightWeightPopupEnabled(true); } /** * Triggered when any action event occurs in the components of File Panel */ public void actionPerformed(ActionEvent e) { gui.setCursor(Cursor.WAIT_CURSOR); JMenuItem source = (JMenuItem)(e.getSource()); String selection = source.getText(); if(selection.equalsIgnoreCase("Open")) { doOpenEvent(); } else if(selection.equalsIgnoreCase("Copy To")) { try { doCopyToEvent(); } catch(java.rmi.RemoteException exep){ } } else if(selection.equalsIgnoreCase("Move To")) { try { doMoveToEvent(); } catch(java.rmi.RemoteException exep){ exep.printStackTrace(); } } else if(selection.equalsIgnoreCase("Delete")) { doDeleteEvent(); } else if(selection.equalsIgnoreCase("Rename")) { doRenameEvent(); } else if(selection.equalsIgnoreCase("Properties")) { doPropertyEvent(); } JTree tree=gui.getFolderpanel().tree; TreePath currentTreePath=tree.getSelectionPath(); if(currentTreePath.getParentPath()==null) return; tree.setSelectionPath(currentTreePath.getParentPath()); tree.setSelectionPath(currentTreePath); gui.setCursor(Cursor.DEFAULT_CURSOR); } /** * Used to rename a file ,rename dialog is dispalyed */ public void doRenameEvent() { int tableSelectedRow=table.getSelectedRow(); if(tableSelectedRow==-1) return; Object obj=table.getValueAt(tableSelectedRow,1); if(!(obj instanceof explorer.FileSystemDAO)) return; FileSystemDAO dao=(FileSystemDAO)obj; if(dao.isDrive()) return; String newName= JOptionPane.showInputDialog(gui,"Enter the new name to :" + dao.getFilePath()); if(newName==null) return; if(gui.getClient().setProperties(gui.getPresentClient(),dao, newName)) { dao.setFileName(newName); table.setValueAt(dao,tableSelectedRow,1); } } /** * Used to delete a file object, Delete dialog is displayed */ public void doDeleteEvent() { int tableSelectedRow=table.getSelectedRow(); if(tableSelectedRow==-1) return; Object obj=table.getValueAt(tableSelectedRow,1); JTree tree = gui.getFolderpanel().getTree(); if(JOptionPane.showConfirmDialog(gui, "Are you sure to delete this file? Action cannot be revoked", "Delete Window", JOptionPane.YES_NO_OPTION)==JOptionPane.NO_OPTION) { return; } if(obj instanceof explorer.FileSystemDAO) { FileSystemDAO dao=(FileSystemDAO) obj; if(dao.isDirectory() || dao.isFile()) { client.deleteFile(gui.getPresentClient(), dao.getFilePath()); } else { return; } } } /** * Used to display Property window of a file Object */ public void doPropertyEvent() { int tableSelectedRow=table.getSelectedRow(); if(tableSelectedRow==-1) return; Object obj=table.getValueAt(tableSelectedRow,1); if(obj instanceof explorer.FileSystemDAO) { FileSystemDAO dao=(FileSystemDAO) obj; PropertyWindow propWin = new PropertyWindow(gui, dao); } } /** * Used to map a file to its application */ public void doOpenEvent() { try{ int tableSelectedRow=table.getSelectedRow(); if(tableSelectedRow==-1) return; Object obj=table.getValueAt(tableSelectedRow,1); JTree tree = gui.getFolderpanel().getTree(); if(obj instanceof explorer.client.CrossXClient) { CrossXClient clientObj=(CrossXClient)obj; DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)gui.getFolderpanel().getSelectedNode(); DefaultMutableTreeNode childNode= (DefaultMutableTreeNode)parentNode.getChildAt(tableSelectedRow); tree.scrollPathToVisible(new TreePath(childNode.getPath())); tree.setSelectionPath(new TreePath(childNode.getPath())); } else if(obj instanceof explorer.FileSystemDAO) { FileSystemDAO dao=(FileSystemDAO) obj; if(dao.isDirectory() || dao.isDrive()) { DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) gui.getFolderpanel().getSelectedNode(); DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) parentNode.getChildAt(tableSelectedRow); tree.scrollPathToVisible(new TreePath(childNode.getPath())); tree.setSelectionPath(new TreePath(childNode.getPath())); } else if(dao.isFile()) { OpenFileAsApplication openFile= new OpenFileAsApplication(gui.getPresentClient(),dao); Thread openAppThread=new Thread(openFile); openAppThread.start(); } } CrossXClientFactory.blnEnd = false; } catch(Exception e) { JOptionPane.showMessageDialog(gui,e.getMessage()); } } /** * Used to copy a file, Shows the file path to be selected in tree form */ public void doCopyToEvent() throws java.rmi.RemoteException { int tableSelectedRow=table.getSelectedRow(); if(tableSelectedRow==-1) return; String filePath = ""; CopyToDialog copydialog=new CopyToDialog(gui); copydialog.setVisible(true); TreePath selected = copydialog.getSelectedPath(); if (selected==null) return; Object obj=table.getValueAt(tableSelectedRow,1); JTree tree = gui.getFolderpanel().getTree(); for(int i=2; i3) { filePath += copydialog.getFileSeparator(); } filePath += selected.getPathComponent(i).toString(); } System.out.println("COPY FILE PATH : "+filePath); if(obj instanceof explorer.FileSystemDAO) { FileSystemDAO dao=(FileSystemDAO) obj; String strDestination = selected.getPathComponent(1).toString(); if(dao.isDirectory() || dao.isFile()) { ProgressDialog dialog = new ProgressDialog(gui, "Copying....", dao.getFilePath(), (filePath.endsWith( copydialog.getFileSeparator())?filePath:filePath + copydialog.getFileSeparator()) + dao.getFileName()); DefaultMutableTreeNode selectednode= (DefaultMutableTreeNode)selected.getPathComponent(1); Object selectedobj=selectednode.getUserObject(); if(selectedobj instanceof explorer.client.CrossXClient) { CrossXClient clientObj=(CrossXClient)selectedobj; client.copyFileTo(clientObj, (filePath.endsWith( copydialog.getFileSeparator())? filePath: filePath + copydialog.getFileSeparator()) + dao.getFileName(), gui.getPresentClient(), dao.getFilePath()); } } else { return; } } else { return; } } /** * Used to move a file, Shows the file path to be selected in tree form */ public void doMoveToEvent() throws java.rmi.RemoteException { int tableSelectedRow=table.getSelectedRow(); String filePath = ""; if(tableSelectedRow==-1) return; CopyToDialog copydialog=new CopyToDialog(gui); copydialog.setVisible(true); TreePath selected = copydialog.getSelectedPath(); if (selected==null) return; Object obj=table.getValueAt(tableSelectedRow,1); JTree tree = gui.getFolderpanel().getTree(); for(int i=2; i3) { filePath += copydialog.getFileSeparator(); } filePath += selected.getPathComponent(i).toString(); } System.out.println("MOVE FILE PATH : "+filePath); if(obj instanceof explorer.FileSystemDAO) { FileSystemDAO dao=(FileSystemDAO) obj; String strDestination = selected.getPathComponent(1).toString(); if(dao.isDirectory() || dao.isFile()) { ProgressDialog dialog = new ProgressDialog(gui, "Moving....", dao.getFilePath(), (filePath.endsWith( copydialog.getFileSeparator())?filePath:filePath + copydialog.getFileSeparator()) + dao.getFileName()); DefaultMutableTreeNode selectednode= (DefaultMutableTreeNode)selected.getPathComponent(1); Object selectedobj=selectednode.getUserObject(); if(selectedobj instanceof explorer.client.CrossXClient) { CrossXClient clientObj=(CrossXClient)selectedobj; client.moveFileTo(clientObj, filePath + copydialog.getFileSeparator() + dao.getFileName(), gui.getPresentClient(),dao.getFilePath()); } } else { System.out.println("Is neither directory nor file"); return; } } else { System.out.println("Is not filesystem"); return; } } public void setTableValues(Object[] objs) { MyTableModel tmodel=new MyTableModel(objs.length); gui.setNoOfObjects(objs.length); for(int i=0;i-->