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.serviceprovider.view;
19 import java.util.List;
21 import oic.simulator.serviceprovider.Activator;
22 import oic.simulator.serviceprovider.manager.ResourceManager;
23 import oic.simulator.serviceprovider.model.AttributeElement;
24 import oic.simulator.serviceprovider.model.AutomationSettingHelper;
25 import oic.simulator.serviceprovider.model.CollectionResource;
26 import oic.simulator.serviceprovider.model.Resource;
27 import oic.simulator.serviceprovider.model.ResourceRepresentation;
28 import oic.simulator.serviceprovider.model.SingleResource;
29 import oic.simulator.serviceprovider.utils.AttributeValueBuilder;
30 import oic.simulator.serviceprovider.utils.Utility;
31 import oic.simulator.serviceprovider.view.dialogs.AutomationSettingDialog;
33 import org.eclipse.jface.dialogs.MessageDialog;
34 import org.eclipse.jface.viewers.CellEditor;
35 import org.eclipse.jface.viewers.CheckboxCellEditor;
36 import org.eclipse.jface.viewers.ComboBoxCellEditor;
37 import org.eclipse.jface.viewers.EditingSupport;
38 import org.eclipse.jface.viewers.IStructuredSelection;
39 import org.eclipse.jface.viewers.TreeViewer;
40 import org.eclipse.jface.window.Window;
41 import org.eclipse.swt.SWT;
42 import org.eclipse.swt.custom.CCombo;
43 import org.eclipse.swt.events.ModifyEvent;
44 import org.eclipse.swt.events.ModifyListener;
45 import org.eclipse.swt.widgets.Display;
46 import org.eclipse.swt.widgets.MessageBox;
47 import org.eclipse.swt.widgets.Tree;
48 import org.eclipse.swt.widgets.TreeItem;
49 import org.oic.simulator.AttributeProperty;
50 import org.oic.simulator.AttributeValue;
51 import org.oic.simulator.AttributeValue.TypeInfo;
52 import org.oic.simulator.AttributeValue.ValueType;
53 import org.oic.simulator.InvalidArgsException;
54 import org.oic.simulator.SimulatorResourceAttribute;
55 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
58 * This class provides editing support to the resources attributes table in the
61 public class AttributeEditingSupport {
63 private AttributeValueEditor attValueEditor;
64 private AutomationEditor automationEditor;
66 public AttributeValueEditor createAttributeValueEditor(TreeViewer viewer) {
67 attValueEditor = new AttributeValueEditor(viewer);
68 return attValueEditor;
71 public AutomationEditor createAutomationEditor(TreeViewer viewer) {
72 automationEditor = new AutomationEditor(viewer);
73 return automationEditor;
76 class AttributeValueEditor extends EditingSupport {
78 private final TreeViewer viewer;
79 private CCombo comboBox;
81 public AttributeValueEditor(TreeViewer viewer) {
87 protected boolean canEdit(Object arg0) {
92 protected CellEditor getCellEditor(final Object element) {
93 ResourceManager resourceManager = Activator.getDefault()
94 .getResourceManager();
96 Resource res = resourceManager.getCurrentResourceInSelection();
101 // If selected resource is a collection, then editor support is not
103 if (res instanceof CollectionResource) {
107 final SimulatorResourceAttribute attribute;
108 if (!(element instanceof AttributeElement)) {
112 AttributeElement attributeElement = ((AttributeElement) element);
113 attribute = attributeElement.getSimulatorResourceAttribute();
114 if (null == attribute) {
118 // CellEditor is not required as the automation is in progress.
119 if (attributeElement.isAutoUpdateInProgress()) {
123 final AttributeValue val = attribute.value();
128 final TypeInfo type = val.typeInfo();
129 if (type.mBaseType == ValueType.RESOURCEMODEL) {
133 AttributeProperty prop = attribute.property();
138 if (!resourceManager.isAttHasRangeOrAllowedValues(attribute)) {
142 String values[] = null;
143 List<String> valueSet = resourceManager
144 .getAllValuesOfAttribute(attribute);
145 values = convertListToStringArray(valueSet);
147 ComboBoxCellEditor comboEditor;
148 if (type.mType == ValueType.ARRAY) {
149 comboEditor = new ComboBoxCellEditor(viewer.getTree(), values);
151 comboEditor = new ComboBoxCellEditor(viewer.getTree(), values,
154 comboBox = (CCombo) comboEditor.getControl();
155 comboBox.addModifyListener(new ModifyListener() {
158 public void modifyText(ModifyEvent event) {
159 if (type.mType == ValueType.ARRAY) {
162 String oldValue = String.valueOf(Utility
163 .getAttributeValueAsString(val));
164 String newValue = comboBox.getText();
165 if (!oldValue.equals(newValue)) {
166 // Get the AttriuteValue from the string
167 AttributeValue value = AttributeValueBuilder.build(
168 newValue, type.mBaseType);
169 TypeInfo resTypeInfo = value.typeInfo();
170 if (null == value || type.mDepth != resTypeInfo.mDepth
171 || type.mType != resTypeInfo.mType
172 || type.mBaseType != resTypeInfo.mBaseType) {
173 MessageBox dialog = new MessageBox(viewer.getTree()
174 .getShell(), SWT.ICON_ERROR | SWT.OK);
175 dialog.setText("Invalid Value");
176 dialog.setMessage("Given value is invalid");
179 updateAttributeValue(attribute, value);
180 MessageBox dialog = new MessageBox(viewer.getTree()
181 .getShell(), SWT.ICON_QUESTION | SWT.OK
183 dialog.setText("Confirm action");
184 dialog.setMessage("Do you want to modify the value?");
185 int retval = dialog.open();
186 if (retval != SWT.OK) {
187 value = AttributeValueBuilder.build(oldValue,
189 updateAttributeValue(attribute, value);
191 ResourceManager resourceManager;
192 resourceManager = Activator.getDefault()
193 .getResourceManager();
195 Resource resource = resourceManager
196 .getCurrentResourceInSelection();
198 SimulatorResourceAttribute result = getResultantValue(value);
200 resourceManager.attributeValueUpdated(
201 (SingleResource) resource,
202 result.name(), result.value());
206 viewer.update(element, null);
207 comboBox.setVisible(false);
215 protected Object getValue(Object element) {
217 SimulatorResourceAttribute att = null;
219 if (element instanceof AttributeElement) {
220 att = ((AttributeElement) element)
221 .getSimulatorResourceAttribute();
228 String valueString = Utility.getAttributeValueAsString(att.value());
229 List<String> valueSet = Activator.getDefault().getResourceManager()
230 .getAllValuesOfAttribute(att);
231 if (null != valueSet) {
232 indexOfItem = valueSet.indexOf(valueString);
234 if (indexOfItem == -1) {
241 protected void setValue(Object element, Object value) {
242 SimulatorResourceAttribute att = null;
244 if (element instanceof AttributeElement) {
245 att = ((AttributeElement) element)
246 .getSimulatorResourceAttribute();
253 AttributeValue val = att.value();
257 TypeInfo type = val.typeInfo();
258 if (type.mType == ValueType.ARRAY) {
261 index = Integer.parseInt(String.valueOf(value));
262 } catch (NumberFormatException nfe) {
266 String oldValue = String.valueOf(Utility
267 .getAttributeValueAsString(val));
268 String newValue = comboBox.getText();
269 if (!oldValue.equals(newValue)) {
270 // Get the AttriuteValue from the string
271 AttributeValue attValue = AttributeValueBuilder.build(
272 newValue, type.mBaseType);
273 TypeInfo resTypeInfo = attValue.typeInfo();
275 || type.mDepth != resTypeInfo.mDepth
276 || type.mType != resTypeInfo.mType
277 || type.mBaseType != resTypeInfo.mBaseType) {
278 MessageBox dialog = new MessageBox(viewer.getTree()
279 .getShell(), SWT.ICON_ERROR | SWT.OK);
280 dialog.setText("Invalid Value");
281 dialog.setMessage("Given value is invalid");
284 updateAttributeValue(att, attValue);
285 MessageBox dialog = new MessageBox(viewer.getTree()
286 .getShell(), SWT.ICON_QUESTION | SWT.OK
288 dialog.setText("Confirm action");
289 dialog.setMessage("Do you want to modify the value?");
290 int retval = dialog.open();
291 if (retval != SWT.OK) {
292 attValue = AttributeValueBuilder.build(
293 oldValue, type.mBaseType);
294 updateAttributeValue(att, attValue);
296 ResourceManager resourceManager;
297 resourceManager = Activator.getDefault()
298 .getResourceManager();
300 Resource resource = resourceManager
301 .getCurrentResourceInSelection();
303 SimulatorResourceAttribute result = getResultantValue(attValue);
305 resourceManager.attributeValueUpdated(
306 (SingleResource) resource,
307 result.name(), result.value());
314 viewer.update(element, null);
317 public String[] convertListToStringArray(List<String> valueList) {
319 if (null != valueList && valueList.size() > 0) {
320 strArr = valueList.toArray(new String[1]);
322 strArr = new String[1];
327 public void updateAttributeValue(SimulatorResourceAttribute att,
328 AttributeValue value) {
331 IStructuredSelection selection = (IStructuredSelection) viewer
333 if (null == selection) {
337 Object obj = selection.getFirstElement();
342 Tree t = viewer.getTree();
343 TreeItem item = t.getSelection()[0];
348 if (item.getData() instanceof AttributeElement) {
349 AttributeElement attributeElement = (AttributeElement) item
351 attributeElement.getSimulatorResourceAttribute()
354 TreeItem parent = item.getParentItem();
355 if (null != parent) {
356 Object data = parent.getData();
358 ((AttributeElement) data).deepSetChildValue(att);
359 } catch (InvalidArgsException e) {
366 public SimulatorResourceAttribute getResultantValue(
367 AttributeValue newValue) {
368 AttributeValue val = null;
369 IStructuredSelection selection = (IStructuredSelection) viewer
371 if (null == selection) {
375 Object obj = selection.getFirstElement();
380 Tree t = viewer.getTree();
381 TreeItem item = t.getSelection()[0];
386 SimulatorResourceAttribute result = null;
387 TreeItem parent = item.getParentItem();
388 if (null == parent) {
389 Object data = item.getData();
390 result = ((AttributeElement) data)
391 .getSimulatorResourceAttribute();
393 while (parent.getParentItem() != null) {
394 parent = parent.getParentItem();
397 // Parent will point to the top-level attribute of type
398 Object data = parent.getData();
399 result = ((AttributeElement) data)
400 .getSimulatorResourceAttribute();
407 class AutomationEditor extends EditingSupport {
409 private final TreeViewer viewer;
411 public AutomationEditor(TreeViewer viewer) {
413 this.viewer = viewer;
417 protected boolean canEdit(Object arg0) {
422 protected CellEditor getCellEditor(Object element) {
423 // CellEditor is not required as the automation is in progress.
424 ResourceManager resourceManager = Activator.getDefault()
425 .getResourceManager();
426 Resource resource = resourceManager.getCurrentResourceInSelection();
428 if (null == resource) {
432 if (resource instanceof CollectionResource) {
435 if (((SingleResource) resource).isResourceAutomationInProgress()) {
439 SimulatorResourceAttribute att = null;
440 if (element instanceof AttributeElement) {
441 att = ((AttributeElement) element)
442 .getSimulatorResourceAttribute();
449 AttributeValue val = att.value();
454 TypeInfo type = val.typeInfo();
456 if (type.mType == ValueType.RESOURCEMODEL
457 || type.mType == ValueType.ARRAY) {
461 Object parent = ((AttributeElement) element).getParent();
462 if (null != parent && !(parent instanceof ResourceRepresentation)) {
466 return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
470 protected Object getValue(Object element) {
471 if (element instanceof AttributeElement) {
472 return ((AttributeElement) element).isAutoUpdateInProgress();
479 protected void setValue(Object element, Object value) {
480 if (!(element instanceof AttributeElement)) {
484 ResourceManager resourceManager = Activator.getDefault()
485 .getResourceManager();
486 // As automation depends on the current resource in selection, its
487 // presence is being checked.
488 Resource resource = resourceManager.getCurrentResourceInSelection();
489 if (null == resource) {
493 AttributeElement att = (AttributeElement) element;
494 boolean checked = (Boolean) value;
496 // Start the automation
497 // Fetch the settings data
498 List<AutomationSettingHelper> automationSettings;
499 automationSettings = AutomationSettingHelper
500 .getAutomationSettings(att);
502 // Open the settings dialog
503 AutomationSettingDialog dialog = new AutomationSettingDialog(
504 viewer.getTree().getShell(), automationSettings);
506 if (dialog.open() == Window.OK) {
507 String automationType = dialog.getAutomationType();
508 String updateFreq = dialog.getUpdateFrequency();
510 AutoUpdateType autoType = AutoUpdateType
511 .valueOf(automationType);
512 int updFreq = Utility
513 .getUpdateIntervalFromString(updateFreq);
514 int autoId = resourceManager.startAutomation(
515 (SingleResource) resource, att, autoType, updFreq);
517 MessageDialog.openInformation(Display.getDefault()
518 .getActiveShell(), "Automation Status",
519 "Automation start failed!!");
521 // viewer.update(element, null);
525 // Stop the automation
526 resourceManager.stopAutomation((SingleResource) resource, att,
527 att.getAutoUpdateId());
528 MessageDialog.openInformation(Display.getDefault()
529 .getActiveShell(), "Automation Status",
530 "Automation stopped.");
531 // viewer.update(element, null);