X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FDesktop.java;h=7c08705e1cbcfd6023a74b8ed457f9da6920d002;hb=4d64932654de3f6ffe07db11d18f2d21f558c6e6;hp=e592abf6c6c662314928ad4b1e82ef64ec7b66d2;hpb=26d0f2d76f2377947eed04c2f64d94587127c0c3;p=jalview.git diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index e592abf..7c08705 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -20,8 +20,6 @@ */ package jalview.gui; -import static jalview.util.UrlConstants.SEQUENCE_ID; - import jalview.api.AlignViewportI; import jalview.api.AlignmentViewPanel; import jalview.bin.Cache; @@ -38,6 +36,7 @@ import jalview.io.JalviewFileChooser; import jalview.io.JalviewFileView; import jalview.jbgui.GSplitFrame; import jalview.jbgui.GStructureViewer; +import jalview.project.Jalview2XML; import jalview.structure.StructureSelectionManager; import jalview.urls.IdOrgSettings; import jalview.util.ImageMaker; @@ -62,6 +61,14 @@ import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.ClipboardOwner; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; +import java.awt.desktop.AboutEvent; +import java.awt.desktop.AboutHandler; +import java.awt.desktop.PreferencesEvent; +import java.awt.desktop.PreferencesHandler; +import java.awt.desktop.QuitEvent; +import java.awt.desktop.QuitHandler; +import java.awt.desktop.QuitResponse; +import java.awt.desktop.QuitStrategy; import java.awt.dnd.DnDConstants; import java.awt.dnd.DropTargetDragEvent; import java.awt.dnd.DropTargetDropEvent; @@ -69,6 +76,7 @@ import java.awt.dnd.DropTargetEvent; import java.awt.dnd.DropTargetListener; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; @@ -79,9 +87,11 @@ import java.beans.PropertyChangeListener; import java.io.BufferedInputStream; import java.io.File; import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; import java.net.URL; import java.util.ArrayList; +import java.util.HashMap; import java.util.Hashtable; import java.util.List; import java.util.ListIterator; @@ -92,19 +102,22 @@ import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; import javax.swing.AbstractAction; +import javax.swing.Action; +import javax.swing.ActionMap; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.DefaultDesktopManager; import javax.swing.DesktopManager; +import javax.swing.InputMap; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JDesktopPane; -import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLabel; import javax.swing.JMenuItem; +import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JProgressBar; @@ -140,6 +153,10 @@ public class Desktop extends jalview.jbgui.GDesktop private static final String EXPERIMENTAL_FEATURES = "EXPERIMENTAL_FEATURES"; + private static final String CONFIRM_KEYBOARD_QUIT = "CONFIRM_KEYBOARD_QUIT"; + + public static HashMap savingFiles = new HashMap<>(); + private JalviewChangeSupport changeSupport = new JalviewChangeSupport(); /** @@ -149,6 +166,8 @@ public class Desktop extends jalview.jbgui.GDesktop private File projectFile; + private static boolean setAPQHandlers = false; + /** * @param listener * @see jalview.gui.JalviewChangeSupport#addJalviewPropertyChangeListener(java.beans.PropertyChangeListener) @@ -341,14 +360,131 @@ public class Desktop extends jalview.jbgui.GDesktop doConfigureStructurePrefs(); setTitle("Jalview " + jalview.bin.Cache.getProperty("VERSION")); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + /* + if (!Platform.isAMac()) + { + // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + } + else + { + this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); + } + */ + + // flagging this test to avoid unnecessary reflection + if (!setAPQHandlers) + { + // see if the Quit, About and Preferences handlers are available + Class desktopClass = java.awt.Desktop.class; + java.awt.Desktop hdesktop = java.awt.Desktop.getDesktop(); + + try + { + + if (desktopClass.getDeclaredMethod("setAboutHandler", + new Class[] + { AboutHandler.class }) != null) + { + + hdesktop.setAboutHandler(new AboutHandler() + { + @Override + public void handleAbout(AboutEvent e) + { + aboutMenuItem_actionPerformed(null); + } + }); + + } + + if (desktopClass.getDeclaredMethod("setPreferencesHandler", + new Class[] + { PreferencesHandler.class }) != null) + { + + hdesktop.setPreferencesHandler(new PreferencesHandler() + { + @Override + public void handlePreferences(PreferencesEvent e) + { + preferences_actionPerformed(null); + } + }); + + } + + if (desktopClass.getDeclaredMethod("setQuitHandler", + new Class[] + { QuitHandler.class }) != null) + { + + hdesktop.setQuitHandler(new QuitHandler() + { + @Override + public void handleQuitRequestWith(QuitEvent e, QuitResponse r) + { + boolean confirmQuit = jalview.bin.Cache + .getDefault(CONFIRM_KEYBOARD_QUIT, true); + int n; + if (confirmQuit) + { + n = JOptionPane.showConfirmDialog(null, + MessageManager.getString("label.quit_jalview"), + MessageManager.getString("action.quit"), + JOptionPane.OK_CANCEL_OPTION, + JOptionPane.PLAIN_MESSAGE, null); + } + else + { + n = JOptionPane.OK_OPTION; + } + if (n == JOptionPane.OK_OPTION) + { + System.out.println("Shortcut Quit confirmed by user"); + quit(); + r.performQuit(); // probably won't reach this line, but just in + // case + } + else + { + r.cancelQuit(); + System.out.println("Shortcut Quit cancelled by user"); + } + } + }); + hdesktop.setQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS); + + } + + } catch (NoSuchMethodException e) + { + System.out.println( + "NoSuchMethodException when looking for About, Preferences, Quit Handlers"); + e.printStackTrace(); + } + + setAPQHandlers = true; + } + + addWindowListener(new WindowAdapter() + { + + @Override + public void windowClosing(WindowEvent ev) + { + quit(); + } + }); + boolean selmemusage = jalview.bin.Cache.getDefault("SHOW_MEMUSAGE", false); + boolean showjconsole = jalview.bin.Cache.getDefault("SHOW_JAVA_CONSOLE", false); desktop = new MyDesktopPane(selmemusage); showMemusage.setSelected(selmemusage); desktop.setBackground(Color.white); + getContentPane().setLayout(new BorderLayout()); // alternate config - have scrollbars - see notes in JAL-153 // JScrollPane sp = new JScrollPane(); @@ -359,13 +495,12 @@ public class Desktop extends jalview.jbgui.GDesktop // This line prevents Windows Look&Feel resizing all new windows to maximum // if previous window was maximised - desktop.setDesktopManager( - new MyDesktopManager( - (Platform.isWindows() ? new DefaultDesktopManager() - : Platform.isAMac() - ? new AquaInternalFrameManager( - desktop.getDesktopManager()) - : desktop.getDesktopManager()))); + desktop.setDesktopManager(new MyDesktopManager( + (Platform.isWindows() ? new DefaultDesktopManager() + : Platform.isAMac() + ? new AquaInternalFrameManager( + desktop.getDesktopManager()) + : desktop.getDesktopManager()))); Rectangle dims = getLastKnownDimensions(""); if (dims != null) @@ -515,7 +650,7 @@ public class Desktop extends jalview.jbgui.GDesktop { final Desktop me = this; // Thread off the news reader, in case there are connection problems. - addDialogThread(new Runnable() + new Thread(new Runnable() { @Override public void run() @@ -526,13 +661,13 @@ public class Desktop extends jalview.jbgui.GDesktop showNews.setVisible(true); Cache.log.debug("Completed news thread."); } - }); + }).start(); } public void getIdentifiersOrgData() { // Thread off the identifiers fetcher - addDialogThread(new Runnable() + new Thread(new Runnable() { @Override public void run() @@ -549,7 +684,8 @@ public class Desktop extends jalview.jbgui.GDesktop + e.getMessage()); } } - }); + }).start(); + ; } @Override @@ -903,8 +1039,6 @@ public class Desktop extends jalview.jbgui.GDesktop menuItem.removeActionListener(menuItem.getActionListeners()[0]); } windowMenu.remove(menuItem); - - System.gc(); }; }); @@ -924,6 +1058,8 @@ public class Desktop extends jalview.jbgui.GDesktop } }); + setKeyBindings(frame); + desktop.add(frame); windowMenu.add(menuItem); @@ -943,6 +1079,42 @@ public class Desktop extends jalview.jbgui.GDesktop } } + /** + * Add key bindings to a JInternalFrame so that Ctrl-W and Cmd-W will close + * the window + * + * @param frame + */ + private static void setKeyBindings(JInternalFrame frame) + { + @SuppressWarnings("serial") + final Action closeAction = new AbstractAction() + { + @Override + public void actionPerformed(ActionEvent e) + { + frame.dispose(); + } + }; + + /* + * set up key bindings for Ctrl-W and Cmd-W, with the same (Close) action + */ + KeyStroke ctrlWKey = KeyStroke.getKeyStroke(KeyEvent.VK_W, + InputEvent.CTRL_DOWN_MASK); + KeyStroke cmdWKey = KeyStroke.getKeyStroke(KeyEvent.VK_W, + Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()); + + InputMap inputMap = frame + .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); + String ctrlW = ctrlWKey.toString(); + inputMap.put(ctrlWKey, ctrlW); + inputMap.put(cmdWKey, ctrlW); + + ActionMap actionMap = frame.getActionMap(); + actionMap.put(ctrlW, closeAction); + } + @Override public void lostOwnership(Clipboard clipboard, Transferable contents) { @@ -1210,6 +1382,8 @@ public class Desktop extends jalview.jbgui.GDesktop @Override public void quit() { + System.out.println("********** Desktop.quit()"); + System.out.println(savingFiles.toString()); Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); jalview.bin.Cache.setProperty("SCREENGEOMETRY_WIDTH", screen.width + ""); @@ -1392,7 +1566,6 @@ public class Desktop extends jalview.jbgui.GDesktop { ssm.resetAll(); } - System.gc(); } @Override @@ -1559,13 +1732,11 @@ public class Desktop extends jalview.jbgui.GDesktop } /** - * DOCUMENT ME! - * - * @param e - * DOCUMENT ME! + * Shows a file chooser dialog and writes out the current session as a Jalview + * project file */ @Override - public void saveState_actionPerformed(ActionEvent e) + public void saveState_actionPerformed() { JalviewFileChooser chooser = new JalviewFileChooser("jvp", "Jalview Project"); @@ -1632,19 +1803,18 @@ public class Desktop extends jalview.jbgui.GDesktop } /** - * DOCUMENT ME! - * - * @param e - * DOCUMENT ME! + * Shows a file chooser dialog and tries to read in the selected file as a + * Jalview project */ @Override - public void loadState_actionPerformed(ActionEvent e) + public void loadState_actionPerformed() { + final String[] suffix = new String[] { "jvp", "jar" }; + final String[] desc = new String[] { "Jalview Project", + "Jalview Project (old)" }; JalviewFileChooser chooser = new JalviewFileChooser( - Cache.getProperty("LAST_DIRECTORY"), new String[] - { "jvp", "jar" }, - new String[] - { "Jalview Project", "Jalview Project (old)" }, + Cache.getProperty("LAST_DIRECTORY"), suffix, + desc, "Jalview Project"); chooser.setFileView(new JalviewFileView()); chooser.setDialogTitle(MessageManager.getString("label.restore_state")); @@ -2341,7 +2511,7 @@ public class Desktop extends jalview.jbgui.GDesktop while (li.hasNext()) { String link = li.next(); - if (link.contains(SEQUENCE_ID) + if (link.contains(jalview.util.UrlConstants.SEQUENCE_ID) && !UrlConstants.isDefaultString(link)) { check = true; @@ -2495,7 +2665,6 @@ public class Desktop extends jalview.jbgui.GDesktop } } - /** * Accessor method to quickly get all the AlignmentFrames loaded. * @@ -3297,6 +3466,21 @@ public class Desktop extends jalview.jbgui.GDesktop return groovyConsole; } + /** + * handles the payload of a drag and drop event. + * + * TODO refactor to desktop utilities class + * + * @param files + * - Data source strings extracted from the drop event + * @param protocols + * - protocol for each data source extracted from the drop event + * @param evt + * - the drop event + * @param t + * - the payload from the drop event + * @throws Exception + */ public static void transferFromDropTarget(List files, List protocols, DropTargetDropEvent evt, Transferable t) throws Exception @@ -3318,15 +3502,15 @@ public class Desktop extends jalview.jbgui.GDesktop try { - java.net.URL url = (URL) t.getTransferData(urlFlavour); + java.net.URL url = (URL) t.getTransferData(urlFlavour); // nb: java 8 osx bug https://bugs.openjdk.java.net/browse/JDK-8156099 // means url may be null. - if (url != null) - { - protocols.add(DataSourceType.URL); - files.add(url.toString()); - Cache.log.debug("Drop handled as URL dataflavor " - + files.get(files.size() - 1)); + if (url != null) + { + protocols.add(DataSourceType.URL); + files.add(url.toString()); + Cache.log.debug("Drop handled as URL dataflavor " + + files.get(files.size() - 1)); return; } else @@ -3337,7 +3521,7 @@ public class Desktop extends jalview.jbgui.GDesktop "Please ignore plist error - occurs due to problem with java 8 on OSX"); } ; - } + } } catch (Throwable ex) { Cache.log.debug("URL drop handler failed.", ex); @@ -3413,32 +3597,8 @@ public class Desktop extends jalview.jbgui.GDesktop files.add(file.toString()); } } - if (Platform.isWindows()) - { - Cache.log - .debug("Scanning dropped content for Windows Link Files"); - - // resolve any .lnk files in the file drop - for (int f = 0; f < files.size(); f++) - { - if (protocols.get(f).equals(DataSourceType.FILE)) - { - File lf = new File(files.get(f)); - if (WindowsShortcut.isPotentialValidLink(lf)) - { - // process link file to get a URL - Cache.log.debug("Found potential link file: " + lf); - WindowsShortcut wscfile = new WindowsShortcut(lf); - String fullname = wscfile.getRealFilename(); - protocols.set(f, FormatAdapter.checkProtocol(fullname)); - files.set(f, fullname); - Cache.log.debug("Parsed real filename " + fullname - + " to extract protocol: " + protocols.get(f)); - } - } - } - } } + if (Cache.log.isDebugEnabled()) { if (data == null || !added) @@ -3472,6 +3632,39 @@ public class Desktop extends jalview.jbgui.GDesktop } } } + if (Platform.isWindows()) + + { + Cache.log.debug("Scanning dropped content for Windows Link Files"); + + // resolve any .lnk files in the file drop + for (int f = 0; f < files.size(); f++) + { + String source = files.get(f).toLowerCase(); + if (protocols.get(f).equals(DataSourceType.FILE) + && (source.endsWith(".lnk") || source.endsWith(".url") + || source.endsWith(".site"))) + { + try + { + File lf = new File(files.get(f)); + // process link file to get a URL + Cache.log.debug("Found potential link file: " + lf); + WindowsShortcut wscfile = new WindowsShortcut(lf); + String fullname = wscfile.getRealFilename(); + protocols.set(f, FormatAdapter.checkProtocol(fullname)); + files.set(f, fullname); + Cache.log.debug("Parsed real filename " + fullname + + " to extract protocol: " + protocols.get(f)); + } catch (Exception ex) + { + Cache.log.error( + "Couldn't parse " + files.get(f) + " as a link file.", + ex); + } + } + } + } } /**