Displaying and editing the complex value types for attributes.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / clientcontroller / view / AttributeEditingSupport.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package oic.simulator.clientcontroller.view;
18
19 import java.util.List;
20
21 import oic.simulator.clientcontroller.Activator;
22 import oic.simulator.clientcontroller.manager.ResourceManager;
23 import oic.simulator.clientcontroller.remoteresource.AttributeElement;
24 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
25 import oic.simulator.clientcontroller.utils.AttributeValueBuilder;
26 import oic.simulator.clientcontroller.utils.Utility;
27 import oic.simulator.clientcontroller.view.dialogs.PostRequestDialog;
28
29 import org.eclipse.jface.dialogs.TitleAreaDialog;
30 import org.eclipse.jface.viewers.CellEditor;
31 import org.eclipse.jface.viewers.CheckboxCellEditor;
32 import org.eclipse.jface.viewers.ComboBoxCellEditor;
33 import org.eclipse.jface.viewers.EditingSupport;
34 import org.eclipse.jface.viewers.IStructuredSelection;
35 import org.eclipse.jface.viewers.TreeViewer;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.custom.CCombo;
38 import org.eclipse.swt.events.ModifyEvent;
39 import org.eclipse.swt.events.ModifyListener;
40 import org.eclipse.swt.widgets.MessageBox;
41 import org.eclipse.swt.widgets.Tree;
42 import org.eclipse.swt.widgets.TreeItem;
43 import org.oic.simulator.AttributeValue;
44 import org.oic.simulator.AttributeValue.TypeInfo;
45 import org.oic.simulator.AttributeValue.ValueType;
46 import org.oic.simulator.InvalidArgsException;
47 import org.oic.simulator.SimulatorResourceAttribute;
48
49 /**
50  * This class provides editing support to the resources attributes table in the
51  * attributes view.
52  */
53 public class AttributeEditingSupport {
54
55     private AttributeValueEditor attValueEditor;
56     private PostRequestEditor    postReqEditor;
57
58     public AttributeValueEditor createAttributeValueEditor(TreeViewer viewer,
59             TitleAreaDialog dialog) {
60         attValueEditor = new AttributeValueEditor(viewer, dialog);
61         return attValueEditor;
62     }
63
64     public PostRequestEditor createAutomationEditor(TreeViewer viewer) {
65         postReqEditor = new PostRequestEditor(viewer);
66         return postReqEditor;
67     }
68
69     class AttributeValueEditor extends EditingSupport {
70
71         private final TreeViewer viewer;
72         private CCombo           comboBox;
73         private TitleAreaDialog  dialog;
74
75         public AttributeValueEditor(TreeViewer viewer, TitleAreaDialog dialog) {
76             super(viewer);
77             this.viewer = viewer;
78             this.dialog = dialog;
79         }
80
81         @Override
82         protected boolean canEdit(Object arg0) {
83             return true;
84         }
85
86         @Override
87         protected CellEditor getCellEditor(final Object element) {
88             ResourceManager resourceManager = Activator.getDefault()
89                     .getResourceManager();
90
91             RemoteResource res = resourceManager
92                     .getCurrentResourceInSelection();
93             if (null == res) {
94                 return null;
95             }
96
97             final SimulatorResourceAttribute attribute;
98             if (!(element instanceof AttributeElement)) {
99                 return null;
100             }
101
102             final AttributeElement attributeElement = ((AttributeElement) element);
103             attribute = attributeElement.getSimulatorResourceAttribute();
104             if (null == attribute) {
105                 return null;
106             }
107
108             final AttributeValue val = attribute.value();
109             if (null == val) {
110                 return null;
111             }
112
113             final TypeInfo type = val.typeInfo();
114             if (type.mBaseType == ValueType.RESOURCEMODEL) {
115                 return null;
116             }
117
118             String values[] = null;
119             List<String> valueSet = resourceManager
120                     .getAllValuesOfAttribute(attribute);
121             values = convertListToStringArray(valueSet);
122
123             ComboBoxCellEditor comboEditor;
124             comboEditor = new ComboBoxCellEditor(viewer.getTree(), values);
125             comboBox = (CCombo) comboEditor.getControl();
126             comboBox.addModifyListener(new ModifyListener() {
127
128                 @Override
129                 public void modifyText(ModifyEvent event) {
130
131                     String newValue = comboBox.getText();
132
133                     if (null != newValue && !newValue.isEmpty()) {
134                         attributeElement.setPostState(true);
135                     } else {
136                         attributeElement.setPostState(false);
137                     }
138
139                     /*
140                      * if (type.mType == ValueType.ARRAY) { return; }
141                      */
142                     /*
143                      * String oldValue = String.valueOf(Utility
144                      * .getAttributeValueAsString(val)); String newValue =
145                      * comboBox.getText();
146                      * 
147                      * if (null != newValue && !newValue.isEmpty()) {
148                      * attributeElement.setPostState(true); } else {
149                      * attributeElement.setPostState(false); }
150                      * 
151                      * if (!oldValue.equals(newValue)) { // Get the
152                      * AttriuteValue from the string AttributeValue value =
153                      * AttributeValueBuilder.build( newValue, type.mBaseType);
154                      * TypeInfo resTypeInfo = value.typeInfo(); if (null ==
155                      * value || type.mDepth != resTypeInfo.mDepth || type.mType
156                      * != resTypeInfo.mType || type.mBaseType !=
157                      * resTypeInfo.mBaseType) { MessageBox dialog = new
158                      * MessageBox(viewer.getTree() .getShell(), SWT.ICON_ERROR |
159                      * SWT.OK); dialog.setText("Invalid Value");
160                      * dialog.setMessage("Given value is invalid");
161                      * dialog.open(); } else { updateAttributeValue(attribute,
162                      * value); } }
163                      */
164                     if (dialog instanceof PostRequestDialog) {
165                         viewer.update(attributeElement, null);
166                         // comboBox.setVisible(false);
167                     }
168                 }
169             });
170             return comboEditor;
171         }
172
173         @Override
174         protected Object getValue(Object element) {
175             int indexOfItem = 0;
176             SimulatorResourceAttribute att = null;
177
178             if (element instanceof AttributeElement) {
179                 att = ((AttributeElement) element)
180                         .getSimulatorResourceAttribute();
181             }
182
183             if (att == null) {
184                 return 0;
185             }
186
187             String valueString = Utility.getAttributeValueAsString(att.value());
188             List<String> valueSet = Activator.getDefault().getResourceManager()
189                     .getAllValuesOfAttribute(att);
190             if (null != valueSet) {
191                 indexOfItem = valueSet.indexOf(valueString);
192             }
193             if (indexOfItem == -1) {
194                 indexOfItem = 0;
195             }
196             return indexOfItem;
197         }
198
199         @Override
200         protected void setValue(Object element, Object value) {
201             SimulatorResourceAttribute att = null;
202             if (element instanceof AttributeElement) {
203                 att = ((AttributeElement) element)
204                         .getSimulatorResourceAttribute();
205             }
206
207             if (att == null) {
208                 return;
209             }
210
211             AttributeValue val = att.value();
212             if (null == val) {
213                 return;
214             }
215
216             TypeInfo type = val.typeInfo();
217             /*
218              * if (type.mType == ValueType.ARRAY) { int index; try { index =
219              * Integer.parseInt(String.valueOf(value)); } catch
220              * (NumberFormatException nfe) { index = -1; } if (index == -1) {
221              * String oldValue = String.valueOf(Utility
222              * .getAttributeValueAsString(val)); String newValue =
223              * comboBox.getText(); if (!oldValue.equals(newValue)) { // Get the
224              * AttriuteValue from the string AttributeValue attValue =
225              * AttributeValueBuilder.build( newValue, type.mBaseType); TypeInfo
226              * resTypeInfo = attValue.typeInfo(); if (null == attValue ||
227              * type.mDepth != resTypeInfo.mDepth || type.mType !=
228              * resTypeInfo.mType || type.mBaseType != resTypeInfo.mBaseType) {
229              * MessageBox dialog = new MessageBox(viewer.getTree() .getShell(),
230              * SWT.ICON_ERROR | SWT.OK); dialog.setText("Invalid Value");
231              * dialog.setMessage("Given value is invalid"); dialog.open(); }
232              * else { updateAttributeValue(att, attValue); } } } }
233              */
234
235             String oldValue = String.valueOf(Utility
236                     .getAttributeValueAsString(val));
237             String newValue = comboBox.getText();
238             if (!oldValue.equals(newValue)) {
239                 // Get the AttriuteValue from the string
240                 AttributeValue attValue = AttributeValueBuilder.build(newValue,
241                         type.mBaseType);
242                 TypeInfo resTypeInfo = attValue.typeInfo();
243                 if (null == attValue || type.mDepth != resTypeInfo.mDepth
244                         || type.mType != resTypeInfo.mType
245                         || type.mBaseType != resTypeInfo.mBaseType) {
246                     MessageBox dialog = new MessageBox(viewer.getTree()
247                             .getShell(), SWT.ICON_ERROR | SWT.OK);
248                     dialog.setText("Invalid Value");
249                     dialog.setMessage("Given value is invalid");
250                     dialog.open();
251                 } else {
252                     updateAttributeValue(att, attValue);
253                 }
254             }
255
256             viewer.update(element, null);
257         }
258
259         public String[] convertListToStringArray(List<String> values) {
260             String[] strArr;
261             if (null != values && values.size() > 0) {
262                 strArr = values.toArray(new String[1]);
263             } else {
264                 strArr = new String[1];
265             }
266             return strArr;
267         }
268
269         public void updateAttributeValue(SimulatorResourceAttribute att,
270                 AttributeValue value) {
271             att.setValue(value);
272
273             IStructuredSelection selection = (IStructuredSelection) viewer
274                     .getSelection();
275             if (null == selection) {
276                 return;
277             }
278
279             Object obj = selection.getFirstElement();
280             if (null == obj) {
281                 return;
282             }
283
284             Tree t = viewer.getTree();
285             TreeItem item = t.getSelection()[0];
286             if (null == item) {
287                 return;
288             }
289
290             TreeItem parent = item.getParentItem();
291             if (null != parent) {
292                 while (parent.getParentItem() != null) {
293                     parent = parent.getParentItem();
294                 }
295                 Object data = parent.getData();
296                 ((AttributeElement) data).setPostState(true);
297             }
298
299             if (item.getData() instanceof AttributeElement) {
300                 AttributeElement attributeElement = (AttributeElement) item
301                         .getData();
302                 attributeElement.getSimulatorResourceAttribute()
303                         .setValue(value);
304
305                 parent = item.getParentItem();
306                 if (null != parent) {
307                     Object data = parent.getData();
308                     try {
309                         ((AttributeElement) data).deepSetChildValue(att);
310                     } catch (InvalidArgsException e) {
311                         e.printStackTrace();
312                     }
313                 }
314             }
315         }
316
317         public AttributeValue getResultantValue(AttributeValue newValue) {
318             AttributeValue val = null;
319             IStructuredSelection selection = (IStructuredSelection) viewer
320                     .getSelection();
321             if (null == selection) {
322                 return null;
323             }
324
325             Object obj = selection.getFirstElement();
326             if (null == obj) {
327                 return null;
328             }
329
330             Tree t = viewer.getTree();
331             TreeItem item = t.getSelection()[0];
332             if (null == item) {
333                 return null;
334             }
335
336             TreeItem parent = item.getParentItem();
337             if (null == parent) {
338                 val = newValue;
339             } else {
340                 while (parent.getParentItem() != null) {
341                     parent = parent.getParentItem();
342                 }
343                 // Parent will point to the top-level attribute of type
344                 // LocalResourceAttribute
345                 Object data = parent.getData();
346                 val = ((AttributeElement) data).getSimulatorResourceAttribute()
347                         .value();
348             }
349
350             return val;
351         }
352     }
353
354     class PostRequestEditor extends EditingSupport {
355
356         private final TreeViewer viewer;
357
358         public PostRequestEditor(TreeViewer viewer) {
359             super(viewer);
360             this.viewer = viewer;
361         }
362
363         @Override
364         protected boolean canEdit(Object arg0) {
365             return true;
366         }
367
368         @Override
369         protected CellEditor getCellEditor(Object element) {
370             SimulatorResourceAttribute att = null;
371             if (element instanceof AttributeElement) {
372                 att = ((AttributeElement) element)
373                         .getSimulatorResourceAttribute();
374             }
375
376             if (null == att) {
377                 return null;
378             }
379
380             AttributeValue val = att.value();
381             if (null == val) {
382                 return null;
383             }
384
385             TypeInfo type = val.typeInfo();
386
387             if (type.mType == ValueType.RESOURCEMODEL
388                     || type.mBaseType == ValueType.RESOURCEMODEL) {
389                 return null;
390             }
391
392             return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
393         }
394
395         @Override
396         protected Object getValue(Object element) {
397             if (element instanceof AttributeElement) {
398                 return ((AttributeElement) element).getPostState();
399             }
400
401             return false;
402         }
403
404         @Override
405         protected void setValue(Object element, Object value) {
406             if (!(element instanceof AttributeElement)) {
407                 return;
408             }
409             boolean status = (Boolean) value;
410             ((AttributeElement) element).setPostState(status);
411             viewer.update(element, null);
412         }
413     }
414 }