com.ccg.swing
Class ColorChoices

java.lang.Object
  extended by com.ccg.swing.ColorChoices
All Implemented Interfaces:
DirtySaver, ActionListener, Serializable, EventListener

public class ColorChoices
extends Object
implements ActionListener, DirtySaver, Serializable

A object which generates ColorButton objects.

This class is used to create ColorButton objects. The idea is that all ColorButton objects share a common set of quick choices. Here are some of the features:

Here is a quick sample application which demonstrates how to use this class. When you run it, notice how the popup menu is shared by both buttons created.

import com.ccg.swing.*;

import javax.swing.*;
import java.awt.*;
import java.text.*;

public class Foo extends JFrame {

  public Foo(String[] args) {
    super();
    JPanel pan = new JPanel();
    pan.setLayout(new java.awt.GridLayout(0,1));

                                // let user specify new global color
                                // format as first argument (optional)
                                // default is "{0}-{1}-{2}"
    if (args.length > 0) try {
      ColorButton.setGlobalLabelFormat(new MessageFormat(args[0]));
    } catch (Exception e) { }

    _CC = ColorChoices.getInstance();

    ColorButton cby = _CC.createButton(java.awt.Color.yellow);
                                // if second argument specified, treat
                                // as specific color format for button
    if (args.length > 1) try {
      cby.setLabelFormat(new MessageFormat(args[1]));
    } catch (Exception e) { }
    pan.add(cby);

    ColorButton cbb = _CC.createButton(java.awt.Color.blue);
    pan.add(cbb);


    setContentPane(pan);

                                // trap exit to save color picker state
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent we) {
        if (_CC.isDirty()) try {
          _CC.attemptSave();
        } catch (Exception e) {
          e.printStackTrace();
        }
        System.exit(0);
      }
    });

    pack();
    show();
  }

  public static void main(String[] args) {
    new Foo(args);
  }

  private ColorChoices _CC;
}
 

Since:
1.0
Version:
$Revision: 1.4 $
Author:
$Author: pkb $
See Also:
ColorButton, Serialized Form

Nested Class Summary
(package private)  class ColorChoices.ColorPopupMenu
           
 
Constructor Summary
protected ColorChoices()
          Initializes object to...
 
Method Summary
 void actionPerformed(ActionEvent ae)
          One line summary...
 void attemptSave()
          One line summary...
 ColorButton createButton(Color c)
          One line summary...
 JMenuItem createColorMenuItem(ColorButton cb, Color c)
          One line summary...
static ColorChoices getInstance()
          One line summary...
 MessageFormat getLabelFormat()
          Get the format to use for labeling the menu items.
 Color getMostRecentPick(int i)
          One line summary...
 boolean isDirty()
          Has user modified selection choices such that they should be saved?
 void setDirtyChecker(DirtyChecker dc)
          One line summary...
 void setLabelFormat(MessageFormat val)
          Set the format to use for labeling the menu items.
 void setMostRecentPick(Color c)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ColorChoices

protected ColorChoices()
Initializes object to... It performs the following tasks:

Since:
1.0 #see [class][#meth]|Text
Method Detail

attemptSave

public void attemptSave()
                 throws Exception
One line summary... Describe...

Specified by:
attemptSave in interface DirtySaver
Parameters:
arg1 - arg1-describe
arg2 - arg2-describe
Throws:
exc - Why thrown
Exception
Since:
1.0
See Also:
isDirty(), setDirtyChecker(com.ccg.swing.DirtyChecker)

setDirtyChecker

public void setDirtyChecker(DirtyChecker dc)
One line summary... Describe...

Parameters:
arg1 - arg1-describe
arg2 - arg2-describe
Throws:
exc - Why thrown
Since:
1.0
See Also:
attemptSave(), isDirty()

isDirty

public boolean isDirty()
Has user modified selection choices such that they should be saved?

This method indicates the object has modified its internal state such that it would be a good idea to invoke the attemptSave() method prior to leaving your application. You only need to worry about doing this manually if you don't assign a DirtyChecker to the object.

Returns:
true if object has been modified since last saved.
Since:
1.0
See Also:
attemptSave()

getInstance

public static ColorChoices getInstance()
One line summary... Describe...

Parameters:
arg1 - arg1-describe
arg2 - arg2-describe
Returns:
ret-description
Throws:
exc - Why thrown
Since:
1.0 #see [class][#meth]|Text

createButton

public ColorButton createButton(Color c)
One line summary... Describe...

Parameters:
arg1 - arg1-describe
arg2 - arg2-describe
Returns:
ret-description
Throws:
exc - Why thrown
Since:
1.0
See Also:
ColorButton

getMostRecentPick

public Color getMostRecentPick(int i)
One line summary... Describe...

Parameters:
arg1 - arg1-describe
arg2 - arg2-describe
Returns:
ret-description
Throws:
exc - Why thrown
Since:
1.0 #see [class][#meth]|Text

setMostRecentPick

public void setMostRecentPick(Color c)

createColorMenuItem

public JMenuItem createColorMenuItem(ColorButton cb,
                                     Color c)
One line summary... Describe...

Parameters:
arg1 - arg1-describe
arg2 - arg2-describe
Returns:
ret-description
Throws:
exc - Why thrown
Since:
1.0 #see [class][#meth]|Text

actionPerformed

public void actionPerformed(ActionEvent ae)
One line summary... Describe...

Specified by:
actionPerformed in interface ActionListener
Parameters:
arg1 - arg1-describe
arg2 - arg2-describe
Throws:
exc - Why thrown
Since:
1.0 #see [class][#meth]|Text

setLabelFormat

public void setLabelFormat(MessageFormat val)
Set the format to use for labeling the menu items.

This method allows one to specify a specific MessageFormat to use for creating labels for the popup-menu. If you specify null or an invalid format, then the global label format will be used. The default format (if you never specify one) is:

 "{0}-{1}-{2}"
 

The "{0}", "{1}" and "{2}" values will be subsituted with the value of the red, green and blue values of the color (each which will range from 0 to 255. If you specify a invalid format, then the format specified will be ignored.

Parameters:
val - New MessageFormat value to assign, or specify null to fall back to the global format.
See Also:
getLabelFormat(), ColorButton.setGlobalLabelFormat(java.text.MessageFormat)

getLabelFormat

public MessageFormat getLabelFormat()
Get the format to use for labeling the menu items.

Note, if a specific label format hasn't been defined for the button, then the global format (from the ColorButton class will be returned - this method will never return null.

Returns:
Current MessageFormat value assigned - or global format.
See Also:
setLabelFormat(java.text.MessageFormat), ColorButton.getGlobalLabelFormat()


Copyright 1998-1998-2006 null. All Rights Reserved.