X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FDesktop.java;h=3cc2c1439369bead240cfe297864b14909f56513;hb=c708b0d5bed26752c34e62960f0acdeada8742df;hp=f570217448003dfbae92037e887e86b9c102da1d;hpb=ea7b9666447dea727e9d607de3694cfe27bc811e;p=jalview.git diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index f570217..3cc2c14 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -61,6 +61,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; +import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; @@ -82,6 +83,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; +import javax.swing.AbstractAction; import javax.swing.DefaultDesktopManager; import javax.swing.DesktopManager; import javax.swing.JButton; @@ -96,6 +98,7 @@ import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JProgressBar; +import javax.swing.KeyStroke; import javax.swing.SwingUtilities; import javax.swing.event.HyperlinkEvent; import javax.swing.event.HyperlinkEvent.EventType; @@ -822,7 +825,15 @@ public class Desktop extends jalview.jbgui.GDesktop implements javax.swing.event.InternalFrameEvent evt) { PaintRefresher.RemoveComponent(frame); - openFrameCount--; + + /* + * defensive check to prevent frames being + * added half off the window + */ + if (openFrameCount > 0) + { + openFrameCount--; + } windowMenu.remove(menuItem); JInternalFrame itf = desktop.getSelectedFrame(); if (itf != null) @@ -950,53 +961,15 @@ public class Desktop extends jalview.jbgui.GDesktop implements { boolean success = true; Transferable t = evt.getTransferable(); - java.util.List files = null; - java.util.List protocols = null; + java.util.List files = new ArrayList(); + java.util.List protocols = new ArrayList(); try { - DataFlavor uriListFlavor = new DataFlavor( - "text/uri-list;class=java.lang.String"); - if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) - { - // Works on Windows and MacOSX - evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); - files = (java.util.List) t - .getTransferData(DataFlavor.javaFileListFlavor); - } - else if (t.isDataFlavorSupported(uriListFlavor)) - { - // This is used by Unix drag system - evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); - String data = (String) t.getTransferData(uriListFlavor); - files = new java.util.ArrayList(1); - protocols = new java.util.ArrayList(1); - for (java.util.StringTokenizer st = new java.util.StringTokenizer( - data, "\r\n"); st.hasMoreTokens();) - { - String s = st.nextToken(); - if (s.startsWith("#")) - { - // the line is a comment (as per the RFC 2483) - continue; - } - java.net.URI uri = new java.net.URI(s); - if (uri.getScheme().toLowerCase().startsWith("http")) - { - protocols.add(FormatAdapter.URL); - files.add(uri.toString()); - } - else - { - // otherwise preserve old behaviour: catch all for file objects - java.io.File file = new java.io.File(uri); - protocols.add(FormatAdapter.FILE); - files.add(file.toString()); - } - } - } + Desktop.transferFromDropTarget(files, protocols, evt, t); } catch (Exception e) { + e.printStackTrace(); success = false; } @@ -1216,6 +1189,13 @@ public class Desktop extends jalview.jbgui.GDesktop implements dialogExecutor.shutdownNow(); } closeAll_actionPerformed(null); + + if (groovyConsole != null) + { + // suppress a possible repeat prompt to save script + groovyConsole.setDirty(false); + groovyConsole.exit(); + } System.exit(0); } @@ -1834,7 +1814,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements * * @param af */ - public void explodeViews(AlignFrame af) + public static void explodeViews(AlignFrame af) { int size = af.alignPanels.size(); if (size < 2) @@ -2503,39 +2483,63 @@ public class Desktop extends jalview.jbgui.GDesktop implements */ void openGroovyConsole() { - groovyConsole = new groovy.ui.Console(); + if (groovyConsole == null) + { + groovyConsole = new groovy.ui.Console(); + groovyConsole.setVariable("Jalview", this); + groovyConsole.run(); - /* - * bind groovy variable 'Jalview' to the Desktop object - */ - groovyConsole.setVariable("Jalview", this); + /* + * We allow only one console at a time, so that AlignFrame menu option + * 'Calculate | Run Groovy script' is unambiguous. + * Disable 'Groovy Console', and enable 'Run script', when the console is + * opened, and the reverse when it is closed + */ + Window window = (Window) groovyConsole.getFrame(); + window.addWindowListener(new WindowAdapter() + { + @Override + public void windowClosed(WindowEvent e) + { + /* + * rebind CMD-Q from Groovy Console to Jalview Quit + */ + addQuitHandler(); + enableExecuteGroovy(false); + } + }); + } /* - * start the console + * show Groovy console window (after close and reopen) */ - groovyConsole.run(); + ((Window) groovyConsole.getFrame()).setVisible(true); /* - * Allow only one console at a time, so that the AlignFrame menu option - * 'Calculate | Run Groovy script' is unambiguous. - * Disable 'new console', and enable 'Run script', when the console is - * opened, and the reverse when it is closed + * if we got this far, enable 'Run Groovy' in AlignFrame menus + * and disable opening a second console */ - Window window = (Window) groovyConsole.getFrame(); - window.addWindowListener(new WindowAdapter() + enableExecuteGroovy(true); + } + + /** + * Bind Ctrl/Cmd-Q to Quit - for reset as Groovy Console takes over this + * binding when opened + */ + protected void addQuitHandler() + { + getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put( + KeyStroke.getKeyStroke(KeyEvent.VK_Q, Toolkit + .getDefaultToolkit().getMenuShortcutKeyMask()), + "Quit"); + getRootPane().getActionMap().put("Quit", new AbstractAction() { @Override - public void windowClosed(WindowEvent e) + public void actionPerformed(ActionEvent e) { - enableExecuteGroovy(false); + quit(); } }); - - /* - * if we got this far, enable 'Run Groovy' in AlignFrame menus - * and disable opening a second console - */ - enableExecuteGroovy(true); } /** @@ -3182,4 +3186,102 @@ public class Desktop extends jalview.jbgui.GDesktop implements return groovyConsole; } + public static void transferFromDropTarget(List files, + List protocols, DropTargetDropEvent evt, Transferable t) + throws Exception + { + + DataFlavor uriListFlavor = new DataFlavor( + "text/uri-list;class=java.lang.String"); + if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) + { + // Works on Windows and MacOSX + Cache.log.debug("Drop handled as javaFileListFlavor"); + evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); + for (Object file : (List) t + .getTransferData(DataFlavor.javaFileListFlavor)) + { + files.add(((File)file).toString()); + protocols.add(FormatAdapter.FILE); + } + } + else + { + // Unix like behaviour + boolean added = false; + String data = null; + if (t.isDataFlavorSupported(uriListFlavor)) + { + Cache.log.debug("Drop handled as uriListFlavor"); + // This is used by Unix drag system + evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); + data = (String) t.getTransferData(uriListFlavor); + } + if (data == null) + { + // fallback to text: workaround - on OSX where there's a JVM bug + Cache.log.debug("standard URIListFlavor failed. Trying text"); + // try text fallback + data = (String) t.getTransferData(new DataFlavor( + "text/plain;class=java.lang.String")); + if (Cache.log.isDebugEnabled()) + { + Cache.log.debug("fallback returned " + data); + } + } + while (protocols.size() < files.size()) + { + Cache.log.debug("Adding missing FILE protocol for " + + files.get(protocols.size())); + protocols.add(FormatAdapter.FILE); + } + for (java.util.StringTokenizer st = new java.util.StringTokenizer( + data, "\r\n"); st.hasMoreTokens();) + { + added = true; + String s = st.nextToken(); + if (s.startsWith("#")) + { + // the line is a comment (as per the RFC 2483) + continue; + } + java.net.URI uri = new java.net.URI(s); + if (uri.getScheme().toLowerCase().startsWith("http")) + { + protocols.add(FormatAdapter.URL); + files.add(uri.toString()); + } + else + { + // otherwise preserve old behaviour: catch all for file objects + java.io.File file = new java.io.File(uri); + protocols.add(FormatAdapter.FILE); + files.add(file.toString()); + } + } + if (Cache.log.isDebugEnabled()) + { + if (data == null || !added) + { + Cache.log + .debug("Couldn't resolve drop data. Here are the supported flavors:"); + for (DataFlavor fl : t.getTransferDataFlavors()) + { + Cache.log.debug("Supported transfer dataflavor: " + + fl.toString()); + evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); + Object df = t.getTransferData(fl); + if (df != null) + { + Cache.log.debug("Retrieves: " + df); + } + else + { + Cache.log.debug("Retrieved nothing"); + } + } + } + } + } + } }