package frame; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.io.File; import java.io.IOException; import java.util.ArrayList; import javax.swing.ImageIcon; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTabbedPane; import javax.swing.KeyStroke; import org.apache.commons.net.ftp.FTPClient; import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.SFTPv3Client; import ch.ethz.ssh2.Session; import filesystem.FSInfo; @SuppressWarnings("serial") public class SlideBrowser extends JFrame { // Class variables FSInfo fsinfo; private ArrayList sbpList; private ArrayList sbpScrollList; SlideBrowserPanel sbp; private terminalWindow tw; JCheckBoxMenuItem terminal; private JTabbedPane tabPane; String[] networkDetails; private JFileChooser fc; private String homeName; /** * Default constructor for main JFrame * * @return */ public SlideBrowser() { // Create frame and size super("Slide Browser"); this.setSize(800, 600); // Set the frame icon to an image loaded from a file. this.setIconImage(new ImageIcon(getClass().getResource( "/resources/icon.png")).getImage()); // Create and add standard menu bar JMenuBar menu = new JMenuBar(); this.setJMenuBar(menu); // Create default panels tw = new terminalWindow(this); sbpList = new ArrayList(); sbpScrollList = new ArrayList(); // Get file system information fsinfo = FSInfo.getInstance(); // Grab home directory File f = new File(fsinfo.getHomeDir()); terminalMessage("Added home directory"); homeName = f.getName(); sbp = new SlideBrowserPanel(f, this); sbp.setPreferredSize(new Dimension(2000, 2000)); sbpList.add(sbp); // Create scrollable viewer JScrollPane sbpScroll = new JScrollPane(sbp); sbpScrollList.add(sbpScroll); // Make tabbed panel tabPane = new JTabbedPane(JTabbedPane.TOP); // Add initial panel tabPane.addTab(f.getName(), new ImageIcon(getClass().getResource("/resources/16x16/house.png")), sbpScrollList.get(0)); // TODO new tab tab /* * //Add "new tab" tab JPanel newTabPanel = new JPanel(); ImageIcon * addTabIcon = new * ImageIcon(getClass().getResource("/resources/16x16/add.png")); * * //Add "new tab" tab tabPane.add("New Tab", addTabIcon, newTabPanel); */ // Make Menu "File" JMenu file = new JMenu("File"); // Make and add New Window item to File JMenuItem newWindowMenuItem = new JMenuItem("New Window", new ImageIcon(getClass().getResource( "/resources/16x16/file_manager.png"))); newWindowMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK)); newWindowMenuItem.getAccessibleContext().setAccessibleDescription( "Make new window"); newWindowMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { SlideBrowser sb = new SlideBrowser(); sb.setVisible(true); } }); file.add(newWindowMenuItem); file.addSeparator(); // Make and add New Tab item to File JMenuItem newTabMenuItem = new JMenuItem("New Tab", new ImageIcon( getClass().getResource("/resources/16x16/tab_add.png"))); newTabMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK)); newTabMenuItem.getAccessibleContext().setAccessibleDescription( "Add new tab"); newTabMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { newTab(); } }); file.add(newTabMenuItem); // Make and add New Network Tab item to File JMenuItem newNetworkTabMenuItem = new JMenuItem("New Network Tab", new ImageIcon(getClass().getResource( "/resources/16x16/orbit.png"))); newNetworkTabMenuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_N, ActionEvent.CTRL_MASK)); newNetworkTabMenuItem.getAccessibleContext().setAccessibleDescription( "Add new tab"); newNetworkTabMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { showNetworkDialog(); } }); file.add(newNetworkTabMenuItem); // Make and add Remove Tab item to File JMenuItem removeTabMenuItem = new JMenuItem("Remove Tab", new ImageIcon(getClass().getResource( "/resources/16x16/tab_delete.png"))); removeTabMenuItem.getAccessibleContext().setAccessibleDescription( "Remove current tab"); removeTabMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { removeTab(); } }); file.add(removeTabMenuItem); file.addSeparator(); // Make and add Exit item to File JMenuItem exit = new JMenuItem("Exit", new ImageIcon(getClass() .getResource("/resources/16x16/stop.png"))); exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.ALT_MASK)); exit.getAccessibleContext().setAccessibleDescription("Quit program"); exit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { displayExitDialog(); } }); file.add(exit); // Make Menu "Tools" JMenu tools = new JMenu("Tools"); // Make and add terminal to Help terminal = new JCheckBoxMenuItem("Show Terminal", new ImageIcon( getClass().getResource( "/resources/16x16/application_osx_terminal.png"))); terminal.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.ALT_MASK)); terminal.getAccessibleContext().setAccessibleDescription( "Display terminal window"); terminal.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // Show and hide terminal frame if (!terminal.isSelected()) { tw.setVisible(false); terminal.setSelected(false); } else { tw.setVisible(true); terminal.setSelected(true); } } }); tools.add(terminal); // Make and add tick box for showing icon hit boxes to Tools JCheckBoxMenuItem showIconHitBox = new JCheckBoxMenuItem( "Show Icon Hit Boxes", new ImageIcon(getClass().getResource( "/resources/16x16/box.png"))); showIconHitBox.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.ALT_MASK)); showIconHitBox.getAccessibleContext().setAccessibleDescription( "Reveal Icon Hit Boxes"); showIconHitBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { sbp.toggleShowHitBoxes(); sbp.repaint(); } }); tools.add(showIconHitBox); // Make and add tick box for showing icon hit boxes to Tools JCheckBoxMenuItem showHiddenFiles = new JCheckBoxMenuItem( "Show hidden files", new ImageIcon(getClass().getResource( "/resources/16x16/folder_hidden.png"))); showHiddenFiles.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.ALT_MASK)); showHiddenFiles.getAccessibleContext().setAccessibleDescription( "Reveal Icon Hit Boxes"); showHiddenFiles.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { sbp.toggleShowHiddenFiles(); sbp.repaint(); } }); tools.add(showHiddenFiles); // Make Menu "Help" JMenu help = new JMenu("Help"); // Make and add About to Help JMenuItem license = new JMenuItem("License info", new ImageIcon(getClass().getResource( "/resources/16x16/creative_commons.png"))); // license.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, // ActionEvent.ALT_MASK)); license.getAccessibleContext().setAccessibleDescription( "License info for this program"); license.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // Make and display about window frame licenseWindow lw = new licenseWindow(); lw.setVisible(true); terminalMessage("Show License Window"); } }); help.add(license); // Make and add About to Help JMenuItem about = new JMenuItem("About", new ImageIcon(getClass() .getResource("/resources/16x16/help.png"))); about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.ALT_MASK)); about.getAccessibleContext().setAccessibleDescription( "Info about this program"); about.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // Make and display about window frame aboutWindow aw = new aboutWindow(); aw.setVisible(true); terminalMessage("Show About Window"); } }); help.add(about); // Add Menus to menu bar menu.add(file); menu.add(tools); menu.add(help); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); // Add Panel to Frame this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(tabPane); } protected void showNetworkDialog() { new networkWindow(this); } /** * Adds a new remote location via networking * * @param String * to add to message log */ protected void newNetworkTab() { if (networkDetails[0].equals("FTP")) { // Make FTP tab SlideBrowserFTPPanel newSBFTPP = new SlideBrowserFTPPanel( networkDetails, this); newSBFTPP.setPreferredSize(new Dimension(2000, 2000)); // Create scrollable viewer JScrollPane sbpScroll = new JScrollPane(newSBFTPP); sbpList.add(newSBFTPP); sbpScrollList.add(sbpScroll); tabPane.addTab(networkDetails[1], new ImageIcon(getClass() .getResource("/resources/16x16/network_folder.png")), sbpScroll); } else if (networkDetails[0].equals("SFTP")) { // Make SFTP tab SlideBrowserSFTPPanel newSBSFTPP = new SlideBrowserSFTPPanel( networkDetails, this); newSBSFTPP.setPreferredSize(new Dimension(2000, 2000)); // Create scrollable viewer JScrollPane sbpScroll = new JScrollPane(newSBSFTPP); sbpList.add(newSBSFTPP); sbpScrollList.add(sbpScroll); tabPane.addTab(networkDetails[1], new ImageIcon(getClass() .getResource("/resources/16x16/network_folder.png")), sbpScroll); } } /** * Removes currently viewed tab * */ protected void removeTab() { int i = tabPane.getSelectedIndex(); // Check to see if it is a networked tab Object s = sbpList.get(i); if (s instanceof SlideBrowserFTPPanel) { // TODO fix disconnect SlideBrowserFTPPanel SBFTP = (SlideBrowserFTPPanel) s; FTPClient client = SBFTP.getClient(); try { if (client.isConnected()) { client.logout(); client.disconnect(); } } catch (IOException e) { JOptionPane.showMessageDialog(this, new JLabel("Lost connection with remote server"), "Connection Lost", JOptionPane.WARNING_MESSAGE); } terminalMessage("Closed connection to remote server" ); sbpList.remove(i); } else if(s instanceof SlideBrowserSFTPPanel) { SlideBrowserSFTPPanel sftp = (SlideBrowserSFTPPanel) s; Connection c = sftp.getConnection(); Session sess = sftp.getSession(); SFTPv3Client scp = sftp.getClient(); try { scp.close(); sess.close(); c.close(); } catch(NullPointerException e) { JOptionPane.showMessageDialog(this, new JLabel("Lost connection with remote server"), "Connection Lost", JOptionPane.WARNING_MESSAGE); } sbpList.remove(i); } else { // TODO clean up sbpList.remove(i); } // Remove from scroll list as well sbpScrollList.remove(i); tabPane.removeTabAt(i); } /** * Shows a dialog box to confirm program exit. * */ public void displayExitDialog() { int result = JOptionPane.showConfirmDialog(this, new JLabel("Exit?"), "Exit", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); if (result == JOptionPane.YES_OPTION) { dispose(); } } /** * Add given string to terminal message log * * @param String * to add to message log */ public void terminalMessage(String s) { tw.addMessage(s); } /** * Add a new tab to the browser * */ public void newTab() { fc = new JFileChooser(); fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); int val = fc.showOpenDialog(this); if (val == JFileChooser.APPROVE_OPTION) { SlideBrowserPanel newSBP = new SlideBrowserPanel( fc.getSelectedFile(), this); newSBP.setPreferredSize(new Dimension(2000, 2000)); // Create scrollable viewer JScrollPane sbpScroll = new JScrollPane(newSBP); sbpList.add(newSBP); sbpScrollList.add(sbpScroll); System.out.println(fc.getSelectedFile().getName()); System.out.println(fsinfo.getHomeDir()); if (fc.getSelectedFile().getName().equals(homeName)) { tabPane.addTab(fc.getSelectedFile().getName(), new ImageIcon( getClass().getResource("/resources/16x16/house.png")), sbpScroll); } else tabPane.addTab(fc.getSelectedFile().getName(), new ImageIcon( getClass().getResource("/resources/16x16/folder.png")), sbpScroll); } } /** * Add a new tab to the browser with given file * */ public void newTab(File f) { SlideBrowserPanel newSBP = new SlideBrowserPanel( f, this); newSBP.setPreferredSize(new Dimension(2000, 2000)); // Create scrollable viewer JScrollPane sbpScroll = new JScrollPane(newSBP); sbpList.add(newSBP); sbpScrollList.add(sbpScroll); System.out.println(f.getName()); System.out.println(fsinfo.getHomeDir()); if (f.equals(homeName)) { tabPane.addTab(f.getName(), new ImageIcon( getClass().getResource("/resources/16x16/house.png")), sbpScroll); } else tabPane.addTab(f.getName(), new ImageIcon( getClass().getResource("/resources/16x16/folder.png")), sbpScroll); } public JScrollPane getScrollPane() { return sbpScrollList.get(tabPane.getSelectedIndex()); } /** * Method to grab network details for making a network tab * from the dialog box * * @param details given for networking */ public void setNetworkDetails(String[] details) { networkDetails = details; } /** * main method which starts the slide browser * * @param args */ public static void main(String[] args) { // TODO implement save session state and persistence SlideBrowser sb = new SlideBrowser(); sb.setVisible(true); } }