1 package org.catacomb.druid.gui.edit;
2
3 import org.catacomb.druid.event.LabelActor;
4 import org.catacomb.druid.swing.DButton;
5 import org.catacomb.druid.swing.DColorChooser;
6 import org.catacomb.druid.swing.DLabel;
7 import org.catacomb.interlish.content.ColorValue;
8 import org.catacomb.interlish.structure.*;
9 import org.catacomb.report.E;
10 import org.catacomb.util.ColorUtil;
11
12
13 import java.awt.Color;
14
15
16 public class DruColorChoice extends DruGCPanel
17 implements LabelActor, Ablable, ColorValueEditor, ValueWatcher {
18 static final long serialVersionUID = 1001;
19
20 DLabel dLabel;
21 DButton dButton;
22
23
24 ColorValue colorValue;
25
26 Color currentColor;
27 String label;
28
29 public LabelActor labelActor;
30
31
32 public DruColorChoice(String lab, String mnm) {
33 super();
34 label = lab;
35 methodName = mnm;
36 setFlowLeft(2, 2);
37 dLabel = new DLabel(label);
38 dButton = new DButton(" ");
39 dButton.setActionCommand("select");
40 addDComponent(dButton);
41 addDComponent(dLabel);
42
43 dButton.setLabelActor(this);
44 dButton.applyRollover();
45 setColorValue(new ColorValue("#b0b000"));
46 currentColor = new Color(colorValue.getIntColor());
47
48 setTooltipTarget(dButton);
49 }
50
51
52
53 public void valueChangedBy(Value pv, Object src) {
54 if (src == this) {
55 setColor(new Color(colorValue.getIntColor()));
56 valueChange(currentColor);
57
58 } else {
59 if (colorValue == pv) {
60 if (colorValue == null) {
61 setColor(Color.red);
62
63 } else {
64 setColor(new Color(colorValue.getIntColor()));
65 }
66 } else {
67 E.error("value changed by called with mismatched value");
68 }
69 }
70 }
71
72
73 public void setColorValue(ColorValue cv) {
74 if (colorValue != null) {
75 colorValue.removeValueWatcher(this);
76 }
77 colorValue = cv;
78 if (colorValue == null) {
79 dButton.setEnabled(false);
80 } else {
81 dButton.setEnabled(true);
82 setColor(new Color(colorValue.getIntColor()));
83
84 colorValue.addValueWatcher(this);
85 }
86 }
87
88
89
90
91 public void labelAction(String s, boolean b) {
92 if (s.equals("select")) {
93
94 Color newColor = DColorChooser.showDialog(null, label, currentColor);
95
96 if (newColor != null) {
97 colorValue.reportableSetColor(newColor.getRGB(), this);
98 }
99
100 } else {
101 E.warning("unhandled action " + s);
102 }
103 }
104
105
106 public void setBg(Color c) {
107 dLabel.setBg(c);
108 super.setBg(c);
109 }
110
111
112 public void postApply() {
113 dButton.setMouseActor(this);
114 }
115
116
117 public void able(boolean b) {
118 dButton.setEnabled(b);
119 dLabel.setEnabled(b);
120 }
121
122
123 public void setColor(Color c) {
124 if (c == null) {
125 currentColor = Color.gray;
126 } else {
127 currentColor = c;
128 }
129 dButton.setBg(currentColor);
130 }
131
132
133
134 public Color getColor() {
135 return currentColor;
136 }
137
138
139 public String getStringValue() {
140 return ColorUtil.serializeColor(currentColor);
141 }
142
143
144 }