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 org.eclipse.jface.dialogs.MessageDialog;
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 import org.eclipse.ui.IPartListener2;
38 import org.eclipse.ui.IWorkbenchPartReference;
40 import java.util.Date;
41 import java.util.List;
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 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
51 import oic.simulator.serviceprovider.Activator;
52 import oic.simulator.serviceprovider.manager.ResourceManager;
53 import oic.simulator.serviceprovider.model.AttributeElement;
54 import oic.simulator.serviceprovider.model.AutomationSettingHelper;
55 import oic.simulator.serviceprovider.model.Resource;
56 import oic.simulator.serviceprovider.model.ResourceRepresentation;
57 import oic.simulator.serviceprovider.model.SingleResource;
58 import oic.simulator.serviceprovider.utils.AttributeValueBuilder;
59 import oic.simulator.serviceprovider.utils.Utility;
60 import oic.simulator.serviceprovider.view.dialogs.AutomationSettingDialog;
61 import oic.simulator.serviceprovider.view.dialogs.UpdatePrimitiveArrayAttributeDialog;
64 * This class provides editing support to the resources attributes table in the
67 public class AttributeEditingSupport {
69 private AttributeValueEditor attValueEditor;
70 private AutomationEditor automationEditor;
72 class SyncValueUpdate {
73 private Boolean valueChangeInProgress;
75 public boolean isValueChangeInProgress() {
76 return valueChangeInProgress;
79 public void setValueChangeInProgress(boolean value) {
80 valueChangeInProgress = value;
84 private final SyncValueUpdate syncValueChange = new SyncValueUpdate();
86 public AttributeValueEditor createAttributeValueEditor(TreeViewer viewer,
87 Boolean nativeUpdateValueCall) {
88 attValueEditor = new AttributeValueEditor(viewer, nativeUpdateValueCall);
89 syncValueChange.setValueChangeInProgress(false);
90 return attValueEditor;
93 public AutomationEditor createAutomationEditor(TreeViewer viewer) {
94 automationEditor = new AutomationEditor(viewer);
95 return automationEditor;
98 class AttributeValueEditor extends EditingSupport {
100 private final TreeViewer viewer;
101 private CCombo comboBox;
103 private Boolean callNativeUpdateValue;
105 public AttributeValueEditor(TreeViewer viewer,
106 Boolean nativeUpdateValueCall) {
108 this.viewer = viewer;
109 callNativeUpdateValue = nativeUpdateValueCall;
110 // Using the part listener to refresh the viewer on various part
112 // If combo list is open, then click events on other parts of the
113 // view or outside the combo should hide the editor.
114 // Refreshing the viewer hides the combo and other editors which are
116 IPartListener2 partListener;
117 partListener = new IPartListener2() {
120 public void partVisible(IWorkbenchPartReference partRef) {
124 public void partOpened(IWorkbenchPartReference partRef) {
128 public void partInputChanged(IWorkbenchPartReference partRef) {
132 public void partHidden(IWorkbenchPartReference partRef) {
136 public void partDeactivated(IWorkbenchPartReference partRef) {
137 String viewId = partRef.getId();
138 if (viewId.equals(AttributeView.VIEW_ID)) {
139 synchronized (syncValueChange) {
146 public void partClosed(IWorkbenchPartReference partRef) {
150 public void partBroughtToTop(IWorkbenchPartReference partRef) {
154 public void partActivated(IWorkbenchPartReference partRef) {
155 String viewId = partRef.getId();
156 if (viewId.equals(AttributeView.VIEW_ID)) {
157 if (null == AttributeValueEditor.this.viewer)
160 Tree tree = AttributeValueEditor.this.viewer.getTree();
161 if (null == tree || tree.isDisposed())
164 AttributeValueEditor.this.viewer.refresh();
170 Activator.getDefault().getWorkbench()
171 .getActiveWorkbenchWindow().getActivePage()
172 .addPartListener(partListener);
173 } catch (Exception e) {
177 .log(Level.ERROR.ordinal(),
179 "There is an error while configuring the listener for UI.\n"
180 + Utility.getSimulatorErrorString(e,
185 public void updateUnSavedData() {
186 if (null == viewer || null == comboBox)
189 Tree tree = viewer.getTree();
190 if (null == tree || tree.isDisposed())
193 TreeItem[] selectedItems = tree.getSelection();
194 if (null == selectedItems || 1 != selectedItems.length)
197 Object element = selectedItems[0].getData();
198 if (null == element || !(element instanceof AttributeElement))
201 AttributeElement attElement = (AttributeElement) element;
202 SimulatorResourceAttribute att = attElement
203 .getSimulatorResourceAttribute();
207 if (Activator.getDefault().getResourceManager()
208 .isAttHasRangeOrAllowedValues(att)) {
213 synchronized (syncValueChange) {
214 if (!syncValueChange.isValueChangeInProgress()) {
216 AttributeValue value = att.value();
220 TypeInfo type = value.typeInfo();
222 if (type.mBaseType == ValueType.RESOURCEMODEL
223 || type.mType == ValueType.ARRAY) {
227 if (null == value.get()) {
231 String oldValue = String.valueOf(Utility
232 .getAttributeValueAsString(value));
234 attElement.setEditLock(true);
235 compareAndUpdateAttribute(oldValue, comboBox.getText(),
237 attElement.setEditLock(false);
245 protected boolean canEdit(Object arg0) {
250 protected CellEditor getCellEditor(final Object element) {
251 ResourceManager resourceManager = Activator.getDefault()
252 .getResourceManager();
254 Resource res = resourceManager.getCurrentResourceInSelection();
259 // If selected resource is not a single resource, then editor
260 // support is not required.
261 if (!(res instanceof SingleResource)) {
265 final SimulatorResourceAttribute attribute;
266 if (!(element instanceof AttributeElement)) {
270 final AttributeElement attributeElement = ((AttributeElement) element);
271 attribute = attributeElement.getSimulatorResourceAttribute();
272 if (null == attribute) {
276 // CellEditor is not required as the automation is in progress.
277 if (attributeElement.isAutoUpdateInProgress()) {
281 final AttributeValue val = attribute.value();
286 final TypeInfo type = val.typeInfo();
287 if (type.mBaseType == ValueType.RESOURCEMODEL) {
291 synchronized (syncValueChange) {
292 syncValueChange.setValueChangeInProgress(false);
296 if (type.mType == ValueType.ARRAY) {
297 editor = new TextCellEditor(viewer.getTree());
298 editor.setStyle(SWT.READ_ONLY);
299 final Text txt = (Text) editor.getControl();
300 txt.addModifyListener(new ModifyListener() {
302 public void modifyText(ModifyEvent e) {
303 String currentAttValue = txt.getText();
304 UpdatePrimitiveArrayAttributeDialog dialog = new UpdatePrimitiveArrayAttributeDialog(
305 Display.getDefault().getActiveShell(),
307 if (dialog.open() == Window.OK) {
308 updateAttributeValue(attribute,
309 dialog.getNewValueObj());
311 if (callNativeUpdateValue) {
312 ResourceManager resourceManager;
313 resourceManager = Activator.getDefault()
314 .getResourceManager();
316 Resource resource = resourceManager
317 .getCurrentResourceInSelection();
319 SimulatorResourceAttribute result = getResultantAttribute();
320 if (null == result) {
324 .log(Level.ERROR.ordinal(),
326 "There is an error while updating the attribute value.\n");
330 boolean updated = resourceManager
331 .attributeValueUpdated(
332 (SingleResource) resource,
333 result.name(), result.value());
336 updateAttributeValue(attribute,
337 AttributeValueBuilder.build(
340 } catch (Exception ex) {
344 .log(Level.ERROR.ordinal(),
346 "There is an error while updating the attribute value.\n"
348 .getSimulatorErrorString(
353 .openInformation(Display
357 "Failed to update the attribute value.");
362 // Update the viewer in a separate UI thread.
363 Display.getDefault().asyncExec(new Runnable() {
366 viewer.refresh(element, true);
372 String values[] = null;
373 List<String> valueSet = resourceManager
374 .getAllValuesOfAttribute(attribute);
375 values = Utility.convertListToStringArray(valueSet);
377 boolean hasValueSet = resourceManager
378 .isAttHasRangeOrAllowedValues(attribute);
381 editor = new ComboBoxCellEditor(viewer.getTree(), values,
384 editor = new ComboBoxCellEditor(viewer.getTree(), values);
387 comboBox = (CCombo) editor.getControl();
389 comboBox.addModifyListener(new ModifyListener() {
392 public void modifyText(ModifyEvent event) {
393 String oldValue = String.valueOf(Utility
394 .getAttributeValueAsString(val));
395 if (null == oldValue) {
398 String newValue = comboBox.getText();
400 attributeElement.setEditLock(true);
401 compareAndUpdateAttribute(oldValue, newValue,
402 element, attribute, type);
403 attributeElement.setEditLock(false);
405 comboBox.setVisible(false);
414 protected Object getValue(Object element) {
416 SimulatorResourceAttribute att = null;
418 if (element instanceof AttributeElement) {
419 att = ((AttributeElement) element)
420 .getSimulatorResourceAttribute();
427 final AttributeValue val = att.value();
432 final TypeInfo type = val.typeInfo();
433 if (type.mBaseType == ValueType.RESOURCEMODEL) {
437 String valueString = Utility.getAttributeValueAsString(att.value());
438 if (null == valueString) {
442 if (type.mType == ValueType.ARRAY) {
446 List<String> valueSet = Activator.getDefault().getResourceManager()
447 .getAllValuesOfAttribute(att);
448 if (null != valueSet) {
449 indexOfItem = valueSet.indexOf(valueString);
451 if (indexOfItem == -1) {
458 protected void setValue(Object element, Object value) {
459 synchronized (syncValueChange) {
460 if (syncValueChange.isValueChangeInProgress()) {
464 SimulatorResourceAttribute att = null;
465 if (element instanceof AttributeElement) {
466 att = ((AttributeElement) element)
467 .getSimulatorResourceAttribute();
474 if (Activator.getDefault().getResourceManager()
475 .isAttHasRangeOrAllowedValues(att)) {
479 AttributeValue val = att.value();
484 TypeInfo type = val.typeInfo();
486 String oldValue = String.valueOf(Utility
487 .getAttributeValueAsString(val));
488 if (null == oldValue) {
492 String newValue = comboBox.getText();
494 ((AttributeElement) element).setEditLock(true);
495 compareAndUpdateAttribute(oldValue, newValue, element, att,
497 ((AttributeElement) element).setEditLock(false);
503 public void compareAndUpdateAttribute(String oldValue, String newValue,
504 Object element, SimulatorResourceAttribute att, TypeInfo type) {
505 if (null == oldValue || null == newValue || null == element
506 || null == att || null == type) {
510 synchronized (syncValueChange) {
511 syncValueChange.setValueChangeInProgress(true);
514 if (!oldValue.equals(newValue)) {
515 boolean invalid = false;
517 // Get the AttriuteValue from the string
518 AttributeValue attValue = null;
520 attValue = AttributeValueBuilder.build(newValue,
522 } catch (Exception e) {
526 .log(Level.ERROR.ordinal(),
528 "There is an error while creating the new attribute value.\n"
529 + Utility.getSimulatorErrorString(
533 if (null == attValue) {
536 TypeInfo resTypeInfo = attValue.typeInfo();
537 if (type.mDepth != resTypeInfo.mDepth
538 || type.mType != resTypeInfo.mType
539 || type.mBaseType != resTypeInfo.mBaseType) {
544 MessageBox dialog = new MessageBox(viewer.getTree()
545 .getShell(), SWT.ICON_ERROR | SWT.OK);
546 dialog.setText("Invalid Value");
547 dialog.setMessage("Given value is invalid");
551 // To show the new value till decision made.
552 updateAttributeValue(att, attValue);
553 viewer.update(element, null);
555 if (callNativeUpdateValue) {
556 MessageBox dialog = new MessageBox(viewer.getTree()
557 .getShell(), SWT.ICON_QUESTION | SWT.OK
559 dialog.setText("Confirm action");
560 dialog.setMessage("Do you want to modify the value?");
561 int retval = dialog.open();
562 if (retval != SWT.OK) {
564 attValue = AttributeValueBuilder.build(
565 oldValue, type.mBaseType);
566 updateAttributeValue(att, attValue);
567 } catch (Exception e) {
571 .log(Level.ERROR.ordinal(),
573 "There is an error while updating the attribute value.\n"
575 .getSimulatorErrorString(
579 ResourceManager resourceManager;
580 resourceManager = Activator.getDefault()
581 .getResourceManager();
583 Resource resource = resourceManager
584 .getCurrentResourceInSelection();
586 SimulatorResourceAttribute result = getResultantAttribute();
587 if (null == result) {
591 .log(Level.ERROR.ordinal(), new Date(),
592 "There is an error while updating the attribute value.\n");
596 boolean updated = resourceManager
597 .attributeValueUpdated(
598 (SingleResource) resource,
599 result.name(), result.value());
602 attValue = AttributeValueBuilder.build(
603 oldValue, type.mBaseType);
604 updateAttributeValue(att, attValue);
605 } catch (Exception e) {
609 .log(Level.ERROR.ordinal(),
611 "There is an error while updating the attribute value.\n"
613 .getSimulatorErrorString(
618 .openInformation(Display.getDefault()
621 "Failed to update the attribute value.");
627 viewer.update(element, null);
630 public void updateAttributeValue(SimulatorResourceAttribute att,
631 AttributeValue value) {
632 if (null == att || null == value) {
636 IStructuredSelection selection = (IStructuredSelection) viewer
638 if (null == selection) {
642 Object obj = selection.getFirstElement();
647 Tree t = viewer.getTree();
648 TreeItem item = t.getSelection()[0];
653 if (item.getData() instanceof AttributeElement) {
654 AttributeElement attributeElement = (AttributeElement) item
656 attributeElement.getSimulatorResourceAttribute()
659 TreeItem parent = item.getParentItem();
660 if (null != parent) {
661 Object data = parent.getData();
663 ((AttributeElement) data).deepSetChildValue(att);
664 } catch (InvalidArgsException e) {
668 .log(Level.ERROR.ordinal(),
670 "There is an error while updating the attribute value.\n"
672 .getSimulatorErrorString(
679 public SimulatorResourceAttribute getResultantAttribute() {
680 IStructuredSelection selection = (IStructuredSelection) viewer
682 if (null == selection) {
686 Object obj = selection.getFirstElement();
691 Tree t = viewer.getTree();
692 TreeItem item = t.getSelection()[0];
697 SimulatorResourceAttribute result;
698 TreeItem parent = item.getParentItem();
699 if (null == parent) {
700 Object data = item.getData();
701 result = ((AttributeElement) data)
702 .getSimulatorResourceAttribute();
704 while (parent.getParentItem() != null) {
705 parent = parent.getParentItem();
708 // Parent will point to the top-level attribute of type
709 Object data = parent.getData();
710 result = ((AttributeElement) data)
711 .getSimulatorResourceAttribute();
718 class AutomationEditor extends EditingSupport {
720 private final TreeViewer viewer;
722 public AutomationEditor(TreeViewer viewer) {
724 this.viewer = viewer;
728 protected boolean canEdit(Object arg0) {
733 protected CellEditor getCellEditor(Object element) {
734 // CellEditor is not required as the automation is in progress.
735 ResourceManager resourceManager = Activator.getDefault()
736 .getResourceManager();
737 Resource resource = resourceManager.getCurrentResourceInSelection();
739 if (null == resource) {
743 if (!(resource instanceof SingleResource)) {
746 if (((SingleResource) resource).isResourceAutomationInProgress()) {
750 SimulatorResourceAttribute att = null;
751 if (element instanceof AttributeElement) {
752 att = ((AttributeElement) element)
753 .getSimulatorResourceAttribute();
760 AttributeValue val = att.value();
765 TypeInfo type = val.typeInfo();
767 if (type.mType == ValueType.RESOURCEMODEL
768 || type.mType == ValueType.ARRAY) {
772 Object parent = ((AttributeElement) element).getParent();
773 if (null != parent && !(parent instanceof ResourceRepresentation)) {
777 if (((AttributeElement) element).isReadOnly()) {
781 return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
785 protected Object getValue(Object element) {
786 if (element instanceof AttributeElement) {
787 return ((AttributeElement) element).isAutoUpdateInProgress();
794 protected void setValue(Object element, Object value) {
795 if (!(element instanceof AttributeElement)) {
799 ResourceManager resourceManager = Activator.getDefault()
800 .getResourceManager();
801 // As automation depends on the current resource in selection, its
802 // presence is being checked.
803 Resource resource = resourceManager.getCurrentResourceInSelection();
804 if (null == resource) {
808 AttributeElement att = (AttributeElement) element;
809 boolean checked = (Boolean) value;
811 // Start the automation
812 // Fetch the settings data
813 List<AutomationSettingHelper> automationSettings;
814 automationSettings = AutomationSettingHelper
815 .getAutomationSettings(att);
817 // Open the settings dialog
818 AutomationSettingDialog dialog = new AutomationSettingDialog(
819 viewer.getTree().getShell(), automationSettings);
821 if (dialog.open() == Window.OK) {
822 String automationType = dialog.getAutomationType();
823 String updateFreq = dialog.getUpdateFrequency();
825 AutoUpdateType autoType = AutoUpdateType
826 .valueOf(automationType);
827 int updFreq = Utility
828 .getUpdateIntervalFromString(updateFreq);
829 int autoId = resourceManager.startAutomation(
830 (SingleResource) resource, att, autoType, updFreq);
832 MessageDialog.openInformation(Display.getDefault()
833 .getActiveShell(), "Automation Status",
834 "Automation start failed!!");
838 // Stop the automation
839 resourceManager.stopAutomation((SingleResource) resource, att,
840 att.getAutoUpdateId());