c527308eba0da8b49b952b78a700112972772885
[jalview.git] / src / jalview / appletgui / EmbmenuFrame.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.appletgui;
22
23 import jalview.util.MessageManager;
24
25 import java.awt.BorderLayout;
26 import java.awt.Color;
27 import java.awt.FlowLayout;
28 import java.awt.Font;
29 import java.awt.Frame;
30 import java.awt.HeadlessException;
31 import java.awt.Label;
32 import java.awt.Menu;
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;
40
41 /**
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
50  * 
51  * @author Jim Procter and Andrew Waterhouse
52  * 
53  */
54 public class EmbmenuFrame extends Frame implements MouseListener
55 {
56   /**
57    * map from labels to popup menus for the embedded menubar
58    */
59   protected Hashtable embeddedPopup;
60
61   /**
62    * the embedded menu is built on this and should be added to the frame at the
63    * appropriate position.
64    * 
65    */
66   protected Panel embeddedMenu;
67
68   public EmbmenuFrame() throws HeadlessException
69   {
70     super();
71   }
72
73   public EmbmenuFrame(String title) throws HeadlessException
74   {
75     super(title);
76   }
77
78   /**
79    * Check if the applet is running on a platform that requires the Frame
80    * menuBar to be embedded, and if so, embeds it.
81    * 
82    * @param tobeAdjusted
83    *          the panel that is to be reduced to make space for the embedded
84    *          menu bar
85    * @return true if menuBar was embedded and tobeAdjusted's height modified
86    */
87   protected boolean embedMenuIfNeeded(Panel tobeAdjusted)
88   {
89     MenuBar topMenuBar = getMenuBar();
90     if (topMenuBar == null)
91     {
92       return false;
93     }
94     // DEBUG Hint: can test embedded menus by inserting true here.
95     if (new jalview.util.Platform().isAMac())
96     {
97       // Build the embedded menu panel
98       embeddedMenu = makeEmbeddedPopupMenu(topMenuBar, "Arial", Font.PLAIN,
99               11, true); // try to pickup system font.
100       setMenuBar(null);
101       // add the components to the TreePanel area.
102       add(embeddedMenu, BorderLayout.NORTH);
103       tobeAdjusted.setSize(getSize().width, getSize().height
104               - embeddedMenu.HEIGHT);
105       return true;
106     }
107     return false;
108   }
109
110   /**
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
114    * already attached.
115    * 
116    * @param menuBar
117    * @param fn
118    * @param fstyle
119    * @param fsz
120    * @param overrideFonts
121    *          true if we take the menuBar fonts in preference to the supplied
122    *          defaults
123    * @return the embedded menu instance to be added to the frame.
124    */
125   protected Panel makeEmbeddedPopupMenu(MenuBar menuBar, String fn,
126           int fstyle, int fsz, boolean overrideFonts)
127   {
128     return makeEmbeddedPopupMenu(menuBar, fn, fstyle, fsz, overrideFonts,
129             false);
130   }
131
132   /**
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.
136    * 
137    * @param menuBar
138    * @param fn
139    * @param fstyle
140    * @param fsz
141    * @param overrideFonts
142    * @param append
143    *          true means existing menu will be emptied before adding new
144    *          elements
145    * @return
146    */
147   protected Panel makeEmbeddedPopupMenu(MenuBar menuBar, String fn,
148           int fstyle, int fsz, boolean overrideFonts, boolean append)
149   {
150     if (!append)
151     {
152       if (embeddedPopup != null)
153       {
154         embeddedPopup.clear(); // TODO: check if j1.1
155       }
156       if (embeddedMenu != null)
157       {
158         embeddedMenu.removeAll();
159       }
160     }
161     if (embeddedPopup == null)
162     {
163       embeddedPopup = new Hashtable();
164     }
165
166     embeddedMenu = makeEmbeddedPopupMenu(menuBar, fn, fstyle, fsz,
167             overrideFonts, embeddedPopup, new Panel(), this);
168     return embeddedMenu;
169   }
170
171   /**
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
176    * already attached.
177    * 
178    * @param menuBar
179    *          must be non-null
180    * @param fn
181    * @param fstyle
182    * @param fsz
183    * @param overrideFonts
184    * @param embeddedPopup
185    *          must be non-null
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.
192    */
193   protected Panel makeEmbeddedPopupMenu(MenuBar menuBar, String fn,
194           int fstyle, int fsz, boolean overrideFonts,
195           Hashtable embeddedPopup, Panel embeddedMenu,
196           MouseListener clickHandler)
197   {
198     if (embeddedPopup == null)
199     {
200       throw new Error(MessageManager.getString("error.implementation_error_embeddedpopup_not_null"));
201     }
202     if (overrideFonts)
203     {
204       Font mbf = menuBar.getFont();
205       if (mbf != null)
206       {
207         fn = mbf.getName();
208         fstyle = mbf.getStyle();
209         fsz = mbf.getSize();
210       }
211     }
212     if (embeddedMenu == null)
213       embeddedMenu = new Panel();
214     FlowLayout flowLayout1 = new FlowLayout();
215     embeddedMenu.setBackground(Color.lightGray);
216     embeddedMenu.setLayout(flowLayout1);
217     // loop thru
218     for (int mbi = 0, nMbi = menuBar.getMenuCount(); mbi < nMbi; mbi++)
219     {
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++)
227       {
228         popup.add(mi.getItem(m));
229         mSize--;
230         m--;
231       }
232       embeddedPopup.put(elab, popup);
233       embeddedMenu.add(elab);
234       elab.addMouseListener(clickHandler);
235     }
236     flowLayout1.setAlignment(FlowLayout.LEFT);
237     flowLayout1.setHgap(2);
238     flowLayout1.setVgap(0);
239     return embeddedMenu;
240   }
241
242   public void mousePressed(MouseEvent evt)
243   {
244     PopupMenu popup = null;
245     Label source = (Label) evt.getSource();
246     popup = getPopupMenu(source);
247     if (popup != null)
248     {
249       embeddedMenu.add(popup);
250       popup.show(embeddedMenu, source.getBounds().x, source.getBounds().y
251               + source.getBounds().getSize().height);
252     }
253   }
254
255   /**
256    * get the menu for source from the hash.
257    * 
258    * @param source
259    *          what was clicked on.
260    */
261   PopupMenu getPopupMenu(Label source)
262   {
263     return (PopupMenu) embeddedPopup.get(source);
264   }
265
266   public void mouseClicked(MouseEvent evt)
267   {
268   }
269
270   public void mouseReleased(MouseEvent evt)
271   {
272   }
273
274   public void mouseEntered(MouseEvent evt)
275   {
276   }
277
278   public void mouseExited(MouseEvent evt)
279   {
280   }
281
282   /**
283    * called to clear the GUI resources taken up for embedding and remove any
284    * self references so we can be garbage collected.
285    */
286   public void destroyMenus()
287   {
288     if (embeddedPopup != null)
289     {
290       Enumeration e = embeddedPopup.keys();
291       while (e.hasMoreElements())
292       {
293         Label lb = (Label) e.nextElement();
294         lb.removeMouseListener(this);
295       }
296       embeddedPopup.clear();
297     }
298     if (embeddedMenu != null)
299     {
300       embeddedMenu.removeAll();
301     }
302   }
303
304   /**
305    * calls destroyMenus()
306    */
307   public void finalize() throws Throwable
308   {
309     destroyMenus();
310     embeddedPopup = null;
311     embeddedMenu = null;
312     super.finalize();
313   }
314 }