2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.appletgui;
23 import jalview.util.Platform;
25 import java.awt.BorderLayout;
26 import java.awt.Color;
27 import java.awt.FlowLayout;
29 import java.awt.Frame;
30 import java.awt.HeadlessException;
31 import java.awt.Label;
33 import java.awt.MenuBar;
34 import java.awt.Panel;
35 import java.awt.PopupMenu;
36 import java.awt.event.MouseEvent;
37 import java.awt.event.MouseListener;
38 import java.util.HashMap;
42 * This class implements a pattern for embedding toolbars as a panel with popups
43 * for situations where the system menu bar is either invisible or
44 * inappropriate. It was derived from the code for embedding the jalview applet
45 * alignFrame as a component on the web-page, which requires the local
46 * alignFrame menu to be attached to that panel rather than placed on the parent
47 * (which isn't allowed anyhow). TODO: try to modify the embeddedMenu display so
48 * it looks like a real toolbar menu TODO: modify click/mouse handler for
49 * embeddedMenu so it behaves more like a real pulldown menu toolbar
51 * @author Jim Procter and Andrew Waterhouse
54 public class EmbmenuFrame extends Frame
55 implements MouseListener, AutoCloseable
57 protected static final Font FONT_ARIAL_PLAIN_11 = new Font("Arial",
60 public static final Font DEFAULT_MENU_FONT = FONT_ARIAL_PLAIN_11;
63 * map from labels to popup menus for the embedded menubar
65 protected Map<Label, PopupMenu> embeddedPopup = new HashMap<>();
68 * the embedded menu is built on this and should be added to the frame at the
69 * appropriate position.
72 protected Panel embeddedMenu;
74 public EmbmenuFrame() throws HeadlessException
79 public EmbmenuFrame(String title) throws HeadlessException
85 * Check if the applet is running on a platform that requires the Frame
86 * menuBar to be embedded, and if so, embeds it.
89 * the panel that is to be reduced to make space for the embedded
91 * @return true if menuBar was embedded and tobeAdjusted's height modified
93 protected boolean embedMenuIfNeeded(Panel tobeAdjusted)
95 MenuBar topMenuBar = getMenuBar();
96 if (topMenuBar == null)
100 // DEBUG Hint: can test embedded menus by inserting true here.
101 if (Platform.isAMacAndNotJS())
103 // Build the embedded menu panel, allowing override with system font
104 embeddedMenu = makeEmbeddedPopupMenu(topMenuBar, true, false);
106 // add the components to the Panel area.
107 add(embeddedMenu, BorderLayout.NORTH);
108 tobeAdjusted.setSize(getSize().width,
109 getSize().height - embeddedMenu.getHeight());
116 * Create or add elements to the embedded menu from menuBar. This removes all
117 * menu from menuBar and it is up to the caller to remove the now useless
118 * menuBar from the Frame if it is already attached.
121 * @param overrideFonts
123 * true means existing menu will be emptied before adding new
127 protected Panel makeEmbeddedPopupMenu(MenuBar menuBar,
128 boolean overrideFonts, boolean append)
132 embeddedPopup.clear(); // TODO: check if j1.1
133 if (embeddedMenu != null)
135 embeddedMenu.removeAll();
138 embeddedMenu = makeEmbeddedPopupMenu(menuBar, DEFAULT_MENU_FONT,
139 overrideFonts, new Panel(), this);
144 * Generic method to move elements from menubar onto embeddedMenu using the
145 * existing or the supplied font, and adds binding from panel to attached
146 * menus in embeddedPopup This removes all menu from menuBar and it is up to
147 * the caller to remove the now useless menuBar from the Frame if it is
153 * @param overrideFonts
154 * @param embeddedMenu
155 * if null, a new panel will be created and returned
156 * @param clickHandler
157 * - usually the instance of EmbmenuFrame that holds references to
158 * embeddedPopup and embeddedMenu
159 * @return the panel instance for convenience.
161 protected Panel makeEmbeddedPopupMenu(MenuBar menuBar, Font font,
162 boolean overrideFonts, Panel embeddedMenu,
163 MouseListener clickHandler)
167 Font mbf = menuBar.getFont();
173 if (embeddedMenu == null)
175 embeddedMenu = new Panel();
177 FlowLayout flowLayout1 = new FlowLayout();
178 embeddedMenu.setBackground(Color.lightGray);
179 embeddedMenu.setLayout(flowLayout1);
181 for (int mbi = 0, nMbi = menuBar.getMenuCount(); mbi < nMbi; mbi++)
183 Menu mi = menuBar.getMenu(mbi);
184 Label elab = new Label(mi.getLabel());
186 // add the menu entries
187 PopupMenu popup = new PopupMenu();
188 int m, mSize = mi.getItemCount();
189 for (m = 0; m < mSize; m++)
191 popup.add(mi.getItem(m));
195 embeddedPopup.put(elab, popup);
196 embeddedMenu.add(elab);
197 elab.addMouseListener(clickHandler);
199 flowLayout1.setAlignment(FlowLayout.LEFT);
200 flowLayout1.setHgap(2);
201 flowLayout1.setVgap(0);
206 public void mousePressed(MouseEvent evt)
208 PopupMenu popup = null;
209 Label source = (Label) evt.getSource();
210 popup = getPopupMenu(source);
213 embeddedMenu.add(popup);
214 popup.show(embeddedMenu, source.getBounds().x,
215 source.getBounds().y + source.getBounds().getSize().height);
220 * get the menu for source from the hash.
223 * what was clicked on.
225 PopupMenu getPopupMenu(Label source)
227 return embeddedPopup.get(source);
231 public void mouseClicked(MouseEvent evt)
236 public void mouseReleased(MouseEvent evt)
241 public void mouseEntered(MouseEvent evt)
246 public void mouseExited(MouseEvent evt)
251 * called to clear the GUI resources taken up for embedding and remove any
252 * self references so we can be garbage collected.
254 public void destroyMenus()
256 if (embeddedPopup != null)
258 for (Label lb : embeddedPopup.keySet())
260 lb.removeMouseListener(this);
262 embeddedPopup.clear();
264 if (embeddedMenu != null)
266 embeddedMenu.removeAll();
271 * calls destroyMenus()
277 embeddedPopup = null;