import java.awt.*; import java.awt.event.*; import java.util.*; public class ColorControls extends Panel { TextField tfield1, tfield2, tfield3; Scrollbar sb1, sb2, sb3; private int max1, max2, max3; private int curVal1, curVal2, curVal3; private Vector colorListeners = new Vector(); public ColorControls(Object listener, String label1, String label2, String label3) { this(listener, label1, 255, label2, 255, label3, 255); } public ColorControls(Object listener, String label1, int lmax1, String label2, int lmax2, String label3, int lmax3) { max1 = lmax1; max2 = lmax2; max3 = lmax3; curVal1 = 0; curVal2 = 0; curVal3 = 0; setLayout(new GridLayout(3, 3, 10, 10)); tfield1 = new TextField("0", 3); sb1 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, max1); tfield2 = new TextField("0", 3); sb2 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, max2); tfield3 = new TextField("0", 3); sb3 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, max3); add(new Label(label1, Label.RIGHT)); add(tfield1); add(sb1); add(new Label(label2, Label.RIGHT)); add(tfield2); add(sb2); add(new Label(label3, Label.RIGHT)); add(tfield3); add(sb3); // create event handlers ColorTextHandler handler1 = new ColorTextHandler(); ColorFocusHandler handler2 = new ColorFocusHandler(); ColorAdjustmentHandler handler3 = new ColorAdjustmentHandler(); // add event handlers to widgets tfield1.addActionListener(handler1); tfield2.addActionListener(handler1); tfield3.addActionListener(handler1); tfield1.addFocusListener(handler2); tfield2.addFocusListener(handler2); tfield3.addFocusListener(handler2); sb1.addAdjustmentListener(handler3); sb2.addAdjustmentListener(handler3); sb3.addAdjustmentListener(handler3); addColorChangeListener((ColorChangeListener)listener); } public synchronized void addColorChangeListener( ColorChangeListener listener) { if (!colorListeners.contains(listener)) { colorListeners.addElement(listener); } } public synchronized void removeColorChangeListener( ColorChangeListener listener) { if (colorListeners.contains(listener)) { colorListeners.removeElement(listener); } } public void notifyColorChange(int val1, int val2, int val3, int changer) throws ColorControlsException { ColorChangeEvent evt = new ColorChangeEvent(this, val1, val2, val3, changer); Vector v; ColorChangeListener client; if (areValidArgs(val1, val2, val3)) { synchronized (this) { curVal1 = val1; curVal2 = val2; curVal3 = val3; v = (Vector) colorListeners.clone(); for (int i=0; i< v.size(); i++) { client = (ColorChangeListener)v.elementAt(i); client.colorChanged(evt); } } } else { // Throw an exception throw (new ColorControlsException()); } } private boolean areValidArgs(int val1, int val2, int val3) { boolean fValid = (val1 <= max1) && (val2 <= max2) && (val3 <= max3) && (val1 >= 0) && (val2 >= 0) && (val3 >= 0); // System.out.println("Valid Args? " + val1 + " " + val2 + " " + // val3 + " " + max1 + " " + max2 + " " + max3 + " " + fValid); return (fValid); } class ColorTextHandler implements ActionListener { public void actionPerformed(ActionEvent e) { int val1 = Integer.parseInt(tfield1.getText()); int val2 = Integer.parseInt(tfield2.getText()); int val3 = Integer.parseInt(tfield3.getText()); // System.out.println("notifying Color Change(action): " + val1 + // ", " + val2 + ", " + val3); try { notifyColorChange(val1, val2, val3, ColorChangeEvent.TEXTFIELD); } catch (ColorControlsException exc) { System.out.println("Exception!"); tfield1.setText(String.valueOf(curVal1)); tfield2.setText(String.valueOf(curVal2)); tfield3.setText(String.valueOf(curVal3)); } } } class ColorFocusHandler extends FocusAdapter { public void focusLost(FocusEvent e) { int val1 = Integer.parseInt(tfield1.getText()); int val2 = Integer.parseInt(tfield2.getText()); int val3 = Integer.parseInt(tfield3.getText()); // System.out.println("notifying Color Change(focus): " + val1 + // ", " + val2 + ", " + val3); try { notifyColorChange(val1, val2, val3, ColorChangeEvent.TEXTFIELD); } catch (ColorControlsException exc) { System.out.println("Exception!"); tfield1.setText(String.valueOf(curVal1)); tfield2.setText(String.valueOf(curVal2)); tfield3.setText(String.valueOf(curVal3)); } } } class ColorAdjustmentHandler implements AdjustmentListener { public void adjustmentValueChanged(AdjustmentEvent e) { int val1 = sb1.getValue(); int val2 = sb2.getValue(); int val3 = sb3.getValue(); // System.out.println("notifying Color Change(adjust): " + val1 + // ", " + val2 + ", " + val3); try { notifyColorChange(val1, val2, val3, ColorChangeEvent.SCROLLBAR); } catch (ColorControlsException exc) { // This should never happen as the scrollbar component should // never allow us to go out of a valid range. System.out.println("Scrollbar exception"); exc.toString(); } } } }