Source code: de/hunsicker/swing/util/SwingHelper.java
1 /*
2 * Copyright (c) 2001-2002, Marco Hunsicker. All rights reserved.
3 *
4 * This software is distributable under the BSD license. See the terms of the
5 * BSD license in the documentation provided with this software.
6 */
7 package de.hunsicker.swing.util;
8
9 import java.awt.BorderLayout;
10 import java.awt.Component;
11 import java.awt.Container;
12 import java.awt.Frame;
13 import java.awt.GridBagConstraints;
14 import java.awt.Insets;
15 import java.awt.Window;
16 import java.awt.event.ActionEvent;
17 import java.awt.event.ActionListener;
18
19 import javax.swing.AbstractButton;
20 import javax.swing.JButton;
21 import javax.swing.JDialog;
22 import javax.swing.JFileChooser;
23 import javax.swing.JTable;
24 import javax.swing.SwingUtilities;
25
26
27 /**
28 * UI related helper functions.
29 *
30 * @author <a href="http://jalopy.sf.net/contact.html">Marco Hunsicker</a>
31 * @version $Revision: 1.2 $
32 */
33 public final class SwingHelper
34 {
35 //~ Constructors ---------------------------------------------------------------------
36
37 /**
38 * Creates a new SwingHelper object.
39 */
40 private SwingHelper()
41 {
42 }
43
44 //~ Methods --------------------------------------------------------------------------
45
46 /**
47 * Sets the constraints for the given constraints object. Helper function to be able
48 * to reuse a constraints object.
49 *
50 * @param constraints the constraints object to initialize.
51 * @param gridx the initial gridx value.
52 * @param gridy the initial gridy value.
53 * @param gridwidth the initial gridwidth value.
54 * @param gridheight the initial gridheight value.
55 * @param weightx the initial weightx value.
56 * @param weighty the initial weighty value.
57 * @param anchor the initial anchor value.
58 * @param fill the initial fill value.
59 * @param insets the initial insets value.
60 * @param ipadx the initial ipadx value.
61 * @param ipady the initial ipady value.
62 *
63 * @return the initialized constraints object.
64 */
65 public static GridBagConstraints setConstraints(
66 GridBagConstraints constraints,
67 int gridx,
68 int gridy,
69 int gridwidth,
70 int gridheight,
71 double weightx,
72 double weighty,
73 int anchor,
74 int fill,
75 Insets insets,
76 int ipadx,
77 int ipady)
78 {
79 constraints.gridx = gridx;
80 constraints.gridy = gridy;
81 constraints.gridwidth = gridwidth;
82 constraints.gridheight = gridheight;
83 constraints.weightx = weightx;
84 constraints.weighty = weighty;
85 constraints.anchor = anchor;
86 constraints.fill = fill;
87 constraints.insets.top = insets.top;
88 constraints.insets.bottom = insets.bottom;
89 constraints.insets.left = insets.left;
90 constraints.insets.right = insets.right;
91 constraints.ipadx = ipadx;
92 constraints.ipady = ipady;
93
94 return constraints;
95 }
96
97
98 /**
99 * DOCUMENT ME!
100 *
101 * @param item DOCUMENT ME!
102 * @param text DOCUMENT ME!
103 * @param useMnemonic DOCUMENT ME!
104 */
105 public static void setMenuText(
106 AbstractButton item,
107 String text,
108 boolean useMnemonic)
109 {
110 int i = text.indexOf('&');
111
112 if (i < 0)
113 {
114 item.setText(text);
115 }
116 else
117 {
118 item.setText(text.substring(0, i) + text.substring(i + 1));
119
120 if (useMnemonic)
121 {
122 item.setMnemonic(text.charAt(i + 1));
123 }
124 }
125 }
126
127
128 /**
129 * Returns the frame that contains the given component.
130 *
131 * @param component component.
132 *
133 * @return frame that contains the given component or <code>null</code> if there is
134 * no parent frame.
135 */
136 public static Frame getOwnerFrame(Component component)
137 {
138 Window window = SwingUtilities.windowForComponent(component);
139 Frame mother = null;
140
141 if (window != null)
142 {
143 Window owner = window.getOwner();
144
145 if ((owner != null) && owner instanceof Frame)
146 {
147 mother = (Frame) owner;
148 }
149 }
150
151 return mother;
152 }
153
154
155 /**
156 * Returns the visual height of the given table.
157 *
158 * @param table the table.
159 *
160 * @return the table height.
161 */
162 public static int getTableHeight(JTable table)
163 {
164 int result = 0;
165 int rowHeight = 0;
166
167 for (int i = 0, rows = table.getRowCount(); i < rows; i++)
168 {
169 int height = table.getRowHeight(i);
170 result += height;
171
172 if (height > rowHeight)
173 {
174 rowHeight = height;
175 }
176 }
177
178 return result + rowHeight + (table.getRowCount() * table.getRowMargin());
179 }
180
181
182 /**
183 * Creates a new button with the given text.
184 *
185 * @param text DOCUMENT ME!
186 * @param parse DOCUMENT ME!
187 *
188 * @return new button with the given text.
189 *
190 * @since 1.0b9
191 */
192 public static JButton createButton(
193 String text,
194 boolean parse)
195 {
196 JButton button = new JButton();
197
198 if (parse)
199 {
200 setMenuText(button, text, true);
201 }
202 else
203 {
204 button.setText(text);
205 }
206
207 return button;
208 }
209
210
211 /**
212 * Creates a new button with the given text.
213 *
214 * @param text DOCUMENT ME!
215 *
216 * @return new button with the given text.
217 *
218 * @since 1.0b9
219 */
220 public static JButton createButton(String text)
221 {
222 return createButton(text, true);
223 }
224
225
226 /**
227 * Displays the given file chooser. Utility method for avoiding of memory leak in JDK
228 * 1.3 {@link javax.swing.JFileChooser#showDialog}.
229 *
230 * @param chooser the file chooser to display.
231 * @param parent the parent window.
232 * @param approveButtonText the text for the approve button.
233 *
234 * @return the return code of the chooser.
235 */
236 public static final int showJFileChooser(
237 JFileChooser chooser,
238 Component parent,
239 String approveButtonText)
240 {
241 if (approveButtonText != null)
242 {
243 chooser.setApproveButtonText(approveButtonText);
244 chooser.setDialogType(javax.swing.JFileChooser.CUSTOM_DIALOG);
245 }
246
247 Frame frame =
248 (parent instanceof Frame) ? (Frame) parent
249 : (Frame) javax.swing.SwingUtilities
250 .getAncestorOfClass(java.awt.Frame.class, parent);
251 String title = chooser.getDialogTitle();
252
253 if (title == null)
254 {
255 title = chooser.getUI().getDialogTitle(chooser);
256 }
257
258 final JDialog dialog = new JDialog(frame, title, true);
259 dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
260
261 Container contentPane = dialog.getContentPane();
262 contentPane.setLayout(new BorderLayout());
263 contentPane.add(chooser, BorderLayout.CENTER);
264 dialog.pack();
265 dialog.setLocationRelativeTo(parent);
266 chooser.rescanCurrentDirectory();
267
268 final int[] retValue = new int[] { javax.swing.JFileChooser.CANCEL_OPTION };
269
270 ActionListener l =
271 new ActionListener()
272 {
273 public void actionPerformed(ActionEvent ev)
274 {
275 if (ev.getActionCommand() == JFileChooser.APPROVE_SELECTION)
276 {
277 retValue[0] = JFileChooser.APPROVE_OPTION;
278 }
279
280 dialog.setVisible(false);
281 dialog.dispose();
282 }
283 };
284
285 chooser.addActionListener(l);
286 dialog.show();
287
288 return (retValue[0]);
289 }
290 }