001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 */
028
029package org.jfree.chart.ui;
030
031import java.awt.BorderLayout;
032import java.awt.Color;
033import java.awt.Container;
034import java.awt.Dialog;
035import java.awt.Dimension;
036import java.awt.Font;
037import java.awt.Rectangle;
038import java.awt.Window;
039import javax.swing.JButton;
040import javax.swing.JLabel;
041import javax.swing.JPanel;
042import javax.swing.JScrollPane;
043import javax.swing.JTable;
044import javax.swing.table.TableColumn;
045import javax.swing.table.TableModel;
046
047/**
048 * A collection of utility methods relating to user interfaces.
049 */
050public class UIUtils {
051
052    private UIUtils() {
053    }
054
055    /**
056     * Positions the specified frame in the middle of the screen.
057     *
058     * @param frame  the frame to be centered on the screen.
059     */
060    public static void centerFrameOnScreen(Window frame) {
061        positionFrameOnScreen(frame, 0.5, 0.5);
062    }
063
064    /**
065     * Positions the specified frame at a relative position in the screen, where 50% is considered
066     * to be the center of the screen.
067     *
068     * @param frame  the frame.
069     * @param horizontalPercent  the relative horizontal position of the frame (0.0 to 1.0,
070     *                           where 0.5 is the center of the screen).
071     * @param verticalPercent  the relative vertical position of the frame (0.0 to 1.0, where
072     *                         0.5 is the center of the screen).
073     */
074    public static void positionFrameOnScreen(Window frame, 
075            double horizontalPercent, double verticalPercent) {
076
077        Rectangle s = frame.getGraphicsConfiguration().getBounds();
078        Dimension f = frame.getSize();
079        int w = Math.max(s.width - f.width, 0);
080        int h = Math.max(s.height - f.height, 0);
081        int x = (int) (horizontalPercent * w) + s.x;
082        int y = (int) (verticalPercent * h) + s.y;
083        frame.setBounds(x, y, f.width, f.height);
084
085    }
086
087    /**
088     * Positions the specified frame at a random location on the screen while ensuring that the
089     * entire frame is visible (provided that the frame is smaller than the screen).
090     *
091     * @param frame  the frame.
092     */
093    public static void positionFrameRandomly(Window frame) {
094        positionFrameOnScreen(frame, Math.random(), Math.random());
095    }
096
097    /**
098     * Positions the specified dialog within its parent.
099     *
100     * @param dialog  the dialog to be positioned on the screen.
101     */
102    public static void centerDialogInParent(Dialog dialog) {
103        positionDialogRelativeToParent(dialog, 0.5, 0.5);
104    }
105
106    /**
107     * Positions the specified dialog at a position relative to its parent.
108     *
109     * @param dialog  the dialog to be positioned.
110     * @param horizontalPercent  the relative location.
111     * @param verticalPercent  the relative location.
112     */
113    public static void positionDialogRelativeToParent(Dialog dialog,
114            double horizontalPercent, double verticalPercent) {
115        Container parent = dialog.getParent();
116        if (parent == null) {
117            centerFrameOnScreen(dialog);
118            return;
119        }
120
121        Dimension d = dialog.getSize();
122        Dimension p = parent.getSize();
123
124        int baseX = parent.getX();
125        int baseY = parent.getY();
126
127        int x = baseX + (int) (horizontalPercent * p.width);
128        int y = baseY + (int) (verticalPercent * p.height);
129
130        // make sure the dialog fits completely on the screen...
131        Rectangle s = parent.getGraphicsConfiguration().getBounds();
132        Rectangle r = new Rectangle(x, y, d.width, d.height);
133        dialog.setBounds(r.intersection(s));
134    }
135
136    /**
137     * Creates a panel that contains a table based on the specified table model.
138     *
139     * @param model  the table model to use when constructing the table.
140     *
141     * @return The panel.
142     */
143    public static JPanel createTablePanel(TableModel model) {
144
145        JPanel panel = new JPanel(new BorderLayout());
146        JTable table = new JTable(model);
147        for (int columnIndex = 0; columnIndex < model.getColumnCount(); columnIndex++) {
148            TableColumn column = table.getColumnModel().getColumn(columnIndex);
149            Class c = model.getColumnClass(columnIndex);
150            if (c.equals(Number.class)) {
151                column.setCellRenderer(new NumberCellRenderer());
152            }
153        }
154        panel.add(new JScrollPane(table));
155        return panel;
156
157    }
158
159    /**
160     * Creates a label with a specific font.
161     *
162     * @param text  the text for the label.
163     * @param font  the font.
164     *
165     * @return The label.
166     */
167    public static JLabel createJLabel(String text, Font font) {
168        JLabel result = new JLabel(text);
169        result.setFont(font);
170        return result;
171    }
172
173    /**
174     * Creates a label with a specific font and color.
175     *
176     * @param text  the text for the label.
177     * @param font  the font.
178     * @param color  the color.
179     *
180     * @return The label.
181     */
182    public static JLabel createJLabel(String text, Font font, Color color) {
183        JLabel result = new JLabel(text);
184        result.setFont(font);
185        result.setForeground(color);
186        return result;
187    }
188
189    /**
190     * Creates a {@link JButton}.
191     *
192     * @param label  the label.
193     * @param font  the font.
194     *
195     * @return The button.
196     */
197    public static JButton createJButton(String label, Font font) {
198        JButton result = new JButton(label);
199        result.setFont(font);
200        return result;
201    }
202
203}
204
205
206