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.AutomationSettingHelper;
24 import oic.simulator.serviceprovider.model.CollectionResource;
25 import oic.simulator.serviceprovider.model.LocalResourceAttribute;
26 import oic.simulator.serviceprovider.model.Resource;
27 import oic.simulator.serviceprovider.model.SRMItem;
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.SimulatorResourceModel;
56 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
59 * This class provides editing support to the resources attributes table in the
62 public class AttributeEditingSupport {
64 private AttributeValueEditor attValueEditor;
65 private AutomationEditor automationEditor;
67 public AttributeValueEditor createAttributeValueEditor(TreeViewer viewer) {
68 attValueEditor = new AttributeValueEditor(viewer);
69 return attValueEditor;
72 public AutomationEditor createAutomationEditor(TreeViewer viewer) {
73 automationEditor = new AutomationEditor(viewer);
74 return automationEditor;
77 class AttributeValueEditor extends EditingSupport {
79 private final TreeViewer viewer;
80 private CCombo comboBox;
82 public AttributeValueEditor(TreeViewer viewer) {
88 protected boolean canEdit(Object arg0) {
93 protected CellEditor getCellEditor(final Object element) {
94 ResourceManager resourceManager = Activator.getDefault()
95 .getResourceManager();
97 Resource res = resourceManager.getCurrentResourceInSelection();
102 // If selected resource is a collection, then editor support is not
104 if (res instanceof CollectionResource) {
108 final SimulatorResourceAttribute att;
109 if (element instanceof SimulatorResourceAttribute
110 || element instanceof LocalResourceAttribute) {
111 if (element instanceof LocalResourceAttribute) {
112 LocalResourceAttribute localAtt = (LocalResourceAttribute) element;
113 att = localAtt.getResourceAttributeRef();
115 att = (SimulatorResourceAttribute) element;
125 final AttributeValue val = att.value();
130 final TypeInfo type = val.typeInfo();
131 if (type.mType == ValueType.RESOURCEMODEL
132 || (type.mType == ValueType.ARRAY && type.mBaseType == ValueType.RESOURCEMODEL)) {
136 AttributeProperty prop = att.property();
141 if (!resourceManager.isAttHasRangeOrAllowedValues(att)) {
145 // CellEditor is not required as the automation is in progress.
146 if (element instanceof LocalResourceAttribute
147 && ((LocalResourceAttribute) element)
148 .isAutomationInProgress()) {
152 String values[] = null;
153 List<String> valueSet = resourceManager
154 .getAllValuesOfAttribute(att);
155 values = convertListToStringArray(valueSet);
157 ComboBoxCellEditor comboEditor;
158 if (type.mType == ValueType.ARRAY) {
159 comboEditor = new ComboBoxCellEditor(viewer.getTree(), values);
161 comboEditor = new ComboBoxCellEditor(viewer.getTree(), values,
164 comboBox = (CCombo) comboEditor.getControl();
165 comboBox.addModifyListener(new ModifyListener() {
168 public void modifyText(ModifyEvent event) {
169 if (type.mType == ValueType.ARRAY) {
172 String oldValue = String.valueOf(Utility
173 .getAttributeValueAsString(val));
174 String newValue = comboBox.getText();
175 if (!oldValue.equals(newValue)) {
176 // Get the AttriuteValue from the string
177 AttributeValue value = AttributeValueBuilder.build(
178 newValue, type.mBaseType);
179 TypeInfo resTypeInfo = value.typeInfo();
180 if (null == value || type.mDepth != resTypeInfo.mDepth
181 || type.mType != resTypeInfo.mType
182 || type.mBaseType != resTypeInfo.mBaseType) {
183 MessageBox dialog = new MessageBox(viewer.getTree()
184 .getShell(), SWT.ICON_ERROR | SWT.OK);
185 dialog.setText("Invalid Value");
186 dialog.setMessage("Given value is invalid");
189 updateAttributeValue(att, value);
190 MessageBox dialog = new MessageBox(viewer.getTree()
191 .getShell(), SWT.ICON_QUESTION | SWT.OK
193 dialog.setText("Confirm action");
194 dialog.setMessage("Do you want to modify the value?");
195 int retval = dialog.open();
196 if (retval != SWT.OK) {
197 value = AttributeValueBuilder.build(oldValue,
199 updateAttributeValue(att, value);
201 ResourceManager resourceManager;
202 resourceManager = Activator.getDefault()
203 .getResourceManager();
205 Resource resource = resourceManager
206 .getCurrentResourceInSelection();
208 AttributeValue resultValue = getResultantValue(value);
210 resourceManager.attributeValueUpdated(
211 (SingleResource) resource, att.name(),
216 viewer.update(element, null);
217 comboBox.setVisible(false);
225 protected Object getValue(Object element) {
227 SimulatorResourceAttribute att;
229 if (element instanceof LocalResourceAttribute) {
230 LocalResourceAttribute localAtt = (LocalResourceAttribute) element;
231 att = localAtt.getResourceAttributeRef();
235 } else if (element instanceof SimulatorResourceAttribute) {
236 att = (SimulatorResourceAttribute) element;
241 String valueString = Utility.getAttributeValueAsString(att.value());
242 List<String> valueSet = Activator.getDefault().getResourceManager()
243 .getAllValuesOfAttribute(att);
244 if (null != valueSet) {
245 indexOfItem = valueSet.indexOf(valueString);
247 if (indexOfItem == -1) {
254 protected void setValue(Object element, Object value) {
255 SimulatorResourceAttribute att;
257 if (element instanceof LocalResourceAttribute) {
258 LocalResourceAttribute localAtt = (LocalResourceAttribute) element;
259 att = localAtt.getResourceAttributeRef();
263 } else if (element instanceof SimulatorResourceAttribute) {
264 att = (SimulatorResourceAttribute) element;
269 AttributeValue val = att.value();
273 TypeInfo type = val.typeInfo();
274 if (type.mType == ValueType.ARRAY) {
277 index = Integer.parseInt(String.valueOf(value));
278 } catch (NumberFormatException nfe) {
282 String oldValue = String.valueOf(Utility
283 .getAttributeValueAsString(val));
284 String newValue = comboBox.getText();
285 if (!oldValue.equals(newValue)) {
286 // Get the AttriuteValue from the string
287 AttributeValue attValue = AttributeValueBuilder.build(
288 newValue, type.mBaseType);
289 TypeInfo resTypeInfo = attValue.typeInfo();
291 || type.mDepth != resTypeInfo.mDepth
292 || type.mType != resTypeInfo.mType
293 || type.mBaseType != resTypeInfo.mBaseType) {
294 MessageBox dialog = new MessageBox(viewer.getTree()
295 .getShell(), SWT.ICON_ERROR | SWT.OK);
296 dialog.setText("Invalid Value");
297 dialog.setMessage("Given value is invalid");
300 updateAttributeValue(att, attValue);
301 MessageBox dialog = new MessageBox(viewer.getTree()
302 .getShell(), SWT.ICON_QUESTION | SWT.OK
304 dialog.setText("Confirm action");
305 dialog.setMessage("Do you want to modify the value?");
306 int retval = dialog.open();
307 if (retval != SWT.OK) {
308 attValue = AttributeValueBuilder.build(
309 oldValue, type.mBaseType);
310 updateAttributeValue(att, attValue);
312 ResourceManager resourceManager;
313 resourceManager = Activator.getDefault()
314 .getResourceManager();
316 Resource resource = resourceManager
317 .getCurrentResourceInSelection();
319 AttributeValue resultValue = getResultantValue(attValue);
321 resourceManager.attributeValueUpdated(
322 (SingleResource) resource, att.name(),
330 viewer.update(element, null);
333 public String[] convertListToStringArray(List<String> valueList) {
335 if (null != valueList && valueList.size() > 0) {
336 strArr = valueList.toArray(new String[1]);
338 strArr = new String[1];
343 public void updateAttributeValue(SimulatorResourceAttribute att,
344 AttributeValue value) {
347 IStructuredSelection selection = (IStructuredSelection) viewer
349 if (null == selection) {
353 Object obj = selection.getFirstElement();
358 Tree t = viewer.getTree();
359 TreeItem item = t.getSelection()[0];
363 TreeItem parent = item.getParentItem();
365 // Get the parent model
366 if (null == parent) {
367 // Top-level attribute
368 Resource res = Activator.getDefault().getResourceManager()
369 .getCurrentResourceInSelection();
373 SimulatorResourceModel model = res.getResourceModel();
378 model.setAttributeValue(att.name(), value);
379 } catch (InvalidArgsException e) {
380 // TODO Auto-generated catch block
384 SimulatorResourceModel model;
385 Object data = parent.getData();
389 if (data instanceof SRMItem) {
390 model = ((SRMItem) data).getModel();
392 SimulatorResourceAttribute parentAtt;
393 if (data instanceof LocalResourceAttribute) {
394 parentAtt = ((LocalResourceAttribute) data)
395 .getResourceAttributeRef();
397 parentAtt = (SimulatorResourceAttribute) data;
399 model = (SimulatorResourceModel) parentAtt.value().get();
402 model.setAttributeValue(att.name(), value);
403 } catch (InvalidArgsException e) {
404 // TODO Auto-generated catch block
410 public AttributeValue getResultantValue(AttributeValue newValue) {
411 AttributeValue val = null;
412 IStructuredSelection selection = (IStructuredSelection) viewer
414 if (null == selection) {
418 Object obj = selection.getFirstElement();
423 Tree t = viewer.getTree();
424 TreeItem item = t.getSelection()[0];
429 TreeItem parent = item.getParentItem();
430 if (null == parent) {
431 Object data = item.getData();
432 // SimulatorResourceAttribute att =
433 // ((LocalResourceAttribute)data).getResourceAttributeRef();
436 while (parent.getParentItem() != null) {
437 parent = parent.getParentItem();
439 // Parent will point to the top-level attribute of type
440 // LocalResourceAttribute
441 Object data = parent.getData();
442 val = ((LocalResourceAttribute) data).getResourceAttributeRef()
450 class AutomationEditor extends EditingSupport {
452 private final TreeViewer viewer;
454 public AutomationEditor(TreeViewer viewer) {
456 this.viewer = viewer;
460 protected boolean canEdit(Object arg0) {
465 protected CellEditor getCellEditor(Object element) {
466 // CellEditor is not required as the automation is in progress.
467 ResourceManager resourceManager = Activator.getDefault()
468 .getResourceManager();
469 Resource resource = resourceManager.getCurrentResourceInSelection();
471 if (null == resource) {
475 if (resource instanceof CollectionResource) {
478 if (((SingleResource) resource).isResourceAutomationInProgress()) {
482 SimulatorResourceAttribute att = null;
483 if (element instanceof LocalResourceAttribute) {
484 LocalResourceAttribute localAtt = (LocalResourceAttribute) element;
485 att = localAtt.getResourceAttributeRef();
492 AttributeValue val = att.value();
497 TypeInfo type = val.typeInfo();
499 if (type.mType == ValueType.RESOURCEMODEL
500 || type.mType == ValueType.ARRAY) {
504 return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
508 protected Object getValue(Object element) {
509 LocalResourceAttribute att = (LocalResourceAttribute) element;
510 return att.isAutomationInProgress();
514 protected void setValue(Object element, Object value) {
515 ResourceManager resourceManager = Activator.getDefault()
516 .getResourceManager();
517 // As automation depends on the current resource in selection, its
518 // presence is being checked.
519 Resource resource = resourceManager.getCurrentResourceInSelection();
520 if (null == resource) {
524 LocalResourceAttribute att = (LocalResourceAttribute) element;
525 boolean checked = (Boolean) value;
527 // Start the automation
528 // Fetch the settings data
529 List<AutomationSettingHelper> automationSettings;
530 automationSettings = AutomationSettingHelper
531 .getAutomationSettings(att);
533 // Open the settings dialog
534 AutomationSettingDialog dialog = new AutomationSettingDialog(
535 viewer.getTree().getShell(), automationSettings);
537 if (dialog.open() == Window.OK) {
538 String automationType = dialog.getAutomationType();
539 String updateFreq = dialog.getUpdateFrequency();
541 AutoUpdateType autoType = AutoUpdateType
542 .valueOf(automationType);
543 int updFreq = Utility
544 .getUpdateIntervalFromString(updateFreq);
545 int autoId = resourceManager.startAutomation(
546 (SingleResource) resource, att, autoType, updFreq);
548 MessageDialog.openInformation(Display.getDefault()
549 .getActiveShell(), "Automation Status",
550 "Automation start failed!!");
552 viewer.update(element, null);
556 // Stop the automation
557 resourceManager.stopAutomation((SingleResource) resource, att,
558 att.getAutomationId());
559 MessageDialog.openInformation(Display.getDefault()
560 .getActiveShell(), "Automation Status",
561 "Automation stopped.");
562 viewer.update(element, null);