4
* $Date: 2007-01-04 18:26:00 +0100 (Thu, 04 Jan 2007) $
7
* Copyright (C) 1997-2007 The JChemPaint project
9
* Contact: jchempaint-devel@lists.sourceforge.net
11
* This program is free software; you can redistribute it and/or
12
* modify it under the terms of the GNU Lesser General Public License
13
* as published by the Free Software Foundation; either version 2.1
14
* of the License, or (at your option) any later version.
15
* All we ask is that proper credit is given for our work, which includes
16
* - but is not limited to - adding the above copyright notice to the beginning
17
* of your source code files, and to any copyright notice that you may distribute
18
* with programs based on this work.
20
* This program is distributed in the hope that it will be useful,
21
* but WITHOUT ANY WARRANTY; without even the implied warranty of
22
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
* GNU Lesser General Public License for more details.
25
* You should have received a copy of the GNU Lesser General Public License
26
* along with this program; if not, write to the Free Software
27
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29
package org.openscience.cdk.applications.jchempaint;
31
import java.lang.reflect.Field;
32
import java.util.MissingResourceException;
33
import java.util.Properties;
34
import java.util.StringTokenizer;
36
import javax.swing.Box;
37
import javax.swing.JCheckBoxMenuItem;
38
import javax.swing.JComponent;
39
import javax.swing.JMenu;
40
import javax.swing.JMenuBar;
41
import javax.swing.JMenuItem;
42
import javax.swing.KeyStroke;
44
import org.openscience.cdk.applications.jchempaint.action.JCPAction;
45
import org.openscience.cdk.tools.LoggingTool;
51
* @cdk.module jchempaint
53
public class JChemPaintMenuBar extends JMenuBar {
55
private static final long serialVersionUID = -8358165408129203644L;
57
private LoggingTool logger;
59
private String guiString = "stable";
62
* The default constructor method.
64
* @param jcpPanel Description of the Parameter
66
public JChemPaintMenuBar(JChemPaintPanel jcpPanel) {
67
this(jcpPanel, "stable");
72
* The more flexible constructor method.
74
* @param jcpPanel Description of the Parameter
75
* @param guiString Description of the Parameter
77
public JChemPaintMenuBar(JChemPaintPanel jcpPanel, String guiString) {
78
this(jcpPanel, null, guiString);
83
* Constructor for the JChemPaintMenuBar object
85
* @param jcpPanel Description of the Parameter
86
* @param guiString Description of the Parameter
87
* @param menuDefinition Description of the Parameter
89
public JChemPaintMenuBar(JChemPaintPanel jcpPanel, String menuDefinition, String guiString) {
90
logger = new LoggingTool(this);
91
this.guiString = guiString;
92
createMenubar(jcpPanel, menuDefinition);
97
* Creates a JMenuBar with all the menues that are specified in the properties
100
* The menu items in the bar are defined by the property 'menubar' in
101
* org.openscience.cdk.applications.jchempaint.resources.JChemPaint.properties.
103
* @param jcpPanel Description of the Parameter
104
* @param menuDefinition Description of the Parameter
106
protected void createMenubar(JChemPaintPanel jcpPanel,
107
String menuDefinition) {
108
addNormalMenuBar(jcpPanel, menuDefinition);
109
this.add(Box.createHorizontalGlue());
110
this.add(createMenu(jcpPanel, "help"));
115
* Adds a feature to the NormalMenuBar attribute of the JChemPaintMenuBar
118
* @param jcpPanel The feature to be added to the NormalMenuBar
120
* @param menuDefinition The feature to be added to the NormalMenuBar
123
private void addNormalMenuBar(JChemPaintPanel jcpPanel, String menuDefinition) {
124
String definition = menuDefinition;
125
if (definition == null) {
126
definition = getMenuResourceString("menubar");
128
String[] menuKeys = StringHelper.tokenize(definition);
129
for (int i = 0; i < menuKeys.length; i++) {
130
JMenu m = createMenu(jcpPanel, menuKeys[i]);
139
* Creates a JMenu which can be part of the menu of an application embedding jcp.
141
* @param jcpPanel Description of the Parameter
142
* @return The created JMenu
144
public JMenu getMenuForEmbedded(JChemPaintPanel jcpPanel) {
145
String definition = getMenuResourceString("menubar");
146
String[] menuKeys = StringHelper.tokenize(definition);
147
JMenu superMenu = new JMenu("JChemPaint");
148
for (int i = 0; i < menuKeys.length; i++) {
149
JMenu m = createMenu(jcpPanel, menuKeys[i]);
159
* Creates a JMenu given by a String with all the MenuItems specified in the
162
* @param key The String used to identify the Menu
163
* @param jcpPanel Description of the Parameter
164
* @return The created JMenu
166
protected JMenu createMenu(JChemPaintPanel jcpPanel, String key) {
167
logger.debug("Creating menu: ", key);
168
String[] itemKeys = StringHelper.tokenize(getMenuResourceString(key));
169
JMenu menu = new JMenu(JCPLocalizationHandler.getInstance().getString(key));
170
for (int i = 0; i < itemKeys.length; i++) {
171
if (itemKeys[i].equals("-")) {
174
else if (itemKeys[i].startsWith("@")) {
175
JMenu me = createMenu(jcpPanel, itemKeys[i].substring(1));
178
else if (itemKeys[i].endsWith("+")) {
179
JMenuItem mi = createMenuItem(jcpPanel,
180
itemKeys[i].substring(0, itemKeys[i].length() - 1),
183
if(itemKeys[i].substring(0, itemKeys[i].length() - 1).equals("addImplHydrogen"))
184
((JCheckBoxMenuItem)mi).setSelected(true);
185
if(itemKeys[i].substring(0, itemKeys[i].length() - 1).equals("insertstructure") && !jcpPanel.guiString.equals("applet"))
186
((JCheckBoxMenuItem)mi).setSelected(true);
187
// default off, because we cannot turn it on anywhere (yet)
191
JMenuItem mi = createMenuItem(jcpPanel, itemKeys[i], false, false);
200
* Gets the menuResourceString attribute of the JChemPaint object
202
* @param key Description of the Parameter
203
* @return The menuResourceString value
205
public String getMenuResourceString(String key) {
208
str = JCPPropertyHandler.getInstance().getGUIDefinition(this.guiString).getString(key);
209
} catch (MissingResourceException mre) {
217
* Adds ShortCuts to the JChemPaintMenuBar object
219
* @param cmd String The Strin to identify the MenuItem
220
* @param mi the regarding MenuItem
221
* @param jcp The feature to be added to the ShortCuts attribute
223
private void addShortCuts(String cmd, JMenuItem mi, JChemPaintPanel jcp) {
224
Properties shortCutProps = JCPPropertyHandler.getInstance().getJCPShort_Cuts();
225
String shortCuts = shortCutProps.getProperty(cmd);
226
String charString = null;
227
if (shortCuts != null) {
229
String[] scStrings = shortCuts.trim().split(",");
230
if (scStrings.length > 1) {
231
charString = scStrings[1];
232
String altKey = scStrings[0] + "_MASK";
233
Field field = Class.forName("java.awt.event.InputEvent").getField(altKey);
234
int i = field.getInt(Class.forName("java.awt.event.InputEvent"));
235
mi.setAccelerator(KeyStroke.getKeyStroke(charString.charAt(0), i));
236
jcp.registerKeyboardAction(mi.getActionListeners()[0], charString, KeyStroke.getKeyStroke(charString.charAt(0), i), JComponent.WHEN_IN_FOCUSED_WINDOW);
239
charString = "VK_" + scStrings[0];
240
Field field = Class.forName("java.awt.event.KeyEvent").getField(charString);
241
int i = field.getInt(Class.forName("java.awt.event.KeyEvent"));
242
mi.setAccelerator(KeyStroke.getKeyStroke(i, 0));
243
jcp.registerKeyboardAction(mi.getActionListeners()[0], charString, KeyStroke.getKeyStroke(i, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
245
} catch (ClassNotFoundException cnfe) {
246
cnfe.printStackTrace();
247
} catch (NoSuchFieldException nsfe) {
248
nsfe.printStackTrace();
249
} catch (IllegalAccessException iae) {
250
iae.printStackTrace();
256
* Craetes a JMenuItem given by a String and adds the right ActionListener to
259
* @param cmd String The Strin to identify the MenuItem
260
* @param jcpPanel Description of the Parameter
261
* @param isCheckBox Description of the Parameter
262
* @param isChecked Description of the Parameter
263
* @return JMenuItem The created JMenuItem
265
protected JMenuItem createMenuItem(JChemPaintPanel jcpPanel, String cmd, boolean isCheckBox, boolean isChecked) {
266
logger.debug("Creating menu item: ", cmd);
267
String translation = "***" + cmd + "***";
269
translation = JCPLocalizationHandler.getInstance().getString(cmd);
270
logger.debug("Found translation: ", translation);
271
} catch (MissingResourceException mre) {
272
logger.error("Could not find translation for: " + cmd);
276
mi = new JCheckBoxMenuItem(translation);
277
mi.setSelected(isChecked);
280
mi = new JMenuItem(translation);
282
logger.debug("Created new menu item...");
283
String astr = JCPPropertyHandler.getInstance().getResourceString(cmd + JCPAction.actionSuffix);
287
mi.setActionCommand(astr);
288
JCPAction action = jcpPanel.getJCPAction().getAction(jcpPanel, astr);
289
if (action != null) {
290
// sync some action properties with menu
291
mi.setEnabled(action.isEnabled());
292
mi.addActionListener(action);
293
logger.debug("Coupled action to new menu item...");
296
logger.error("Could not find JCPAction class for:" + astr);
297
mi.setEnabled(false);
299
addShortCuts(cmd, mi, jcpPanel);