001: package net.xoetrope.builder.editor;
002:
003: import java.util.Hashtable;
004: import java.util.Stack;
005: import java.util.Vector;
006: import java.util.prefs.Preferences;
007:
008: import java.awt.Component;
009: import java.awt.event.ActionEvent;
010: import java.awt.event.ActionListener;
011: import java.awt.event.InputEvent;
012: import java.awt.event.ItemEvent;
013: import java.awt.event.ItemListener;
014: import java.awt.event.KeyEvent;
015: import javax.swing.Box;
016: import javax.swing.ButtonGroup;
017: import javax.swing.JCheckBoxMenuItem;
018: import javax.swing.JMenu;
019: import javax.swing.JMenuBar;
020: import javax.swing.JMenuItem;
021: import javax.swing.JRadioButtonMenuItem;
022: import javax.swing.KeyStroke;
023: import javax.swing.MenuElement;
024: import javax.swing.event.MenuEvent;
025: import javax.swing.event.MenuListener;
026:
027: import net.xoetrope.builder.XuiBuilder;
028: import net.xoetrope.builder.editor.events.CommandListener;
029: import net.xoetrope.builder.editor.events.ProjectListener;
030: import net.xoetrope.builder.editor.plugin.PluginListener;
031: import net.xoetrope.xui.XPage;
032: import net.xoetrope.debug.DebugLogger;
033:
034: /**
035: * A set of menus for the editor
036: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003</p>
037: * $Revision: 1.19 $
038: */
039: public class XEditorMenu extends JMenuBar implements ItemListener,
040: ActionListener, ProjectListener, MenuListener, PluginListener {
041: public final static String MENU_NEW_PROJECT = "New project...";
042: public final static String MENU_OPEN_PROJECT = "Open project...";
043: public final static String MENU_NEW_PAGE = "New page...";
044: public final static String MENU_SAVE_PROJECT = "Save";
045: public final static String MENU_SAVEAS_PROJECT = "Save As...";
046: public final static String MENU_EXIT_IDE = "Exit";
047:
048: public final static String MENU_VIEW_STYLES = "Styles";
049: public final static String MENU_VIEW_TOOLBOX = "Toolbox";
050: public final static String MENU_VIEW_PROPERTIES = "Properties";
051: public final static String MENU_VIEW_PROJECT = "Project";
052: public final static String MENU_VIEW_DESIGN = "Form designer";
053: public final static String MENU_VIEW_SOURCE = "Source code";
054: public final static String MENU_VIEW_XML = "Form XML";
055: public final static String MENU_VIEW_VALIDATIONS = "Validations";
056: public final static String MENU_VIEW_DATA = "Static Data";
057: public final static String MENU_VIEW_MESSAGES = "Messages";
058: public final static String MENU_VIEW_FRAMES = "Frames";
059: public final static String MENU_VIEW_PAGES = "Pages";
060: public final static String MENU_VIEW_GRID = "Guides";
061: public final static String MENU_VIEW_LAF = "System Look & Feel";
062: public final static String MENU_VIEW_DEVMODE = "Developer";
063: public final static String MENU_VIEW_EXPERTMODE = "Expert";
064: public final static String MENU_VIEW_NOVICEMODE = "Novice";
065:
066: public final static String MENU_TEST_BUNDLE = "Bundle";
067: public final static String MENU_TEST_PREVIEW = "Preview";
068: public final static String MENU_TEST_BUILD = "Build";
069:
070: public final static String MENU_PLUGIN_ADD = "Add...";
071: public final static String MENU_PLUGIN_REMOVE = "Remove...";
072:
073: public final static String MENU_CUT = "Cut";
074: public final static String MENU_COPY = "Copy";
075: public final static String MENU_PASTE = "Paste";
076: public final static String MENU_DELETE = "Delete";
077: public final static String MENU_HELP_CONTENTS = "Contents...";
078: public final static String MENU_HELP_ABOUT = "About...";
079:
080: public final static String MENU_ALIGN_LEFT = "Align left";
081: public final static String MENU_ALIGN_RIGHT = "Align right";
082: public final static String MENU_ALIGN_TOP = "Align top";
083: public final static String MENU_ALIGN_BOTTOM = "Align bottom";
084: public final static String MENU_ALIGN_CENTER = "Align center";
085: public final static String MENU_JUSTIFY_HORZ = "Justify horizontal";
086: public final static String MENU_JUSTIFY_VERT = "Justify vertical";
087:
088: public final static int ID_MENU_NEW_PROJECT = -1;
089: public final static int ID_MENU_OPEN_PROJECT = -2;
090: public final static int ID_MENU_NEW_PAGE = -3;
091: public final static int ID_MENU_SAVE_PROJECT = -4;
092: public final static int ID_MENU_SAVEAS_PROJECT = -5;
093: public final static int ID_MENU_EXIT_IDE = -6;
094:
095: public final static int ID_MENU_CUT = -10;
096: public final static int ID_MENU_COPY = -11;
097: public final static int ID_MENU_PASTE = -12;
098: public final static int ID_MENU_DELETE = -13;
099:
100: public final static int ID_MENU_VIEW_STYLES = -20;
101: public final static int ID_MENU_VIEW_TOOLBOX = -21;
102: public final static int ID_MENU_VIEW_PROPERTIES = -22;
103: public final static int ID_MENU_VIEW_PROJECT = -23;
104: public final static int ID_MENU_VIEW_DESIGN = -24;
105: public final static int ID_MENU_VIEW_SOURCE = -25;
106: public final static int ID_MENU_VIEW_XML = -26;
107: public final static int ID_MENU_VIEW_VALIDATIONS = -27;
108: public final static int ID_MENU_VIEW_DATA = -28;
109: public final static int ID_MENU_VIEW_MESSAGES = -29;
110: public final static int ID_MENU_VIEW_FRAMES = -30;
111: public final static int ID_MENU_VIEW_PAGE_BORDER = -31;
112: public final static int ID_MENU_VIEW_GRID = -32;
113: public final static int ID_MENU_VIEW_LAF = -33;
114: public final static int ID_VIEW_DEVMODE = -34;
115: public final static int ID_VIEW_EXPERTMODE = -35;
116: public final static int ID_VIEW_NOVICEMODE = -36;
117:
118: public final static int ID_MENU_ALIGN_LEFT = -100;
119: public final static int ID_MENU_ALIGN_RIGHT = -101;
120: public final static int ID_MENU_ALIGN_TOP = -102;
121: public final static int ID_MENU_ALIGN_BOTTOM = -103;
122: public final static int ID_MENU_ALIGN_CENTER = -104;
123: public final static int ID_MENU_JUSTIFY_HORZ = -105;
124: public final static int ID_MENU_JUSTIFY_VERT = -106;
125:
126: public final static int ID_MENU_TEST_PREVIEW = -200;
127: public final static int ID_MENU_TEST_BUILD = -201;
128: public final static int ID_MENU_TEST_BUNDLE = -202;
129:
130: public final static int ID_MENU_PLUGIN_ADD = -300;
131: public final static int ID_MENU_PLUGIN_REMOVE = -301;
132: public final static int ID_MENU_PLUGIN_0 = 10000;
133:
134: public final static int ID_MENU_HELP_ABOUT = -1000;
135: public final static int ID_MENU_HELP_CONTENTS = -1001;
136:
137: private static Hashtable commandNames;
138: private static int MAX_RECENT_FILES = 10;
139:
140: private JCheckBoxMenuItem mnuStyles, mnuToolbox, mnuProperties,
141: mnuProject, previewMode;
142: private JMenu fileMenu, editMenu, viewMenu, insertMenu, alignMenu,
143: testMenu, pluginMenu, helpMenu;
144: private JMenuItem mnuNewProj, mnuOpenProj, mnuNewPage, mnuSave,
145: mnuSaveAs, mnuExit;
146: private JMenuItem mnuCut, mnuCopy, mnuPaste, mnuDelete, buildMode;
147: private JMenuItem alignLeft, alignTop, alignRight, alignBottom,
148: alignCenter, justifyHorz, justifyVert;
149: private JMenuItem addPlugin, mnuBundle;
150: private JCheckBoxMenuItem viewFrames, viewMessages, viewData,
151: viewValidations, viewPages, viewGrid, viewLaf;
152:
153: private CommandListener commandListener;
154: private XEditorProject currentProject;
155: private MenuListener editMenuListener;
156: private Stack recentFiles;
157: private XuiEditor editor;
158:
159: private static String componentNames[] = { XPage.LABEL, XPage.EDIT,
160: XPage.TEXTAREA, XPage.BUTTON, XPage.RADIO, XPage.CHECK,
161: XPage.METACONTENT, XPage.TABLE, XPage.COMBO,
162: XPage.HOTSPOTIMAGE, XPage.LIST, XPage.IMAGE,
163: XPage.IMAGEMAP, XPage.PASSWORD, "", XPage.PANEL,
164: XPage.SCROLLPANE, "", XPage.MENUBAR, XPage.MENU };
165:
166: private static int componentIds[] = { XPage.XLABEL, XPage.XEDIT,
167: XPage.XTEXTAREA, XPage.XBUTTON, XPage.XRADIO, XPage.XCHECK,
168: XPage.XMETACONTENT, XPage.XTABLE, XPage.XCOMBO,
169: XPage.XHOTSPOTIMAGE, XPage.XLIST, XPage.XIMAGE,
170: XPage.XIMAGEMAP, XPage.XPASSWORD, -2, XPage.XPANEL,
171: XPage.XSCROLLPANE, -2, XPage.XMENUBAR, XPage.XMENU };
172:
173: public XEditorMenu(XuiEditor _editor) {
174: editor = _editor;
175: recentFiles = new Stack();
176:
177: fileMenu = addMenu("File", KeyEvent.VK_F, true);
178: editMenu = addMenu(XPage.EDIT, KeyEvent.VK_E, false);
179: viewMenu = addMenu("View", KeyEvent.VK_V, false);
180: insertMenu = addMenu("Insert", KeyEvent.VK_I, false);
181: alignMenu = addMenu("Align", KeyEvent.VK_A, false);
182: testMenu = addMenu("Test", KeyEvent.VK_T, false);
183: pluginMenu = addMenu("Plugins", KeyEvent.VK_W, false);
184: addPlugin = addMenuItem(pluginMenu, MENU_PLUGIN_ADD, 'A', -1);
185: add(Box.createHorizontalGlue());
186: helpMenu = addMenu("Help", KeyEvent.VK_H, true);
187:
188: mnuNewProj = addMenuItem(fileMenu, MENU_NEW_PROJECT, 'N', -1);
189: mnuNewPage = addMenuItem(fileMenu, MENU_NEW_PAGE, 'e',
190: KeyEvent.VK_N);
191: fileMenu.addSeparator();
192: mnuOpenProj = addMenuItem(fileMenu, MENU_OPEN_PROJECT, 'O',
193: KeyEvent.VK_O);
194: fileMenu.addSeparator();
195: mnuSave = addMenuItem(fileMenu, MENU_SAVE_PROJECT, 'S',
196: KeyEvent.VK_S);
197: mnuSaveAs = addMenuItem(fileMenu, MENU_SAVEAS_PROJECT, 'A',
198: KeyEvent.VK_A);
199: fileMenu.addSeparator();
200: mnuExit = addMenuItem(fileMenu, MENU_EXIT_IDE, 'x', -1);
201: addRecentFiles(fileMenu);
202:
203: mnuCut = addMenuItem(editMenu, MENU_CUT, 't', KeyEvent.VK_X);
204: mnuCopy = addMenuItem(editMenu, MENU_COPY, 'C', KeyEvent.VK_C);
205: mnuPaste = addMenuItem(editMenu, MENU_PASTE, 'P', KeyEvent.VK_V);
206: editMenu.addSeparator();
207: mnuDelete = addMenuItem(editMenu, MENU_DELETE, 'D',
208: KeyEvent.VK_DELETE, 0, 0);
209:
210: mnuToolbox = addCheckMenuItem(viewMenu, MENU_VIEW_TOOLBOX, 'T',
211: -1, true);
212: mnuStyles = addCheckMenuItem(viewMenu, MENU_VIEW_STYLES, 'S',
213: -1, true);
214: mnuProperties = addCheckMenuItem(viewMenu,
215: MENU_VIEW_PROPERTIES, 'P', -1, true);
216: mnuProject = addCheckMenuItem(viewMenu, MENU_VIEW_PROJECT, 'r',
217: -1, true);
218: viewMenu.addSeparator();
219: ButtonGroup bg = new ButtonGroup();
220: bg.add(addRadioButtonItem(viewMenu, MENU_VIEW_DESIGN, 'd', -1,
221: true));
222: bg.add(addRadioButtonItem(viewMenu, MENU_VIEW_SOURCE, 's', -1,
223: false));
224: bg.add(addRadioButtonItem(viewMenu, MENU_VIEW_XML, 'x', -1,
225: false));
226: viewMenu.addSeparator();
227: viewData = addCheckMenuItem(viewMenu, MENU_VIEW_DATA, 'a', -1,
228: false);
229: viewValidations = addCheckMenuItem(viewMenu,
230: MENU_VIEW_VALIDATIONS, 'v', -1, false);
231: viewMenu.addSeparator();
232: viewMessages = addCheckMenuItem(viewMenu, MENU_VIEW_MESSAGES,
233: 'm', -1, false);
234: viewMenu.addSeparator();
235: viewFrames = addCheckMenuItem(viewMenu, MENU_VIEW_FRAMES, 'f',
236: -1, false);
237: viewPages = addCheckMenuItem(viewMenu, MENU_VIEW_PAGES, 'p',
238: -1, false);
239: viewMenu.addSeparator();
240: viewGrid = addCheckMenuItem(viewMenu, MENU_VIEW_GRID, 'g', -1,
241: false);
242: viewMenu.addSeparator();
243: viewLaf = addCheckMenuItem(viewMenu, MENU_VIEW_LAF, 'l', -1,
244: true);
245: viewMenu.addSeparator();
246: ButtonGroup modeGroup = new ButtonGroup();
247: modeGroup.add(addRadioButtonItem(viewMenu, MENU_VIEW_DEVMODE,
248: 'd', -1, true));
249: modeGroup.add(addRadioButtonItem(viewMenu,
250: MENU_VIEW_EXPERTMODE, 'e', -1, false));
251: modeGroup.add(addRadioButtonItem(viewMenu,
252: MENU_VIEW_NOVICEMODE, 'n', -1, false));
253:
254: int numCompNames = componentNames.length;
255: for (int i = 0; i < numCompNames; i++) {
256: if (componentNames[i].length() > 0)
257: addMenuItem(insertMenu, componentNames[i],
258: componentNames[i].charAt(0), -1);
259: else
260: insertMenu.addSeparator();
261: }
262:
263: alignLeft = addMenuItem(alignMenu, MENU_ALIGN_LEFT, 'L', 0);
264: alignRight = addMenuItem(alignMenu, MENU_ALIGN_RIGHT, 'R', 0);
265: alignTop = addMenuItem(alignMenu, MENU_ALIGN_TOP, 'T', 0);
266: alignBottom = addMenuItem(alignMenu, MENU_ALIGN_BOTTOM, 'B', 0);
267: alignCenter = addMenuItem(alignMenu, MENU_ALIGN_CENTER, 'E', 0);
268: alignMenu.addSeparator();
269: justifyHorz = addMenuItem(alignMenu, MENU_JUSTIFY_HORZ, 'H', 0);
270: justifyVert = addMenuItem(alignMenu, MENU_JUSTIFY_VERT, 'V', 0);
271:
272: buildMode = addMenuItem(testMenu, MENU_TEST_BUILD, 'B',
273: KeyEvent.VK_F9);
274: mnuBundle = addMenuItem(testMenu, MENU_TEST_BUNDLE, 'B', 0);
275: testMenu.addSeparator();
276: previewMode = addCheckMenuItem(testMenu, MENU_TEST_PREVIEW,
277: 'P', KeyEvent.VK_P, false);
278:
279: addMenuItem(helpMenu, MENU_HELP_CONTENTS, 'H', -1);
280: helpMenu.addSeparator();
281: addMenuItem(helpMenu, MENU_HELP_ABOUT, 'A', -1);
282:
283: initMenus();
284: initEditMenu(false, false, false, false);
285: initAlignmentMenu(false);
286:
287: editMenu.addMenuListener(this );
288: }
289:
290: private JMenu addMenu(String name, int mnemonic, boolean enabled) {
291: JMenu menuObj = new JMenu(name, true);
292: menuObj.setMnemonic(mnemonic);
293: menuObj.setFont(XuiDefaults.defaultFont);
294: menuObj.setEnabled(enabled);
295: add(menuObj);
296:
297: return menuObj;
298: }
299:
300: private void addRecentFiles(JMenu fileMenu) {
301: try {
302: Preferences prefs = Preferences
303: .userNodeForPackage(XuiEditor.class);
304:
305: int numRecentFiles = Integer.parseInt(prefs.get(
306: "NumRecentFiles", "0"));
307: numRecentFiles = Math.min(numRecentFiles, MAX_RECENT_FILES);
308: recentFiles.ensureCapacity(numRecentFiles);
309:
310: if (numRecentFiles > 0)
311: fileMenu.addSeparator();
312: for (int i = 0; i < numRecentFiles; i++) {
313: String recentFile = prefs.get("RecentFiles_" + i, "");
314: recentFiles.push(recentFile);
315: }
316:
317: for (int i = 0; i < numRecentFiles; i++) {
318: int arrayIdx = numRecentFiles - i - 1;
319: String idxStr = new Integer(i + 1).toString();
320: String file = idxStr + " "
321: + (String) recentFiles.elementAt(arrayIdx);
322:
323: JMenuItem mi = new JMenuItem(file);
324: mi.setFont(XuiDefaults.defaultFont);
325: mi.setEnabled(currentProject == null); // Disable reopening of projects till tested
326: mi.addActionListener(new ActionListener() {
327: public void actionPerformed(ActionEvent e) {
328: editor.openProject(e.getActionCommand()
329: .substring(2));
330: }
331: });
332:
333: mi.setMnemonic(idxStr.charAt(0));
334:
335: fileMenu.add(mi);
336: }
337: } catch (Exception e) {
338: DebugLogger
339: .logWarning("Unable to add previous files to menu");
340: }
341: }
342:
343: public void save() {
344: Preferences prefs = Preferences
345: .userNodeForPackage(XuiEditor.class);
346:
347: int numRecentFiles = recentFiles.size();
348: numRecentFiles = Math.min(numRecentFiles, MAX_RECENT_FILES);
349: prefs.put("NumRecentFiles", new Integer(numRecentFiles)
350: .toString());
351:
352: int counter = 0;
353: for (int i = 0; i < numRecentFiles; i++) {
354: String recentFile = (String) recentFiles.elementAt(i);
355: prefs.put("RecentFiles_" + i, recentFile);
356: }
357: }
358:
359: public void setRecentFile(String fileName) {
360: int numRecentFiles = recentFiles.size();
361: numRecentFiles = Math.min(numRecentFiles, 10);
362:
363: for (int i = 0; i < numRecentFiles; i++) {
364: if (recentFiles.elementAt(i).toString().equals(fileName)) {
365: recentFiles.remove(i);
366: break;
367: }
368: }
369:
370: if (numRecentFiles >= MAX_RECENT_FILES)
371: recentFiles.remove(0);
372: recentFiles.push(fileName);
373: save();
374: }
375:
376: private void initMenus() {
377: mnuNewProj.setEnabled(true);
378: mnuOpenProj.setEnabled(true);
379: mnuNewPage.setEnabled(false);
380: mnuSave.setEnabled(false);
381: mnuSaveAs.setEnabled(false);
382: mnuExit.setEnabled(true);
383: mnuCut.setEnabled(false);
384: mnuCopy.setEnabled(false);
385: mnuPaste.setEnabled(false);
386: mnuDelete.setEnabled(false);
387: mnuStyles.setEnabled(false);
388: mnuToolbox.setEnabled(false);
389: mnuProperties.setEnabled(false);
390: mnuProject.setEnabled(false);
391: previewMode.setEnabled(false);
392: }
393:
394: public void projectLoaded(XEditorProject project) {
395: currentProject = project;
396:
397: editMenu.setEnabled(true);
398: viewMenu.setEnabled(true);
399: testMenu.setEnabled(true);
400:
401: mnuNewProj.setEnabled(false); // Disable reopening of projects till tested
402: mnuOpenProj.setEnabled(false); // Disable reopening of projects till tested
403: MenuElement children[] = fileMenu.getSubElements()[0]
404: .getSubElements();
405: for (int i = 0; i < children.length; i++) {
406: if (Character.isDigit(((JMenuItem) children[i]
407: .getComponent()).getText().charAt(0)))
408: children[i].getComponent().setEnabled(false);
409: }
410: mnuNewPage.setEnabled(true);
411: mnuSave.setEnabled(true);
412: mnuSaveAs.setEnabled(true);
413: mnuExit.setEnabled(true);
414: mnuCut.setEnabled(false);
415: mnuCopy.setEnabled(false);
416: mnuPaste.setEnabled(false);
417: mnuDelete.setEnabled(false);
418: mnuStyles.setEnabled(true);
419: mnuToolbox.setEnabled(true);
420: mnuProperties.setEnabled(true);
421: mnuProject.setEnabled(true);
422: previewMode.setEnabled(true);
423:
424: pluginMenu.setEnabled(true);
425: addPlugin.setEnabled(true);
426:
427: insertMenu.setEnabled(true);
428:
429: alignMenu.setEnabled(true);
430:
431: pluginsLoaded();
432: }
433:
434: /**
435: * The project has been updated
436: */
437: public void projectUpdated() {
438: }
439:
440: /**
441: * Delegate the menu event to the selected tab panel
442: * @param e the menu event
443: */
444: public void menuCanceled(MenuEvent e) {
445: if (editMenuListener != null)
446: editMenuListener.menuCanceled(e);
447: }
448:
449: /**
450: * Delegate the menu event to the selected tab panel
451: * @param e the menu event
452: */
453: public void menuDeselected(MenuEvent e) {
454: if (editMenuListener != null)
455: editMenuListener.menuDeselected(e);
456: }
457:
458: /**
459: * Delegate the menu event to the selected tab panel
460: * @param e the menu event
461: */
462: public void menuSelected(MenuEvent e) {
463: if (editMenuListener != null)
464: editMenuListener.menuSelected(e);
465: }
466:
467: /**
468: * Set a listener for the menu events so that the state can be changed prior to display
469: * @param l the menu listener
470: */
471: public void setMenuListener(MenuListener l) {
472: editMenuListener = l;
473: }
474:
475: /**
476: * Change the state of the edit menu
477: * @param cut enable the cut menu item
478: * @param copy enable the copy menu item
479: * @param paste enable the paste menu item
480: * @param delete enable the cut delete item
481: */
482: public void initEditMenu(boolean cut, boolean copy, boolean paste,
483: boolean delete) {
484: mnuCut.setEnabled(cut);
485: mnuCopy.setEnabled(copy);
486: mnuPaste.setEnabled(paste);
487: mnuDelete.setEnabled(delete);
488: }
489:
490: /**
491: * (Re)Initialize the view menu
492: * @param hasFrames is the frameset visible or just the contents element
493: * @param hasGrid is the grid visible
494: * @param hasPageBorders are the pages shown with borders (relates to frames)
495: * @param hasMessages is the message area visible
496: */
497: public void initViewMenu(boolean hasFrames, boolean hasGrid,
498: boolean hasPageBorders, boolean hasMessages) {
499: viewFrames.setSelected(hasFrames);
500: viewGrid.setSelected(hasGrid);
501: viewPages.setSelected(hasPageBorders);
502: viewMessages.setSelected(hasMessages);
503: }
504:
505: /**
506: * Initialize the alignment menu items
507: * @param enabled true to enable the menuitem
508: */
509: public void initAlignmentMenu(boolean enabled) {
510: alignLeft.setEnabled(enabled);
511: alignTop.setEnabled(enabled);
512: alignRight.setEnabled(enabled);
513: alignBottom.setEnabled(enabled);
514: alignCenter.setEnabled(enabled);
515: justifyHorz.setEnabled(enabled);
516: justifyVert.setEnabled(enabled);
517: }
518:
519: /**
520: * Adds a menu item
521: * @param menu the target menu
522: * @param menuText the command text to appear on the menu
523: * @param mnemonicChar the underlined letter
524: * @param shortCutKey the accelerator key code
525: * @return the new menu item
526: */
527: public JMenuItem addMenuItem(JMenu menu, String menuText,
528: char mnemonicChar, int shortCutKey) {
529: return addMenuItem(menu, menuText, mnemonicChar, shortCutKey,
530: InputEvent.CTRL_MASK, 0);
531: }
532:
533: /**
534: * Adds a menu item
535: * @param menu the target menu
536: * @param menuText the command text to appear on the menu
537: * @param mnemonicChar the underlined letter
538: * @param shortCutKey the accelerator key code
539: * @param state the state of the checkbox item
540: * @return the new menu item
541: */
542: public JCheckBoxMenuItem addCheckMenuItem(JMenu menu,
543: String menuText, char mnemonicChar, int shortCutKey,
544: boolean state) {
545: JCheckBoxMenuItem menuItem = (JCheckBoxMenuItem) addMenuItem(
546: menu, menuText, mnemonicChar, shortCutKey,
547: InputEvent.CTRL_MASK, 1);
548: menuItem.setState(state);
549: return menuItem;
550: }
551:
552: /**
553: * Adds a menu item
554: * @param menu the target menu
555: * @param menuText the command text to appear on the menu
556: * @param mnemonicChar the underlined letter
557: * @param shortCutKey the accelerator key code
558: * @param state the state of the radiobitton item
559: * @return the new menu item
560: */
561: public JRadioButtonMenuItem addRadioButtonItem(JMenu menu,
562: String menuText, char mnemonicChar, int shortCutKey,
563: boolean state) {
564: JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) addMenuItem(
565: menu, menuText, mnemonicChar, shortCutKey,
566: InputEvent.CTRL_MASK, 2);
567: menuItem.setSelected(state);
568: return menuItem;
569: }
570:
571: /**
572: * Adds a menu item
573: * @param menu the target menu
574: * @param menuText the command text to appear on the menu
575: * @param mnemonicChar the underlined letter
576: * @param shortCutKey the accelerator key code
577: * @param acceleratorMask the mask for the accelerator keystroke
578: * @param menuType the type of menu - normal, checkbox or radio button
579: * @return the new menu item
580: */
581: private JMenuItem addMenuItem(JMenu menu, String menuText,
582: char mnemonicChar, int shortCutKey, int acceleratorMask,
583: int menuType) {
584: JMenuItem menuItem;
585: if (menuType == 0)
586: menuItem = new JMenuItem(menuText);
587: else if (menuType == 1) {
588: menuItem = new JCheckBoxMenuItem(menuText);
589: ((JCheckBoxMenuItem) menuItem).addItemListener(this );
590: } else {
591: menuItem = new JRadioButtonMenuItem(menuText);
592: ((JRadioButtonMenuItem) menuItem).addItemListener(this );
593: }
594:
595: if (mnemonicChar > 0)
596: menuItem.setMnemonic(mnemonicChar);
597:
598: if (shortCutKey > 0)
599: menuItem.setAccelerator(KeyStroke.getKeyStroke(shortCutKey,
600: acceleratorMask));
601:
602: menuItem.addActionListener(this );
603: menuItem.setFont(XuiDefaults.defaultFont);
604: menu.add(menuItem);
605:
606: return menuItem;
607: }
608:
609: public void setCommandListener(CommandListener listener) {
610: commandListener = listener;
611: }
612:
613: public void itemStateChanged(ItemEvent evt) {
614: }
615:
616: public void actionPerformed(ActionEvent evt) {
617: JMenuItem menu = (JMenuItem) evt.getSource();
618: boolean selected = false;
619: if (menu instanceof JCheckBoxMenuItem)
620: selected = ((JCheckBoxMenuItem) menu).getState();
621: int commandId = getCommandId(menu.getText());
622: if (commandId >= 0)
623: commandListener.processCommand(menu.getText(), evt,
624: selected);
625: else
626: commandListener.processCommand(commandId, evt, selected);
627: }
628:
629: private static void setupHashTable() {
630: if (commandNames == null) {
631: commandNames = new Hashtable(40);
632:
633: commandNames.put(MENU_NEW_PROJECT, new Integer(
634: ID_MENU_NEW_PROJECT));
635: commandNames.put(MENU_OPEN_PROJECT, new Integer(
636: ID_MENU_OPEN_PROJECT));
637: commandNames.put(MENU_NEW_PAGE, new Integer(
638: ID_MENU_NEW_PAGE));
639: commandNames.put(MENU_SAVE_PROJECT, new Integer(
640: ID_MENU_SAVE_PROJECT));
641: commandNames.put(MENU_SAVEAS_PROJECT, new Integer(
642: ID_MENU_SAVEAS_PROJECT));
643: commandNames.put(MENU_EXIT_IDE, new Integer(
644: ID_MENU_EXIT_IDE));
645:
646: commandNames.put(MENU_CUT, new Integer(ID_MENU_CUT));
647: commandNames.put(MENU_COPY, new Integer(ID_MENU_COPY));
648: commandNames.put(MENU_PASTE, new Integer(ID_MENU_PASTE));
649: commandNames.put(MENU_DELETE, new Integer(ID_MENU_DELETE));
650:
651: commandNames.put(MENU_VIEW_STYLES, new Integer(
652: ID_MENU_VIEW_STYLES));
653: commandNames.put(MENU_VIEW_TOOLBOX, new Integer(
654: ID_MENU_VIEW_TOOLBOX));
655: commandNames.put(MENU_VIEW_PROPERTIES, new Integer(
656: ID_MENU_VIEW_PROPERTIES));
657: commandNames.put(MENU_VIEW_PROJECT, new Integer(
658: ID_MENU_VIEW_PROJECT));
659: commandNames.put(MENU_VIEW_DESIGN, new Integer(
660: ID_MENU_VIEW_DESIGN));
661: commandNames.put(MENU_VIEW_SOURCE, new Integer(
662: ID_MENU_VIEW_SOURCE));
663: commandNames.put(MENU_VIEW_XML, new Integer(
664: ID_MENU_VIEW_XML));
665: commandNames.put(MENU_VIEW_VALIDATIONS, new Integer(
666: ID_MENU_VIEW_VALIDATIONS));
667: commandNames.put(MENU_VIEW_DATA, new Integer(
668: ID_MENU_VIEW_DATA));
669: commandNames.put(MENU_VIEW_MESSAGES, new Integer(
670: ID_MENU_VIEW_MESSAGES));
671: commandNames.put(MENU_VIEW_FRAMES, new Integer(
672: ID_MENU_VIEW_FRAMES));
673: commandNames.put(MENU_VIEW_PAGES, new Integer(
674: ID_MENU_VIEW_PAGE_BORDER));
675: commandNames.put(MENU_VIEW_GRID, new Integer(
676: ID_MENU_VIEW_GRID));
677: commandNames.put(MENU_VIEW_LAF, new Integer(
678: ID_MENU_VIEW_LAF));
679: commandNames.put(MENU_VIEW_DEVMODE, new Integer(
680: ID_VIEW_DEVMODE));
681: commandNames.put(MENU_VIEW_EXPERTMODE, new Integer(
682: ID_VIEW_EXPERTMODE));
683: commandNames.put(MENU_VIEW_NOVICEMODE, new Integer(
684: ID_VIEW_NOVICEMODE));
685:
686: commandNames.put(MENU_ALIGN_LEFT, new Integer(
687: ID_MENU_ALIGN_LEFT));
688: commandNames.put(MENU_ALIGN_RIGHT, new Integer(
689: ID_MENU_ALIGN_RIGHT));
690: commandNames.put(MENU_ALIGN_TOP, new Integer(
691: ID_MENU_ALIGN_TOP));
692: commandNames.put(MENU_ALIGN_BOTTOM, new Integer(
693: ID_MENU_ALIGN_BOTTOM));
694: commandNames.put(MENU_ALIGN_CENTER, new Integer(
695: ID_MENU_ALIGN_CENTER));
696: commandNames.put(MENU_JUSTIFY_HORZ, new Integer(
697: ID_MENU_JUSTIFY_HORZ));
698: commandNames.put(MENU_JUSTIFY_VERT, new Integer(
699: ID_MENU_JUSTIFY_VERT));
700:
701: commandNames.put(MENU_TEST_PREVIEW, new Integer(
702: ID_MENU_TEST_PREVIEW));
703: commandNames.put(MENU_TEST_BUILD, new Integer(
704: ID_MENU_TEST_BUILD));
705: commandNames.put(MENU_TEST_BUNDLE, new Integer(
706: ID_MENU_TEST_BUNDLE));
707:
708: commandNames.put(MENU_PLUGIN_ADD, new Integer(
709: ID_MENU_PLUGIN_ADD));
710: commandNames.put(MENU_PLUGIN_REMOVE, new Integer(
711: ID_MENU_PLUGIN_REMOVE));
712:
713: commandNames.put(MENU_HELP_CONTENTS, new Integer(
714: ID_MENU_HELP_CONTENTS));
715: commandNames.put(MENU_HELP_ABOUT, new Integer(
716: ID_MENU_HELP_ABOUT));
717:
718: int numCompNames = componentNames.length;
719: for (int i = 0; i < numCompNames; i++) {
720: if (componentNames[i].length() > 0)
721: commandNames.put(componentNames[i], new Integer(
722: componentIds[i]));
723: }
724: }
725: }
726:
727: public static int getCommandId(String name) {
728: setupHashTable();
729:
730: Integer iValue = (Integer) commandNames.get(name);
731: return iValue.intValue();
732: }
733:
734: /**
735: * Clear the plugins menu
736: */
737: public void resetPluginsMenu() {
738: pluginMenu.removeAll();
739: }
740:
741: /**
742: * Add the plugins
743: */
744: public void pluginsLoaded() {
745: if ((currentProject == null)
746: || (pluginMenu.getComponentCount() > 0))
747: return;
748:
749: resetPluginsMenu();
750: setupHashTable();
751: pluginMenu.add(addPlugin);
752:
753: Vector menuItems = currentProject.getPluginManager()
754: .getPluginMenus();
755: int numPlugins = menuItems.size();
756: if (numPlugins > 0) {
757: addMenuItem(pluginMenu, XEditorMenu.MENU_PLUGIN_REMOVE,
758: 'R', -1);
759: pluginMenu.addSeparator();
760: }
761:
762: for (int i = 0; i < numPlugins; i++) {
763: Object item = menuItems.elementAt(i);
764: String pluginName;
765: if (item instanceof JMenuItem) {
766: pluginName = ((JMenuItem) item).getText();
767: pluginMenu.add((JMenuItem) item);
768: } else {
769: pluginName = (String) item;
770: addMenuItem(pluginMenu, (String) item, pluginName
771: .charAt(0), -1);
772: }
773: commandNames.put(pluginName, new Integer(
774: XEditorMenu.ID_MENU_PLUGIN_0 + i));
775: }
776: }
777:
778: /**
779: * Insert a context based menu second from the right - just before the help menu
780: * @param contextMenu
781: */
782: public void insertMenu(JMenu contextMenu) {
783: int numMenuItems = getComponentCount();
784: Component comp = getComponent(numMenuItems - 3);
785: if (comp != pluginMenu) {
786: remove(comp);
787: numMenuItems--;
788: }
789: add(contextMenu, numMenuItems - 2);
790:
791: doLayout();
792: }
793: }
|