001: /*
002: $Id: Utils.java,v 1.6 2005/02/14 12:06:20 vauclair Exp $
003:
004: Copyright (C) 2002-2005 Sebastien Vauclair
005:
006: This file is part of Extensible Java Profiler.
007:
008: Extensible Java Profiler is free software; you can redistribute it and/or
009: modify it under the terms of the GNU General Public License as published by
010: the Free Software Foundation; either version 2 of the License, or
011: (at your option) any later version.
012:
013: Extensible Java Profiler is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU General Public License for more details.
017:
018: You should have received a copy of the GNU General Public License
019: along with Extensible Java Profiler; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: */
022:
023: package ejp.presenter.gui;
024:
025: import java.awt.Dialog;
026: import java.awt.Dimension;
027: import java.awt.Font;
028: import java.awt.GridBagConstraints;
029: import java.awt.Insets;
030: import java.awt.Point;
031: import java.awt.Window;
032: import java.awt.event.ActionEvent;
033: import java.awt.event.KeyEvent;
034:
035: import javax.accessibility.AccessibleContext;
036: import javax.swing.AbstractAction;
037: import javax.swing.JButton;
038: import javax.swing.JComponent;
039: import javax.swing.JDialog;
040: import javax.swing.JLabel;
041: import javax.swing.JPanel;
042: import javax.swing.JTextField;
043: import javax.swing.KeyStroke;
044:
045: /**
046: * Various GUI utilities methods.
047: *
048: * @author Sebastien Vauclair
049: * @version <code>$Revision: 1.6 $<br>$Date: 2005/02/14 12:06:20 $</code>
050: */
051: public abstract class Utils {
052: public static final Font FONT_11 = new Font("Default", 0, 11);
053:
054: public static final Insets INSETS_5 = new Insets(5, 5, 5, 5);
055:
056: public static void setCommonProperties(JComponent aComponent) {
057: aComponent.setDoubleBuffered(true);
058: aComponent.setFont(FONT_11);
059: }
060:
061: public static void recursiveSetFont(AccessibleContext aContext) {
062: aContext.getAccessibleComponent().setFont(FONT_11);
063: int nb = aContext.getAccessibleChildrenCount();
064: for (int i = 0; i < nb; i++)
065: recursiveSetFont(aContext.getAccessibleChild(i)
066: .getAccessibleContext());
067: }
068:
069: public static void centerDialog(Dialog aDialog, Window aParent) {
070: Point parentLoc = aParent.getLocation();
071: Dimension parentDim = aParent.getSize();
072: int x = (int) (parentLoc.getX() + (parentDim.getWidth() - aDialog
073: .getWidth()) / 2);
074: int y = (int) (parentLoc.getY() + (parentDim.getHeight() - aDialog
075: .getHeight()) / 2);
076: aDialog.setLocation((x < 0 ? 0 : x), (y < 0 ? 0 : y));
077: }
078:
079: public static void setSameSize(JButton[] aButtons) {
080: int maxX = Integer.MIN_VALUE;
081: int maxY = Integer.MIN_VALUE;
082: for (int i = 0; i < aButtons.length; i++) {
083: Dimension d = aButtons[i].getPreferredSize();
084: int x = (int) d.getWidth();
085: if (x > maxX)
086: maxX = x;
087: int y = (int) d.getHeight();
088: if (y > maxY)
089: maxY = y;
090: }
091: Dimension max = new Dimension(maxX, maxY);
092: for (int i = 0; i < aButtons.length; i++)
093: aButtons[i].setPreferredSize(max);
094: }
095:
096: /**
097: * @param aPanel
098: * a panel with using a <code>GridBagLayout</code>
099: */
100: protected static JTextField addTextField(JPanel aPanel,
101: String aDescription, int aColumns, boolean aReadOnly,
102: int aGridY, String aToolTip) {
103: // description
104: JLabel jlTemp = new JLabel(aDescription);
105: setCommonProperties(jlTemp);
106: GridBagConstraints gbcTemp = new GridBagConstraints();
107: gbcTemp.anchor = GridBagConstraints.NORTHWEST;
108: gbcTemp.gridx = 0;
109: gbcTemp.gridy = aGridY;
110: gbcTemp.insets = INSETS_5;
111: aPanel.add(jlTemp, gbcTemp);
112:
113: // value
114: JTextField result = new JTextField(aColumns);
115: setCommonProperties(result);
116: result.setEditable(!aReadOnly);
117: result.setToolTipText(aToolTip);
118: gbcTemp.gridx = 1;
119: gbcTemp.fill = GridBagConstraints.BOTH;
120: gbcTemp.weightx = 1;
121: gbcTemp.weighty = 1;
122: aPanel.add(result, gbcTemp);
123:
124: return result;
125: }
126:
127: // ///////////////////////////////////////////////////////////////////////////
128: // SET A COMPONENT'S TEXT WHILE KEEPING ITS PREFERRED SIZE
129: // ///////////////////////////////////////////////////////////////////////////
130:
131: /**
132: * Sets a button's text while keeping its preferred size
133: */
134: protected static void setButtonText(JButton aButton, String aText) {
135: Dimension size = aButton.getPreferredSize();
136: aButton.setText(aText);
137: aButton.setPreferredSize(size);
138: }
139:
140: /**
141: * Sets a text field's text while keeping its preferred size
142: *
143: * This method is <b>very </b> important since if the text is set directly and
144: * it exceeds text field's current size, the whole window will collapse.
145: */
146: public static void setTextFieldText(JTextField aTextField,
147: String aText, String aDefaultText) {
148: Dimension size = aTextField.getPreferredSize();
149:
150: if (aText == null || aText.length() == 0)
151: aTextField.setText(aDefaultText);
152: else
153: aTextField.setText(aText);
154: aTextField.setCaretPosition(0); // ensure beginning of text is readable
155:
156: aTextField.setPreferredSize(size);
157: }
158:
159: public static void setTextFieldText(JTextField aTextField,
160: String aText) {
161: setTextFieldText(aTextField, aText, null);
162: }
163:
164: /**
165: * Registers the ESCAPE key to close dialog.
166: */
167: public static void registerEscapeKey(JDialog dialog_,
168: final JButton cancelButton_) {
169: dialog_.getRootPane().getActionMap().put("close",
170: new AbstractAction() {
171: public void actionPerformed(ActionEvent e) {
172: cancelButton_.doClick();
173: }
174: });
175: dialog_.getRootPane().getInputMap().put(
176: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");
177: }
178: }
|