1 /* -------------------------------------------------------------------
2 * Java source file for the class UiUtil
3 *
4 * Copyright (c), 2002, Masahiro Takatsuka.
5 * All Rights Researved.
6 *
7 * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8 * $Author: takatsukam $
9 *
10 * $Date: 2003/07/25 04:51:46 $
11 *
12 * $Id: UiUtil.java,v 1.1.1.1 2003/07/25 04:51:46 takatsukam Exp $
13 *
14 * Reference: Document no:
15 * ___ ___
16 *
17 * To Do:
18 * ___
19 *
20 ------------------------------------------------------------------- */
21
22 /* --------------------------- Package ---------------------------- */
23 package net.jbeans.ui;
24
25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">color="#329900"> ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900"> ------------------ Import classes (packages) ------------------- */
26 import java.awt.*;
27 import java.awt.event.*;
28 import java.util.*;
29 import javax.swing.*;
30 import javax.swing.border.*;
31 import javax.swing.text.*;
32
33 import net.jbeans.ui.border.*;
34 import net.jbeans.ui.label.*;
35 import net.jbeans.util.*;
36
37 /*====================================================================
38 Implementation of class UiUtil
39 ====================================================================*/
40 /***
41 * UiUtil provides various swing related utilities. Many of constants
42 * (component sizes, colors, fonts) are obtained from
43 * "Java(TM) Look and Feel Design Guidelines, Sun Microsystems, Inc.,
44 * Addison-Wesley, 1999"
45 *
46 * @version $Revision: 1.1.1.1 $
47 * @author Masahiro Takatsuka (masa@jbeans.net)
48 */
49
50 public class UiUtil {
51 private UiUtil() {
52 super();
53 }
54
55 /*
56 * private method to retrieve String form resource.
57 */
58 private static String swingResources = "net.jbeans.ui.resources.UIConstants";
59 private static String getResourceValue(String key) {
60 return ResourceBundleUtil.getResourceValue(swingResources, key);
61 }
62
63 /*
64 * Texts for JButton.
65 */
66 public static final String BUTTONTEXT_ADD = getResourceValue("net.jbeans.ui.string.add");
67 public static final String BUTTONTEXT_DEL = getResourceValue("net.jbeans.ui.string.delete");
68 public static final String BUTTONTEXT_RENAME = getResourceValue("net.jbeans.ui.string.rename");
69 public static final String BUTTONTEXT_APPLY = getResourceValue("net.jbeans.ui.string.apply");
70 public static final String BUTTONTEXT_EDIT = getResourceValue("net.jbeans.ui.string.edit");
71 public static final String BUTTONTEXT_NEW = getResourceValue("net.jbeans.ui.string.new");
72 public static final String BUTTONTEXT_REMOVE = getResourceValue("net.jbeans.ui.string.remove");
73 public static final String BUTTONTEXT_OK = getResourceValue("net.jbeans.ui.string.ok");
74 public static final String BUTTONTEXT_CANCEL = getResourceValue("net.jbeans.ui.string.cancel");
75 public static final String BUTTONTEXT_CLOSE = getResourceValue("net.jbeans.ui.string.close");
76 public static final String BUTTONTEXT_NEXT = getResourceValue("net.jbeans.ui.string.next");
77 public static final String BUTTONTEXT_BACK = getResourceValue("net.jbeans.ui.string.back");
78 public static final String BUTTONTEXT_FINISH = getResourceValue("net.jbeans.ui.string.finish");
79 public static final String BUTTONTEXT_MOVEUP = getResourceValue("net.jbeans.ui.string.move_up");
80 public static final String BUTTONTEXT_MOVEDOWN = getResourceValue("net.jbeans.ui.string.move_down");
81 public static final String BUTTONTEXT_HELP = getResourceValue("net.jbeans.ui.string.help");
82
83 /*
84 * Mnemonic for JButton.
85 */
86 public static final int MNEMONIC_ADD = 'A';
87 public static final int MNEMONIC_DEL = 'D';
88 public static final int MNEMONIC_RENAME = 'R';
89 public static final int MNEMONIC_APPLY = 'A';
90 public static final int MNEMONIC_EDIT = 'E';
91 public static final int MNEMONIC_NEW = 'N';
92 public static final int MNEMONIC_REMOVE = 'R';
93 public static final int MNEMONIC_OK = 'O';
94 public static final int MNEMONIC_CANCEL = 'C';
95 public static final int MNEMONIC_CLOSE = 'L';
96 public static final int MNEMONIC_NEXT = 'N';
97 public static final int MNEMONIC_BACK = 'B';
98 public static final int MNEMONIC_FINISH = 'F';
99 public static final int MNEMONIC_MOVEUP = 'U';
100 public static final int MNEMONIC_MOVEDOWN = 'D';
101 public static final int MNEMONIC_HELP = 'H';
102
103 // Size of a standard Button Size
104 public static final int BUTTON_WIDTH = 100;
105 public static final int BUTTON_HEIGHT = 26;
106
107 // Size of a standard toolbar button
108 public static final int BUTTCON_WIDTH = 28;
109 public static final int BUTTCON_HEIGHT = 28;
110
111 // Size of a smaller Button Size
112 public static final int SM_BUTTON_WIDTH = 72;
113 public static final int SM_BUTTON_HEIGHT = 26;
114
115 // Size of a label
116 public static final int LABEL_WIDTH = 100;
117 public static final int LABEL_HEIGHT = 20;
118
119 // Size of a textfield
120 public static final int TEXT_WIDTH = 150;
121 public static final int TEXT_HEIGHT = 20;
122
123 // Command Strings
124 public static final String BUTTON_CMD_OK = "ok-command";
125 public static final String BUTTON_CMD_CANCEL = "cancel-command";
126 public static final String BUTTON_CMD_NEXT = "next-command";
127 public static final String BUTTON_CMD_BACK = "back-command";
128 public static final String BUTTON_CMD_FINISH = "finish-command";
129 public static final String BUTTON_CMD_ADD = "add-command";
130 public static final String BUTTON_CMD_REMOVE = "remove-command";
131 public static final String BUTTON_CMD_MOVEUP = "moveup-command";
132 public static final String BUTTON_CMD_MOVEDOWN = "movedown-command";
133 public static final String BUTTON_CMD_HELP = "help-command";
134
135 // Preferred size for buttons
136 public static Dimension buttonPrefSize = new Dimension(BUTTON_WIDTH,
137 BUTTON_HEIGHT);
138
139 public static Dimension buttconPrefSize = new Dimension(BUTTCON_WIDTH,
140 BUTTCON_HEIGHT);
141
142 public static Dimension smbuttonPrefSize = new Dimension(SM_BUTTON_WIDTH,
143 SM_BUTTON_HEIGHT);
144 // Preferred size for labels
145 public static Dimension labelPrefSize = new Dimension(LABEL_WIDTH,
146 LABEL_HEIGHT);
147 // Preferred size for textfields
148 public static Dimension textPrefSize = new Dimension(TEXT_WIDTH,
149 TEXT_HEIGHT);
150
151 /***
152 * Creates a label which will be displayed.
153 *
154 * @param text Label's text.
155 * @param mnemonic Hot key
156 * @param comp Component that this label is for.
157 */
158 public static JLabel createLabel(String text, int mnemonic,
159 Component comp) {
160 OrientedLabel label = new OrientedLabel(" " + text);
161 label.setMinimumSize(labelPrefSize);
162
163 if (mnemonic != -1)
164 label.setDisplayedMnemonic(mnemonic);
165
166 if (comp != null)
167 label.setLabelFor(comp);
168
169 if (text.length() == 0) {
170 label.setPreferredSize(labelPrefSize);
171 }
172
173 return label;
174 }
175
176 /***
177 * Creates a label which will be displayed.
178 *
179 * @param text Label's text.
180 */
181 public static JLabel createLabel(String text) {
182 return createLabel(text, -1, null);
183 }
184
185 /***
186 * Creates a text field
187 *
188 * @param text TextField's text.
189 * @param listener KeyListener
190 * @param numbers A flag to indicates that the field contains numbers only.
191 */
192 public static JTextField createTextField(String text,
193 KeyListener listener,
194 boolean numbers) {
195 JTextField field = new JTextField(text);
196
197 field.setMinimumSize(textPrefSize);
198
199 if (text.length() == 0) {
200 field.setPreferredSize(textPrefSize);
201 }
202
203 if (listener != null) {
204 field.addKeyListener(listener);
205 }
206
207 if (numbers) {
208 field.setDocument(new NumberDocument());
209 }
210
211 return field;
212 }
213
214 /***
215 * Creates a text field
216 *
217 * @param text TextField's text.
218 * @param numbers A flag to indicates that the field contains numbers only.
219 */
220 public static JTextField createTextField(String text, boolean numbers) {
221 return createTextField(text, null, numbers);
222 }
223
224 /***
225 * Creates a text field
226 *
227 * @param text TextField's text.
228 * @param listener KeyListener
229 */
230 public static JTextField createTextField(String text, KeyListener listener) {
231 return createTextField(text, listener, false);
232 }
233
234 /***
235 * Creates a text field
236 *
237 * @param text TextField's text.
238 */
239 public static JTextField createTextField(String text) {
240 return createTextField(text, null, false);
241 }
242
243 /***
244 * Subclass of javax.swing.text.PlainDocument class.
245 * It accepts only numbers.
246 */
247 static class NumberDocument extends PlainDocument {
248 public void insertString(int offs, String str, AttributeSet atts) throws BadLocationException {
249 if (!Character.isDigit(str.charAt(0))) {
250 return;
251 }
252 super.insertString(offs, str, atts);
253 }
254 }
255
256 /***
257 * Creates a Radio Button
258 *
259 * @param text Button's text.
260 * @param mnemonic Hot key.
261 * @param listener ActionListener.
262 * @param selected A flag to indicate if button should be selected.
263 */
264 public static JRadioButton createRadioButton(String text, int mnemonic,
265 ActionListener listener,
266 boolean selected) {
267 JRadioButton button = new JRadioButton(text);
268 button.setMnemonic(mnemonic);
269 button.setSelected(selected);
270 button.setMinimumSize(labelPrefSize);
271
272 if (listener != null) {
273 button.addActionListener(listener);
274 }
275
276 if (text.length() == 0) {
277 button.setPreferredSize(labelPrefSize);
278 }
279
280 return button;
281 }
282
283 /***
284 * Creates a Radio Button
285 *
286 * @param text Button's text.
287 * @param mnemonic Hot key.
288 * @param selected A flag to indicate if button should be selected.
289 */
290 public static JRadioButton createRadioButton(String text, int mnemonic,
291 boolean selected) {
292 return createRadioButton(text, mnemonic, null, selected);
293 }
294
295 /***
296 * Creates a Radio Button
297 *
298 * @param text Button's text.
299 * @param mnemonic Hot key.
300 * @param listener ActionListener.
301 */
302 public static JRadioButton createRadioButton(String text, int mnemonic,
303 ActionListener listener) {
304 return createRadioButton(text, mnemonic, listener, false);
305 }
306
307 /***
308 * Creates a Radio Button
309 *
310 * @param text Button's text.
311 * @param mnemonic Hot key.
312 */
313 public static JRadioButton createRadioButton(String text, int mnemonic) {
314 return createRadioButton(text, mnemonic, null, false);
315 }
316
317 /***
318 * Creates a Radio Button
319 *
320 * @param text Button's text.
321 */
322 public static JRadioButton createRadioButton(String text) {
323 return createRadioButton(text, -1, null, false);
324 }
325
326 /***
327 * Create a checkbox
328 *
329 * @param text CheckBox's text.
330 * @param mnemonic Hot key
331 * @param listener ActionListener
332 * @param selected A flag to indicate if button should be selected.
333 */
334 public static JCheckBox createCheckBox(String text, int mnemonic,
335 ActionListener listener,
336 boolean selected) {
337 JCheckBox checkbox = new JCheckBox(text);
338 checkbox.setMinimumSize(labelPrefSize);
339
340 if (mnemonic != -1)
341 checkbox.setMnemonic(mnemonic);
342
343 checkbox.setSelected(selected);
344
345 if (text.length() == 0) {
346 checkbox.setPreferredSize(labelPrefSize);
347 }
348
349 if (listener != null) {
350 checkbox.addActionListener(listener);
351 }
352
353 return checkbox;
354 }
355
356 /***
357 * Create a checkbox
358 *
359 * @param text CheckBox's text.
360 * @param mnemonic Hot key
361 * @param listener ActionListener
362 */
363 public static JCheckBox createCheckBox(String text, int mnemonic,
364 ActionListener listener) {
365 return createCheckBox(text, mnemonic, listener, false);
366 }
367
368 /***
369 * Create a checkbox
370 *
371 * @param text CheckBox's text.
372 * @param mnemonic Hot key
373 * @param selected A flag to indicate if button should be selected.
374 */
375 public static JCheckBox createCheckBox(String text, int mnemonic,
376 boolean selected) {
377 return createCheckBox(text, mnemonic, null, selected);
378 }
379
380 /***
381 * Create a checkbox
382 *
383 * @param text CheckBox's text.
384 * @param mnemonic Hot key
385 */
386 public static JCheckBox createCheckBox(String text, int mnemonic) {
387 return createCheckBox(text, mnemonic, null, false);
388 }
389
390 /***
391 * Create a checkbox
392 *
393 * @param text CheckBox's text.
394 */
395 public static JCheckBox createCheckBox(String text) {
396 return createCheckBox(text, -1, null, false);
397 }
398
399 /***
400 * Creates a JComboBox
401 *
402 * @param items An array of object.
403 * @param listener The action listener to handle events
404 * @param editable A flag that indicates if this combo box is editable
405 */
406 public static JComboBox createComboBox(Object[] items,
407 ActionListener listener,
408 boolean editable) {
409 JComboBox comboBox = new JComboBox(items);
410
411 if (listener != null)
412 comboBox.addActionListener(listener);
413
414 comboBox.setEditable(editable);
415
416 return comboBox;
417 }
418
419 /***
420 * Creates a JComboBox
421 *
422 * @param items An array of object.
423 * @param editable A flag that indicates if this combo box is editable
424 */
425 public static JComboBox createComboBox(Object[] items, boolean editable) {
426 return createComboBox(items, null, editable);
427 }
428
429 /***
430 * Creates a JComboBox
431 *
432 * @param items Vector containing items.
433 * @param listener The action listener to handle events
434 * @param editable A flag that indicates if this combo box is editable
435 */
436 public static JComboBox createComboBox(Vector items,
437 ActionListener listener,
438 boolean editable) {
439 JComboBox comboBox = new JComboBox(items);
440
441 if (listener != null)
442 comboBox.addActionListener(listener);
443
444 comboBox.setEditable(editable);
445
446 return comboBox;
447 }
448
449 /***
450 * Creates a JComboBox
451 *
452 * @param items Vector containing items.
453 * @param editable A flag that indicates if this combo box is editable
454 */
455 public static JComboBox createComboBox(Vector items, boolean editable) {
456 return createComboBox(items, null, editable);
457 }
458
459 /***
460 * Creates a JButton
461 *
462 * @param text Button's text.
463 * @param listener The action listener to handle events.
464 * @param mnemonic Letter combination
465 */
466 public static JButton createButton(String text, ActionListener listener,
467 int mnemonic) {
468 JButton button = new JButton(text);
469 button.setMinimumSize(buttonPrefSize);
470
471 if (listener != null)
472 button.addActionListener(listener);
473
474 if (mnemonic != -1)
475 button.setMnemonic(mnemonic);
476
477 if (text.length() == 0) {
478 button.setPreferredSize(buttonPrefSize);
479 } else {
480 // Make sure that the button has a minimum size.
481 // This is a bit of a hack since setMinimumSize doens't work for all
482 // layout managers.
483 Dimension size = button.getPreferredSize();
484 if (size.width < buttonPrefSize.width) {
485 button.setPreferredSize(buttonPrefSize);
486 }
487 }
488
489 return button;
490 }
491
492 /***
493 * Creates a JButton
494 *
495 * @param text Button's text.
496 * @param listener The action listener to handle events.
497 */
498 public static JButton createButton(String text, ActionListener listener) {
499 return createButton(text, listener, -1);
500 }
501
502 /***
503 * Creates a small JButton
504 *
505 * @param text Button's text.
506 * @param listener The action listener to handle events.
507 * @param mnemonic Letter combination
508 */
509 public static JButton createSmallButton(String text,
510 ActionListener listener,
511 int mnemonic) {
512 JButton button = createButton(text, listener, mnemonic);
513 button.setMinimumSize(smbuttonPrefSize);
514
515 if (text.length() == 0) {
516 button.setPreferredSize(smbuttonPrefSize);
517 } else {
518 Dimension size = button.getPreferredSize();
519 if (size.width < smbuttonPrefSize.width) {
520 button.setPreferredSize(smbuttonPrefSize);
521 }
522 }
523
524 return button;
525 }
526
527 /***
528 * Creates a small JButton
529 *
530 * @param text Button's text.
531 * @param listener The action listener to handle events.
532 */
533 public static JButton createSmallButton(String text, ActionListener listener) {
534 return createSmallButton(text, listener, -1);
535 }
536
537 /***
538 * Creates an etched border with the displayed text
539 */
540 public static Border createBorder(String text, int justification, int orientation) {
541 return CustomBorderFactory.createBorder(text, justification, orientation);
542 }
543
544 /***
545 * Creates an etched border with the displayed text
546 */
547 public static Border createBorder(String text) {
548 return createBorder(text, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.TOP);
549 }
550
551 /***
552 * Creates an blank border.
553 */
554 public static Border createBorder() {
555 return CustomBorderFactory.createBorder();
556 }
557
558 /***
559 * Creates a scrollpane with and etched border and a title around a JList.
560 *
561 * @param list JList to contain.
562 * @param text Text to display in the border.
563 */
564 public static JScrollPane createListPane(JList list, String text) {
565 return createListPane(list, text, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.TOP);
566 }
567
568 /***
569 * Creates a scrollpane with and etched border and a title around a JList.
570 *
571 * @param list JList to contain.
572 * @param text Text to display in the border.
573 */
574 public static JScrollPane createListPane(JList list, String text,
575 int justification, int orientation) {
576 JScrollPane pane = new JScrollPane(list);
577
578 pane.setBorder(BorderFactory.createCompoundBorder(createBorder(text, justification, orientation), BorderFactory.createLoweredBevelBorder()));
579 return pane;
580 }
581
582 /***
583 * Centers the specified component in it's parent component.
584 * If parent is null, the compoenent is centered in screen.
585 *
586 * The source and parent components should be correctly sized.
587
588 */
589 public static void centerComponent(Component source, Component parent) {
590 Rectangle rect;
591 Dimension dim = source.getSize();
592
593 if (parent != null) {
594 rect = parent.getBounds();
595 } else {
596 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
597 rect = new Rectangle(0, 0, d.width, d.height);
598 }
599
600 int x = rect.x + (rect.width - dim.width)/2;
601 int y = rect.y + (rect.height - dim.height)/2;
602
603 source.setLocation(x, y);
604 }
605
606 /***
607 * Centers the Component on the screen.
608 */
609 public static void centerComponent(Component source) {
610 centerComponent(source, null);
611 }
612
613 /***
614 * Retrieves the parent JFrame for a component. This is handy when you
615 * want the frame as an owner for modal dialogs.
616 *
617 * @param soruce - Source component.
618 * @return The JFrame which contains the component or null if not found.
619 */
620 public static JFrame getParentFrame(Component source) {
621 Container parent = source.getParent();
622
623 while (parent != null) {
624 if (parent instanceof JFrame) {
625 break;
626 } else {
627 parent = parent.getParent();
628 }
629 }
630 if (parent == null) {
631 return null;
632 } else {
633 return (JFrame) parent;
634 }
635 }
636
637 // Cursor operations
638 public static void setWaitCursor(Component comp) {
639 comp.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
640 }
641
642 public static void setDefaultCursor(Component comp) {
643 comp.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
644 }
645
646 public static void addMenuItem(ActionListener listener, JComponent m, JMenuItem mi) {
647 // Register the Studio as the event handler.
648 mi.addActionListener(listener);
649 // Add the menuitem to the menu
650 m.add(mi);
651 }
652
653 public static void addSeparator(JComponent comp) {
654 if (comp instanceof JPopupMenu) {
655 ((JPopupMenu) comp).addSeparator();
656 } else if (comp instanceof JMenu) {
657 ((JMenu) comp).addSeparator();
658 }
659 }
660
661 public static JMenu getMenuForName(String name, JMenuBar bar) {
662 int count = bar.getMenuCount();
663 JMenu m = null;
664 for (int i = 0; i < count; i++) {
665 m = bar.getMenu(i);
666 if (name.equals(m.getText())) {
667 return m;
668 }
669 }
670 return null;
671 }
672 }
This page was automatically generated by Maven