|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.swing.ColorChoices
public class ColorChoices
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:
getInstance() to get access.
DirtyChecker object
with the instance if you would like the state to be automatically
saved at exit. Or, you can use the attemptSave() method directly to save the state of the object for
the next session.
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;
}
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 |
|---|
protected ColorChoices()
| Method Detail |
|---|
public void attemptSave()
throws Exception
attemptSave in interface DirtySaverarg1 - arg1-describearg2 - arg2-describe
exc - Why thrown
ExceptionisDirty(),
setDirtyChecker(com.ccg.swing.DirtyChecker)public void setDirtyChecker(DirtyChecker dc)
arg1 - arg1-describearg2 - arg2-describe
exc - Why thrownattemptSave(),
isDirty()public boolean isDirty()
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.
attemptSave()public static ColorChoices getInstance()
arg1 - arg1-describearg2 - arg2-describe
exc - Why thrownpublic ColorButton createButton(Color c)
arg1 - arg1-describearg2 - arg2-describe
exc - Why thrownColorButtonpublic Color getMostRecentPick(int i)
arg1 - arg1-describearg2 - arg2-describe
exc - Why thrownpublic void setMostRecentPick(Color c)
public JMenuItem createColorMenuItem(ColorButton cb,
Color c)
arg1 - arg1-describearg2 - arg2-describe
exc - Why thrownpublic void actionPerformed(ActionEvent ae)
actionPerformed in interface ActionListenerarg1 - arg1-describearg2 - arg2-describe
exc - Why thrownpublic void setLabelFormat(MessageFormat val)
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.
val - New MessageFormat value to assign, or specify null to fall
back to the global format.getLabelFormat(),
ColorButton.setGlobalLabelFormat(java.text.MessageFormat)public MessageFormat getLabelFormat()
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.
setLabelFormat(java.text.MessageFormat),
ColorButton.getGlobalLabelFormat()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||