2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.MessageManager;
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.Enumeration;
39 import java.util.Hashtable;
42 * This class implements a pattern form embedding toolbars as a panel with
43 * popups 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 implements MouseListener
57 * map from labels to popup menus for the embedded menubar
59 protected Hashtable embeddedPopup;
62 * the embedded menu is built on this and should be added to the frame at the
63 * appropriate position.
66 protected Panel embeddedMenu;
68 public EmbmenuFrame() throws HeadlessException
73 public EmbmenuFrame(String title) throws HeadlessException
79 * Check if the applet is running on a platform that requires the Frame
80 * menuBar to be embedded, and if so, embeds it.
83 * the panel that is to be reduced to make space for the embedded
85 * @return true if menuBar was embedded and tobeAdjusted's height modified
87 protected boolean embedMenuIfNeeded(Panel tobeAdjusted)
89 MenuBar topMenuBar = getMenuBar();
90 if (topMenuBar == null)
94 // DEBUG Hint: can test embedded menus by inserting true here.
95 if (new jalview.util.Platform().isAMac())
97 // Build the embedded menu panel
98 embeddedMenu = makeEmbeddedPopupMenu(topMenuBar, "Arial", Font.PLAIN,
99 11, true); // try to pickup system font.
101 // add the components to the TreePanel area.
102 add(embeddedMenu, BorderLayout.NORTH);
103 tobeAdjusted.setSize(getSize().width, getSize().height
104 - embeddedMenu.HEIGHT);
111 * move all menus on menuBar onto embeddedMenu. embeddedPopup is used to store
112 * the popups for each menu removed from the menuBar and added to the panel.
113 * NOTE: it is up to the caller to remove menuBar from the Frame if it is
120 * @param overrideFonts
121 * true if we take the menuBar fonts in preference to the supplied
123 * @return the embedded menu instance to be added to the frame.
125 protected Panel makeEmbeddedPopupMenu(MenuBar menuBar, String fn,
126 int fstyle, int fsz, boolean overrideFonts)
128 return makeEmbeddedPopupMenu(menuBar, fn, fstyle, fsz, overrideFonts,
133 * Create or add elements to the embedded menu from menuBar. This removes all
134 * menu from menuBar and it is up to the caller to remove the now useless
135 * menuBar from the Frame if it is already attached.
141 * @param overrideFonts
143 * true means existing menu will be emptied before adding new
147 protected Panel makeEmbeddedPopupMenu(MenuBar menuBar, String fn,
148 int fstyle, int fsz, boolean overrideFonts, boolean append)
152 if (embeddedPopup != null)
154 embeddedPopup.clear(); // TODO: check if j1.1
156 if (embeddedMenu != null)
158 embeddedMenu.removeAll();
161 if (embeddedPopup == null)
163 embeddedPopup = new Hashtable();
166 embeddedMenu = makeEmbeddedPopupMenu(menuBar, fn, fstyle, fsz,
167 overrideFonts, embeddedPopup, new Panel(), this);
172 * Generic method to move elements from menubar onto embeddedMenu using the
173 * existing or the supplied font, and adds binding from panel to attached
174 * menus in embeddedPopup This removes all menu from menuBar and it is up to
175 * the caller to remove the now useless menuBar from the Frame if it is
183 * @param overrideFonts
184 * @param embeddedPopup
186 * @param embeddedMenu
187 * if null, a new panel will be created and returned
188 * @param clickHandler
189 * - usually the instance of EmbmenuFrame that holds references to
190 * embeddedPopup and embeddedMenu
191 * @return the panel instance for convenience.
193 protected Panel makeEmbeddedPopupMenu(MenuBar menuBar, String fn,
194 int fstyle, int fsz, boolean overrideFonts,
195 Hashtable embeddedPopup, Panel embeddedMenu,
196 MouseListener clickHandler)
198 if (embeddedPopup == null)
200 throw new Error(MessageManager.getString("error.implementation_error_embeddedpopup_not_null"));
204 Font mbf = menuBar.getFont();
208 fstyle = mbf.getStyle();
212 if (embeddedMenu == null)
213 embeddedMenu = new Panel();
214 FlowLayout flowLayout1 = new FlowLayout();
215 embeddedMenu.setBackground(Color.lightGray);
216 embeddedMenu.setLayout(flowLayout1);
218 for (int mbi = 0, nMbi = menuBar.getMenuCount(); mbi < nMbi; mbi++)
220 Menu mi = menuBar.getMenu(mbi);
221 Label elab = new Label(mi.getLabel());
222 elab.setFont(new java.awt.Font(fn, fstyle, fsz));
223 // add the menu entries
224 PopupMenu popup = new PopupMenu();
225 int m, mSize = mi.getItemCount();
226 for (m = 0; m < mSize; m++)
228 popup.add(mi.getItem(m));
232 embeddedPopup.put(elab, popup);
233 embeddedMenu.add(elab);
234 elab.addMouseListener(clickHandler);
236 flowLayout1.setAlignment(FlowLayout.LEFT);
237 flowLayout1.setHgap(2);
238 flowLayout1.setVgap(0);
242 public void mousePressed(MouseEvent evt)
244 PopupMenu popup = null;
245 Label source = (Label) evt.getSource();
246 popup = getPopupMenu(source);
249 embeddedMenu.add(popup);
250 popup.show(embeddedMenu, source.getBounds().x, source.getBounds().y
251 + source.getBounds().getSize().height);
256 * get the menu for source from the hash.
259 * what was clicked on.
261 PopupMenu getPopupMenu(Label source)
263 return (PopupMenu) embeddedPopup.get(source);
266 public void mouseClicked(MouseEvent evt)
270 public void mouseReleased(MouseEvent evt)
274 public void mouseEntered(MouseEvent evt)
278 public void mouseExited(MouseEvent evt)
283 * called to clear the GUI resources taken up for embedding and remove any
284 * self references so we can be garbage collected.
286 public void destroyMenus()
288 if (embeddedPopup != null)
290 Enumeration e = embeddedPopup.keys();
291 while (e.hasMoreElements())
293 Label lb = (Label) e.nextElement();
294 lb.removeMouseListener(this);
296 embeddedPopup.clear();
298 if (embeddedMenu != null)
300 embeddedMenu.removeAll();
305 * calls destroyMenus()
307 public void finalize() throws Throwable
310 embeddedPopup = null;