001: /*
002: $Id: AbstractParameter.java,v 1.6 2005/02/14 12:06:19 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.api.filters.parameters;
024:
025: import java.awt.Dialog;
026: import java.awt.Dimension;
027: import java.awt.GridBagConstraints;
028: import java.awt.GridBagLayout;
029:
030: import javax.swing.JComponent;
031: import javax.swing.JDialog;
032: import javax.swing.JFrame;
033: import javax.swing.JLabel;
034: import javax.swing.JPanel;
035: import javax.swing.border.TitledBorder;
036:
037: import ejp.presenter.gui.Utils;
038:
039: /**
040: * Abstract super class of all parameters instances.
041: *
042: * @author Sebastien Vauclair
043: * @version <code>$Revision: 1.6 $<br>$Date: 2005/02/14 12:06:19 $</code>
044: */
045: public abstract class AbstractParameter {
046: // ///////////////////////////////////////////////////////////////////////////
047: // CONSTANTS
048: // ///////////////////////////////////////////////////////////////////////////
049:
050: /**
051: * Width (in pixels) of a parameter's label.
052: */
053: public static final int LABEL_WIDTH = 150;
054:
055: // ///////////////////////////////////////////////////////////////////////////
056: // FIELDS
057: // ///////////////////////////////////////////////////////////////////////////
058:
059: /**
060: * Parameter name.
061: */
062: public final String name;
063:
064: /**
065: * Parameter panel (graphical box).
066: */
067: protected final JPanel panel;
068:
069: /**
070: * Current row number in parameter's lines.
071: */
072: protected int y = 0;
073:
074: /**
075: * Dialog currently showing the parameter (<code>null</code> if none).
076: */
077: protected Dialog dialog = null;
078:
079: // ///////////////////////////////////////////////////////////////////////////
080: // CONSTRUCTOR
081: // ///////////////////////////////////////////////////////////////////////////
082:
083: /**
084: * Creates a new parameter instance..
085: *
086: * @param aName
087: * the parameter's unique name, as referenced in XML files.
088: * @param aTitle
089: * the parameter's graphical title (free text value).
090: * @param aToolTipText
091: * a tooltip text explaining the parameter's purpose.
092: */
093: public AbstractParameter(String aName, String aTitle,
094: String aToolTipText) {
095: name = aName;
096:
097: TitledBorder tbTemp = new TitledBorder(aTitle + " [" + aName
098: + "]");
099: tbTemp.setTitleFont(Utils.FONT_11);
100:
101: panel = new JPanel();
102: panel.setDoubleBuffered(true);
103: panel.setLayout(new GridBagLayout());
104: panel.setToolTipText(aToolTipText);
105: panel.setBorder(tbTemp);
106: }
107:
108: // ///////////////////////////////////////////////////////////////////////////
109: // MEMBERS
110: // ///////////////////////////////////////////////////////////////////////////
111:
112: /**
113: * Returns the parameter's graphical customization panel.
114: *
115: * @return the panel.
116: */
117: public JPanel getPanel() {
118: return panel;
119: }
120:
121: /**
122: * Sets the parameter to be read-only, ie. no longer changeable.
123: *
124: * Might be called at most once.
125: */
126: public abstract void setReadOnly();
127:
128: /**
129: * Sets current dialog showing the parameter's customization.
130: *
131: * @param aDialog
132: * an initialized dialog.
133: */
134: public void setDialog(Dialog aDialog) {
135: dialog = aDialog;
136: }
137:
138: /**
139: * Adds a configuration line to the paramter.
140: *
141: * Such a line is made of a title and one or two visual objects used to set
142: * the value of the customization line.
143: *
144: * @param aTitle
145: * the line's meaning.
146: * @param aToolTipText
147: * a free tool-tip text.
148: * @param aComponent1
149: * first component (might be <code>null</code> to skip it).
150: * @param aComponent2
151: * second component (might be <code>null</code> to skip it).
152: */
153: protected final void addLine(String aTitle, String aToolTipText,
154: JComponent aComponent1, JComponent aComponent2) {
155: // description
156: JLabel jlTemp = new JLabel(aTitle);
157: Utils.setCommonProperties(jlTemp);
158: jlTemp.setPreferredSize(new Dimension(LABEL_WIDTH, (int) jlTemp
159: .getPreferredSize().getHeight()));
160: GridBagConstraints gbcTemp = new GridBagConstraints();
161: gbcTemp.anchor = GridBagConstraints.NORTHWEST;
162: gbcTemp.gridx = 0;
163: gbcTemp.gridy = y;
164: gbcTemp.insets = Utils.INSETS_5;
165: panel.add(jlTemp, gbcTemp);
166:
167: // component 1
168: if (aComponent1 != null) {
169: gbcTemp.fill = GridBagConstraints.BOTH;
170: gbcTemp.gridx = 1;
171: gbcTemp.weightx = 1;
172: panel.add(aComponent1, gbcTemp);
173: }
174:
175: // component 2
176: if (aComponent2 != null) {
177: gbcTemp.gridx = 2;
178: panel.add(aComponent2, gbcTemp);
179: }
180:
181: ++y;
182: }
183:
184: /**
185: * Returns current value of the parameter, as any object.
186: *
187: * @return an <code>Object</code> value.
188: */
189: public abstract Object getValue();
190:
191: /**
192: * Returns current value of the parameter, as a text value.
193: *
194: * The returned value will be used for XML storage.
195: *
196: * @return a text value.
197: */
198: public abstract String getValueAsText();
199:
200: /**
201: * Sets current value of the parameter, from an object value.
202: *
203: * This method should handle object values returned by the
204: * <code>getValue()</code> method.
205: *
206: * @param aObject
207: * an <code>Object</code> value.
208: * @exception ClassCastException
209: * if the object is of an unknown type.
210: */
211: public abstract void setValue(Object aObject)
212: throws ClassCastException;
213:
214: /**
215: * Sets current value of the parameter, from a text value.
216: *
217: * This method should parse text values returned by the
218: * <code>getValueAsText()</code> method.
219: *
220: * @param aTextValue
221: * a text value.
222: * @exception IllegalArgumentException
223: * if the text is not parseable.
224: */
225: public abstract void setValueAsText(String aTextValue)
226: throws IllegalArgumentException;
227:
228: // ///////////////////////////////////////////////////////////////////////////
229: // TEST
230: // ///////////////////////////////////////////////////////////////////////////
231:
232: /**
233: * Shows the parameter in a test frame.
234: */
235: public final void showInTestFrame() {
236: JFrame jf = new JFrame("Test frame");
237:
238: JDialog jd = new JDialog(jf, true);
239: setDialog(jd);
240: jd.getContentPane().add(panel);
241: jd.pack();
242: jd.setVisible(true);
243: }
244: }
|