1 /*** MainFrame.java - part of the MirkE (say murky) application for colormetric analysis emphesizing
2 kinetics.
3
4 Created by: Scott Menor on 21 July, 2004.
5 Last modified by: Scott Menor on 2 February, 2005
6
7 Copyright (c) 2004 Arizona State University - Cancer Research Institute. All rights reserved.
8
9 MirkE is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 MirkE is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with MirkE; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 US
22 */
23
24 package edu.asu.cri.MirkE.gui;
25
26 import edu.asu.cri.MirkE.exceptions.MenuException;
27 import edu.asu.cri.MirkE.exceptions.MirkEApplicationException;
28 import edu.asu.cri.MirkE.menu.MenuItem;
29 import edu.asu.cri.MirkE.menu.MenuList;
30 import edu.asu.cri.MirkE.menu.MenuXMLParser;
31 import edu.asu.cri.MirkE.util.MirkeMessages;
32
33 import java.awt.Toolkit;
34 import java.awt.event.KeyEvent;
35 import java.lang.reflect.Field;
36 import java.util.List;
37 import java.util.Vector;
38
39 import javax.swing.JFrame;
40 import javax.swing.JMenu;
41 import javax.swing.JMenuBar;
42 import javax.swing.JMenuItem;
43 import javax.swing.KeyStroke;
44
45 import edu.asu.cri.MirkE.MirkE;
46
47 import org.apache.commons.logging.Log;
48 import org.apache.commons.logging.LogFactory;
49
50 /***
51 * @author smenor
52 *
53 */
54 public class MainFrame extends JFrame
55 {
56 private String identifyingTitle;
57
58 private MirkE mirke;
59
60 private MenuActionListener menuActionListener = null;
61
62 /***
63 *
64 * logging
65 * */
66 private static final Log log = LogFactory.getLog(MainFrame.class);
67
68 /***
69 @param mirke
70 */
71 public void setMirke(MirkE mirke)
72 {
73 this.mirke = mirke;
74 }
75
76 /***
77 @return mirke
78 */
79 public MirkE getMirke()
80 {
81 return mirke;
82 }
83
84 /*** default constructor
85 */
86 public MainFrame()
87 {
88 menuActionListener = new MenuActionListener();
89 scheduleInitialization();
90 }
91
92 /*** constructor with a <code>DataSet</code>
93
94 @param mirke
95 */
96 public MainFrame(MirkE mirke)
97 {
98 menuActionListener = new MenuActionListener();
99 setMirke(mirke);
100 scheduleInitialization();
101 }
102
103 /***
104 */
105 public void scheduleInitialization()
106 {
107 javax.swing.SwingUtilities.invokeLater(new Runnable()
108 {
109 public void run()
110 {
111 try{
112 createAndShow();
113 }catch(MirkEApplicationException mae){}
114
115 }
116 });
117 }
118
119 /*** create the JFrame, populate the JMenuBar and Pane then show the window
120 * @throws MirkEApplicationException
121 */
122 public void createAndShow() throws MirkEApplicationException
123 {
124
125
126
127 setDefaultLookAndFeelDecorated(true);
128
129
130 String frameTitle = MirkeMessages.getMessage("menu-title");
131 if (identifyingTitle != null)
132 {
133 frameTitle += " " + identifyingTitle;
134 }
135
136 setTitle(frameTitle);
137
138
139
140 try
141 {
142 setJMenuBar(createMenuBarFromXml());
143 }
144 catch (MenuException exception)
145 {
146 exception.printStackTrace();
147 }
148
149
150 Vector selectableParameters = new Vector();
151
152
153 selectableParameters.add(
154 MirkeMessages.getMessage("menu-drug-control"));
155 selectableParameters.add(
156 MirkeMessages.getMessage("menu-media-control"));
157 selectableParameters.add(
158 MirkeMessages.getMessage("menu-growth-control"));
159
160 selectableParameters.add(MirkeMessages.getMessage("menu-strain-1"));
161 selectableParameters.add(MirkeMessages.getMessage("menu-strain-2"));
162
163 for (int n = 9; n >= -1; n--)
164 {
165 selectableParameters.add("" + Math.pow(2, n));
166 }
167
168 getContentPane().add(
169 new PlateWellPropertySelectionWidget(
170 mirke,
171 selectableParameters));
172
173
174 pack();
175 setVisible(true);
176 }
177
178 /***
179 * Returns Swing menubar for the menu.xml configured
180 * @return Swing menu bar
181 * @throws MenuException Will be thrown if there is any exception while
182 * building menu
183 */
184 public JMenuBar createMenuBarFromXml() throws MenuException
185 {
186 MenuList menuList = null;
187 List allMenus = null;
188 JMenuBar menuBar = null;
189 int menuIndex = 0;
190 int menuSize = 0;
191 MenuList childMenu = null;
192 try
193 {
194 menuBar = new JMenuBar();
195
196
197 menuList = MenuXMLParser.getMenuList();
198
199 allMenus = menuList.getChildItems();
200 menuSize = allMenus.size();
201
202
203 for (menuIndex = 0; menuIndex < menuSize; menuIndex++)
204 {
205 childMenu = (MenuList) allMenus.get(menuIndex);
206 menuBar.add(createMenu(childMenu));
207 }
208 }
209 catch (MenuException exception)
210 {
211 throw exception;
212 }
213 catch (Throwable exception)
214 {
215 log.error("createMenuBarFromXml() --> "+exception);
216
217
218 exception.printStackTrace();
219 }
220
221 return menuBar;
222 }
223
224 /***
225 * Returns Swing menu for the given menu list details
226 *
227 * @param menuList Menu list
228 * @return Swing menu
229 */
230 private JMenu createMenu(MenuList menuList)
231 {
232 JMenu menu = null;
233 String menuName = null;
234 List childItems = null;
235 int itemIndex = 0;
236 int itemLength = 0;
237 try
238 {
239
240 menuName = menuList.getName();
241 menu = new JMenu(menuName);
242 if (menuList.getAccessKeyStroke() != null
243 && !menuList.getAccessKeyStroke().equals(""))
244 {
245 menu.setMnemonic(menuList.getAccessKeyStroke().charAt(0));
246 }
247
248 childItems = menuList.getChildItems();
249 itemLength = childItems.size();
250 for (itemIndex = 0; itemIndex < itemLength; itemIndex++)
251 {
252 Object item = childItems.get(itemIndex);
253 if (item instanceof MenuList)
254 {
255 menu.add(createMenu((MenuList) item));
256 }
257 else if (item instanceof MenuItem)
258 {
259 MenuItem menuItem = (MenuItem) item;
260 if (menuItem.getName() == null)
261 {
262 menu.addSeparator();
263 }
264 else
265 {
266 menu.add(createMenuItem((MenuItem) item));
267 }
268 }
269 else
270 {
271 throw new RuntimeException(
272 MirkeMessages.getMessage("menu.invalidobject"));
273 }
274 }
275 }
276 catch (Throwable exception)
277 {
278
279
280 exception.printStackTrace();
281 }
282
283 return menu;
284 }
285
286 /***
287 * Constructs a swing menu item for the given menu item
288 *
289 * @param itemDetail Menu item details
290 * @return Swing menu item
291 */
292 private JMenuItem createMenuItem(MenuItem itemDetail)
293 {
294 String menuItemName = itemDetail.getName();
295 JMenuItem menuItem = new JMenuItem(menuItemName);
296 if (itemDetail.getAccessKeyStroke() != null
297 && !itemDetail.getAccessKeyStroke().equals(""))
298 {
299 menuItem.setAccelerator(
300 KeyStroke.getKeyStroke(
301 getKeyStrokeValue(itemDetail.getAccessKeyStroke()),
302 Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
303 }
304 menuItem.setActionCommand(
305 itemDetail.getClassName() + " " + itemDetail.getMethodName());
306 menuItem.addActionListener(menuActionListener);
307
308 return menuItem;
309 }
310
311 /***
312 * Returns the key stroke value for the given key in the form of string
313 *
314 * @param keyStroke Key string
315 * @return key stroke value
316 */
317 private int getKeyStrokeValue(String keyStroke)
318 {
319 int keyStrokeValue = 0;
320 String keySrokeFieldName = null;
321 Field keySrokeField = null;
322 try
323 {
324 keySrokeFieldName = "VK_" + keyStroke;
325 keySrokeField = KeyEvent.class.getField(keySrokeFieldName);
326 keyStrokeValue = keySrokeField.getInt(null);
327 }
328 catch (Throwable exception)
329 {
330
331 log.error("getKeyStrokeValue() --> "+exception);
332
333 exception.printStackTrace();
334 }
335 return keyStrokeValue;
336 }
337 }