1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.vectomatic.client.rep.controller;
19
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26
27 import org.vectomatic.client.rep.RepApplication;
28 import org.vectomatic.client.rep.command.SetAttributeCommand;
29 import org.vectomatic.client.rep.events.IShapeSelectionListener;
30 import org.vectomatic.client.rep.view.IStyleMenu;
31 import org.vectomatic.client.rep.view.PlainColorMenu;
32 import org.vectomatic.client.rep.view.StyleWell;
33 import org.vectomatic.common.model.Attribute;
34 import org.vectomatic.common.model.FloatAttributeValue;
35 import org.vectomatic.common.model.Shape;
36 import org.vectomatic.common.model.geometry.ShapeGroup;
37 import org.vectomatic.common.model.style.Color;
38 import org.vectomatic.common.model.style.IStyle;
39 import org.vectomatic.common.model.style.NoneStyle;
40
41 import com.google.gwt.user.client.ui.ChangeListener;
42 import com.google.gwt.user.client.ui.HorizontalPanel;
43 import com.google.gwt.user.client.ui.Label;
44 import com.google.gwt.user.client.ui.ListBox;
45 import com.google.gwt.user.client.ui.Widget;
46
47
48
49
50
51 public class StyleController extends ControllerBase implements IShapeSelectionListener, ChangeListener {
52 private Attribute _styleAttribute;
53 private Attribute _opacityAttribute;
54 private ListBox _styleBox;
55 private StyleWell _styleWell;
56 private Label _styleLabel;
57 private HorizontalPanel _row;
58 private IStyle _defaultStyle;
59 private FloatAttributeValue _defaultOpacity;
60
61 private Set<Integer> _styleKinds;
62 private Set<IStyle> _styleValues;
63 private Set<FloatAttributeValue> _opacities;
64 private Map<Integer, IStyleMenu> _styleKindToMenu;
65
66 public StyleController(RepApplication app, String label, Attribute styleAttribute, Attribute opacityAttribute, IStyle defaultStyle, FloatAttributeValue defaultOpacity) {
67 super(app);
68 _styleKinds = new HashSet<Integer>();
69 _styleValues = new HashSet<IStyle>();
70 _opacities = new HashSet<FloatAttributeValue>();
71 _styleKindToMenu = new HashMap<Integer, IStyleMenu>();
72
73 _app.getSelection().addShapeSelectionListener(this);
74 _styleAttribute = styleAttribute;
75 _opacityAttribute = opacityAttribute;
76 _defaultStyle = defaultStyle;
77 _defaultOpacity = defaultOpacity;
78
79 PlainColorMenu plainColorMenu = new PlainColorMenu(app, this);
80 _styleKindToMenu.put(Color.BLACK.getKind(), plainColorMenu);
81 _styleLabel = new Label(label);
82 _styleWell = new StyleWell();
83 _styleWell.setWidth("30px");
84 _styleWell.setHeight("8px");
85 _styleBox = new ListBox();
86 _styleBox.addItem(_app.getConstants().plainItem());
87
88
89 _styleBox.addItem(_app.getConstants().noneItem());
90 _styleBox.addChangeListener(this);
91 _row = new HorizontalPanel();
92 _row.add(_styleLabel);
93 _row.add(_styleBox);
94 _row.add(_styleWell);
95
96 IStyleMenu menu = _styleKindToMenu.get(_defaultStyle.getKind());
97 _styleWell.setStyle(_defaultStyle, menu);
98 if (menu != null) {
99 menu.setOpacity(_defaultOpacity);
100 }
101 }
102
103 public Widget getWidget() {
104 return _row;
105 }
106
107 public void selectionChanged(ShapeSelection selection) {
108 List<Shape> shapes = new ArrayList<Shape>(selection.getSelectedShapes());
109 _styleKinds.clear();
110 _styleValues.clear();
111 _opacities.clear();
112
113 IStyle styleValue = null;
114 Integer styleKind = null;
115 FloatAttributeValue opacity = null;
116 for (int i = 0; i < shapes.size(); i++) {
117 Shape shape = shapes.get(i);
118 styleValue = (IStyle)shape.getAttribute(_styleAttribute);
119 if ((styleValue == null) && (shape instanceof ShapeGroup)) {
120 shapes.addAll(((ShapeGroup)shape).getShapes());
121 continue;
122 }
123 styleKind = styleValue.getKind();
124 opacity = (FloatAttributeValue)shape.getAttribute(_opacityAttribute);
125 _styleKinds.add(styleKind);
126 _styleValues.add(styleValue);
127 _opacities.add(opacity);
128 }
129
130 if (_styleValues.size() == 0) {
131 _styleBox.setSelectedIndex(_defaultStyle.getKind().intValue());
132 IStyleMenu menu = _styleKindToMenu.get(_defaultStyle.getKind());
133 _styleWell.setStyle(_defaultStyle, menu);
134 if (menu != null) {
135 menu.setOpacity(_defaultOpacity);
136 }
137 } else if (_styleValues.size() == 1) {
138 _styleBox.setSelectedIndex(styleKind.intValue());
139 IStyleMenu menu = _styleKindToMenu.get(styleKind);
140 _styleWell.setStyle(styleValue, menu);
141 if (menu != null) {
142 menu.setOpacity(opacity);
143 }
144 } else {
145 IStyleMenu menu = null;
146 if (_styleKinds.size() == 1) {
147 _styleBox.setSelectedIndex(styleKind.intValue());
148 menu = _styleKindToMenu.get(styleKind);
149 } else {
150 _styleBox.setSelectedIndex(-1);
151 }
152 _styleWell.setStyle(NoneStyle.NONE, menu);
153 }
154 }
155
156
157
158
159 public void onChange(Widget sender) {
160 RepApplication.app.debugPrint("style kind change");
161 IStyleMenu menu = _styleKindToMenu.get(new Integer(_styleBox.getSelectedIndex()));
162 IStyle style = (menu != null) ? menu.getSelectedStyle() : NoneStyle.NONE;
163 _styleWell.setStyle(style, menu);
164
165 if (_app.getSelection().hasAttributeChanged(_styleAttribute, style)) {
166 SetAttributeCommand setAttributeCommand = new SetAttributeCommand(_app, _styleAttribute, style);
167 setAttributeCommand.execute();
168 _app.getHistory().addCommand(setAttributeCommand);
169 }
170 if (_app.getSelection().getSelectedShapes().size() == 0) {
171 _defaultStyle = style;
172 }
173 }
174
175
176
177
178
179 public void setOpacity(FloatAttributeValue opacity) {
180 RepApplication.app.debugPrint("opacity change");
181
182 if (_app.getSelection().hasAttributeChanged(_opacityAttribute, opacity)) {
183 SetAttributeCommand setAttributeCommand = new SetAttributeCommand(_app, _opacityAttribute, opacity);
184 setAttributeCommand.execute();
185 _app.getHistory().addCommand(setAttributeCommand);
186 }
187 if (_app.getSelection().getSelectedShapes().size() == 0) {
188 _defaultOpacity = opacity;
189 }
190 }
191
192
193
194
195 public void setStyle(IStyle style) {
196 RepApplication.app.debugPrint("style value change");
197 IStyleMenu menu = _styleKindToMenu.get(style.getClass().getName());
198 _styleWell.setStyle(style, menu);
199
200 if (_app.getSelection().hasAttributeChanged(_styleAttribute, style)) {
201 SetAttributeCommand setAttributeCommand = new SetAttributeCommand(_app, _styleAttribute, style);
202 setAttributeCommand.execute();
203 _app.getHistory().addCommand(setAttributeCommand);
204 }
205 if (_app.getSelection().getSelectedShapes().size() == 0) {
206 _defaultStyle = style;
207 }
208 }
209
210
211
212
213
214
215
216 public IStyle getStyle() {
217 return _defaultStyle;
218 }
219
220
221
222
223
224
225 public FloatAttributeValue getOpacity() {
226 return _defaultOpacity;
227 }
228
229 public Attribute getStyleAttribute() {
230 return _styleAttribute;
231 }
232
233 }