001: package jimm.datavision.gui;
002:
003: import jimm.datavision.PaperFormat;
004: import jimm.util.I18N;
005: import jimm.util.StringUtils;
006: import java.awt.Font;
007: import java.awt.event.*;
008: import java.util.List;
009: import java.util.Iterator;
010: import javax.swing.*;
011:
012: /**
013: * Menu creation utilities.
014: *
015: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
016: */
017: public class MenuUtils {
018:
019: public static JMenu readMenu(String key) {
020: JMenu menu = new JMenu(I18N.get(I18N.MENU_FILE_PREFIX, key));
021: setKeys(menu, key); // Read .keys property; set mnemonic
022: return menu;
023: }
024:
025: public static JMenuItem readItem(ActionListener listener,
026: String key, Font font) {
027: JMenuItem item = new JMenuItem(I18N.get(I18N.MENU_FILE_PREFIX,
028: key));
029: modifyItem(item, listener, key, font);
030: return item;
031: }
032:
033: public static JCheckBoxMenuItem readCheckboxItem(
034: ActionListener listener, String key, Font font) {
035: JCheckBoxMenuItem item = new JCheckBoxMenuItem(I18N.get(
036: I18N.MENU_FILE_PREFIX, key));
037: modifyItem(item, listener, key, font);
038: return item;
039: }
040:
041: protected static void modifyItem(JMenuItem item,
042: ActionListener listener, String key, Font font) {
043: if (listener != null)
044: item.addActionListener(listener);
045: if (font != null)
046: item.setFont(font);
047:
048: String val = I18N.getNullIfMissing(I18N.MENU_FILE_PREFIX, key
049: + ".action");
050: if (val != null && val.length() > 0)
051: item.setActionCommand(val);
052:
053: val = I18N.getNullIfMissing(I18N.MENU_FILE_PREFIX, key
054: + ".enabled");
055: if (val != null
056: && val.length() > 0
057: && ("false".equalsIgnoreCase(val) || "no"
058: .equalsIgnoreCase(val)))
059: item.setEnabled(false);
060:
061: setKeys(item, key); // Read .keys property; set mnemonic and accel
062: }
063:
064: /**
065: * Adds a single item to a menu.
066: *
067: * @param listener action listener for item; may be <code>null</code>
068: * @param menu the menu
069: * @param key the menu properties file lookup key
070: */
071: public static JMenuItem addToMenu(ActionListener listener,
072: JMenu menu, String key) {
073: return addToMenu(listener, menu, key, null);
074: }
075:
076: /**
077: * Adds a single item to a menu.
078: *
079: * @param listener action listener for item; may be <code>null</code>
080: * @param menu the menu
081: * @param key the menu properties file lookup key
082: * @param font font; may be <code>null</code>
083: */
084: public static JMenuItem addToMenu(ActionListener listener,
085: JMenu menu, String key, Font font) {
086: JMenuItem item = readItem(listener, key, font);
087: menu.add(item);
088: return item;
089: }
090:
091: /**
092: * Adds a single item to a popup menu.
093: *
094: * @param listener action listener for item; may be <code>null</code>
095: * @param menu the menu
096: * @param key the menu properties file lookup key
097: */
098: public static JMenuItem addToMenu(ActionListener listener,
099: JPopupMenu menu, String key) {
100: return addToMenu(listener, menu, key, null);
101: }
102:
103: /**
104: * Adds a single item to a popup menu.
105: *
106: * @param listener action listener for item; may be <code>null</code>
107: * @param menu the menu
108: * @param key the menu properties file lookup key
109: * @param font font; may be <code>null</code>
110: */
111: public static JMenuItem addToMenu(ActionListener listener,
112: JPopupMenu menu, String key, Font font) {
113: JMenuItem item = readItem(listener, key, font);
114: menu.add(item);
115: return item;
116: }
117:
118: /**
119: * Adds a single checkbox item to a menu.
120: *
121: * @param listener action listener for item; may be <code>null</code>
122: * @param menu the menu
123: * @param key the menu properties file lookup key
124: * @param font font; may be <code>null</code>
125: */
126: public static JCheckBoxMenuItem addCheckboxToMenu(
127: ActionListener listener, JMenu menu, String key, Font font) {
128: JCheckBoxMenuItem item = readCheckboxItem(listener, key, font);
129: menu.add(item);
130: return item;
131: }
132:
133: /**
134: * Adds a single checkbox item to a popup menu.
135: *
136: * @param listener action listener for item; may be <code>null</code>
137: * @param menu the menu
138: * @param key the menu properties file lookup key
139: * @param font font; may be <code>null</code>
140: */
141: public static JCheckBoxMenuItem addCheckboxToMenu(
142: ActionListener listener, JPopupMenu menu, String key,
143: Font font) {
144: JCheckBoxMenuItem item = readCheckboxItem(listener, key, font);
145: menu.add(item);
146: return item;
147: }
148:
149: /**
150: * Adds an action to a menu.
151: *
152: * @param menu the menu
153: * @param action the action
154: * @param key the menu properties file lookup key
155: */
156: public static JMenuItem addToMenu(JMenu menu, Action action,
157: String key) {
158: JMenuItem item = menu.add(action);
159: setKeys(item, key); // Read .keys property; set mnemonic and accel
160: return item;
161: }
162:
163: protected static void setKeys(JMenuItem item, String key) {
164: String keys = I18N.getNullIfMissing(I18N.MENU_FILE_PREFIX, key
165: + ".keys");
166: if (keys == null)
167: return;
168:
169: List split = StringUtils.split(keys, " ");
170:
171: if (split.size() >= 1) { // Mnemonic key
172: key = (String) split.get(0);
173: item.setMnemonic((int) key.charAt(0));
174: }
175:
176: if (split.size() >= 2) { // Accelerator key
177: key = (String) split.get(1);
178: int stroke = (int) key.charAt(0);
179: int mask = ActionEvent.CTRL_MASK;
180: if ("DEL".equals(key)) {
181: stroke = KeyEvent.VK_DELETE;
182: mask = 0;
183: }
184: item.setAccelerator(KeyStroke.getKeyStroke(stroke, mask));
185: }
186: }
187:
188: /**
189: * Returns a new align menu.
190: *
191: * @param listener action listener for item; may be <code>null</code>
192: * @param font font; may be <code>null</code>
193: * @return a new menu
194: */
195: public static JMenu buildAlignMenu(ActionListener listener,
196: Font font) {
197: JMenu menu = readMenu("Align.menu");
198:
199: MenuUtils.addToMenu(listener, menu, "Align.menu_tops", font);
200: MenuUtils.addToMenu(listener, menu, "Align.menu_middles", font);
201: MenuUtils.addToMenu(listener, menu, "Align.menu_bottoms", font);
202: MenuUtils.addToMenu(listener, menu, "Align.menu_lefts", font);
203: MenuUtils.addToMenu(listener, menu, "Align.menu_centers", font);
204: MenuUtils.addToMenu(listener, menu, "Align.menu_rights", font);
205: MenuUtils.addToMenu(listener, menu, "Align.menu_snap", font);
206:
207: return menu;
208: }
209:
210: /**
211: * Returns a new size menu.
212: *
213: * @param listener action listener for item; may be <code>null</code>
214: * @param font font; may be <code>null</code>
215: * @return a new menu
216: */
217: public static JMenu buildSizeMenu(ActionListener listener, Font font) {
218: JMenu menu = readMenu("Size.menu");
219:
220: MenuUtils.addToMenu(listener, menu, "Size.menu_same_width",
221: font);
222: MenuUtils.addToMenu(listener, menu, "Size.menu_same_height",
223: font);
224: MenuUtils
225: .addToMenu(listener, menu, "Size.menu_same_size", font);
226:
227: return menu;
228: }
229:
230: /**
231: * Returns a new paper size menu.
232: *
233: * @param listener action button listener for item; may be <code>null</code>
234: * @param currChoice current paper choice (its orientation and name will be
235: * pre-selected)
236: * @param orientationGroup a radio button group for orientations
237: * @param nameGroup a radio button group for names
238: * @return a new menu
239: */
240: public static JMenu buildPaperSizeMenu(ActionListener listener,
241: PaperFormat currChoice, ButtonGroup orientationGroup,
242: ButtonGroup nameGroup) {
243: JMenu menu = readMenu("MenuUtils.menu_paper_size");
244:
245: JRadioButtonMenuItem item = null;
246:
247: // Portrait
248: String key = "MenuUtils.menu_paper_size_portrait";
249: String str = I18N.get(I18N.MENU_FILE_PREFIX, key);
250: item = new JRadioButtonMenuItem(str);
251:
252: String action = I18N.getNullIfMissing(I18N.MENU_FILE_PREFIX,
253: key + ".action");
254: if (action != null && action.length() > 0)
255: item.setActionCommand(action);
256:
257: orientationGroup.add(item);
258: if (listener != null)
259: item.addActionListener(listener);
260: if (currChoice.getOrientation() == PaperFormat.PORTRAIT)
261: item.setSelected(true);
262: menu.add(item);
263:
264: // Landscape
265: key = "MenuUtils.menu_paper_size_landscape";
266: str = I18N.get(I18N.MENU_FILE_PREFIX, key);
267: item = new JRadioButtonMenuItem(str);
268:
269: action = I18N.getNullIfMissing(I18N.MENU_FILE_PREFIX, key
270: + ".action");
271: if (action != null && action.length() > 0)
272: item.setActionCommand(action);
273:
274: orientationGroup.add(item);
275: if (listener != null)
276: item.addActionListener(listener);
277: if (currChoice.getOrientation() == PaperFormat.LANDSCAPE)
278: item.setSelected(true);
279: menu.add(item);
280:
281: menu.addSeparator();
282:
283: // Paper sizes
284: for (Iterator iter = PaperFormat.names(); iter.hasNext();) {
285: String name = (String) iter.next();
286: item = new JRadioButtonMenuItem(name);
287: nameGroup.add(item);
288: if (listener != null)
289: item.addActionListener(listener);
290:
291: if (name.equals(currChoice.getName()))
292: item.setSelected(true);
293:
294: menu.add(item);
295: }
296:
297: return menu;
298: }
299: }
|