/*
* $Id: ColorSelector.java,v 1.1.1.1 2001/02/07 15:24:11 rtfm Exp $
*
* (c)1999 IoS Gesellschaft fr innovative Softwareentwicklung mbH
* http://www.IoS-Online.de mailto:info@IoS-Online.de
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
package de.ios.framework.gui;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.math.*;
import de.ios.framework.basic.*;
/**
* A Panel to display a Color-Selector.
*
* @see ColorCanvas
* @see BrightnessCanvas
*/
public class ColorSelector extends Panel implements ItemSelectable {
ColorCanvas cc = new ColorCanvas();
BrightnessCanvas bc = new BrightnessCanvas();
DecimalField hVal = new DecimalField(3);
DecimalField sVal = new DecimalField(3);
DecimalField bVal = new DecimalField(3);
NumberField redVal = new NumberField( 3 );
NumberField blueVal = new NumberField( 3 );
NumberField greenVal = new NumberField( 3 );
boolean aborted = false;
/**
* Constructs a new Color-Selector-Instance.
* No aditonal initalisation is needed.
*/
public ColorSelector() {
setLayout( new GridBagLayout() );
Panel inputP = new Panel();
inputP.setLayout( new GridLayout( 6,2 ) );
inputP.add( "", new Label( "Hue" ) ); inputP.add( "", hVal );
inputP.add( "", new Label( "Sat" ) ); inputP.add( "", sVal );
inputP.add( "", new Label( "Brt" ) ); inputP.add( "", bVal );
inputP.add( "", new Label( "Red" ) ); inputP.add( "", redVal );
inputP.add( "", new Label( "Green" ) ); inputP.add( "", greenVal );
inputP.add( "", new Label( "Blue" ) ); inputP.add( "", blueVal );
ActionListener alHSB = new ActionListener() {
public void actionPerformed( ActionEvent a ) {
Number h = hVal.getValue();
Number s = sVal.getValue();
Number b = bVal.getValue();
if (h != null && s != null && b != null) {
Color col = Color.getHSBColor( h.floatValue(), s.floatValue(), b.floatValue() );
setColor( col );
fireItemEvent();
}
}
};
ActionListener alRGB = new ActionListener() {
public void actionPerformed( ActionEvent a ) {
Number r = redVal.getValue();
Number g = greenVal.getValue();
Number b = blueVal.getValue();
if (r != null && g != null && b != null) {
Color col = new Color( r.intValue(), g.intValue(), b.intValue() );
setColor( col );
fireItemEvent();
}
}
};
redVal .addActionListener( alRGB );
greenVal.addActionListener( alRGB );
blueVal .addActionListener( alRGB );
hVal.addActionListener( alHSB );
sVal.addActionListener( alHSB );
bVal.addActionListener( alHSB );
ItemListener il = new ItemListener() {
public void itemStateChanged( ItemEvent e ) {
Color col = bc.getSelection();
setColor( col );
fireItemEvent();
}
};
bc.registerTo( cc );
bc.addItemListener( il );
GridBagConstraints gc = new GridBagConstraints();
gc.anchor = gc.NORTHWEST;
gc.gridx = 0;
gc.gridy = 0;
gc.gridwidth =1;
gc.gridheight=1;
gc.weightx = 1.0;
gc.weighty = 1.0;
gc.fill = gc.BOTH;
add( cc, gc );
gc.weightx = 0.0;
gc.gridx = 1;
add( inputP, gc );
gc.gridx = 2;
add( bc, gc );
}
/**
* Get the actual selected color.
*/
public Color getColor( ) {
return (aborted) ? null : bc.getSelection();
}
/**
* Set the actual selected color.
*/
public void setColor( Color col ) {
redVal .setValue( BigInteger.valueOf( col.getRed () ) );
greenVal.setValue( BigInteger.valueOf( col.getGreen() ) );
blueVal .setValue( BigInteger.valueOf( col.getBlue () ) );
float hsb[] = col.RGBtoHSB( col.getRed(), col.getGreen(), col.getBlue(), null );
hVal.setValue( new BigDecimal( hsb[0] ) );
sVal.setValue( new BigDecimal( hsb[1] ) );
bVal.setValue( new BigDecimal( hsb[2] ) );
cc.setSelection ( col );
bc.setBaseColor ( col );
bc.setBrightness( hsb[2] );
}
ItemListener listeners = null;
/**
* Implements ItemSelectable. Returns always only one object of type Color.
*/
public Object[] getSelectedObjects() {
return new Object[] { getColor() };
}
/**
* Add a listener to watch all changes in the selection.
*/
public void addItemListener( ItemListener il ) {
listeners = AWTEventMulticaster.add( listeners, il );
}
/**
* Removes an item-listener.
*/
public void removeItemListener( ItemListener il ) {
listeners = AWTEventMulticaster.remove( listeners, il );
}
/**
* Fires an item-event to all registered listeners.
*/
void fireItemEvent() {
if (listeners != null)
listeners.itemStateChanged( new ItemEvent( this, ItemEvent.ITEM_STATE_CHANGED,
getColor(),
ItemEvent.SELECTED ) );
}
/**
* Static method to select a color within a dialog.
* @param comp Component on with the dialog should be based. The top-level frame of this
* Component is used to create the dialog.
* @param title The title of the dialog. For default set this null.
* @param ok The text of the "OK"-button. For default set this null.
* @param cancel The text of the "CANCEL"-button. For default set this null.
* @param defaultCol The initial selected Color. Can be null.
* @return The selected color or null if the dialog was aborted.
*/
public static Color selectColor( Component comp,
String title, String ok, String cancel, Color defaultCol ) {
if (title == null) title = "Color-Selector";
if (ok == null) ok = "OK";
if (cancel == null) cancel = "Cancel";
while (!(comp instanceof Frame))
comp = comp.getParent();
Frame frm = (Frame)comp;
final Button okB = new Button( ok );
final Button cancelB = new Button( cancel );
final Dialog d = new Dialog( frm, title, true );
final ColorSelector cs = new ColorSelector();
d.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
cs.aborted = true;
d.setVisible( false );
}
} );
ActionListener al = new ActionListener() {
public void actionPerformed( ActionEvent e ) {
cs.aborted = (e.getSource() != okB);
d.setVisible( false );
}
};
okB.addActionListener( al );
cancelB.addActionListener( al );
d.setLayout( new BorderLayout( ) );
Panel buttons = new Panel();
buttons.setLayout( new FlowLayout( FlowLayout.CENTER ) );
buttons.add( "", okB );
buttons.add( "", cancelB );
if (defaultCol != null)
cs.setColor( defaultCol );
d.add( BorderLayout.CENTER, cs );
d.add( BorderLayout.SOUTH, buttons );
d.pack();
d.setVisible( true );
d.removeAll();
if (ViewTools.DISPOSE_FRAME)
d.dispose();
return cs.getColor();
}
/**
* Main for testing.
*/
public static void main( String args[] ) {
System.out.println("Selected Color "+selectColor( new Frame(), null, null, null, null ) );
}
}
/*
* $Log: ColorSelector.java,v $
* Revision 1.1.1.1 2001/02/07 15:24:11 rtfm
* initial
*
* Revision 1.2 1999/08/24 12:46:43 bw
* added static methode for dialog-bases selection
*
* Revision 1.1 1999/08/24 11:01:10 bw
* Simple Panel for displaying and selecting Colors from HSB-Model.
*
*/