KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > ui > UILib


1 package prefuse.util.ui;
2
3 import java.awt.Color JavaDoc;
4 import java.awt.Component JavaDoc;
5 import java.awt.ComponentOrientation JavaDoc;
6 import java.awt.Container JavaDoc;
7 import java.awt.Font JavaDoc;
8 import java.awt.event.InputEvent JavaDoc;
9
10 import javax.swing.Box JavaDoc;
11 import javax.swing.BoxLayout JavaDoc;
12 import javax.swing.JComponent JavaDoc;
13 import javax.swing.UIManager JavaDoc;
14
15 /**
16  * Library routines for user interface tasks.
17  *
18  * @author <a HREF="http://jheer.org">jeffrey heer</a>
19  */

20 public class UILib {
21
22 // private static Image s_appIcon;
23

24     /**
25      * Not instantiable.
26      */

27     private UILib() {
28         // prevent instantiation
29
}
30     
31 // public static synchronized Image getApplicationIcon() {
32
// if ( s_appIcon == null ) {
33
// try {
34
// s_appIcon = new ImageIcon(
35
// UILib.class.getResource("icon.gif")).getImage();
36
// } catch ( Exception e ) {
37
// e.printStackTrace();
38
// }
39
// }
40
// return s_appIcon;
41
// }
42

43     /**
44      * Indicates if a given mouse button is being pressed.
45      * @param e the InputEvent to check
46      * @param button the mouse button to look for
47      * @return true if the button is being pressed, false otherwise
48      * @see prefuse.controls.Control
49      */

50     public static boolean isButtonPressed(InputEvent JavaDoc e, int button) {
51         return (e.getModifiers() & button) == button;
52     }
53
54     /**
55      * Set the look and feel of Java Swing user interface components to match
56      * that of the platform (Windows, Mac, Linux, etc) on which it is
57      * currently running.
58      */

59     public static final void setPlatformLookAndFeel() {
60         try {
61             String JavaDoc laf = UIManager.getSystemLookAndFeelClassName();
62             UIManager.setLookAndFeel(laf);
63         } catch ( Exception JavaDoc e ) {}
64     }
65
66     /**
67      * Convenience method for creating a Box user interface widget container.
68      * @param c an array of components to include in the box
69      * @param horiz indicated is the box should be horizontal (true) or
70      * vertical (false)
71      * @param margin the margins, in pixels, to use on the sides of the box
72      * @param spacing the minimum spacing, in pixels, to use between
73      * components
74      * @return a new Box instance with the given properties.
75      * @see javax.swing.Box
76      */

77     public static Box JavaDoc getBox(Component JavaDoc[] c, boolean horiz,
78             int margin, int spacing)
79     {
80         return getBox(c, horiz, margin, margin, spacing);
81     }
82     
83     /**
84      * Convenience method for creating a Box user interface widget container.
85      * @param c an array of components to include in the box
86      * @param horiz indicated is the box should be horizontal (true) or
87      * vertical (false)
88      * @param margin1 the margin, in pixels, for the left or top side
89      * @param margin2 the margin, in pixels, for the right or bottom side
90      * @param spacing the minimum spacing, in pixels, to use between
91      * components
92      * @return a new Box instance with the given properties.
93      * @see javax.swing.Box
94      */

95     public static Box JavaDoc getBox(Component JavaDoc[] c, boolean horiz,
96             int margin1, int margin2, int spacing)
97     {
98         Box JavaDoc b = new Box JavaDoc(horiz ? BoxLayout.X_AXIS : BoxLayout.Y_AXIS);
99         addStrut(b, horiz, margin1);
100         for ( int i=0; i<c.length; ++i ) {
101             if ( i > 0 ) {
102                 addStrut(b, horiz, spacing);
103                 addGlue(b, horiz);
104             }
105             b.add(c[i]);
106         }
107         addStrut(b, horiz, margin2);
108         return b;
109     }
110     
111     /**
112      * Add a strut, or rigid spacing, to a UI component
113      * @param b the component to add the strut to, should be either a Box or a
114      * Container using a BoxLayout.
115      * @param horiz indicates if the strust should horizontal (true) or vertical
116      * (false)
117      * @param size the length, in pixels, of the strut
118      */

119     public static void addStrut(JComponent JavaDoc b, boolean horiz, int size) {
120         if ( size < 1 ) return;
121         b.add(horiz ? Box.createHorizontalStrut(size)
122                     : Box.createVerticalStrut(size) );
123     }
124     
125     /**
126      * Add a glue, or variable spacing, to a UI component
127      * @param b the component to add the glue to, should be either a Box or a
128      * Container using a BoxLayout.
129      * @param horiz indicates if the glue should horizontal (true) or vertical
130      * (false)
131      */

132     public static void addGlue(JComponent JavaDoc b, boolean horiz) {
133         b.add(horiz ? Box.createHorizontalGlue()
134                     : Box.createVerticalGlue());
135     }
136     
137     /**
138      * Add a strut, or rigid spacing, to a UI component
139      * @param b the component to add the strut to, should be either a Box or a
140      * Container using a BoxLayout.
141      * @param layout the desired layout orientation of the strut. One of
142      * {@link javax.swing.BoxLayout#X_AXIS},
143      * {@link javax.swing.BoxLayout#Y_AXIS},
144      * {@link javax.swing.BoxLayout#LINE_AXIS}, or
145      * {@link javax.swing.BoxLayout#PAGE_AXIS}.
146      * @param size the length, in pixels, of the strut
147      */

148     public static void addStrut(JComponent JavaDoc b, int layout, int size) {
149         if ( size < 1 ) return;
150         b.add( getAxis(b, layout) == BoxLayout.X_AXIS
151                 ? Box.createHorizontalStrut(size)
152                 : Box.createVerticalStrut(size) );
153     }
154     
155     /**
156      * Add a glue, or variable spacing, to a UI component
157      * @param b the component to add the glue to, should be either a Box or a
158      * Container using a BoxLayout.
159      * @param layout the desired layout orientation of the glue. One of
160      * {@link javax.swing.BoxLayout#X_AXIS},
161      * {@link javax.swing.BoxLayout#Y_AXIS},
162      * {@link javax.swing.BoxLayout#LINE_AXIS}, or
163      * {@link javax.swing.BoxLayout#PAGE_AXIS}.
164      */

165     public static void addGlue(JComponent JavaDoc b, int layout) {
166         b.add( getAxis(b, layout) == BoxLayout.X_AXIS
167                 ? Box.createHorizontalGlue()
168                 : Box.createVerticalGlue());
169     }
170     
171     /**
172      * Resolve the axis type of a component, given a layout orientation
173      * @param c a Swing Component, should be either a Box or a Container
174      * using a BoxLayout.
175      * @param layout the layout orientation of the component. One of
176      * {@link javax.swing.BoxLayout#X_AXIS},
177      * {@link javax.swing.BoxLayout#Y_AXIS},
178      * {@link javax.swing.BoxLayout#LINE_AXIS}, or
179      * {@link javax.swing.BoxLayout#PAGE_AXIS}.
180      * @return one of {@link javax.swing.BoxLayout#X_AXIS}, or
181      * {@link javax.swing.BoxLayout#Y_AXIS},
182      */

183     public static int getAxis(JComponent JavaDoc c, int layout) {
184         ComponentOrientation JavaDoc o = c.getComponentOrientation();
185         switch ( layout ) {
186         case BoxLayout.LINE_AXIS:
187             return o.isHorizontal() ? BoxLayout.X_AXIS : BoxLayout.Y_AXIS;
188         case BoxLayout.PAGE_AXIS:
189             return o.isHorizontal() ? BoxLayout.Y_AXIS : BoxLayout.X_AXIS;
190         default:
191             return layout;
192         }
193     }
194     
195     /**
196      * Sets the foreground and background color for a component and all
197      * components contained within it.
198      * @param c the parent component of the component subtree to set
199      * @param back the background color to set
200      * @param fore the foreground color to set
201      */

202     public static void setColor(Component JavaDoc c, Color JavaDoc back, Color JavaDoc fore) {
203         c.setBackground(back);
204         c.setForeground(fore);
205         if ( c instanceof Container JavaDoc ) {
206             Container JavaDoc con = (Container JavaDoc)c;
207             for ( int i=0; i<con.getComponentCount(); ++i )
208                 setColor(con.getComponent(i), back, fore);
209         }
210     }
211     
212     /**
213      * Sets the font for a component and all
214      * components contained within it.
215      * @param c the parent component of the component subtree to set
216      * @param font the font to set
217      */

218     public static void setFont(Component JavaDoc c, Font JavaDoc font) {
219         c.setFont(font);
220         if ( c instanceof Container JavaDoc ) {
221             Container JavaDoc con = (Container JavaDoc)c;
222             for ( int i=0; i<con.getComponentCount(); ++i )
223                 setFont(con.getComponent(i), font);
224         }
225     }
226     
227 } // end of class UILib
228
Popular Tags