2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package oic.simulator.clientcontroller.view;
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;
38 import java.util.Date;
39 import java.util.List;
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;
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;
61 * This class provides editing support to the resources attributes table in the
64 public class AttributeEditingSupport {
66 private AttributeValueEditor attValueEditor;
67 private PostSelectionEditor postSelectionEditor;
69 public AttributeValueEditor createAttributeValueEditor(TreeViewer viewer,
70 TitleAreaDialog dialog) {
71 attValueEditor = new AttributeValueEditor(viewer, dialog);
72 return attValueEditor;
75 public PostSelectionEditor createPostSelectionEditor(TreeViewer viewer) {
76 postSelectionEditor = new PostSelectionEditor(viewer);
77 return postSelectionEditor;
80 class AttributeValueEditor extends EditingSupport {
82 private final TreeViewer viewer;
83 private CCombo comboBox;
84 private TitleAreaDialog dialog;
86 public AttributeValueEditor(TreeViewer viewer, TitleAreaDialog dialog) {
93 protected boolean canEdit(Object arg0) {
98 protected CellEditor getCellEditor(final Object element) {
99 ResourceManager resourceManager = Activator.getDefault()
100 .getResourceManager();
102 RemoteResource res = resourceManager
103 .getCurrentResourceInSelection();
108 final SimulatorResourceAttribute attribute;
109 if (!(element instanceof AttributeElement)) {
113 final AttributeElement attributeElement = ((AttributeElement) element);
114 attribute = attributeElement.getSimulatorResourceAttribute();
115 if (null == attribute) {
119 final AttributeValue val = attribute.value();
124 final TypeInfo type = val.typeInfo();
125 if (type.mBaseType == ValueType.RESOURCEMODEL) {
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() {
137 public void modifyText(ModifyEvent e) {
138 UpdatePrimitiveArrayAttributeDialog dialog = new UpdatePrimitiveArrayAttributeDialog(
139 Display.getDefault().getActiveShell(),
141 if (dialog.open() == Window.OK) {
142 updateAttributeValue(attribute,
143 dialog.getNewValueObj());
146 // Update the viewer in a separate UI thread.
147 Display.getDefault().asyncExec(new Runnable() {
150 // Set the post state of the top-level
152 AttributeElement rootElement = getRootElement(attributeElement);
153 rootElement.setPostState(true);
154 viewer.refresh(rootElement, true);
160 String values[] = null;
161 List<String> valueSet = resourceManager
162 .getAllValuesOfAttribute(attribute);
163 values = convertListToStringArray(valueSet);
165 editor = new ComboBoxCellEditor(viewer.getTree(), values);
166 comboBox = (CCombo) editor.getControl();
167 comboBox.addModifyListener(new ModifyListener() {
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);
184 protected Object getValue(Object element) {
186 SimulatorResourceAttribute att = null;
188 if (element instanceof AttributeElement) {
189 att = ((AttributeElement) element)
190 .getSimulatorResourceAttribute();
197 final AttributeValue val = att.value();
202 final TypeInfo type = val.typeInfo();
203 if (type.mBaseType == ValueType.RESOURCEMODEL) {
207 String valueString = Utility.getAttributeValueAsString(att.value());
208 if (null == valueString) {
212 if (type.mType == ValueType.ARRAY) {
213 ResourceManager resourceManager = Activator.getDefault()
214 .getResourceManager();
216 RemoteResource res = resourceManager
217 .getCurrentResourceInSelection();
218 if (null != res && res.isConfigUploaded()
219 && isArrayAttributeValid(att)) {
224 List<String> valueSet = Activator.getDefault().getResourceManager()
225 .getAllValuesOfAttribute(att);
226 if (null != valueSet) {
227 indexOfItem = valueSet.indexOf(valueString);
229 if (indexOfItem == -1) {
236 protected void setValue(Object element, Object value) {
237 SimulatorResourceAttribute att = null;
238 if (element instanceof AttributeElement) {
239 att = ((AttributeElement) element)
240 .getSimulatorResourceAttribute();
247 AttributeValue val = att.value();
252 TypeInfo type = val.typeInfo();
254 if (type.mBaseType == ValueType.RESOURCEMODEL) {
258 if (type.mType == ValueType.ARRAY) {
259 ResourceManager resourceManager = Activator.getDefault()
260 .getResourceManager();
262 RemoteResource res = resourceManager
263 .getCurrentResourceInSelection();
264 if (null != res && res.isConfigUploaded()
265 && isArrayAttributeValid(att)) {
270 String oldValue = String.valueOf(Utility
271 .getAttributeValueAsString(val));
272 if (null == oldValue) {
276 String newValue = comboBox.getText();
278 if (type.mType == ValueType.ARRAY
279 && type.mBaseType != ValueType.RESOURCEMODEL) {
280 newValue = Utility.removeWhiteSpacesInArrayValues(newValue);
283 if (!oldValue.equals(newValue)) {
284 boolean invalid = false;
286 // Get the AttriuteValue from the string
287 AttributeValue attValue = null;
289 attValue = AttributeValueBuilder.build(newValue,
291 } catch (Exception e) {
295 .log(Level.ERROR.ordinal(),
297 "There is an error while creating the new attribute value.\n"
298 + Utility.getSimulatorErrorString(
301 if (null == attValue) {
304 TypeInfo resTypeInfo = attValue.typeInfo();
305 if (type.mDepth != resTypeInfo.mDepth
306 || type.mType != resTypeInfo.mType
307 || type.mBaseType != resTypeInfo.mBaseType) {
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");
318 updateAttributeValue(att, attValue);
322 viewer.update(element, null);
325 private boolean isArrayAttributeValid(
326 SimulatorResourceAttribute attribute) {
327 if (null == attribute)
330 AttributeValue val = attribute.value();
334 AttributeProperty prop = attribute.property();
335 if (null == prop || !prop.isArray())
338 ArrayProperty arrProp = prop.asArray();
342 AttributeProperty elementProp = arrProp.getElementProperty();
343 if (null == elementProp)
346 TypeInfo info = val.typeInfo();
347 if (info.mBaseType == ValueType.RESOURCEMODEL)
353 public String[] convertListToStringArray(List<String> values) {
355 if (null != values && values.size() > 0) {
356 strArr = values.toArray(new String[1]);
358 strArr = new String[1];
363 public void updateAttributeValue(SimulatorResourceAttribute att,
364 AttributeValue value) {
365 IStructuredSelection selection = (IStructuredSelection) viewer
367 if (null == selection) {
371 Object obj = selection.getFirstElement();
376 Tree t = viewer.getTree();
377 TreeItem item = t.getSelection()[0];
382 TreeItem parent = item.getParentItem();
383 if (null != parent) {
384 while (parent.getParentItem() != null) {
385 parent = parent.getParentItem();
387 Object data = parent.getData();
388 ((AttributeElement) data).setPostState(true);
391 if (item.getData() instanceof AttributeElement) {
392 AttributeElement attributeElement = (AttributeElement) item
394 attributeElement.getSimulatorResourceAttribute()
397 parent = item.getParentItem();
398 if (null != parent) {
399 Object data = parent.getData();
401 ((AttributeElement) data).deepSetChildValue(att);
402 } catch (InvalidArgsException e) {
410 class PostSelectionEditor extends EditingSupport {
412 private final TreeViewer viewer;
414 public PostSelectionEditor(TreeViewer viewer) {
416 this.viewer = viewer;
420 protected boolean canEdit(Object arg0) {
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);
435 protected Object getValue(Object element) {
436 if (element instanceof AttributeElement) {
437 return ((AttributeElement) element).getPostState();
444 protected void setValue(Object element, Object value) {
445 if (!(element instanceof AttributeElement)) {
449 boolean status = (Boolean) value;
450 ((AttributeElement) element).setPostState(status);
451 viewer.update(element, null);
453 Tree t = viewer.getTree();
454 TreeItem item = t.getSelection()[0];
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();
465 Object data = parent.getData();
466 ((AttributeElement) data).setPostState(status);
471 private AttributeElement getRootElement(AttributeElement element) {
472 AttributeElement root = null;
474 Object parent = element.getParent();
475 if (parent instanceof ResourceRepresentation) {
479 while (!(parent instanceof ResourceRepresentation)) {
480 root = (AttributeElement) parent;
481 parent = ((AttributeElement) parent).getParent();