Source for 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.10 2007/05/21 17:13:59 taqua 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;
  69: import java.awt.Color;
  70: import java.awt.Container;
  71: import java.awt.Dialog;
  72: import java.awt.Dimension;
  73: import java.awt.Font;
  74: import java.awt.Toolkit;
  75: import java.awt.Window;
  76: import java.awt.Point;
  77: import java.awt.GraphicsEnvironment;
  78: import java.awt.Rectangle;
  79: import java.lang.reflect.Method;
  80: 
  81: import javax.swing.JButton;
  82: import javax.swing.JLabel;
  83: import javax.swing.JPanel;
  84: import javax.swing.JScrollPane;
  85: import javax.swing.JTable;
  86: import javax.swing.table.TableColumn;
  87: import javax.swing.table.TableModel;
  88: 
  89: /**
  90:  * A collection of utility methods relating to user interfaces.
  91:  *
  92:  * @author David Gilbert
  93:  */
  94: public class RefineryUtilities {
  95: 
  96:     private RefineryUtilities() {
  97:     }
  98: 
  99:     /** Access to logging facilities. */
 100:     // private static final LogContext logger = Log.createContext(RefineryUtilities.class);
 101: 
 102:     /**
 103:      * Computes the center point of the current screen device. If this method is called on JDK 1.4, Xinerama-aware
 104:      * results are returned. (See Sun-Bug-ID 4463949 for details).
 105:      *
 106:      * @return the center point of the current screen.
 107:      */
 108:     public static Point getCenterPoint ()
 109:     {
 110:       final GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
 111:       try
 112:       {
 113:         final Method method = GraphicsEnvironment.class.getMethod("getCenterPoint", (Class[]) null);
 114:         return (Point) method.invoke(localGraphicsEnvironment, (Object[]) null);
 115:       }
 116:       catch(Exception e)
 117:       {
 118:         // ignore ... will fail if this is not a JDK 1.4 ..
 119:       }
 120: 
 121:       final Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
 122:       return new Point (s.width / 2, s.height / 2);
 123:     }
 124: 
 125:   /**
 126:    * Computes the maximum bounds of the current screen device. If this method is called on JDK 1.4, Xinerama-aware
 127:    * results are returned. (See Sun-Bug-ID 4463949 for details).
 128:    *
 129:    * @return the maximum bounds of the current screen.
 130:    */
 131:     public static Rectangle getMaximumWindowBounds ()
 132:     {
 133:       final GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
 134:       try
 135:       {
 136:         final Method method = GraphicsEnvironment.class.getMethod("getMaximumWindowBounds", (Class[]) null);
 137:         return (Rectangle) method.invoke(localGraphicsEnvironment, (Object[]) null);
 138:       }
 139:       catch(Exception e)
 140:       {
 141:         // ignore ... will fail if this is not a JDK 1.4 ..
 142:       }
 143: 
 144:       final Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
 145:       return new Rectangle (0, 0, s.width, s.height);
 146:     }
 147: 
 148:     /**
 149:      * Positions the specified frame in the middle of the screen.
 150:      *
 151:      * @param frame  the frame to be centered on the screen.
 152:      */
 153:     public static void centerFrameOnScreen(final Window frame) {
 154:         positionFrameOnScreen(frame, 0.5, 0.5);
 155:     }
 156: 
 157:     /**
 158:      * Positions the specified frame at a relative position in the screen, where 50% is considered
 159:      * to be the center of the screen.
 160:      *
 161:      * @param frame  the frame.
 162:      * @param horizontalPercent  the relative horizontal position of the frame (0.0 to 1.0,
 163:      *                           where 0.5 is the center of the screen).
 164:      * @param verticalPercent  the relative vertical position of the frame (0.0 to 1.0, where
 165:      *                         0.5 is the center of the screen).
 166:      */
 167:     public static void positionFrameOnScreen(final Window frame,
 168:                                              final double horizontalPercent,
 169:                                              final double verticalPercent) {
 170: 
 171:         final Rectangle s = getMaximumWindowBounds();
 172:         final Dimension f = frame.getSize();
 173:         final int w = Math.max(s.width - f.width, 0);
 174:         final int h = Math.max(s.height - f.height, 0);
 175:         final int x = (int) (horizontalPercent * w) + s.x;
 176:         final int y = (int) (verticalPercent * h) + s.y;
 177:         frame.setBounds(x, y, f.width, f.height);
 178: 
 179:     }
 180: 
 181:     /**
 182:      * Positions the specified frame at a random location on the screen while ensuring that the
 183:      * entire frame is visible (provided that the frame is smaller than the screen).
 184:      *
 185:      * @param frame  the frame.
 186:      */
 187:     public static void positionFrameRandomly(final Window frame) {
 188:         positionFrameOnScreen(frame, Math.random(), Math.random());
 189:     }
 190: 
 191:     /**
 192:      * Positions the specified dialog within its parent.
 193:      *
 194:      * @param dialog  the dialog to be positioned on the screen.
 195:      */
 196:     public static void centerDialogInParent(final Dialog dialog) {
 197:         positionDialogRelativeToParent(dialog, 0.5, 0.5);
 198:     }
 199: 
 200:     /**
 201:      * Positions the specified dialog at a position relative to its parent.
 202:      *
 203:      * @param dialog  the dialog to be positioned.
 204:      * @param horizontalPercent  the relative location.
 205:      * @param verticalPercent  the relative location.
 206:      */
 207:     public static void positionDialogRelativeToParent(final Dialog dialog,
 208:                                                       final double horizontalPercent,
 209:                                                       final double verticalPercent) {
 210:         final Dimension d = dialog.getSize();
 211:         final Container parent = dialog.getParent();
 212:         final Dimension p = parent.getSize();
 213: 
 214:         final int baseX = parent.getX() - d.width;
 215:         final int baseY = parent.getY() - d.height;
 216:         final int w = d.width + p.width;
 217:         final int h = d.height + p.height;
 218:         int x = baseX + (int) (horizontalPercent * w);
 219:         int y = baseY + (int) (verticalPercent * h);
 220: 
 221:         // make sure the dialog fits completely on the screen...
 222:         final Rectangle s = getMaximumWindowBounds();
 223:         x = Math.min(x, (s.width - d.width));
 224:         x = Math.max(x, 0);
 225:         y = Math.min(y, (s.height - d.height));
 226:         y = Math.max(y, 0);
 227: 
 228:         dialog.setBounds(x + s.x, y + s.y, d.width, d.height);
 229: 
 230:     }
 231: 
 232:     /**
 233:      * Creates a panel that contains a table based on the specified table model.
 234:      *
 235:      * @param model  the table model to use when constructing the table.
 236:      *
 237:      * @return The panel.
 238:      */
 239:     public static JPanel createTablePanel(final TableModel model) {
 240: 
 241:         final JPanel panel = new JPanel(new BorderLayout());
 242:         final JTable table = new JTable(model);
 243:         for (int columnIndex = 0; columnIndex < model.getColumnCount(); columnIndex++) {
 244:             final TableColumn column = table.getColumnModel().getColumn(columnIndex);
 245:             final Class c = model.getColumnClass(columnIndex);
 246:             if (c.equals(Number.class)) {
 247:                 column.setCellRenderer(new NumberCellRenderer());
 248:             }
 249:         }
 250:         panel.add(new JScrollPane(table));
 251:         return panel;
 252: 
 253:     }
 254: 
 255:     /**
 256:      * Creates a label with a specific font.
 257:      *
 258:      * @param text  the text for the label.
 259:      * @param font  the font.
 260:      *
 261:      * @return The label.
 262:      */
 263:     public static JLabel createJLabel(final String text, final Font font) {
 264: 
 265:         final JLabel result = new JLabel(text);
 266:         result.setFont(font);
 267:         return result;
 268: 
 269:     }
 270: 
 271:     /**
 272:      * Creates a label with a specific font and color.
 273:      *
 274:      * @param text  the text for the label.
 275:      * @param font  the font.
 276:      * @param color  the color.
 277:      *
 278:      * @return The label.
 279:      */
 280:     public static JLabel createJLabel(final String text, final Font font, final Color color) {
 281: 
 282:         final JLabel result = new JLabel(text);
 283:         result.setFont(font);
 284:         result.setForeground(color);
 285:         return result;
 286: 
 287:     }
 288: 
 289:     /**
 290:      * Creates a {@link JButton}.
 291:      *
 292:      * @param label  the label.
 293:      * @param font  the font.
 294:      *
 295:      * @return The button.
 296:      */
 297:     public static JButton createJButton(final String label, final Font font) {
 298: 
 299:         final JButton result = new JButton(label);
 300:         result.setFont(font);
 301:         return result;
 302: 
 303:     }
 304: 
 305: }
 306: