comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/StyleEditorWindow.java @ 1309:a95e82d6bcc1

Refactored the code to create a context menu and a style editor so that it is also available for maps. flys-client/trunk@2943 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 12 Oct 2011 08:53:17 +0000
parents
children 974c6b3700de
comparison
equal deleted inserted replaced
1308:d194bee456d3 1309:a95e82d6bcc1
1 package de.intevation.flys.client.client.ui;
2
3 import com.google.gwt.core.client.GWT;
4 import com.google.gwt.user.client.rpc.AsyncCallback;
5
6 import com.smartgwt.client.widgets.Window;
7 import com.smartgwt.client.widgets.layout.VLayout;
8 import com.smartgwt.client.widgets.layout.HLayout;
9 import com.smartgwt.client.widgets.Button;
10 import com.smartgwt.client.widgets.form.DynamicForm;
11 import com.smartgwt.client.widgets.form.fields.FormItem;
12 import com.smartgwt.client.widgets.form.fields.CheckboxItem;
13 import com.smartgwt.client.widgets.form.fields.SpinnerItem;
14 import com.smartgwt.client.widgets.form.fields.ColorPickerItem;
15 import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
16 import com.smartgwt.client.widgets.form.fields.TextItem;
17
18 import com.smartgwt.client.widgets.events.ClickEvent;
19 import com.smartgwt.client.widgets.events.ClickHandler;
20 import com.smartgwt.client.widgets.form.events.ItemChangedEvent;
21 import com.smartgwt.client.widgets.form.events.ItemChangedHandler;
22 import com.smartgwt.client.types.Alignment;
23
24 import de.intevation.flys.client.shared.model.Collection;
25 import de.intevation.flys.client.shared.model.CollectionItemAttribute;
26 import de.intevation.flys.client.shared.model.Style;
27 import de.intevation.flys.client.shared.model.StyleSetting;
28 import de.intevation.flys.client.shared.model.FacetRecord;
29
30 import de.intevation.flys.client.client.services.CollectionItemAttributeServiceAsync;
31 import de.intevation.flys.client.client.services.CollectionItemAttributeService;
32 import de.intevation.flys.client.client.ui.CollectionView;
33
34 import de.intevation.flys.client.client.FLYSConstants;
35 import de.intevation.flys.client.client.Config;
36
37 /**
38 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
39 */
40 public class StyleEditorWindow
41 extends Window
42 implements ClickHandler
43 {
44 /** The interface that provides i18n messages. */
45 protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
46
47 /** The collection */
48 protected Collection collection;
49
50 /** The parent collection view */
51 protected CollectionView view;
52
53 /** The attributes */
54 protected CollectionItemAttribute attributes;
55
56 /** The selected facet. */
57 protected FacetRecord facet;
58
59 /** Main layout */
60 protected VLayout layout;
61
62 /** The service used to set collection item attributes. */
63 protected CollectionItemAttributeServiceAsync itemAttributeService =
64 GWT.create(CollectionItemAttributeService.class);
65
66 public StyleEditorWindow (
67 Collection collection,
68 CollectionItemAttribute attributes,
69 FacetRecord facet)
70 {
71 this.collection = collection;
72 this.attributes = attributes;
73 this.facet = facet;
74 this.layout = new VLayout();
75
76 init();
77 initPanels();
78 }
79
80
81 protected void init() {
82 setTitle(MSG.properties());
83 setWidth(270);
84 setHeight(200);
85 setCanDragReposition(true);
86 setCanDragResize(true);
87 setKeepInParentRect(true);
88
89 layout.setWidth100();
90 layout.setHeight100();
91
92 }
93
94
95 protected void initPanels() {
96 HLayout buttons = new HLayout();
97 Button accept = new Button(MSG.label_ok());
98 Button cancel = new Button(MSG.label_cancel());
99 cancel.addClickHandler(this);
100 accept.addClickHandler(new ClickHandler() {
101 public void onClick(ClickEvent e) {
102 saveStyle();
103 }
104 });
105
106 buttons.addMember(accept);
107 buttons.addMember(cancel);
108 buttons.setAlign(Alignment.RIGHT);
109
110 layout.addMember(createPropertyGrid());;
111 layout.addMember(buttons);
112 addItem(layout);
113 }
114
115
116 public void setCollectionView (CollectionView view) {
117 this.view = view;
118 setParentElement(this.view.getParentElement());
119 }
120
121
122 public void onClick(ClickEvent event) {
123 this.hide();
124 }
125
126
127 protected VLayout createPropertyGrid() {
128 VLayout properties = new VLayout();
129
130 Style s = attributes.getStyle(facet.getTheme().getFacet());
131
132 TextItem name = new TextItem("name", "Name");
133 name.setValue(facet.getName());
134 name.setTitleStyle("color:#000; width:120px");
135 name.setTitleAlign(Alignment.LEFT);
136 name.setDisabled(true);
137 name.setShowDisabled(false);
138 DynamicForm f = new DynamicForm();
139 f.setFields(name);
140 properties.addMember(f);
141
142 for (int i = 0; i < s.getNumSettings(); i ++) {
143 final StyleSetting set = s.getSetting(i);
144 DynamicForm property = createPropertyUI(
145 set.getDisplayName(),
146 set.getName(),
147 set.getType(),
148 set.getDefaultValue());
149 properties.addMember(property);
150 }
151 return properties;
152 }
153
154 protected DynamicForm createPropertyUI(
155 String dname,
156 String name,
157 String type,
158 String value)
159 {
160 DynamicForm df = new DynamicForm();
161
162 FormItem f;
163 if(type.equals("int")) {
164 SpinnerItem s = new SpinnerItem(name, dname);
165 s.setMin(1);
166 s.setMax(10);
167 s.setValue(value);
168 f = s;
169 }
170 else if (type.equals("boolean")) {
171 CheckboxItem c = new CheckboxItem(name, dname);
172 if(value.equals("true")) {
173 c.setValue(true);
174 }
175 else {
176 c.setValue(false);
177 }
178 c.setLabelAsTitle(true);
179 f = c;
180 }
181 else if (type.equals("Color")) {
182 ColorPickerItem c = new ColorPickerItem(name, dname);
183 c.setValue(rgbToHtml(value));
184 f = c;
185 }
186 else if (type.equals("Dash")) {
187 f = new ComboBoxItem(name, dname);
188 f.setValue(value);
189 }
190 else {
191 f = new FormItem();
192 }
193 f.setTitleStyle("color:#000; width:120px");
194 f.setTitleAlign(Alignment.LEFT);
195 df.setFields(f);
196 df.addItemChangedHandler(new ItemChangedHandler() {
197 public void onItemChanged(ItemChangedEvent e) {
198 String name = e.getItem().getName();
199 String newValue = e.getNewValue().toString();
200 GWT.log("changed: " + name);
201 setNewValue(name, newValue);
202 }
203 });
204
205 return df;
206 }
207
208 protected String rgbToHtml(String rgb) {
209 String[] parts = rgb.split(",");
210 int values[] = new int[parts.length];
211 for (int i = 0; i < parts.length; i++) {
212 parts[i] = parts[i].trim();
213 try {
214 values[i] = Integer.parseInt(parts[i]);
215 }
216 catch(NumberFormatException nfe) {
217 return "#000000";
218 }
219 }
220 String hex = "#";
221 for (int i = 0; i < values.length; i++) {
222 if (values[i] < 16) {
223 hex += "0";
224 }
225 hex += Integer.toHexString(values[i]);
226 }
227 return hex;
228 }
229
230 protected String htmlToRgb(String html) {
231 if (!html.startsWith("#")) {
232 return "0, 0, 0";
233 }
234
235 int r = Integer.valueOf(html.substring(1, 3), 16);
236 int g = Integer.valueOf(html.substring(3, 5), 16);
237 int b = Integer.valueOf(html.substring(5, 7), 16);
238
239 return r + ", " + g + ", " + b;
240 }
241
242 protected void saveStyle () {
243 GWT.log("StyleEditorWindow.saveStyle()");
244 Config config = Config.getInstance();
245 String url = config.getServerUrl();
246 String locale = config.getLocale();
247
248 itemAttributeService.setCollectionItemAttribute(
249 this.collection,
250 attributes.getArtifact(),
251 url,
252 locale,
253 attributes,
254 new AsyncCallback<Void>() {
255 public void onFailure (Throwable caught) {
256 GWT.log("Could not set Collection item attributes.");
257 }
258 public void onSuccess(Void v) {
259 GWT.log("Successfully saved collection item attributes.");
260 }
261 });
262
263
264 this.hide();
265 }
266
267 protected final void setNewValue(String name, String value) {
268 Style s = attributes.getStyle(facet.getTheme().getFacet());
269 StyleSetting set = s.getSetting(name);
270 if(name.equals("linecolor")) {
271 value = htmlToRgb(value);
272 }
273 set.setDefaultValue(value);
274 }
275 }
276 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org