X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fappletgui%2FEmbmenuFrame.java;h=8b2b279e48c306414b5720078811f971a32a4c63;hb=b9b1f47cc74bbec8c28b75776e1d00c258215dfb;hp=6c0d3a92044c31f940983ba280816b2eb336a9f2;hpb=47168f025aefdaa044802bd5f8f510ffe43a4808;p=jalview.git diff --git a/src/jalview/appletgui/EmbmenuFrame.java b/src/jalview/appletgui/EmbmenuFrame.java index 6c0d3a9..8b2b279 100644 --- a/src/jalview/appletgui/EmbmenuFrame.java +++ b/src/jalview/appletgui/EmbmenuFrame.java @@ -1,6 +1,6 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2) - * Copyright (C) 2014 The Jalview Authors + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors * * This file is part of Jalview. * @@ -20,6 +20,8 @@ */ package jalview.appletgui; +import jalview.util.Platform; + import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; @@ -33,12 +35,12 @@ import java.awt.Panel; import java.awt.PopupMenu; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; -import java.util.Enumeration; -import java.util.Hashtable; +import java.util.HashMap; +import java.util.Map; /** - * This class implements a pattern form embedding toolbars as a panel with - * popups for situations where the system menu bar is either invisible or + * This class implements a pattern for embedding toolbars as a panel with popups + * for situations where the system menu bar is either invisible or * inappropriate. It was derived from the code for embedding the jalview applet * alignFrame as a component on the web-page, which requires the local * alignFrame menu to be attached to that panel rather than placed on the parent @@ -49,12 +51,18 @@ import java.util.Hashtable; * @author Jim Procter and Andrew Waterhouse * */ -public class EmbmenuFrame extends Frame implements MouseListener +public class EmbmenuFrame extends Frame + implements MouseListener, AutoCloseable { + protected static final Font FONT_ARIAL_PLAIN_11 = new Font("Arial", + Font.PLAIN, 11); + + public static final Font DEFAULT_MENU_FONT = FONT_ARIAL_PLAIN_11; + /** * map from labels to popup menus for the embedded menubar */ - protected Hashtable embeddedPopup; + protected Map embeddedPopup = new HashMap<>(); /** * the embedded menu is built on this and should be added to the frame at the @@ -90,79 +98,45 @@ public class EmbmenuFrame extends Frame implements MouseListener return false; } // DEBUG Hint: can test embedded menus by inserting true here. - if (new jalview.util.Platform().isAMac()) + if (Platform.isAMacAndNotJS()) { - // Build the embedded menu panel - embeddedMenu = makeEmbeddedPopupMenu(topMenuBar, "Arial", Font.PLAIN, - 11, true); // try to pickup system font. + // Build the embedded menu panel, allowing override with system font + embeddedMenu = makeEmbeddedPopupMenu(topMenuBar, true, false); setMenuBar(null); - // add the components to the TreePanel area. + // add the components to the Panel area. add(embeddedMenu, BorderLayout.NORTH); - tobeAdjusted.setSize(getSize().width, getSize().height - - embeddedMenu.HEIGHT); + tobeAdjusted.setSize(getSize().width, + getSize().height - embeddedMenu.getHeight()); return true; } return false; } /** - * move all menus on menuBar onto embeddedMenu. embeddedPopup is used to store - * the popups for each menu removed from the menuBar and added to the panel. - * NOTE: it is up to the caller to remove menuBar from the Frame if it is - * already attached. - * - * @param menuBar - * @param fn - * @param fstyle - * @param fsz - * @param overrideFonts - * true if we take the menuBar fonts in preference to the supplied - * defaults - * @return the embedded menu instance to be added to the frame. - */ - protected Panel makeEmbeddedPopupMenu(MenuBar menuBar, String fn, - int fstyle, int fsz, boolean overrideFonts) - { - return makeEmbeddedPopupMenu(menuBar, fn, fstyle, fsz, overrideFonts, - false); - } - - /** * Create or add elements to the embedded menu from menuBar. This removes all * menu from menuBar and it is up to the caller to remove the now useless * menuBar from the Frame if it is already attached. * * @param menuBar - * @param fn - * @param fstyle - * @param fsz * @param overrideFonts * @param append * true means existing menu will be emptied before adding new * elements * @return */ - protected Panel makeEmbeddedPopupMenu(MenuBar menuBar, String fn, - int fstyle, int fsz, boolean overrideFonts, boolean append) + protected Panel makeEmbeddedPopupMenu(MenuBar menuBar, + boolean overrideFonts, boolean append) { if (!append) { - if (embeddedPopup != null) - { - embeddedPopup.clear(); // TODO: check if j1.1 - } + embeddedPopup.clear(); // TODO: check if j1.1 if (embeddedMenu != null) { embeddedMenu.removeAll(); } } - if (embeddedPopup == null) - { - embeddedPopup = new Hashtable(); - } - - embeddedMenu = makeEmbeddedPopupMenu(menuBar, fn, fstyle, fsz, - overrideFonts, embeddedPopup, new Panel(), this); + embeddedMenu = makeEmbeddedPopupMenu(menuBar, DEFAULT_MENU_FONT, + overrideFonts, new Panel(), this); return embeddedMenu; } @@ -175,12 +149,8 @@ public class EmbmenuFrame extends Frame implements MouseListener * * @param menuBar * must be non-null - * @param fn - * @param fstyle - * @param fsz + * @param font * @param overrideFonts - * @param embeddedPopup - * must be non-null * @param embeddedMenu * if null, a new panel will be created and returned * @param clickHandler @@ -188,28 +158,22 @@ public class EmbmenuFrame extends Frame implements MouseListener * embeddedPopup and embeddedMenu * @return the panel instance for convenience. */ - protected Panel makeEmbeddedPopupMenu(MenuBar menuBar, String fn, - int fstyle, int fsz, boolean overrideFonts, - Hashtable embeddedPopup, Panel embeddedMenu, + protected Panel makeEmbeddedPopupMenu(MenuBar menuBar, Font font, + boolean overrideFonts, Panel embeddedMenu, MouseListener clickHandler) { - if (embeddedPopup == null) - { - throw new Error( - "Implementation error - embeddedPopup must be non-null"); - } if (overrideFonts) { Font mbf = menuBar.getFont(); if (mbf != null) { - fn = mbf.getName(); - fstyle = mbf.getStyle(); - fsz = mbf.getSize(); + font = mbf; } } if (embeddedMenu == null) + { embeddedMenu = new Panel(); + } FlowLayout flowLayout1 = new FlowLayout(); embeddedMenu.setBackground(Color.lightGray); embeddedMenu.setLayout(flowLayout1); @@ -218,7 +182,7 @@ public class EmbmenuFrame extends Frame implements MouseListener { Menu mi = menuBar.getMenu(mbi); Label elab = new Label(mi.getLabel()); - elab.setFont(new java.awt.Font(fn, fstyle, fsz)); + elab.setFont(font); // add the menu entries PopupMenu popup = new PopupMenu(); int m, mSize = mi.getItemCount(); @@ -238,6 +202,7 @@ public class EmbmenuFrame extends Frame implements MouseListener return embeddedMenu; } + @Override public void mousePressed(MouseEvent evt) { PopupMenu popup = null; @@ -246,8 +211,8 @@ public class EmbmenuFrame extends Frame implements MouseListener if (popup != null) { embeddedMenu.add(popup); - popup.show(embeddedMenu, source.getBounds().x, source.getBounds().y - + source.getBounds().getSize().height); + popup.show(embeddedMenu, source.getBounds().x, + source.getBounds().y + source.getBounds().getSize().height); } } @@ -259,21 +224,25 @@ public class EmbmenuFrame extends Frame implements MouseListener */ PopupMenu getPopupMenu(Label source) { - return (PopupMenu) embeddedPopup.get(source); + return embeddedPopup.get(source); } + @Override public void mouseClicked(MouseEvent evt) { } + @Override public void mouseReleased(MouseEvent evt) { } + @Override public void mouseEntered(MouseEvent evt) { } + @Override public void mouseExited(MouseEvent evt) { } @@ -286,10 +255,8 @@ public class EmbmenuFrame extends Frame implements MouseListener { if (embeddedPopup != null) { - Enumeration e = embeddedPopup.keys(); - while (e.hasMoreElements()) + for (Label lb : embeddedPopup.keySet()) { - Label lb = (Label) e.nextElement(); lb.removeMouseListener(this); } embeddedPopup.clear(); @@ -303,11 +270,11 @@ public class EmbmenuFrame extends Frame implements MouseListener /** * calls destroyMenus() */ - public void finalize() throws Throwable + @Override + public void close() { destroyMenus(); embeddedPopup = null; embeddedMenu = null; - super.finalize(); } }