KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > ui > Util


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.ui;
31
32 import java.awt.Component JavaDoc;
33 import java.awt.Container JavaDoc;
34 import java.awt.Dimension JavaDoc;
35 import java.awt.Frame JavaDoc;
36 import java.io.File JavaDoc;
37 import java.util.ArrayList JavaDoc;
38
39 import javax.swing.JButton JavaDoc;
40 import javax.swing.JCheckBox JavaDoc;
41 import javax.swing.JComponent JavaDoc;
42 import javax.swing.JFrame JavaDoc;
43 import javax.swing.JLabel JavaDoc;
44 import javax.swing.JOptionPane JavaDoc;
45 import javax.swing.JRadioButton JavaDoc;
46
47 import com.genimen.djeneric.language.Messages;
48
49 public class Util
50 {
51   public static Frame JavaDoc findFrame(Component JavaDoc c)
52   {
53     if (c instanceof Frame JavaDoc) return (Frame JavaDoc) c;
54     Component JavaDoc parent = c.getParent();
55     if (parent != null) return findFrame(parent);
56     return null;
57   }
58
59   public static Frame JavaDoc findActiveFrame()
60   {
61     Frame JavaDoc[] frames = JFrame.getFrames();
62     for (int i = 0; i < frames.length; i++)
63     {
64       Frame JavaDoc frame = frames[i];
65       if (frame.isVisible())
66       {
67         return frame;
68       }
69     }
70     return null;
71   }
72
73   public static void sizeButtons(Container JavaDoc panel)
74   {
75     sizeButtons(panel, true);
76   }
77
78   public static void sizeButtons(Container JavaDoc panel, boolean recurse)
79   {
80     ArrayList JavaDoc lst = new ArrayList JavaDoc();
81     addButtons(panel, lst, recurse);
82     sizeButtons((JButton JavaDoc[]) lst.toArray(new JButton JavaDoc[0]));
83   }
84
85   private static void addButtons(Container JavaDoc panel, ArrayList JavaDoc lst, boolean recurse)
86   {
87     for (int i = 0; i < panel.getComponentCount(); i++)
88     {
89       if (panel.getComponent(i) instanceof JButton JavaDoc) lst.add(panel.getComponent(i));
90       if (recurse && panel.getComponent(i) instanceof Container JavaDoc) addButtons((Container JavaDoc) panel.getComponent(i), lst,
91                                                                             recurse);
92     }
93   }
94
95   public static void sizeLabels(Container JavaDoc panel)
96   {
97     sizeLabels(panel, true);
98   }
99
100   public static void sizeLabels(Container JavaDoc panel, boolean recurse)
101   {
102     ArrayList JavaDoc lst = new ArrayList JavaDoc();
103     addLabels(panel, lst, recurse);
104     sizeLabels((JComponent JavaDoc[]) lst.toArray(new JComponent JavaDoc[0]));
105   }
106
107   private static void addLabels(Container JavaDoc panel, ArrayList JavaDoc lst, boolean recurse)
108   {
109     for (int i = 0; i < panel.getComponentCount(); i++)
110     {
111       if (panel.getComponent(i) instanceof JCheckBox JavaDoc || panel.getComponent(i) instanceof JRadioButton JavaDoc
112           || panel.getComponent(i) instanceof JLabel JavaDoc) lst.add(panel.getComponent(i));
113       if (recurse && panel.getComponent(i) instanceof Container JavaDoc) addLabels((Container JavaDoc) panel.getComponent(i), lst,
114                                                                            recurse);
115     }
116   }
117
118   public static void sizeButtons(JButton JavaDoc[] buttons)
119   {
120     if (buttons.length == 0) return;
121
122     Dimension JavaDoc biggest = buttons[0].getPreferredSize();
123     for (int i = 0; i < buttons.length; i++)
124     {
125       if (buttons[i].getPreferredSize().getWidth() > biggest.getWidth())
126       {
127         biggest = buttons[i].getPreferredSize();
128       }
129     }
130
131     biggest.height = 24;
132
133     for (int i = 0; i < buttons.length; i++)
134     {
135       if (buttons[i].getPreferredSize().width == 24 && buttons[i].getPreferredSize().height == 24) continue;
136       if (buttons[i].getPreferredSize().width == 21 && buttons[i].getPreferredSize().height == 21) continue;
137       buttons[i].setPreferredSize(biggest);
138     }
139   }
140
141   public static void sizeLabels(JComponent JavaDoc[] labels)
142   {
143     if (labels.length == 0) return;
144
145     Dimension JavaDoc biggest = labels[0].getPreferredSize();
146     for (int i = 0; i < labels.length; i++)
147     {
148       if (labels[i].getPreferredSize().getWidth() > biggest.getWidth())
149       {
150         biggest = labels[i].getPreferredSize();
151       }
152     }
153
154     biggest.height = 21;
155
156     for (int i = 0; i < labels.length; i++)
157     {
158       labels[i].setPreferredSize(biggest);
159     }
160   }
161
162   public static boolean isOkToOverwrite(Component JavaDoc c, File JavaDoc file)
163   {
164     int result = JOptionPane.showOptionDialog(findFrame(c), Messages.getString("global.Sure2Overwrite", file
165         .getAbsolutePath()), Messages.getString("global.FileExists"), JOptionPane.DEFAULT_OPTION,
166                                               JOptionPane.INFORMATION_MESSAGE, null, new String JavaDoc[]{
167                                                   Messages.getString("global.Overwrite"),
168                                                   Messages.getString("global.Cancel")}, null);
169     return result == 0;
170   }
171
172   public static Component JavaDoc findParent(Component JavaDoc comp, Class JavaDoc type)
173   {
174     if (type.isAssignableFrom(comp.getClass())) return comp;
175     Component JavaDoc parent = comp.getParent();
176     if (parent != null) return findParent(parent, type);
177     return null;
178   }
179 }
Popular Tags