1: //..+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
2: package cherry.color;
3:
4: import java.awt.*;
5: import java.awt.event.*;
6: import java.util.*;
7: import javax.swing.*;
8:
9: // ----------------------------------------
10: public class ColorButton extends JPanel {
11: Map colorMap;
12: private JPanel canvasPane = new JPanel();
13:
14: // ----------------------------------------
15: public ColorButton() {
16: this.init();
17: this.initComponent();
18: }
19:
20: private void init() {
21: this.colorMap = this.new_colorMap();
22: this.colorMap.put("red", Color.red );
23: this.colorMap.put("green", Color.green);
24: this.colorMap.put("blue", Color.blue );
25: this.canvasPane.setPreferredSize(new Dimension(100,100));
26: }
27:
28: private Map new_colorMap() {
29: String[] colorNames = {"red","green","blue"};
30: final java.util.List colorList = new ArrayList();
31: for (String e: colorNames) {
32: colorList.add(e);
33: }
34: Comparator sorter = new Comparator() {
35: public int compare(String s1, String s2) {
36: return colorList.indexOf(s1) - colorList.indexOf(s2);
37: };
38: };
39: return new TreeMap(sorter);
40: }
41:
42: private void initComponent() {
43: JPanel controlPane = new JPanel(new GridLayout(0,1));
44: controlPane.setPreferredSize(new Dimension(150,100));
45:
46: //------------------------------ Command::Client
47: for (String e: colorMap.keySet()) {
48: CommandButton button = new CommandButton(e);
49: button.setCommand(new ColorCommand(this, e));
50: controlPane.add(button);
51: }
52:
53: JSplitPane splitPane = new JSplitPane(
54: JSplitPane.HORIZONTAL_SPLIT,
55: controlPane,
56: this.canvasPane);
57: splitPane.setDividerLocation(100);
58: this.add(splitPane);
59: }
60:
61: //------------------------------ Command::Receiver
62: public void update(String command) {
63: this.canvasPane.setBackground(this.colorMap.get(command));
64: }
65:
66: // ----------------------------------------
67: static void tips() {
68: JFrame frame = new JFrame("ColorButton");
69: frame.setLocationRelativeTo(null);
70: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
71:
72: JPanel panel = new ColorButton();
73: frame.getContentPane().add(panel);
74:
75: frame.pack();
76: frame.setVisible(true);
77: }
78:
79: //------------------------------
80: // private ColorButton() {}
81: static String version = new ColorButton()
82: .getClass().getName()+": #1.0.19";
83: }
84:
85: // ----------------------------------------
86: class CommandButton extends JButton
87: implements CommandHolder, ActionListener {
88:
89: private Command command;
90:
91: //------------------------------
92: public CommandButton(String name) {
93: super(name);
94: this.addActionListener(this);
95: }
96:
97: public void setCommand(Command command) {
98: this.command = command;
99: }
100:
101: public Command getCommand() {
102: return this.command;
103: }
104:
105: //------------------------------ Command::Invoker
106: public void actionPerformed(ActionEvent e) {
107: this.getCommand().execute();
108: }
109: }
110:
111: // ----------------------------------------
112: class ColorCommand implements Command {
113: ColorButton receiver;
114: String name;
115:
116: //------------------------------
117: public ColorCommand(ColorButton receiver, String name) {
118: this.receiver = receiver;
119: this.name = name;
120: }
121:
122: //------------------------------ Command::ConcreteCommand
123: public void execute() {
124: this.receiver.update(this.name);
125: }
126: }
127:
128: // ========================================
129: /*'****************************************'
130: public void actionPerformed(ActionEvent e) {
131: String command = e.getActionCommand();
132: Color color = null;
133: if (command.equals("red") ) color = Color.red;
134: if (command.equals("green")) color = Color.green;
135: if (command.equals("blue") ) color = Color.blue;
136: this.canvasPane.setBackground(color);
137: }
138: public void actionPerformed(ActionEvent e) {
139: String command = e.getActionCommand();
140: this.canvasPane.setBackground(this.colorMap.get(command));
141: }
142: '****************************************'*/