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.Date;
20 import java.util.List;
22 import oic.simulator.serviceprovider.Activator;
23 import oic.simulator.serviceprovider.manager.ResourceManager;
24 import oic.simulator.serviceprovider.model.AttributeElement;
25 import oic.simulator.serviceprovider.model.AutomationSettingHelper;
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.eclipse.ui.IPartListener2;
50 import org.eclipse.ui.IWorkbenchPartReference;
51 import org.oic.simulator.AttributeProperty;
52 import org.oic.simulator.AttributeValue;
53 import org.oic.simulator.AttributeValue.TypeInfo;
54 import org.oic.simulator.AttributeValue.ValueType;
55 import org.oic.simulator.ILogger.Level;
56 import org.oic.simulator.InvalidArgsException;
57 import org.oic.simulator.SimulatorResourceAttribute;
58 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
61 * This class provides editing support to the resources attributes table in the
64 public class AttributeEditingSupport {
66 private AttributeValueEditor attValueEditor;
67 private AutomationEditor automationEditor;
69 public AttributeValueEditor createAttributeValueEditor(TreeViewer viewer) {
70 attValueEditor = new AttributeValueEditor(viewer);
71 return attValueEditor;
74 public AutomationEditor createAutomationEditor(TreeViewer viewer) {
75 automationEditor = new AutomationEditor(viewer);
76 return automationEditor;
79 class AttributeValueEditor extends EditingSupport {
81 private final TreeViewer viewer;
82 private CCombo comboBox;
84 public AttributeValueEditor(TreeViewer viewer) {
88 // Using the part listener to refresh the viewer on various part
90 // If combo list is open, then click events on other parts of the
91 // view or outside the combo should hide the editor.
92 // Refreshing the viewer hides the combo and other editors which are
94 IPartListener2 partListener;
95 partListener = new IPartListener2() {
98 public void partVisible(IWorkbenchPartReference partRef) {
102 public void partOpened(IWorkbenchPartReference partRef) {
106 public void partInputChanged(IWorkbenchPartReference partRef) {
110 public void partHidden(IWorkbenchPartReference partRef) {
114 public void partDeactivated(IWorkbenchPartReference partRef) {
115 String viewId = partRef.getId();
116 if (viewId.equals(AttributeView.VIEW_ID)) {
122 public void partClosed(IWorkbenchPartReference partRef) {
126 public void partBroughtToTop(IWorkbenchPartReference partRef) {
130 public void partActivated(IWorkbenchPartReference partRef) {
131 String viewId = partRef.getId();
132 if (viewId.equals(AttributeView.VIEW_ID)) {
139 Activator.getDefault().getWorkbench()
140 .getActiveWorkbenchWindow().getActivePage()
141 .addPartListener(partListener);
142 } catch (NullPointerException e) {
146 .log(Level.ERROR.ordinal(),
148 "There is an error while configuring the listener for UI.\n"
149 + Utility.getSimulatorErrorString(e,
154 public void refreshViewer() {
158 Tree tree = viewer.getTree();
159 if (null == tree || tree.isDisposed())
166 protected boolean canEdit(Object arg0) {
171 protected CellEditor getCellEditor(final Object element) {
172 ResourceManager resourceManager = Activator.getDefault()
173 .getResourceManager();
175 Resource res = resourceManager.getCurrentResourceInSelection();
180 // If selected resource is not a single resource, then editor
181 // support is not required.
182 if (!(res instanceof SingleResource)) {
186 final SimulatorResourceAttribute attribute;
187 if (!(element instanceof AttributeElement)) {
191 final AttributeElement attributeElement = ((AttributeElement) element);
192 attribute = attributeElement.getSimulatorResourceAttribute();
193 if (null == attribute) {
197 // CellEditor is not required as the automation is in progress.
198 if (attributeElement.isAutoUpdateInProgress()) {
202 final AttributeValue val = attribute.value();
207 final TypeInfo type = val.typeInfo();
208 if (type.mBaseType == ValueType.RESOURCEMODEL) {
212 AttributeProperty prop = attribute.property();
217 if (!resourceManager.isAttHasRangeOrAllowedValues(attribute)) {
221 String values[] = null;
222 List<String> valueSet = resourceManager
223 .getAllValuesOfAttribute(attribute);
224 values = Utility.convertListToStringArray(valueSet);
226 ComboBoxCellEditor comboEditor;
227 if (type.mType == ValueType.ARRAY) {
228 comboEditor = new ComboBoxCellEditor(viewer.getTree(), values);
230 comboEditor = new ComboBoxCellEditor(viewer.getTree(), values,
233 comboBox = (CCombo) comboEditor.getControl();
234 comboBox.addModifyListener(new ModifyListener() {
237 public void modifyText(ModifyEvent event) {
238 if (type.mType == ValueType.ARRAY) {
241 String oldValue = String.valueOf(Utility
242 .getAttributeValueAsString(val));
243 String newValue = comboBox.getText();
245 attributeElement.setEditLock(true);
246 compareAndUpdateAttribute(oldValue, newValue, element,
248 attributeElement.setEditLock(false);
250 comboBox.setVisible(false);
257 protected Object getValue(Object element) {
259 SimulatorResourceAttribute att = null;
261 if (element instanceof AttributeElement) {
262 att = ((AttributeElement) element)
263 .getSimulatorResourceAttribute();
270 String valueString = Utility.getAttributeValueAsString(att.value());
271 List<String> valueSet = Activator.getDefault().getResourceManager()
272 .getAllValuesOfAttribute(att);
273 if (null != valueSet) {
274 indexOfItem = valueSet.indexOf(valueString);
276 if (indexOfItem == -1) {
283 protected void setValue(Object element, Object value) {
286 public void compareAndUpdateAttribute(String oldValue, String newValue,
287 Object element, SimulatorResourceAttribute att, TypeInfo type) {
288 if (null == oldValue || null == newValue || null == element
289 || null == att || null == type) {
292 if (!oldValue.equals(newValue)) {
293 // Get the AttriuteValue from the string
294 AttributeValue attValue = AttributeValueBuilder.build(newValue,
296 boolean invalid = false;
297 if (null == attValue) {
300 TypeInfo resTypeInfo = attValue.typeInfo();
301 if (type.mDepth != resTypeInfo.mDepth
302 || type.mType != resTypeInfo.mType
303 || type.mBaseType != resTypeInfo.mBaseType) {
308 MessageBox dialog = new MessageBox(viewer.getTree()
309 .getShell(), SWT.ICON_ERROR | SWT.OK);
310 dialog.setText("Invalid Value");
311 dialog.setMessage("Given value is invalid");
314 MessageBox dialog = new MessageBox(viewer.getTree()
315 .getShell(), SWT.ICON_QUESTION | SWT.OK
317 dialog.setText("Confirm action");
318 dialog.setMessage("Do you want to modify the value?");
319 int retval = dialog.open();
320 if (retval != SWT.OK) {
321 attValue = AttributeValueBuilder.build(oldValue,
323 updateAttributeValue(att, attValue);
325 updateAttributeValue(att, attValue);
327 ResourceManager resourceManager;
328 resourceManager = Activator.getDefault()
329 .getResourceManager();
331 Resource resource = resourceManager
332 .getCurrentResourceInSelection();
334 SimulatorResourceAttribute result = getResultantAttribute();
336 boolean updated = resourceManager
337 .attributeValueUpdated(
338 (SingleResource) resource,
339 result.name(), result.value());
341 attValue = AttributeValueBuilder.build(oldValue,
343 updateAttributeValue(att, attValue);
344 MessageDialog.openInformation(Display.getDefault()
345 .getActiveShell(), "Operation failed",
346 "Failed to update the attribute value.");
351 viewer.update(element, null);
354 public void updateAttributeValue(SimulatorResourceAttribute att,
355 AttributeValue value) {
356 IStructuredSelection selection = (IStructuredSelection) viewer
358 if (null == selection) {
362 Object obj = selection.getFirstElement();
367 Tree t = viewer.getTree();
368 TreeItem item = t.getSelection()[0];
373 if (item.getData() instanceof AttributeElement) {
374 AttributeElement attributeElement = (AttributeElement) item
376 attributeElement.getSimulatorResourceAttribute()
379 TreeItem parent = item.getParentItem();
380 if (null != parent) {
381 Object data = parent.getData();
383 ((AttributeElement) data).deepSetChildValue(att);
384 } catch (InvalidArgsException e) {
391 public SimulatorResourceAttribute getResultantAttribute() {
392 IStructuredSelection selection = (IStructuredSelection) viewer
394 if (null == selection) {
398 Object obj = selection.getFirstElement();
403 Tree t = viewer.getTree();
404 TreeItem item = t.getSelection()[0];
409 SimulatorResourceAttribute result = null;
410 TreeItem parent = item.getParentItem();
411 if (null == parent) {
412 Object data = item.getData();
413 result = ((AttributeElement) data)
414 .getSimulatorResourceAttribute();
416 while (parent.getParentItem() != null) {
417 parent = parent.getParentItem();
420 // Parent will point to the top-level attribute of type
421 Object data = parent.getData();
422 result = ((AttributeElement) data)
423 .getSimulatorResourceAttribute();
430 class AutomationEditor extends EditingSupport {
432 private final TreeViewer viewer;
434 public AutomationEditor(TreeViewer viewer) {
436 this.viewer = viewer;
440 protected boolean canEdit(Object arg0) {
445 protected CellEditor getCellEditor(Object element) {
446 // CellEditor is not required as the automation is in progress.
447 ResourceManager resourceManager = Activator.getDefault()
448 .getResourceManager();
449 Resource resource = resourceManager.getCurrentResourceInSelection();
451 if (null == resource) {
455 if (!(resource instanceof SingleResource)) {
458 if (((SingleResource) resource).isResourceAutomationInProgress()) {
462 SimulatorResourceAttribute att = null;
463 if (element instanceof AttributeElement) {
464 att = ((AttributeElement) element)
465 .getSimulatorResourceAttribute();
472 AttributeValue val = att.value();
477 TypeInfo type = val.typeInfo();
479 if (type.mType == ValueType.RESOURCEMODEL
480 || type.mType == ValueType.ARRAY) {
484 Object parent = ((AttributeElement) element).getParent();
485 if (null != parent && !(parent instanceof ResourceRepresentation)) {
489 if (((AttributeElement) element).isReadOnly()) {
493 return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
497 protected Object getValue(Object element) {
498 if (element instanceof AttributeElement) {
499 return ((AttributeElement) element).isAutoUpdateInProgress();
506 protected void setValue(Object element, Object value) {
507 if (!(element instanceof AttributeElement)) {
511 ResourceManager resourceManager = Activator.getDefault()
512 .getResourceManager();
513 // As automation depends on the current resource in selection, its
514 // presence is being checked.
515 Resource resource = resourceManager.getCurrentResourceInSelection();
516 if (null == resource) {
520 AttributeElement att = (AttributeElement) element;
521 boolean checked = (Boolean) value;
523 // Start the automation
524 // Fetch the settings data
525 List<AutomationSettingHelper> automationSettings;
526 automationSettings = AutomationSettingHelper
527 .getAutomationSettings(att);
529 // Open the settings dialog
530 AutomationSettingDialog dialog = new AutomationSettingDialog(
531 viewer.getTree().getShell(), automationSettings);
533 if (dialog.open() == Window.OK) {
534 String automationType = dialog.getAutomationType();
535 String updateFreq = dialog.getUpdateFrequency();
537 AutoUpdateType autoType = AutoUpdateType
538 .valueOf(automationType);
539 int updFreq = Utility
540 .getUpdateIntervalFromString(updateFreq);
541 int autoId = resourceManager.startAutomation(
542 (SingleResource) resource, att, autoType, updFreq);
544 MessageDialog.openInformation(Display.getDefault()
545 .getActiveShell(), "Automation Status",
546 "Automation start failed!!");
550 // Stop the automation
551 resourceManager.stopAutomation((SingleResource) resource, att,
552 att.getAutoUpdateId());