JAL-1355
[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 java.awt.BorderLayout;
24 import java.awt.Color;
25 import java.awt.FlowLayout;
26 import java.awt.Font;
27 import java.awt.Frame;
28 import java.awt.HeadlessException;
29 import java.awt.Label;
30 import java.awt.Menu;
31 import java.awt.MenuBar;
32 import java.awt.Panel;
33 import java.awt.PopupMenu;
34 import java.awt.event.MouseEvent;
35 import java.awt.event.MouseListener;
36 import java.util.Enumeration;
37 import java.util.Hashtable;
38
39 /**
40  * This class implements a pattern form embedding toolbars as a panel with
41  * popups for situations where the system menu bar is either invisible or
42  * inappropriate. It was derived from the code for embedding the jalview applet
43  * alignFrame as a component on the web-page, which requires the local
44  * alignFrame menu to be attached to that panel rather than placed on the parent
45  * (which isn't allowed anyhow). TODO: try to modify the embeddedMenu display so
46  * it looks like a real toolbar menu TODO: modify click/mouse handler for
47  * embeddedMenu so it behaves more like a real pulldown menu toolbar
48  * 
49  * @author Jim Procter and Andrew Waterhouse
50  * 
51  */
52 public class EmbmenuFrame extends Frame implements MouseListener
53 {
54   /**
55    * map from labels to popup menus for the embedded menubar
56    */
57   protected Hashtable embeddedPopup;
58
59   /**
60    * the embedded menu is built on this and should be added to the frame at the
61    * appropriate position.
62    * 
63    */
64   protected Panel embeddedMenu;
65
66   public EmbmenuFrame() throws HeadlessException
67   {
68     super();
69   }
70
71   public EmbmenuFrame(String title) throws HeadlessException
72   {
73     super(title);
74   }
75
76   /**
77    * Check if the applet is running on a platform that requires the Frame
78    * menuBar to be embedded, and if so, embeds it.
79    * 
80    * @param tobeAdjusted
81    *          the panel that is to be reduced to make space for the embedded
82    *          menu bar
83    * @return true if menuBar was embedded and tobeAdjusted's height modified
84    */
85   protected boolean embedMenuIfNeeded(Panel tobeAdjusted)
86   {
87     MenuBar topMenuBar = getMenuBar();
88     if (topMenuBar == null)
89     {
90       return false;
91     }
92     // DEBUG Hint: can test embedded menus by inserting true here.
93     if (new jalview.util.Platform().isAMac())
94     {
95       // Build the embedded menu panel
96       embeddedMenu = makeEmbeddedPopupMenu(topMenuBar, "Arial", Font.PLAIN,
97               11, true); // try to pickup system font.
98       setMenuBar(null);
99       // add the components to the TreePanel area.
100       add(embeddedMenu, BorderLayout.NORTH);
101       tobeAdjusted.setSize(getSize().width, getSize().height
102               - embeddedMenu.HEIGHT);
103       return true;
104     }
105     return false;
106   }
107
108   /**
109    * move all menus on menuBar onto embeddedMenu. embeddedPopup is used to store
110    * the popups for each menu removed from the menuBar and added to the panel.
111    * NOTE: it is up to the caller to remove menuBar from the Frame if it is
112    * already attached.
113    * 
114    * @param menuBar
115    * @param fn
116    * @param fstyle
117    * @param fsz
118    * @param overrideFonts
119    *          true if we take the menuBar fonts in preference to the supplied
120    *          defaults
121    * @return the embedded menu instance to be added to the frame.
122    */
123   protected Panel makeEmbeddedPopupMenu(MenuBar menuBar, String fn,
124           int fstyle, int fsz, boolean overrideFonts)
125   {
126     return makeEmbeddedPopupMenu(menuBar, fn, fstyle, fsz, overrideFonts,
127             false);
128   }
129
130   /**
131    * Create or add elements to the embedded menu from menuBar. This removes all
132    * menu from menuBar and it is up to the caller to remove the now useless
133    * menuBar from the Frame if it is already attached.
134    * 
135    * @param menuBar
136    * @param fn
137    * @param fstyle
138    * @param fsz
139    * @param overrideFonts
140    * @param append
141    *          true means existing menu will be emptied before adding new
142    *          elements
143    * @return
144    */
145   protected Panel makeEmbeddedPopupMenu(MenuBar menuBar, String fn,
146           int fstyle, int fsz, boolean overrideFonts, boolean append)
147   {
148     if (!append)
149     {
150       if (embeddedPopup != null)
151       {
152         embeddedPopup.clear(); // TODO: check if j1.1
153       }
154       if (embeddedMenu != null)
155       {
156         embeddedMenu.removeAll();
157       }
158     }
159     if (embeddedPopup == null)
160     {
161       embeddedPopup = new Hashtable();
162     }
163
164     embeddedMenu = makeEmbeddedPopupMenu(menuBar, fn, fstyle, fsz,
165             overrideFonts, embeddedPopup, new Panel(), this);
166     return embeddedMenu;
167   }
168
169   /**
170    * Generic method to move elements from menubar onto embeddedMenu using the
171    * existing or the supplied font, and adds binding from panel to attached
172    * menus in embeddedPopup This removes all menu from menuBar and it is up to
173    * the caller to remove the now useless menuBar from the Frame if it is
174    * already attached.
175    * 
176    * @param menuBar
177    *          must be non-null
178    * @param fn
179    * @param fstyle
180    * @param fsz
181    * @param overrideFonts
182    * @param embeddedPopup
183    *          must be non-null
184    * @param embeddedMenu
185    *          if null, a new panel will be created and returned
186    * @param clickHandler
187    *          - usually the instance of EmbmenuFrame that holds references to
188    *          embeddedPopup and embeddedMenu
189    * @return the panel instance for convenience.
190    */
191   protected Panel makeEmbeddedPopupMenu(MenuBar menuBar, String fn,
192           int fstyle, int fsz, boolean overrideFonts,
193           Hashtable embeddedPopup, Panel embeddedMenu,
194           MouseListener clickHandler)
195   {
196     if (embeddedPopup == null)
197     {
198       throw new Error(MessageManager.getString("error.implementation_error_embeddedpopup_not_null"));
199     }
200     if (overrideFonts)
201     {
202       Font mbf = menuBar.getFont();
203       if (mbf != null)
204       {
205         fn = mbf.getName();
206         fstyle = mbf.getStyle();
207         fsz = mbf.getSize();
208       }
209     }
210     if (embeddedMenu == null)
211       embeddedMenu = new Panel();
212     FlowLayout flowLayout1 = new FlowLayout();
213     embeddedMenu.setBackground(Color.lightGray);
214     embeddedMenu.setLayout(flowLayout1);
215     // loop thru
216     for (int mbi = 0, nMbi = menuBar.getMenuCount(); mbi < nMbi; mbi++)
217     {
218       Menu mi = menuBar.getMenu(mbi);
219       Label elab = new Label(mi.getLabel());
220       elab.setFont(new java.awt.Font(fn, fstyle, fsz));
221       // add the menu entries
222       PopupMenu popup = new PopupMenu();
223       int m, mSize = mi.getItemCount();
224       for (m = 0; m < mSize; m++)
225       {
226         popup.add(mi.getItem(m));
227         mSize--;
228         m--;
229       }
230       embeddedPopup.put(elab, popup);
231       embeddedMenu.add(elab);
232       elab.addMouseListener(clickHandler);
233     }
234     flowLayout1.setAlignment(FlowLayout.LEFT);
235     flowLayout1.setHgap(2);
236     flowLayout1.setVgap(0);
237     return embeddedMenu;
238   }
239
240   public void mousePressed(MouseEvent evt)
241   {
242     PopupMenu popup = null;
243     Label source = (Label) evt.getSource();
244     popup = getPopupMenu(source);
245     if (popup != null)
246     {
247       embeddedMenu.add(popup);
248       popup.show(embeddedMenu, source.getBounds().x, source.getBounds().y
249               + source.getBounds().getSize().height);
250     }
251   }
252
253   /**
254    * get the menu for source from the hash.
255    * 
256    * @param source
257    *          what was clicked on.
258    */
259   PopupMenu getPopupMenu(Label source)
260   {
261     return (PopupMenu) embeddedPopup.get(source);
262   }
263
264   public void mouseClicked(MouseEvent evt)
265   {
266   }
267
268   public void mouseReleased(MouseEvent evt)
269   {
270   }
271
272   public void mouseEntered(MouseEvent evt)
273   {
274   }
275
276   public void mouseExited(MouseEvent evt)
277   {
278   }
279
280   /**
281    * called to clear the GUI resources taken up for embedding and remove any
282    * self references so we can be garbage collected.
283    */
284   public void destroyMenus()
285   {
286     if (embeddedPopup != null)
287     {
288       Enumeration e = embeddedPopup.keys();
289       while (e.hasMoreElements())
290       {
291         Label lb = (Label) e.nextElement();
292         lb.removeMouseListener(this);
293       }
294       embeddedPopup.clear();
295     }
296     if (embeddedMenu != null)
297     {
298       embeddedMenu.removeAll();
299     }
300   }
301
302   /**
303    * calls destroyMenus()
304    */
305   public void finalize() throws Throwable
306   {
307     destroyMenus();
308     embeddedPopup = null;
309     embeddedMenu = null;
310     super.finalize();
311   }
312 }