KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > view > ViewHelper


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.designer.view;
5
6 import java.awt.Component JavaDoc;
7 import java.awt.Dialog JavaDoc;
8 import java.awt.Frame JavaDoc;
9 import java.awt.HeadlessException JavaDoc;
10 import java.awt.Window JavaDoc;
11 import java.awt.event.ActionEvent JavaDoc;
12
13 import javax.swing.AbstractAction JavaDoc;
14 import javax.swing.Action JavaDoc;
15 import javax.swing.JButton JavaDoc;
16 import javax.swing.JPopupMenu JavaDoc;
17 import javax.swing.SwingUtilities JavaDoc;
18
19 import org.oddjob.designer.Looks;
20 import org.oddjob.designer.model.ComponentAction;
21 import org.oddjob.designer.model.DesignDefinition;
22
23 /**
24  * Utility methods for Swing Views.
25  */

26 public class ViewHelper {
27
28     /**
29      * Create a standard detail button for a DesignDefinition.
30      *
31      * @param def The DesignDefinition.
32      *
33      * @return A button.
34      */

35     public static Component JavaDoc createDetailButton(final DesignDefinition def) {
36         final JButton JavaDoc button = new JButton JavaDoc();
37         button.setAction(new AbstractAction JavaDoc("Edit") {
38             /*
39              * (non-Javadoc)
40              *
41              * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
42              */

43             public void actionPerformed(ActionEvent JavaDoc e) {
44                 Window JavaDoc w = SwingUtilities.windowForComponent(button);
45                 Component JavaDoc form = ViewFactory.create(def).dialog();
46
47                 ValueDialog valueDialog = null;
48                 if (w == null) {
49                     valueDialog = new ValueDialog(form);
50                 }
51                 else {
52                     if (w instanceof Frame JavaDoc) {
53                         valueDialog = new ValueDialog((Frame JavaDoc) w, form);
54                     } else {
55                         valueDialog = new ValueDialog((Dialog JavaDoc) w, form);
56                     }
57                 }
58                 valueDialog.pack();
59                 valueDialog.setVisible(true);
60             }
61         });
62         return button;
63     }
64     
65     /**
66      * Pad the lable of a field.
67      *
68      * @param title The text.
69      * @return Padded text.
70      */

71     public static String JavaDoc padLabel(String JavaDoc title) {
72         StringBuffer JavaDoc paddedTitle = new StringBuffer JavaDoc();
73         paddedTitle.append(title);
74         for (int i = title.length(); i < Looks.LABEL_SIZE; ++i) {
75             paddedTitle.append(' ');
76         }
77         return paddedTitle.toString();
78     }
79     
80     /**
81      * Convert a ComponentAction into a Swing Action.
82      *
83      * @param from The ComponentAction
84      * @return the Swing Action.
85      */

86     public static Action JavaDoc convert(final ComponentAction from) {
87         return new AbstractAction JavaDoc(from.getName()) {
88             public void actionPerformed(ActionEvent JavaDoc e) {
89                 from.perform();
90             }
91         };
92     }
93     
94     /**
95      * Convert an Array of ComponentActions into an Array of
96      * Swing Actions.
97      *
98      * @param froms The ComponentActions
99      * @return The SwingActions
100      */

101     public static Action JavaDoc[] convertAll(ComponentAction[] froms) {
102         Action JavaDoc[] actions = new Action JavaDoc[froms.length];
103         for (int i = 0; i < actions.length; ++i) {
104             actions[i] = convert(froms[i]);
105         }
106         return actions;
107     }
108
109     /**
110      * Utility method to get the parent window for a component. Required for
111      * dialogs.
112      *
113      * @param parentComponent The component who's window were finding.
114      *
115      * @return The wondow.
116      * @throws HeadlessException If the component has no window.
117      */

118     public static Window JavaDoc getWindowForComponent(Component JavaDoc parentComponent)
119     throws HeadlessException JavaDoc {
120         if (parentComponent == null)
121             return null;
122         if (parentComponent instanceof Frame JavaDoc || parentComponent instanceof Dialog JavaDoc)
123             return (Window JavaDoc)parentComponent;
124         if (parentComponent instanceof JPopupMenu JavaDoc) {
125             // a popup doesn't have a parent!!
126
return ViewHelper.getWindowForComponent(
127                     ((JPopupMenu JavaDoc) parentComponent).getInvoker());
128         }
129         return ViewHelper.getWindowForComponent(parentComponent.getParent());
130     }
131 }
Popular Tags