~ubuntu-branches/ubuntu/maverick/cdk/maverick

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/applications/jchempaint/JChemPaintMenuBar.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  $RCSfile$
 
3
 *  $Author: egonw $
 
4
 *  $Date: 2007-01-04 18:26:00 +0100 (Thu, 04 Jan 2007) $
 
5
 *  $Revision: 7634 $
 
6
 *
 
7
 *  Copyright (C) 1997-2007  The JChemPaint project
 
8
 *
 
9
 *  Contact: jchempaint-devel@lists.sourceforge.net
 
10
 *
 
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.
 
19
 *
 
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.
 
24
 *
 
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.
 
28
 */
 
29
package org.openscience.cdk.applications.jchempaint;
 
30
 
 
31
import java.lang.reflect.Field;
 
32
import java.util.MissingResourceException;
 
33
import java.util.Properties;
 
34
import java.util.StringTokenizer;
 
35
 
 
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;
 
43
 
 
44
import org.openscience.cdk.applications.jchempaint.action.JCPAction;
 
45
import org.openscience.cdk.tools.LoggingTool;
 
46
 
 
47
/**
 
48
 *  JChemPaint menu bar
 
49
 *
 
50
 * @author        steinbeck
 
51
 * @cdk.module    jchempaint
 
52
 */
 
53
public class JChemPaintMenuBar extends JMenuBar {
 
54
 
 
55
        private static final long serialVersionUID = -8358165408129203644L;
 
56
 
 
57
        private LoggingTool logger;
 
58
 
 
59
    private String guiString = "stable";
 
60
 
 
61
        /**
 
62
         *  The default constructor method.
 
63
         *
 
64
         * @param  jcpPanel  Description of the Parameter
 
65
         */
 
66
        public JChemPaintMenuBar(JChemPaintPanel jcpPanel) {
 
67
                this(jcpPanel, "stable");
 
68
        }
 
69
 
 
70
        
 
71
        /**
 
72
         *  The more flexible constructor method.
 
73
         *
 
74
         * @param  jcpPanel       Description of the Parameter
 
75
         * @param  guiString      Description of the Parameter
 
76
         */
 
77
        public JChemPaintMenuBar(JChemPaintPanel jcpPanel, String guiString) {
 
78
                this(jcpPanel, null, guiString);
 
79
        }
 
80
 
 
81
 
 
82
        /**
 
83
         *  Constructor for the JChemPaintMenuBar object
 
84
         *
 
85
         * @param  jcpPanel        Description of the Parameter
 
86
         * @param  guiString       Description of the Parameter
 
87
         * @param  menuDefinition  Description of the Parameter
 
88
         */
 
89
        public JChemPaintMenuBar(JChemPaintPanel jcpPanel, String menuDefinition, String guiString) {
 
90
                logger = new LoggingTool(this);
 
91
                this.guiString = guiString;
 
92
        createMenubar(jcpPanel, menuDefinition);
 
93
        }
 
94
 
 
95
 
 
96
        /**
 
97
         *  Creates a JMenuBar with all the menues that are specified in the properties
 
98
         *  file. <p>
 
99
         *
 
100
         *  The menu items in the bar are defined by the property 'menubar' in
 
101
         *  org.openscience.cdk.applications.jchempaint.resources.JChemPaint.properties.
 
102
         *
 
103
         * @param  jcpPanel        Description of the Parameter
 
104
         * @param  menuDefinition  Description of the Parameter
 
105
         */
 
106
        protected void createMenubar(JChemPaintPanel jcpPanel, 
 
107
        String menuDefinition) {
 
108
                addNormalMenuBar(jcpPanel, menuDefinition);
 
109
                this.add(Box.createHorizontalGlue());
 
110
                this.add(createMenu(jcpPanel, "help"));
 
111
        }
 
112
 
 
113
 
 
114
        /**
 
115
         *  Adds a feature to the NormalMenuBar attribute of the JChemPaintMenuBar
 
116
         *  object
 
117
         *
 
118
         * @param  jcpPanel        The feature to be added to the NormalMenuBar
 
119
         *      attribute
 
120
         * @param  menuDefinition  The feature to be added to the NormalMenuBar
 
121
         *      attribute
 
122
         */
 
123
        private void addNormalMenuBar(JChemPaintPanel jcpPanel, String menuDefinition) {
 
124
                String definition = menuDefinition;
 
125
                if (definition == null) {
 
126
                        definition = getMenuResourceString("menubar");
 
127
                }
 
128
                String[] menuKeys = StringHelper.tokenize(definition);
 
129
                for (int i = 0; i < menuKeys.length; i++) {
 
130
                        JMenu m = createMenu(jcpPanel, menuKeys[i]);
 
131
                        if (m != null) {
 
132
                                this.add(m);
 
133
                        }
 
134
                }
 
135
        }
 
136
 
 
137
 
 
138
        /**
 
139
         *  Creates a JMenu which can be part of the menu of an application embedding jcp.
 
140
         *
 
141
         * @param  jcpPanel   Description of the Parameter
 
142
         * @return            The created JMenu
 
143
         */
 
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]);
 
150
                        if (m != null) {
 
151
                                superMenu.add(m);
 
152
                        }
 
153
                }
 
154
                return (superMenu);
 
155
        }
 
156
 
 
157
 
 
158
        /**
 
159
         *  Creates a JMenu given by a String with all the MenuItems specified in the
 
160
         *  properties file.
 
161
         *
 
162
         * @param  key       The String used to identify the Menu
 
163
         * @param  jcpPanel  Description of the Parameter
 
164
         * @return           The created JMenu
 
165
         */
 
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("-")) {
 
172
                                menu.addSeparator();
 
173
                        }
 
174
                        else if (itemKeys[i].startsWith("@")) {
 
175
                                JMenu me = createMenu(jcpPanel, itemKeys[i].substring(1));
 
176
                                menu.add(me);
 
177
                        }
 
178
                        else if (itemKeys[i].endsWith("+")) {
 
179
                                JMenuItem mi = createMenuItem(jcpPanel,
 
180
                                                itemKeys[i].substring(0, itemKeys[i].length() - 1),
 
181
                                                true, false
 
182
                                                );
 
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)
 
188
                                menu.add(mi);
 
189
                        }
 
190
                        else {
 
191
                                JMenuItem mi = createMenuItem(jcpPanel, itemKeys[i], false, false);
 
192
                                menu.add(mi);
 
193
                        }
 
194
                }
 
195
                return menu;
 
196
        }
 
197
 
 
198
 
 
199
        /**
 
200
         *  Gets the menuResourceString attribute of the JChemPaint object
 
201
         *
 
202
         * @param  key  Description of the Parameter
 
203
         * @return      The menuResourceString value
 
204
         */
 
205
        public String getMenuResourceString(String key) {
 
206
                String str;
 
207
                try {
 
208
                        str = JCPPropertyHandler.getInstance().getGUIDefinition(this.guiString).getString(key);
 
209
                } catch (MissingResourceException mre) {
 
210
                        str = null;
 
211
                }
 
212
                return str;
 
213
        }
 
214
 
 
215
 
 
216
        /**
 
217
         *  Adds ShortCuts to the JChemPaintMenuBar object
 
218
         *
 
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
 
222
         */
 
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) {
 
228
                        try {
 
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);
 
237
                                }
 
238
                                else {
 
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);
 
244
                                }
 
245
                        } catch (ClassNotFoundException cnfe) {
 
246
                                cnfe.printStackTrace();
 
247
                        } catch (NoSuchFieldException nsfe) {
 
248
                                nsfe.printStackTrace();
 
249
                        } catch (IllegalAccessException iae) {
 
250
                                iae.printStackTrace();
 
251
                        }
 
252
                }
 
253
        }
 
254
 
 
255
        /**
 
256
         *  Craetes a JMenuItem given by a String and adds the right ActionListener to
 
257
         *  it.
 
258
         *
 
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
 
264
         */
 
265
        protected JMenuItem createMenuItem(JChemPaintPanel jcpPanel, String cmd, boolean isCheckBox, boolean isChecked) {
 
266
                logger.debug("Creating menu item: ", cmd);
 
267
                String translation = "***" + cmd + "***";
 
268
                try {
 
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);
 
273
                }
 
274
                JMenuItem mi = null;
 
275
                if (isCheckBox) {
 
276
                        mi = new JCheckBoxMenuItem(translation);
 
277
                        mi.setSelected(isChecked);
 
278
                }
 
279
                else {
 
280
                        mi = new JMenuItem(translation);
 
281
                }
 
282
                logger.debug("Created new menu item...");
 
283
                String astr = JCPPropertyHandler.getInstance().getResourceString(cmd + JCPAction.actionSuffix);
 
284
        if (astr == null) {
 
285
                        astr = cmd;
 
286
                }
 
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...");
 
294
                }
 
295
                else {
 
296
                        logger.error("Could not find JCPAction class for:" + astr);
 
297
                        mi.setEnabled(false);
 
298
                }
 
299
                addShortCuts(cmd, mi, jcpPanel);
 
300
                return mi;
 
301
        }
 
302
 
 
303
}
 
304