001: package org.enhydra.jawe;
002:
003: import java.awt.Insets;
004: import java.awt.event.ActionEvent;
005: import java.awt.event.KeyEvent;
006: import java.lang.reflect.Constructor;
007:
008: import javax.swing.Action;
009: import javax.swing.ImageIcon;
010: import javax.swing.JButton;
011: import javax.swing.JMenu;
012: import javax.swing.JMenuBar;
013: import javax.swing.JMenuItem;
014: import javax.swing.JPopupMenu;
015: import javax.swing.JToolBar;
016: import javax.swing.KeyStroke;
017: import javax.swing.SwingConstants;
018:
019: import org.enhydra.jawe.base.controller.JaWEType;
020: import org.enhydra.jawe.base.controller.JaWETypeChoiceButton;
021:
022: public class BarFactory {
023:
024: public final static String CLASSNAME_POSTFIX = "ClassName";
025:
026: public final static String SETTINGSNAME_POSTFIX = "SettingsName";
027:
028: public final static String ACCELERATION_POSTFIX = "Accel";
029:
030: public final static String MNEMONIC_POSTFIX = "Mnemonic";
031:
032: public final static String LABEL_POSTFIX = "Label";
033:
034: public final static String TOOLTIP_POSTFIX = "Tooltip";
035:
036: public final static String LANGUAGEDEPENDENTNAME_POSTFIX = "LangName";
037:
038: public final static String JAWE_STANDARD_MENU_PREFIX = "jawe_";
039:
040: public final static String JAWE_STANDARD_ACTION_PREFIX = "jaweAction_";
041:
042: public final static String JAWECOMPONENT_AS_MENU_PREFIX = "@";
043:
044: public final static String SUBMENU_PREFIX = "*";
045:
046: public final static String ACTION_DELIMITER = " ";
047:
048: public final static String ACTION_SEPARATOR = "-";
049:
050: public final static String CTRL_PREFIX = "CTRL";
051:
052: public final static String SHIFT_PREFIX = "SHIFT";
053:
054: public final static String ALT_PREFIX = "ALT";
055:
056: public static JMenuBar createMainMenu(JaWEComponent comp) {
057: JMenuBar mbar = new JMenuBar();
058:
059: String actionOrder = comp.getSettings()
060: .getMainMenuActionOrder();
061: String[] act = Utils.tokenize(actionOrder, ACTION_DELIMITER);
062:
063: for (int i = 0; i < act.length; i++) {
064: if (act[i].startsWith(JAWECOMPONENT_AS_MENU_PREFIX)) {
065: String className = (String) comp
066: .getSettings()
067: .getSetting(
068: act[i].substring(1) + CLASSNAME_POSTFIX);
069: String settingsName = (String) comp.getSettings()
070: .getSetting(
071: act[i].substring(1)
072: + SETTINGSNAME_POSTFIX);
073: JMenu subMenu = getExternalMenu(className, settingsName);
074: if (subMenu != null) {
075: BarFactory.setAccelerator(subMenu, comp
076: .getSettings().getLanguageDependentString(
077: act[i].substring(1)
078: + ACCELERATION_POSTFIX));
079: BarFactory.setMnemonic(subMenu, comp.getSettings()
080: .getLanguageDependentString(
081: act[i].substring(1)
082: + MNEMONIC_POSTFIX));
083: mbar.add(subMenu);
084: }
085: } else if (act[i].startsWith(JAWE_STANDARD_MENU_PREFIX)) {
086: mbar.add(JaWEManager.getInstance().getJaWEController()
087: .getJaWEActions().getActionMenu(act[i], true));
088: } else if (act[i].startsWith(JAWE_STANDARD_ACTION_PREFIX)) {
089: mbar.add(JaWEManager.getInstance().getJaWEController()
090: .getJaWEActions().getActionMenuItem(
091: act[i].substring(11), true));
092: } else if (act[i].startsWith(SUBMENU_PREFIX)) {
093: JMenu sm = createSubMenu(act[i].substring(1), comp,
094: true);
095: if (sm != null) {
096: mbar.add(sm);
097: }
098: } else {
099: JaWEAction ja = comp.getSettings().getAction(act[i]);
100: if (ja != null) {
101: mbar.add(createMenuItem(ja, comp, true));
102: }
103: }
104: }
105:
106: return mbar;
107: }
108:
109: public static JPopupMenu createPopupMenu(String popupMenuName,
110: JaWEComponent comp) {
111: String actionOrder = comp.getSettings().getMenuActionOrder(
112: popupMenuName);
113: return createMenu(actionOrder, comp, false).getPopupMenu();
114: }
115:
116: public static JToolBar createToolbar(String toolbarName,
117: JaWEComponent comp) {
118: String actionOrder = comp.getSettings().getToolbarActionOrder(
119: toolbarName);
120:
121: JToolBar toolbar = new JToolBar();
122: toolbar.setRollover(true);
123: String[] act = Utils.tokenize(actionOrder, ACTION_DELIMITER);
124:
125: for (int i = 0; i < act.length; i++) {
126: if (act[i].equals(ACTION_SEPARATOR)) {
127: toolbar.addSeparator();
128: } else if (act[i].startsWith(JAWE_STANDARD_MENU_PREFIX)) {
129: toolbar.add(JaWEManager.getInstance()
130: .getJaWEController().getJaWEActions()
131: .getActionToolbar(act[i]));
132: } else if (act[i].startsWith(JAWE_STANDARD_ACTION_PREFIX)) {
133: toolbar.add(JaWEManager.getInstance()
134: .getJaWEController().getJaWEActions()
135: .getActionButton(act[i].substring(11)));
136: } else if (act[i].startsWith(SUBMENU_PREFIX)) {
137: toolbar.add(createToolbar(act[i].substring(1), comp));
138: } else {
139: JaWEAction ja = comp.getSettings().getAction(act[i]);
140: if (ja != null) {
141: toolbar.add(createToolbarButton(ja, comp));
142: }
143: }
144: }
145:
146: toolbar.setName(comp.getSettings().getLanguageDependentString(
147: toolbarName + LABEL_POSTFIX));
148:
149: return toolbar;
150: }
151:
152: public static JButton createToolbarButton(JaWEAction ja,
153: JaWEComponent comp) {
154: Action a = ja.getAction();
155: String aname = null;
156: if (a != null) {
157: aname = (String) a.getValue(Action.NAME);
158: }
159: String depName = ja.getLangDepName();
160: String label = comp.getSettings().getLanguageDependentString(
161: depName + LABEL_POSTFIX);
162: if (aname == null) {
163: aname = depName;
164: }
165: if (label == null) {
166: label = aname;
167: }
168: JButton b = null;
169:
170: ImageIcon ai = ja.getIcon();
171: if (ai != null) {
172: if (a instanceof NewActionBase
173: && comp instanceof ChoiceButtonListener) {
174: b = new JaWETypeChoiceButton(JaWEType.class,
175: ((NewActionBase) a).getXPDLTypeClass(),
176: (ChoiceButtonListener) comp, ai);
177: } else {
178: b = new JButton(ai) {
179: public float getAlignmentY() {
180: return 0.5f;
181: }
182: };
183: }
184: } else {
185: if (a instanceof NewActionBase
186: && comp instanceof ChoiceButtonListener) {
187: b = new JaWETypeChoiceButton(JaWEType.class,
188: ((NewActionBase) a).getXPDLTypeClass(),
189: (ChoiceButtonListener) comp, ai);
190: } else {
191: b = new JButton(label) {
192: public float getAlignmentY() {
193: return 0.5f;
194: }
195: };
196: }
197: }
198:
199: b.setName(aname);
200: b.setMargin(new Insets(1, 1, 1, 1));
201: b.setRequestFocusEnabled(false);
202:
203: b.setActionCommand(aname);
204: if (a != null) {
205: b.addActionListener(a);
206: b.setEnabled(a.isEnabled());
207: a
208: .addPropertyChangeListener(new ButtonPropertyChangedListener(
209: b));
210: } else {
211: b.setEnabled(false);
212: }
213: String tip = comp.getSettings().getLanguageDependentString(
214: depName + TOOLTIP_POSTFIX);
215: if (tip != null) {
216: b.setToolTipText(tip);
217: }
218:
219: return b;
220: }
221:
222: protected static JMenu createMenu(String actionOrder,
223: JaWEComponent comp, boolean addBCListener) {
224: JMenu menu = new JMenu();
225:
226: String[] act = Utils.tokenize(actionOrder, ACTION_DELIMITER);
227:
228: for (int i = 0; i < act.length; i++) {
229: if (act[i].equals(ACTION_SEPARATOR)) {
230: menu.addSeparator();
231: } else if (act[i].startsWith(JAWECOMPONENT_AS_MENU_PREFIX)) {
232: String className = (String) comp
233: .getSettings()
234: .getSetting(
235: act[i].substring(1) + CLASSNAME_POSTFIX);
236: String settingsName = (String) comp.getSettings()
237: .getSetting(
238: act[i].substring(1)
239: + SETTINGSNAME_POSTFIX);
240: JMenu subMenu = getExternalMenu(className, settingsName);
241: BarFactory.setAccelerator(subMenu, comp.getSettings()
242: .getLanguageDependentString(
243: act[i].substring(1)
244: + ACCELERATION_POSTFIX));
245: BarFactory
246: .setMnemonic(subMenu, comp.getSettings()
247: .getLanguageDependentString(
248: act[i].substring(1)
249: + MNEMONIC_POSTFIX));
250: menu.add(subMenu);
251: } else if (act[i].startsWith(JAWE_STANDARD_MENU_PREFIX)) {
252: menu.add(JaWEManager.getInstance().getJaWEController()
253: .getJaWEActions().getActionMenu(act[i],
254: addBCListener));
255: } else if (act[i].startsWith(JAWE_STANDARD_ACTION_PREFIX)) {
256: menu.add(JaWEManager.getInstance().getJaWEController()
257: .getJaWEActions().getActionMenuItem(
258: act[i].substring(11), addBCListener));
259: } else if (act[i].startsWith(SUBMENU_PREFIX)) {
260: menu.add(createSubMenu(act[i].substring(1), comp,
261: addBCListener));
262: } else {
263: JaWEAction ja = comp.getSettings().getAction(act[i]);
264: if (ja != null) {
265: menu.add(createMenuItem(ja, comp, addBCListener));
266: }
267: }
268: }
269:
270: return menu;
271: }
272:
273: protected static JMenu createSubMenu(String name,
274: JaWEComponent comp, boolean addBCListener) {
275: JMenu menu = new JMenu();
276:
277: String depName = (String) comp.getSettings().getSetting(
278: name + LANGUAGEDEPENDENTNAME_POSTFIX);
279: String langDepName = comp.getSettings()
280: .getLanguageDependentString(depName + LABEL_POSTFIX);
281: if (langDepName == null) {
282: langDepName = name;
283: }
284:
285: BarFactory.setAccelerator(menu, comp.getSettings()
286: .getLanguageDependentString(
287: depName + ACCELERATION_POSTFIX));
288: BarFactory
289: .setMnemonic(menu, comp.getSettings()
290: .getLanguageDependentString(
291: depName + MNEMONIC_POSTFIX));
292:
293: menu.setText(langDepName);
294:
295: String actionOrder = comp.getSettings()
296: .getMenuActionOrder(name);
297: String[] act = Utils.tokenize(actionOrder, ACTION_DELIMITER);
298: int cnt = 0;
299: for (int i = 0; i < act.length; i++) {
300: if (act[i].equals(ACTION_SEPARATOR)) {
301: menu.addSeparator();
302: cnt++;
303: } else if (act[i].startsWith(JAWECOMPONENT_AS_MENU_PREFIX)) {
304: String className = (String) comp
305: .getSettings()
306: .getSetting(
307: act[i].substring(1) + CLASSNAME_POSTFIX);
308: String settingsName = (String) comp.getSettings()
309: .getSetting(
310: act[i].substring(1)
311: + SETTINGSNAME_POSTFIX);
312: JMenu subMenu = getExternalMenu(className, settingsName);
313: if (subMenu != null) {
314: BarFactory.setAccelerator(subMenu, comp
315: .getSettings().getLanguageDependentString(
316: act[i].substring(1)
317: + ACCELERATION_POSTFIX));
318: BarFactory.setMnemonic(subMenu, comp.getSettings()
319: .getLanguageDependentString(
320: act[i].substring(1)
321: + MNEMONIC_POSTFIX));
322: menu.add(subMenu);
323: cnt++;
324: } else {
325: System.out.println("Can't create ext meny "
326: + settingsName + " for cn " + className);
327: }
328: } else if (act[i].startsWith(JAWE_STANDARD_MENU_PREFIX)) {
329: menu.add(JaWEManager.getInstance().getJaWEController()
330: .getJaWEActions().getActionMenu(act[i],
331: addBCListener));
332: cnt++;
333: } else if (act[i].startsWith(JAWE_STANDARD_ACTION_PREFIX)) {
334: menu.add(JaWEManager.getInstance().getJaWEController()
335: .getJaWEActions().getActionMenuItem(
336: act[i].substring(11), addBCListener));
337: cnt++;
338: } else if (act[i].startsWith(SUBMENU_PREFIX)) {
339: JMenu sm = createSubMenu(act[i], comp, addBCListener);
340: if (sm != null) {
341: menu.add(sm);
342: cnt++;
343: }
344: } else {
345: JaWEAction ja = comp.getSettings().getAction(act[i]);
346: if (ja != null) {
347: menu.add(createMenuItem(ja, comp, addBCListener));
348: cnt++;
349: }
350: }
351: }
352:
353: if (cnt > 0) {
354: return menu;
355: }
356: return null;
357: }
358:
359: protected static JMenu getExternalMenu(String className,
360: String settingsClassName) {
361: try {
362: Constructor sc = Class.forName(settingsClassName)
363: .getConstructor(new Class[] {});
364: JaWEComponentSettings settings = (JaWEComponentSettings) sc
365: .newInstance(new Object[0]);
366:
367: Constructor c = Class.forName(className).getConstructor(
368: new Class[] { JaWEComponentSettings.class });
369: JaWEComponent jc = (JaWEComponent) c
370: .newInstance(new Object[] { settings });
371: return (JMenu) (jc.getView());
372: } catch (Throwable thr) {
373: }
374: return null;
375: }
376:
377: public static JMenuItem createMenuItem(JaWEAction ja,
378: JaWEComponent comp, boolean addBCListener) {
379: Action a = ja.getAction();
380:
381: String aname = null;
382: if (a != null) {
383: aname = (String) a.getValue(Action.NAME);
384: }
385: String depName = ja.getLangDepName();
386: if (aname == null) {
387: aname = depName;
388: }
389:
390: if (depName == null) {
391: depName = aname;
392: }
393:
394: String label = comp.getSettings().getLanguageDependentString(
395: depName + LABEL_POSTFIX);
396: if (label == null) {
397: label = aname;
398: }
399:
400: JMenuItem mi = new JMenuItem(label);
401: mi.setName(aname);
402:
403: ImageIcon ai = ja.getIcon();
404: if (ai != null) {
405: mi.setHorizontalTextPosition(SwingConstants.RIGHT);
406: mi.setIcon(ai);
407: }
408:
409: BarFactory.setAccelerator(mi, comp.getSettings()
410: .getLanguageDependentString(
411: depName + ACCELERATION_POSTFIX));
412: BarFactory
413: .setMnemonic(mi, comp.getSettings()
414: .getLanguageDependentString(
415: depName + MNEMONIC_POSTFIX));
416:
417: mi.setActionCommand(aname);
418: if (a != null) {
419: mi.addActionListener(a);
420: if (addBCListener) {
421: a
422: .addPropertyChangeListener(new ButtonPropertyChangedListener(
423: mi));
424: }
425: mi.setEnabled(a.isEnabled());
426: } else {
427: mi.setEnabled(false);
428: }
429:
430: return mi;
431: }
432:
433: public static void setMnemonic(JMenuItem mi, String mnemonic) {
434: if (mnemonic != null && mnemonic.length() > 0) {
435: mi.setMnemonic(mnemonic.toCharArray()[0]);
436: }
437: }
438:
439: public static void setAccelerator(JMenuItem mi, String accel) {
440: if (accel != null) {
441: try {
442: int mask = 0;
443: if (accel.startsWith(CTRL_PREFIX)) {
444: mask += ActionEvent.CTRL_MASK;
445: accel = accel.substring(CTRL_PREFIX.length() + 1);
446: }
447: if (accel.startsWith(SHIFT_PREFIX)) {
448: mask += ActionEvent.SHIFT_MASK;
449: accel = accel.substring(SHIFT_PREFIX.length() + 1);
450: }
451: if (accel.startsWith(ALT_PREFIX)) {
452: mask += ActionEvent.ALT_MASK;
453: accel = accel.substring(ALT_PREFIX.length() + 1);
454: }
455: int key = KeyEvent.class.getField("VK_" + accel)
456: .getInt(null);
457: mi.setAccelerator(KeyStroke.getKeyStroke(key, mask));
458: } catch (Exception e) {
459: System.err
460: .println("Error while assigning accelerator !!!");
461: }
462: }
463: }
464: }
|