2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
\r
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.appletgui;
\r
21 import java.awt.BorderLayout;
\r
22 import java.awt.Color;
\r
23 import java.awt.Container;
\r
24 import java.awt.FlowLayout;
\r
25 import java.awt.Font;
\r
26 import java.awt.Frame;
\r
27 import java.awt.GraphicsConfiguration;
\r
28 import java.awt.HeadlessException;
\r
29 import java.awt.Label;
\r
30 import java.awt.Menu;
\r
31 import java.awt.MenuBar;
\r
32 import java.awt.Panel;
\r
33 import java.awt.PopupMenu;
\r
34 import java.awt.event.ActionListener;
\r
35 import java.awt.event.ItemListener;
\r
36 import java.awt.event.MouseEvent;
\r
37 import java.awt.event.MouseListener;
\r
38 import java.util.Enumeration;
\r
39 import java.util.Hashtable;
\r
41 * This class implements a pattern form embedding toolbars as a panel with popups
\r
42 * for situations where the system menu bar is either invisible or inappropriate.
\r
43 * It was derived from the code for embedding the jalview applet alignFrame as
\r
44 * a component on the web-page, which requires the local alignFrame menu to be
\r
45 * attached to that panel rather than placed on the parent (which isn't allowed
\r
47 * TODO: try to modify the embeddedMenu display so it looks like a real toolbar menu
\r
48 * TODO: modify click/mouse handler for embeddedMenu so it behaves more like a real pulldown menu toolbar
\r
50 * @author Jim Procter and Andrew Waterhouse
\r
53 public class EmbmenuFrame extends Frame implements MouseListener
\r
56 * map from labels to popup menus for the embedded menubar
\r
58 protected Hashtable embeddedPopup;
\r
60 * the embedded menu is built on this and should be
\r
61 * added to the frame at the appropriate position.
\r
64 protected Panel embeddedMenu;
\r
66 public EmbmenuFrame() throws HeadlessException
\r
70 public EmbmenuFrame(String title) throws HeadlessException
\r
75 * Check if the applet is running on a platform that requires the Frame menuBar to be embedded,
\r
76 * and if so, embeds it.
\r
77 * @param tobeAdjusted the panel that is to be reduced to make space for the embedded menu bar
\r
78 * @return true if menuBar was embedded and tobeAdjusted's height modified
\r
80 protected boolean embedMenuIfNeeded(Panel tobeAdjusted)
\r
82 MenuBar topMenuBar = getMenuBar();
\r
83 if (topMenuBar==null)
\r
87 // DEBUG Hint: can test embedded menus by inserting true here.
\r
88 if (new jalview.util.Platform().isAMac())
\r
90 // Build the embedded menu panel
\r
91 embeddedMenu = makeEmbeddedPopupMenu(topMenuBar, "Arial", Font.PLAIN, 10, true); // try to pickup system font.
\r
93 // add the components to the TreePanel area.
\r
94 add(embeddedMenu, BorderLayout.NORTH);
\r
95 tobeAdjusted.setSize(getSize().width,
\r
97 - embeddedMenu.HEIGHT);
\r
103 * move all menus on menuBar onto embeddedMenu.
\r
104 * embeddedPopup is used to store the popups for each menu removed from the menuBar and added
\r
106 * NOTE: it is up to the caller to remove menuBar from the Frame if it is already attached.
\r
111 * @param overrideFonts true if we take the menuBar fonts in preference to the supplied defaults
\r
112 * @return the embedded menu instance to be added to the frame.
\r
114 protected Panel makeEmbeddedPopupMenu(MenuBar menuBar, String fn,
\r
115 int fstyle, int fsz, boolean overrideFonts)
\r
117 return makeEmbeddedPopupMenu(menuBar, fn, fstyle, fsz, overrideFonts, false);
\r
120 * Create or add elements to the embedded menu from menuBar. This removes all menu from
\r
121 * menuBar and it is up to the caller to remove the now useless menuBar from the Frame if it is already attached.
\r
126 * @param overrideFonts
\r
127 * @param append true means existing menu will be emptied before adding new elements
\r
130 protected Panel makeEmbeddedPopupMenu(MenuBar menuBar, String fn,
\r
131 int fstyle, int fsz, boolean overrideFonts, boolean append)
\r
135 if (embeddedPopup != null)
\r
137 embeddedPopup.clear(); // TODO: check if j1.1
\r
139 if (embeddedMenu!=null)
\r
141 embeddedMenu.removeAll();
\r
144 if (embeddedPopup==null)
\r
146 embeddedPopup = new Hashtable();
\r
149 embeddedMenu = makeEmbeddedPopupMenu(menuBar, fn, fstyle, fsz, overrideFonts, embeddedPopup, new Panel(), this);
\r
150 return embeddedMenu;
\r
153 * Generic method to move elements from menubar onto embeddedMenu using the existing or the supplied font,
\r
154 * and adds binding from panel to attached menus in embeddedPopup
\r
155 * This removes all menu from
\r
156 * menuBar and it is up to the caller to remove the now useless menuBar from the Frame if it is already attached.
\r
157 * @param menuBar must be non-null
\r
161 * @param overrideFonts
\r
162 * @param embeddedPopup must be non-null
\r
163 * @param embeddedMenu if null, a new panel will be created and returned
\r
164 * @param clickHandler - usually the instance of EmbmenuFrame that holds references to embeddedPopup and embeddedMenu
\r
165 * @return the panel instance for convenience.
\r
167 protected Panel makeEmbeddedPopupMenu(MenuBar menuBar, String fn,
\r
168 int fstyle, int fsz, boolean overrideFonts, Hashtable embeddedPopup, Panel embeddedMenu, MouseListener clickHandler)
\r
170 if (embeddedPopup==null)
\r
172 throw new Error("Implementation error - embeddedPopup must be non-null");
\r
176 Font mbf = menuBar.getFont();
\r
179 fn = mbf.getName();
\r
180 fstyle = mbf.getStyle();
\r
181 fsz = mbf.getSize();
\r
184 if (embeddedMenu==null)
\r
185 embeddedMenu = new Panel();
\r
186 FlowLayout flowLayout1 = new FlowLayout();
\r
187 embeddedMenu.setBackground(Color.lightGray);
\r
188 embeddedMenu.setLayout(flowLayout1);
\r
190 for (int mbi = 0,nMbi=menuBar.getMenuCount(); mbi<nMbi; mbi++)
\r
192 Menu mi = menuBar.getMenu(mbi);
\r
193 Label elab = new Label(mi.getLabel());
\r
194 elab.setFont(new java.awt.Font(fn, fstyle, fsz));
\r
195 // add the menu entries
\r
196 PopupMenu popup = new PopupMenu();
\r
197 int m, mSize = mi.getItemCount();
\r
198 for (m = 0; m < mSize; m++)
\r
200 popup.add(mi.getItem(m));
\r
204 embeddedPopup.put(elab, popup);
\r
205 embeddedMenu.add(elab);
\r
206 elab.addMouseListener(clickHandler);
\r
208 flowLayout1.setAlignment(FlowLayout.LEFT);
\r
209 flowLayout1.setHgap(2);
\r
210 flowLayout1.setVgap(0);
\r
211 return embeddedMenu;
\r
214 public void mousePressed(MouseEvent evt)
\r
216 PopupMenu popup = null;
\r
217 Label source = (Label) evt.getSource();
\r
218 popup = getPopupMenu(source);
\r
221 embeddedMenu.add(popup);
\r
222 popup.show(embeddedMenu,
\r
223 source.getBounds().x,
\r
224 source.getBounds().y + source.getBounds().getSize().height);
\r
229 * get the menu for source from the hash.
\r
230 * @param source what was clicked on.
\r
232 PopupMenu getPopupMenu(Label source)
\r
234 return (PopupMenu) embeddedPopup.get(source);
\r
237 public void mouseClicked(MouseEvent evt)
\r
240 public void mouseReleased(MouseEvent evt)
\r
243 public void mouseEntered(MouseEvent evt)
\r
246 public void mouseExited(MouseEvent evt)
\r
250 * called to clear the GUI resources taken up for embedding
\r
251 * and remove any self references so we can be garbage collected.
\r
253 public void destroyMenus()
\r
255 if (embeddedPopup!=null)
\r
257 Enumeration e = embeddedPopup.keys();
\r
258 while (e.hasMoreElements())
\r
260 Label lb = (Label) e.nextElement();
\r
261 lb.removeMouseListener(this);
\r
263 embeddedPopup.clear();
\r
265 if (embeddedMenu!=null)
\r
267 embeddedMenu.removeAll();
\r
271 * calls destroyMenus()
\r
273 public void finalize() throws Throwable {
\r
275 embeddedPopup=null;
\r