001: /*
002: $Id: ColorChooserDialog.java,v 1.6 2005/02/14 12:06:21 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.BorderLayout;
026: import java.awt.Color;
027: import java.awt.Dialog;
028: import java.awt.FlowLayout;
029: import java.awt.GridBagConstraints;
030: import java.awt.GridBagLayout;
031: import java.awt.event.ActionEvent;
032: import java.awt.event.ActionListener;
033: import java.awt.event.WindowAdapter;
034: import java.awt.event.WindowEvent;
035:
036: import javax.swing.JButton;
037: import javax.swing.JColorChooser;
038: import javax.swing.JDialog;
039: import javax.swing.JPanel;
040:
041: import ejp.presenter.api.util.CustomLogger;
042:
043: /**
044: * Custom color chooser dialog.
045: *
046: * @author Sebastien Vauclair
047: * @version <code>$Revision: 1.6 $<br>$Date: 2005/02/14 12:06:21 $</code>
048: */
049: public class ColorChooserDialog extends JDialog implements
050: ActionListener {
051: // ///////////////////////////////////////////////////////////////////////////
052: // CONSTANTS
053: // ///////////////////////////////////////////////////////////////////////////
054:
055: // ///////////////////////////////////////////////////////////////////////////
056: // COMPONENTS
057: // ///////////////////////////////////////////////////////////////////////////
058:
059: protected final JColorChooser jccChooser;
060:
061: protected final JButton jbOK;
062:
063: protected final JButton jbCancel;
064:
065: // ///////////////////////////////////////////////////////////////////////////
066: // FIELDS
067: // ///////////////////////////////////////////////////////////////////////////
068:
069: protected Color returnValue = null;
070:
071: protected boolean disposed = false;
072:
073: // ///////////////////////////////////////////////////////////////////////////
074: // CONSTRUCTOR
075: // ///////////////////////////////////////////////////////////////////////////
076:
077: public ColorChooserDialog(Dialog aParent, Color aColor) {
078: super (aParent, true /* modal */);
079:
080: // MAIN PANEL /////////////////////////////////////////////////////////////
081:
082: JPanel jpMain = new JPanel();
083: jpMain.setDoubleBuffered(true);
084: jpMain.setLayout(new GridBagLayout());
085:
086: jccChooser = new JColorChooser(aColor);
087: Utils.setCommonProperties(jccChooser);
088: Utils.recursiveSetFont(jccChooser.getAccessibleContext());
089:
090: GridBagConstraints gbcTemp = new GridBagConstraints();
091: gbcTemp.gridx = 0;
092: gbcTemp.gridy = 0;
093: gbcTemp.insets = Utils.INSETS_5;
094: jpMain.add(jccChooser, gbcTemp);
095:
096: getContentPane().add(jpMain, BorderLayout.CENTER);
097:
098: // BUTTONS PANEL //////////////////////////////////////////////////////////
099:
100: JPanel jpButtons = new JPanel();
101: jpButtons.setDoubleBuffered(true);
102: jpButtons.setLayout(new FlowLayout(FlowLayout.RIGHT));
103: getContentPane().add(jpButtons, BorderLayout.SOUTH);
104:
105: // OK BUTTON
106:
107: jbOK = new JButton("OK");
108: Utils.setCommonProperties(jbOK);
109: jbOK.addActionListener(this );
110:
111: JPanel jpOK = new JPanel();
112: jpOK.setDoubleBuffered(true);
113: jpOK.add(jbOK);
114: jpButtons.add(jpOK);
115:
116: // CANCEL BUTTON
117:
118: jbCancel = new JButton("Cancel");
119: Utils.setCommonProperties(jbCancel);
120: jbCancel.addActionListener(this );
121:
122: JPanel jpCancel = new JPanel();
123: jpCancel.setDoubleBuffered(true);
124: jpCancel.add(jbCancel);
125: jpButtons.add(jpCancel);
126:
127: // make both buttons same size
128: Utils.setSameSize(new JButton[] { jbOK, jbCancel });
129:
130: // DIALOG /////////////////////////////////////////////////////////////////
131:
132: setResizable(false);
133: setTitle("Choose new color...");
134: addWindowListener(new WindowAdapter() {
135: public void windowClosing(WindowEvent aEvent) {
136: closeDialog(true /* cancel */);
137: }
138: });
139:
140: pack();
141:
142: Utils.centerDialog(this , aParent);
143: }
144:
145: // ///////////////////////////////////////////////////////////////////////////
146: //
147: // ///////////////////////////////////////////////////////////////////////////
148:
149: /**
150: * Blocks until one of the OK or Cancel buttons is clicked or the window is
151: * closed.
152: *
153: * @return the selected color, or <code>null</code> if the user clicked the
154: * Cancel button or closed the dialog
155: */
156: public Color showDialog() {
157: if (disposed == true)
158: throw new IllegalStateException(
159: "This dialog has been disposed and it cannot be displayed again");
160:
161: setVisible(true);
162: return returnValue;
163: }
164:
165: protected void closeDialog(boolean aCancel) {
166: if (!aCancel)
167: returnValue = jccChooser.getColor();
168:
169: setVisible(false);
170:
171: dispose();
172: disposed = true;
173: }
174:
175: // ///////////////////////////////////////////////////////////////////////////
176: // ActionListener INTERFACE
177: // ///////////////////////////////////////////////////////////////////////////
178:
179: public void actionPerformed(ActionEvent aEvent) {
180: Object src = aEvent.getSource();
181: if (src == jbOK || src == jbCancel) {
182: // OK / CANCEL BUTTONS
183:
184: closeDialog(src == jbCancel);
185: } else
186: CustomLogger.INSTANCE
187: .warning("Unable to handle action event from unknown source");
188: }
189: }
|