db3f288bb8c30ec520172000cab8618d81da19a2
[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 org.eclipse.jface.dialogs.TitleAreaDialog;
20 import org.eclipse.jface.viewers.CellEditor;
21 import org.eclipse.jface.viewers.CheckboxCellEditor;
22 import org.eclipse.jface.viewers.ComboBoxCellEditor;
23 import org.eclipse.jface.viewers.EditingSupport;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.viewers.TextCellEditor;
26 import org.eclipse.jface.viewers.TreeViewer;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.custom.CCombo;
30 import org.eclipse.swt.events.ModifyEvent;
31 import org.eclipse.swt.events.ModifyListener;
32 import org.eclipse.swt.widgets.Display;
33 import org.eclipse.swt.widgets.MessageBox;
34 import org.eclipse.swt.widgets.Text;
35 import org.eclipse.swt.widgets.Tree;
36 import org.eclipse.swt.widgets.TreeItem;
37
38 import java.util.Date;
39 import java.util.List;
40
41 import org.oic.simulator.ArrayProperty;
42 import org.oic.simulator.AttributeProperty;
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.ILogger.Level;
47 import org.oic.simulator.InvalidArgsException;
48 import org.oic.simulator.SimulatorResourceAttribute;
49
50 import oic.simulator.clientcontroller.Activator;
51 import oic.simulator.clientcontroller.manager.ResourceManager;
52 import oic.simulator.clientcontroller.remoteresource.AttributeElement;
53 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
54 import oic.simulator.clientcontroller.remoteresource.ResourceRepresentation;
55 import oic.simulator.clientcontroller.utils.AttributeValueBuilder;
56 import oic.simulator.clientcontroller.utils.Utility;
57 import oic.simulator.clientcontroller.view.dialogs.PostRequestDialog;
58 import oic.simulator.clientcontroller.view.dialogs.UpdatePrimitiveArrayAttributeDialog;
59
60 /**
61  * This class provides editing support to the resources attributes table in the
62  * attributes view.
63  */
64 public class AttributeEditingSupport {
65
66     private AttributeValueEditor attValueEditor;
67     private PostSelectionEditor  postSelectionEditor;
68
69     public AttributeValueEditor createAttributeValueEditor(TreeViewer viewer,
70             TitleAreaDialog dialog) {
71         attValueEditor = new AttributeValueEditor(viewer, dialog);
72         return attValueEditor;
73     }
74
75     public PostSelectionEditor createPostSelectionEditor(TreeViewer viewer) {
76         postSelectionEditor = new PostSelectionEditor(viewer);
77         return postSelectionEditor;
78     }
79
80     class AttributeValueEditor extends EditingSupport {
81
82         private final TreeViewer viewer;
83         private CCombo           comboBox;
84         private TitleAreaDialog  dialog;
85
86         public AttributeValueEditor(TreeViewer viewer, TitleAreaDialog dialog) {
87             super(viewer);
88             this.viewer = viewer;
89             this.dialog = dialog;
90         }
91
92         @Override
93         protected boolean canEdit(Object arg0) {
94             return true;
95         }
96
97         @Override
98         protected CellEditor getCellEditor(final Object element) {
99             ResourceManager resourceManager = Activator.getDefault()
100                     .getResourceManager();
101
102             RemoteResource res = resourceManager
103                     .getCurrentResourceInSelection();
104             if (null == res) {
105                 return null;
106             }
107
108             final SimulatorResourceAttribute attribute;
109             if (!(element instanceof AttributeElement)) {
110                 return null;
111             }
112
113             final AttributeElement attributeElement = ((AttributeElement) element);
114             attribute = attributeElement.getSimulatorResourceAttribute();
115             if (null == attribute) {
116                 return null;
117             }
118
119             final AttributeValue val = attribute.value();
120             if (null == val) {
121                 return null;
122             }
123
124             final TypeInfo type = val.typeInfo();
125             if (type.mBaseType == ValueType.RESOURCEMODEL) {
126                 return null;
127             }
128
129             CellEditor editor;
130             if (type.mType == ValueType.ARRAY && res.isConfigUploaded()
131                     && isArrayAttributeValid(attribute)) {
132                 editor = new TextCellEditor(viewer.getTree());
133                 editor.setStyle(SWT.READ_ONLY);
134                 final Text txt = (Text) editor.getControl();
135                 txt.addModifyListener(new ModifyListener() {
136                     @Override
137                     public void modifyText(ModifyEvent e) {
138                         UpdatePrimitiveArrayAttributeDialog dialog = new UpdatePrimitiveArrayAttributeDialog(
139                                 Display.getDefault().getActiveShell(),
140                                 attribute);
141                         if (dialog.open() == Window.OK) {
142                             updateAttributeValue(attribute,
143                                     dialog.getNewValueObj());
144                         }
145
146                         // Update the viewer in a separate UI thread.
147                         Display.getDefault().asyncExec(new Runnable() {
148                             @Override
149                             public void run() {
150                                 // Set the post state of the top-level
151                                 // attribute.
152                                 AttributeElement rootElement = getRootElement(attributeElement);
153                                 rootElement.setPostState(true);
154                                 viewer.refresh(rootElement, true);
155                             }
156                         });
157                     }
158                 });
159             } else {
160                 String values[] = null;
161                 List<String> valueSet = resourceManager
162                         .getAllValuesOfAttribute(attribute);
163                 values = convertListToStringArray(valueSet);
164
165                 editor = new ComboBoxCellEditor(viewer.getTree(), values);
166                 comboBox = (CCombo) editor.getControl();
167                 comboBox.addModifyListener(new ModifyListener() {
168
169                     @Override
170                     public void modifyText(ModifyEvent event) {
171                         // Set the post state of the top-level attribute.
172                         AttributeElement rootElement = getRootElement(attributeElement);
173                         rootElement.setPostState(true);
174                         if (AttributeValueEditor.this.dialog instanceof PostRequestDialog) {
175                             viewer.update(rootElement, null);
176                         }
177                     }
178                 });
179             }
180             return editor;
181         }
182
183         @Override
184         protected Object getValue(Object element) {
185             int indexOfItem = 0;
186             SimulatorResourceAttribute att = null;
187
188             if (element instanceof AttributeElement) {
189                 att = ((AttributeElement) element)
190                         .getSimulatorResourceAttribute();
191             }
192
193             if (att == null) {
194                 return 0;
195             }
196
197             final AttributeValue val = att.value();
198             if (null == val) {
199                 return null;
200             }
201
202             final TypeInfo type = val.typeInfo();
203             if (type.mBaseType == ValueType.RESOURCEMODEL) {
204                 return null;
205             }
206
207             String valueString = Utility.getAttributeValueAsString(att.value());
208             if (null == valueString) {
209                 valueString = "";
210             }
211
212             if (type.mType == ValueType.ARRAY) {
213                 ResourceManager resourceManager = Activator.getDefault()
214                         .getResourceManager();
215
216                 RemoteResource res = resourceManager
217                         .getCurrentResourceInSelection();
218                 if (null != res && res.isConfigUploaded()
219                         && isArrayAttributeValid(att)) {
220                     return valueString;
221                 }
222             }
223
224             List<String> valueSet = Activator.getDefault().getResourceManager()
225                     .getAllValuesOfAttribute(att);
226             if (null != valueSet) {
227                 indexOfItem = valueSet.indexOf(valueString);
228             }
229             if (indexOfItem == -1) {
230                 indexOfItem = 0;
231             }
232             return indexOfItem;
233         }
234
235         @Override
236         protected void setValue(Object element, Object value) {
237             SimulatorResourceAttribute att = null;
238             if (element instanceof AttributeElement) {
239                 att = ((AttributeElement) element)
240                         .getSimulatorResourceAttribute();
241             }
242
243             if (att == null) {
244                 return;
245             }
246
247             AttributeValue val = att.value();
248             if (null == val) {
249                 return;
250             }
251
252             TypeInfo type = val.typeInfo();
253
254             if (type.mBaseType == ValueType.RESOURCEMODEL) {
255                 return;
256             }
257
258             if (type.mType == ValueType.ARRAY) {
259                 ResourceManager resourceManager = Activator.getDefault()
260                         .getResourceManager();
261
262                 RemoteResource res = resourceManager
263                         .getCurrentResourceInSelection();
264                 if (null != res && res.isConfigUploaded()
265                         && isArrayAttributeValid(att)) {
266                     return;
267                 }
268             }
269
270             String oldValue = String.valueOf(Utility
271                     .getAttributeValueAsString(val));
272             if (null == oldValue) {
273                 oldValue = "";
274             }
275
276             String newValue = comboBox.getText();
277
278             if (type.mType == ValueType.ARRAY
279                     && type.mBaseType != ValueType.RESOURCEMODEL) {
280                 newValue = Utility.removeWhiteSpacesInArrayValues(newValue);
281             }
282
283             if (!oldValue.equals(newValue)) {
284                 boolean invalid = false;
285
286                 // Get the AttriuteValue from the string
287                 AttributeValue attValue = null;
288                 try {
289                     attValue = AttributeValueBuilder.build(newValue,
290                             type.mBaseType);
291                 } catch (Exception e) {
292                     Activator
293                             .getDefault()
294                             .getLogManager()
295                             .log(Level.ERROR.ordinal(),
296                                     new Date(),
297                                     "There is an error while creating the new attribute value.\n"
298                                             + Utility.getSimulatorErrorString(
299                                                     e, null));
300                 }
301                 if (null == attValue) {
302                     invalid = true;
303                 } else {
304                     TypeInfo resTypeInfo = attValue.typeInfo();
305                     if (type.mDepth != resTypeInfo.mDepth
306                             || type.mType != resTypeInfo.mType
307                             || type.mBaseType != resTypeInfo.mBaseType) {
308                         invalid = true;
309                     }
310                 }
311                 if (invalid) {
312                     MessageBox dialog = new MessageBox(viewer.getTree()
313                             .getShell(), SWT.ICON_ERROR | SWT.OK);
314                     dialog.setText("Invalid Value");
315                     dialog.setMessage("Given value is invalid");
316                     dialog.open();
317                 } else {
318                     updateAttributeValue(att, attValue);
319                 }
320             }
321
322             viewer.update(element, null);
323         }
324
325         private boolean isArrayAttributeValid(
326                 SimulatorResourceAttribute attribute) {
327             if (null == attribute)
328                 return false;
329
330             AttributeValue val = attribute.value();
331             if (null == val)
332                 return false;
333
334             AttributeProperty prop = attribute.property();
335             if (null == prop || !prop.isArray())
336                 return false;
337
338             ArrayProperty arrProp = prop.asArray();
339             if (null == arrProp)
340                 return false;
341
342             AttributeProperty elementProp = arrProp.getElementProperty();
343             if (null == elementProp)
344                 return false;
345
346             TypeInfo info = val.typeInfo();
347             if (info.mBaseType == ValueType.RESOURCEMODEL)
348                 return false;
349
350             return true;
351         }
352
353         public String[] convertListToStringArray(List<String> values) {
354             String[] strArr;
355             if (null != values && values.size() > 0) {
356                 strArr = values.toArray(new String[1]);
357             } else {
358                 strArr = new String[1];
359             }
360             return strArr;
361         }
362
363         public void updateAttributeValue(SimulatorResourceAttribute att,
364                 AttributeValue value) {
365             IStructuredSelection selection = (IStructuredSelection) viewer
366                     .getSelection();
367             if (null == selection) {
368                 return;
369             }
370
371             Object obj = selection.getFirstElement();
372             if (null == obj) {
373                 return;
374             }
375
376             Tree t = viewer.getTree();
377             TreeItem item = t.getSelection()[0];
378             if (null == item) {
379                 return;
380             }
381
382             TreeItem parent = item.getParentItem();
383             if (null != parent) {
384                 while (parent.getParentItem() != null) {
385                     parent = parent.getParentItem();
386                 }
387                 Object data = parent.getData();
388                 ((AttributeElement) data).setPostState(true);
389             }
390
391             if (item.getData() instanceof AttributeElement) {
392                 AttributeElement attributeElement = (AttributeElement) item
393                         .getData();
394                 attributeElement.getSimulatorResourceAttribute()
395                         .setValue(value);
396
397                 parent = item.getParentItem();
398                 if (null != parent) {
399                     Object data = parent.getData();
400                     try {
401                         ((AttributeElement) data).deepSetChildValue(att);
402                     } catch (InvalidArgsException e) {
403                         e.printStackTrace();
404                     }
405                 }
406             }
407         }
408     }
409
410     class PostSelectionEditor extends EditingSupport {
411
412         private final TreeViewer viewer;
413
414         public PostSelectionEditor(TreeViewer viewer) {
415             super(viewer);
416             this.viewer = viewer;
417         }
418
419         @Override
420         protected boolean canEdit(Object arg0) {
421             return true;
422         }
423
424         @Override
425         protected CellEditor getCellEditor(Object element) {
426             if (element instanceof AttributeElement
427                     && ((AttributeElement) element).getParent() instanceof ResourceRepresentation) {
428                 return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
429             }
430
431             return null;
432         }
433
434         @Override
435         protected Object getValue(Object element) {
436             if (element instanceof AttributeElement) {
437                 return ((AttributeElement) element).getPostState();
438             }
439
440             return false;
441         }
442
443         @Override
444         protected void setValue(Object element, Object value) {
445             if (!(element instanceof AttributeElement)) {
446                 return;
447             }
448
449             boolean status = (Boolean) value;
450             ((AttributeElement) element).setPostState(status);
451             viewer.update(element, null);
452
453             Tree t = viewer.getTree();
454             TreeItem item = t.getSelection()[0];
455             if (null == item) {
456                 return;
457             }
458
459             // Update the post state of the top-most parent of this attribute.
460             TreeItem parent = item.getParentItem();
461             if (null != parent) {
462                 while (parent.getParentItem() != null) {
463                     parent = parent.getParentItem();
464                 }
465                 Object data = parent.getData();
466                 ((AttributeElement) data).setPostState(status);
467             }
468         }
469     }
470
471     private AttributeElement getRootElement(AttributeElement element) {
472         AttributeElement root = null;
473
474         Object parent = element.getParent();
475         if (parent instanceof ResourceRepresentation) {
476             return element;
477         }
478
479         while (!(parent instanceof ResourceRepresentation)) {
480             root = (AttributeElement) parent;
481             parent = ((AttributeElement) parent).getParent();
482         }
483
484         return root;
485     }
486 }