KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > ui > RefineryUtilities


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jcommon/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ----------------------
28  * RefineryUtilities.java
29  * ----------------------
30  * (C) Copyright 2000-2005, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): Jon Iles;
34  *
35  * $Id: RefineryUtilities.java,v 1.8 2005/10/18 13:18:34 mungady Exp $
36  *
37  * Changes (from 26-Oct-2001)
38  * --------------------------
39  * 26-Oct-2001 : Changed package to com.jrefinery.ui.*;
40  * 26-Nov-2001 : Changed name to SwingRefinery.java to make it obvious that this is not part of
41  * the Java APIs (DG);
42  * 10-Dec-2001 : Changed name (again) to JRefineryUtilities.java (DG);
43  * 28-Feb-2002 : Moved system properties classes into com.jrefinery.ui.about (DG);
44  * 19-Apr-2002 : Renamed JRefineryUtilities-->RefineryUtilities. Added drawRotatedString()
45  * method (DG);
46  * 21-May-2002 : Changed frame positioning methods to accept Window parameters, as suggested by
47  * Laurence Vanhelsuwe (DG);
48  * 27-May-2002 : Added getPointInRectangle method (DG);
49  * 26-Jun-2002 : Removed unnecessary imports (DG);
50  * 12-Jul-2002 : Added workaround for rotated text (JI);
51  * 14-Oct-2002 : Fixed errors reported by Checkstyle (DG);
52  * 08-May-2003 : Added a new drawRotatedString() method (DG);
53  * 09-May-2003 : Added a drawRotatedShape() method (DG);
54  * 10-Jun-2003 : Updated aligned and rotated string methods (DG);
55  * 29-Oct-2003 : Added workaround for font alignment in PDF output (DG);
56  * 07-Nov-2003 : Added rotateShape() method (DG);
57  * 16-Mar-2004 : Moved rotateShape() method to ShapeUtils.java (DG);
58  * 07-Apr-2004 : Modified text bounds calculation with TextUtilities.getTextBounds() (DG);
59  * 21-May-2004 : Fixed bug 951870 - precision in drawAlignedString() method (DG);
60  * 30-Sep-2004 : Deprecated and moved a number of methods to the TextUtilities class (DG);
61  * 04-Oct-2004 : Renamed ShapeUtils --> ShapeUtilities (DG);
62  * 11-Jan-2005 : Removed deprecated code in preparation for the 1.0.0 release (DG);
63  *
64  */

65
66 package org.jfree.ui;
67
68 import java.awt.BorderLayout JavaDoc;
69 import java.awt.Color JavaDoc;
70 import java.awt.Container JavaDoc;
71 import java.awt.Dialog JavaDoc;
72 import java.awt.Dimension JavaDoc;
73 import java.awt.Font JavaDoc;
74 import java.awt.Toolkit JavaDoc;
75 import java.awt.Window JavaDoc;
76
77 import javax.swing.JButton JavaDoc;
78 import javax.swing.JLabel JavaDoc;
79 import javax.swing.JPanel JavaDoc;
80 import javax.swing.JScrollPane JavaDoc;
81 import javax.swing.JTable JavaDoc;
82 import javax.swing.table.TableColumn JavaDoc;
83 import javax.swing.table.TableModel JavaDoc;
84
85 import org.jfree.util.Log;
86 import org.jfree.util.LogContext;
87
88 /**
89  * A collection of utility methods relating to user interfaces.
90  *
91  * @author David Gilbert
92  */

93 public abstract class RefineryUtilities {
94
95     /** Access to logging facilities. */
96     protected static final LogContext logger = Log.createContext(RefineryUtilities.class);
97
98     /**
99      * Positions the specified frame in the middle of the screen.
100      *
101      * @param frame the frame to be centered on the screen.
102      */

103     public static void centerFrameOnScreen(final Window JavaDoc frame) {
104         positionFrameOnScreen(frame, 0.5, 0.5);
105     }
106
107     /**
108      * Positions the specified frame at a relative position in the screen, where 50% is considered
109      * to be the center of the screen.
110      *
111      * @param frame the frame.
112      * @param horizontalPercent the relative horizontal position of the frame (0.0 to 1.0,
113      * where 0.5 is the center of the screen).
114      * @param verticalPercent the relative vertical position of the frame (0.0 to 1.0, where
115      * 0.5 is the center of the screen).
116      */

117     public static void positionFrameOnScreen(final Window JavaDoc frame,
118                                              final double horizontalPercent,
119                                              final double verticalPercent) {
120
121         final Dimension JavaDoc s = Toolkit.getDefaultToolkit().getScreenSize();
122         final Dimension JavaDoc f = frame.getSize();
123         final int w = Math.max(s.width - f.width, 0);
124         final int h = Math.max(s.height - f.height, 0);
125         final int x = (int) (horizontalPercent * w);
126         final int y = (int) (verticalPercent * h);
127         frame.setBounds(x, y, f.width, f.height);
128
129     }
130
131     /**
132      * Positions the specified frame at a random location on the screen while ensuring that the
133      * entire frame is visible (provided that the frame is smaller than the screen).
134      *
135      * @param frame the frame.
136      */

137     public static void positionFrameRandomly(final Window JavaDoc frame) {
138         positionFrameOnScreen(frame, Math.random(), Math.random());
139     }
140
141     /**
142      * Positions the specified dialog within its parent.
143      *
144      * @param dialog the dialog to be positioned on the screen.
145      */

146     public static void centerDialogInParent(final Dialog JavaDoc dialog) {
147         positionDialogRelativeToParent(dialog, 0.5, 0.5);
148     }
149
150     /**
151      * Positions the specified dialog at a position relative to its parent.
152      *
153      * @param dialog the dialog to be positioned.
154      * @param horizontalPercent the relative location.
155      * @param verticalPercent the relative location.
156      */

157     public static void positionDialogRelativeToParent(final Dialog JavaDoc dialog,
158                                                       final double horizontalPercent,
159                                                       final double verticalPercent) {
160         final Dimension JavaDoc d = dialog.getSize();
161         final Container JavaDoc parent = dialog.getParent();
162         final Dimension JavaDoc p = parent.getSize();
163
164         final int baseX = parent.getX() - d.width;
165         final int baseY = parent.getY() - d.height;
166         final int w = d.width + p.width;
167         final int h = d.height + p.height;
168         int x = baseX + (int) (horizontalPercent * w);
169         int y = baseY + (int) (verticalPercent * h);
170
171         // make sure the dialog fits completely on the screen...
172
final Dimension JavaDoc s = Toolkit.getDefaultToolkit().getScreenSize();
173         x = Math.min(x, (s.width - d.width));
174         x = Math.max(x, 0);
175         y = Math.min(y, (s.height - d.height));
176         y = Math.max(y, 0);
177
178         dialog.setBounds(x, y, d.width, d.height);
179
180     }
181
182     /**
183      * Creates a panel that contains a table based on the specified table model.
184      *
185      * @param model the table model to use when constructing the table.
186      *
187      * @return The panel.
188      */

189     public static JPanel JavaDoc createTablePanel(final TableModel JavaDoc model) {
190
191         final JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc());
192         final JTable JavaDoc table = new JTable JavaDoc(model);
193         for (int columnIndex = 0; columnIndex < model.getColumnCount(); columnIndex++) {
194             final TableColumn JavaDoc column = table.getColumnModel().getColumn(columnIndex);
195             final Class JavaDoc c = model.getColumnClass(columnIndex);
196             if (c.equals(Number JavaDoc.class)) {
197                 column.setCellRenderer(new NumberCellRenderer());
198             }
199         }
200         panel.add(new JScrollPane JavaDoc(table));
201         return panel;
202
203     }
204
205     /**
206      * Creates a label with a specific font.
207      *
208      * @param text the text for the label.
209      * @param font the font.
210      *
211      * @return The label.
212      */

213     public static JLabel JavaDoc createJLabel(final String JavaDoc text, final Font JavaDoc font) {
214
215         final JLabel JavaDoc result = new JLabel JavaDoc(text);
216         result.setFont(font);
217         return result;
218
219     }
220
221     /**
222      * Creates a label with a specific font and color.
223      *
224      * @param text the text for the label.
225      * @param font the font.
226      * @param color the color.
227      *
228      * @return The label.
229      */

230     public static JLabel JavaDoc createJLabel(final String JavaDoc text, final Font JavaDoc font, final Color JavaDoc color) {
231
232         final JLabel JavaDoc result = new JLabel JavaDoc(text);
233         result.setFont(font);
234         result.setForeground(color);
235         return result;
236
237     }
238
239     /**
240      * Creates a {@link JButton}.
241      *
242      * @param label the label.
243      * @param font the font.
244      *
245      * @return The button.
246      */

247     public static JButton JavaDoc createJButton(final String JavaDoc label, final Font JavaDoc font) {
248
249         final JButton JavaDoc result = new JButton JavaDoc(label);
250         result.setFont(font);
251         return result;
252
253     }
254
255 }
256
257
258
Popular Tags