001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ----------------------
028: * RefineryUtilities.java
029: * ----------------------
030: * (C) Copyright 2000-2005, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): Jon Iles;
034: *
035: * $Id: RefineryUtilities.java,v 1.10 2007/05/21 17:13:59 taqua Exp $
036: *
037: * Changes (from 26-Oct-2001)
038: * --------------------------
039: * 26-Oct-2001 : Changed package to com.jrefinery.ui.*;
040: * 26-Nov-2001 : Changed name to SwingRefinery.java to make it obvious that this is not part of
041: * the Java APIs (DG);
042: * 10-Dec-2001 : Changed name (again) to JRefineryUtilities.java (DG);
043: * 28-Feb-2002 : Moved system properties classes into com.jrefinery.ui.about (DG);
044: * 19-Apr-2002 : Renamed JRefineryUtilities-->RefineryUtilities. Added drawRotatedString()
045: * method (DG);
046: * 21-May-2002 : Changed frame positioning methods to accept Window parameters, as suggested by
047: * Laurence Vanhelsuwe (DG);
048: * 27-May-2002 : Added getPointInRectangle method (DG);
049: * 26-Jun-2002 : Removed unnecessary imports (DG);
050: * 12-Jul-2002 : Added workaround for rotated text (JI);
051: * 14-Oct-2002 : Fixed errors reported by Checkstyle (DG);
052: * 08-May-2003 : Added a new drawRotatedString() method (DG);
053: * 09-May-2003 : Added a drawRotatedShape() method (DG);
054: * 10-Jun-2003 : Updated aligned and rotated string methods (DG);
055: * 29-Oct-2003 : Added workaround for font alignment in PDF output (DG);
056: * 07-Nov-2003 : Added rotateShape() method (DG);
057: * 16-Mar-2004 : Moved rotateShape() method to ShapeUtils.java (DG);
058: * 07-Apr-2004 : Modified text bounds calculation with TextUtilities.getTextBounds() (DG);
059: * 21-May-2004 : Fixed bug 951870 - precision in drawAlignedString() method (DG);
060: * 30-Sep-2004 : Deprecated and moved a number of methods to the TextUtilities class (DG);
061: * 04-Oct-2004 : Renamed ShapeUtils --> ShapeUtilities (DG);
062: * 11-Jan-2005 : Removed deprecated code in preparation for the 1.0.0 release (DG);
063: *
064: */
065:
066: package org.jfree.ui;
067:
068: import java.awt.BorderLayout;
069: import java.awt.Color;
070: import java.awt.Container;
071: import java.awt.Dialog;
072: import java.awt.Dimension;
073: import java.awt.Font;
074: import java.awt.Toolkit;
075: import java.awt.Window;
076: import java.awt.Point;
077: import java.awt.GraphicsEnvironment;
078: import java.awt.Rectangle;
079: import java.lang.reflect.Method;
080:
081: import javax.swing.JButton;
082: import javax.swing.JLabel;
083: import javax.swing.JPanel;
084: import javax.swing.JScrollPane;
085: import javax.swing.JTable;
086: import javax.swing.table.TableColumn;
087: import javax.swing.table.TableModel;
088:
089: /**
090: * A collection of utility methods relating to user interfaces.
091: *
092: * @author David Gilbert
093: */
094: public class RefineryUtilities {
095:
096: private RefineryUtilities() {
097: }
098:
099: /** Access to logging facilities. */
100: // private static final LogContext logger = Log.createContext(RefineryUtilities.class);
101: /**
102: * Computes the center point of the current screen device. If this method is called on JDK 1.4, Xinerama-aware
103: * results are returned. (See Sun-Bug-ID 4463949 for details).
104: *
105: * @return the center point of the current screen.
106: */
107: public static Point getCenterPoint() {
108: final GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment
109: .getLocalGraphicsEnvironment();
110: try {
111: final Method method = GraphicsEnvironment.class.getMethod(
112: "getCenterPoint", (Class[]) null);
113: return (Point) method.invoke(localGraphicsEnvironment,
114: (Object[]) null);
115: } catch (Exception e) {
116: // ignore ... will fail if this is not a JDK 1.4 ..
117: }
118:
119: final Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
120: return new Point(s.width / 2, s.height / 2);
121: }
122:
123: /**
124: * Computes the maximum bounds of the current screen device. If this method is called on JDK 1.4, Xinerama-aware
125: * results are returned. (See Sun-Bug-ID 4463949 for details).
126: *
127: * @return the maximum bounds of the current screen.
128: */
129: public static Rectangle getMaximumWindowBounds() {
130: final GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment
131: .getLocalGraphicsEnvironment();
132: try {
133: final Method method = GraphicsEnvironment.class.getMethod(
134: "getMaximumWindowBounds", (Class[]) null);
135: return (Rectangle) method.invoke(localGraphicsEnvironment,
136: (Object[]) null);
137: } catch (Exception e) {
138: // ignore ... will fail if this is not a JDK 1.4 ..
139: }
140:
141: final Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
142: return new Rectangle(0, 0, s.width, s.height);
143: }
144:
145: /**
146: * Positions the specified frame in the middle of the screen.
147: *
148: * @param frame the frame to be centered on the screen.
149: */
150: public static void centerFrameOnScreen(final Window frame) {
151: positionFrameOnScreen(frame, 0.5, 0.5);
152: }
153:
154: /**
155: * Positions the specified frame at a relative position in the screen, where 50% is considered
156: * to be the center of the screen.
157: *
158: * @param frame the frame.
159: * @param horizontalPercent the relative horizontal position of the frame (0.0 to 1.0,
160: * where 0.5 is the center of the screen).
161: * @param verticalPercent the relative vertical position of the frame (0.0 to 1.0, where
162: * 0.5 is the center of the screen).
163: */
164: public static void positionFrameOnScreen(final Window frame,
165: final double horizontalPercent, final double verticalPercent) {
166:
167: final Rectangle s = getMaximumWindowBounds();
168: final Dimension f = frame.getSize();
169: final int w = Math.max(s.width - f.width, 0);
170: final int h = Math.max(s.height - f.height, 0);
171: final int x = (int) (horizontalPercent * w) + s.x;
172: final int y = (int) (verticalPercent * h) + s.y;
173: frame.setBounds(x, y, f.width, f.height);
174:
175: }
176:
177: /**
178: * Positions the specified frame at a random location on the screen while ensuring that the
179: * entire frame is visible (provided that the frame is smaller than the screen).
180: *
181: * @param frame the frame.
182: */
183: public static void positionFrameRandomly(final Window frame) {
184: positionFrameOnScreen(frame, Math.random(), Math.random());
185: }
186:
187: /**
188: * Positions the specified dialog within its parent.
189: *
190: * @param dialog the dialog to be positioned on the screen.
191: */
192: public static void centerDialogInParent(final Dialog dialog) {
193: positionDialogRelativeToParent(dialog, 0.5, 0.5);
194: }
195:
196: /**
197: * Positions the specified dialog at a position relative to its parent.
198: *
199: * @param dialog the dialog to be positioned.
200: * @param horizontalPercent the relative location.
201: * @param verticalPercent the relative location.
202: */
203: public static void positionDialogRelativeToParent(
204: final Dialog dialog, final double horizontalPercent,
205: final double verticalPercent) {
206: final Dimension d = dialog.getSize();
207: final Container parent = dialog.getParent();
208: final Dimension p = parent.getSize();
209:
210: final int baseX = parent.getX() - d.width;
211: final int baseY = parent.getY() - d.height;
212: final int w = d.width + p.width;
213: final int h = d.height + p.height;
214: int x = baseX + (int) (horizontalPercent * w);
215: int y = baseY + (int) (verticalPercent * h);
216:
217: // make sure the dialog fits completely on the screen...
218: final Rectangle s = getMaximumWindowBounds();
219: x = Math.min(x, (s.width - d.width));
220: x = Math.max(x, 0);
221: y = Math.min(y, (s.height - d.height));
222: y = Math.max(y, 0);
223:
224: dialog.setBounds(x + s.x, y + s.y, d.width, d.height);
225:
226: }
227:
228: /**
229: * Creates a panel that contains a table based on the specified table model.
230: *
231: * @param model the table model to use when constructing the table.
232: *
233: * @return The panel.
234: */
235: public static JPanel createTablePanel(final TableModel model) {
236:
237: final JPanel panel = new JPanel(new BorderLayout());
238: final JTable table = new JTable(model);
239: for (int columnIndex = 0; columnIndex < model.getColumnCount(); columnIndex++) {
240: final TableColumn column = table.getColumnModel()
241: .getColumn(columnIndex);
242: final Class c = model.getColumnClass(columnIndex);
243: if (c.equals(Number.class)) {
244: column.setCellRenderer(new NumberCellRenderer());
245: }
246: }
247: panel.add(new JScrollPane(table));
248: return panel;
249:
250: }
251:
252: /**
253: * Creates a label with a specific font.
254: *
255: * @param text the text for the label.
256: * @param font the font.
257: *
258: * @return The label.
259: */
260: public static JLabel createJLabel(final String text, final Font font) {
261:
262: final JLabel result = new JLabel(text);
263: result.setFont(font);
264: return result;
265:
266: }
267:
268: /**
269: * Creates a label with a specific font and color.
270: *
271: * @param text the text for the label.
272: * @param font the font.
273: * @param color the color.
274: *
275: * @return The label.
276: */
277: public static JLabel createJLabel(final String text,
278: final Font font, final Color color) {
279:
280: final JLabel result = new JLabel(text);
281: result.setFont(font);
282: result.setForeground(color);
283: return result;
284:
285: }
286:
287: /**
288: * Creates a {@link JButton}.
289: *
290: * @param label the label.
291: * @param font the font.
292: *
293: * @return The button.
294: */
295: public static JButton createJButton(final String label,
296: final Font font) {
297:
298: final JButton result = new JButton(label);
299: result.setFont(font);
300: return result;
301:
302: }
303:
304: }
|