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.TextCellEditor;
25 import org.eclipse.jface.viewers.TreeViewer;
26 import org.eclipse.jface.window.Window;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.custom.CCombo;
29 import org.eclipse.swt.events.ModifyEvent;
30 import org.eclipse.swt.events.ModifyListener;
31 import org.eclipse.swt.widgets.Display;
32 import org.eclipse.swt.widgets.MessageBox;
33 import org.eclipse.swt.widgets.Text;
34 import org.eclipse.swt.widgets.Tree;
35 import org.eclipse.swt.widgets.TreeItem;
36 import org.eclipse.ui.IPartListener2;
37 import org.eclipse.ui.IWorkbenchPartReference;
39 import java.util.Date;
40 import java.util.List;
42 import org.oic.simulator.AttributeValue;
43 import org.oic.simulator.AttributeValue.TypeInfo;
44 import org.oic.simulator.AttributeValue.ValueType;
45 import org.oic.simulator.ILogger.Level;
46 import org.oic.simulator.InvalidArgsException;
47 import org.oic.simulator.SimulatorResourceAttribute;
48 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
50 import oic.simulator.serviceprovider.Activator;
51 import oic.simulator.serviceprovider.manager.ResourceManager;
52 import oic.simulator.serviceprovider.model.AttributeElement;
53 import oic.simulator.serviceprovider.model.AutomationSettingHelper;
54 import oic.simulator.serviceprovider.model.Resource;
55 import oic.simulator.serviceprovider.model.ResourceRepresentation;
56 import oic.simulator.serviceprovider.model.SingleResource;
57 import oic.simulator.serviceprovider.utils.AttributeValueBuilder;
58 import oic.simulator.serviceprovider.utils.Utility;
59 import oic.simulator.serviceprovider.view.dialogs.AutomationSettingDialog;
60 import oic.simulator.serviceprovider.view.dialogs.UpdatePrimitiveArrayAttributeDialog;
63 * This class provides editing support to the resources attributes table in the
66 public class AttributeEditingSupport {
68 private AttributeValueEditor attValueEditor;
69 private AutomationEditor automationEditor;
71 private static class SyncValueUpdate {
72 private Boolean valueChangeInProgress;
74 public boolean isValueChangeInProgress() {
75 return valueChangeInProgress;
78 public void setValueChangeInProgress(boolean value) {
79 valueChangeInProgress = value;
83 private final SyncValueUpdate syncValueChange = new SyncValueUpdate();
85 public AttributeValueEditor createAttributeValueEditor(TreeViewer viewer,
86 Boolean nativeUpdateValueCall) {
87 attValueEditor = new AttributeValueEditor(viewer, nativeUpdateValueCall);
88 syncValueChange.setValueChangeInProgress(false);
89 return attValueEditor;
92 public AutomationEditor createAutomationEditor(TreeViewer viewer) {
93 automationEditor = new AutomationEditor(viewer);
94 return automationEditor;
97 class AttributeValueEditor extends EditingSupport {
99 private final TreeViewer viewer;
100 private CCombo comboBox;
102 private Boolean callNativeUpdateValue;
104 public AttributeValueEditor(TreeViewer viewer,
105 Boolean nativeUpdateValueCall) {
107 this.viewer = viewer;
108 callNativeUpdateValue = nativeUpdateValueCall;
109 // Using the part listener to refresh the viewer on various part
111 // If combo list is open, then click events on other parts of the
112 // view or outside the combo should hide the editor.
113 // Refreshing the viewer hides the combo and other editors which are
115 IPartListener2 partListener;
116 partListener = new IPartListener2() {
119 public void partVisible(IWorkbenchPartReference partRef) {
123 public void partOpened(IWorkbenchPartReference partRef) {
127 public void partInputChanged(IWorkbenchPartReference partRef) {
131 public void partHidden(IWorkbenchPartReference partRef) {
135 public void partDeactivated(IWorkbenchPartReference partRef) {
136 String viewId = partRef.getId();
137 if (viewId.equals(AttributeView.VIEW_ID)) {
138 synchronized (syncValueChange) {
145 public void partClosed(IWorkbenchPartReference partRef) {
149 public void partBroughtToTop(IWorkbenchPartReference partRef) {
153 public void partActivated(IWorkbenchPartReference partRef) {
154 String viewId = partRef.getId();
155 if (viewId.equals(AttributeView.VIEW_ID)) {
156 if (null == AttributeValueEditor.this.viewer)
159 Tree tree = AttributeValueEditor.this.viewer.getTree();
160 if (null == tree || tree.isDisposed())
163 AttributeValueEditor.this.viewer.refresh();
169 Activator.getDefault().getWorkbench()
170 .getActiveWorkbenchWindow().getActivePage()
171 .addPartListener(partListener);
172 } catch (Exception e) {
176 .log(Level.ERROR.ordinal(),
178 "There is an error while configuring the listener for UI.\n"
179 + Utility.getSimulatorErrorString(e,
184 public void updateUnSavedData() {
185 if (null == viewer || null == comboBox)
188 Tree tree = viewer.getTree();
189 if (null == tree || tree.isDisposed())
192 TreeItem[] selectedItems = tree.getSelection();
193 if (null == selectedItems || 1 != selectedItems.length)
196 Object element = selectedItems[0].getData();
197 if (null == element || !(element instanceof AttributeElement))
200 AttributeElement attElement = (AttributeElement) element;
201 SimulatorResourceAttribute att = attElement
202 .getSimulatorResourceAttribute();
206 if (Activator.getDefault().getResourceManager()
207 .isAttHasRangeOrAllowedValues(att)) {
212 synchronized (syncValueChange) {
213 if (!syncValueChange.isValueChangeInProgress()) {
215 AttributeValue value = att.value();
219 TypeInfo type = value.typeInfo();
221 if (type.mBaseType == ValueType.RESOURCEMODEL
222 || type.mType == ValueType.ARRAY) {
226 if (null == value.get()) {
230 String oldValue = String.valueOf(Utility
231 .getAttributeValueAsString(value));
233 attElement.setEditLock(true);
234 compareAndUpdateAttribute(oldValue, comboBox.getText(),
235 (AttributeElement) element, att, type);
236 attElement.setEditLock(false);
244 protected boolean canEdit(Object arg0) {
249 protected CellEditor getCellEditor(final Object element) {
250 ResourceManager resourceManager = Activator.getDefault()
251 .getResourceManager();
253 Resource res = resourceManager.getCurrentResourceInSelection();
258 // If selected resource is not a single resource, then editor
259 // support is not required.
260 if (!(res instanceof SingleResource)) {
264 final SimulatorResourceAttribute attribute;
265 if (!(element instanceof AttributeElement)) {
269 final AttributeElement attributeElement = ((AttributeElement) element);
270 attribute = attributeElement.getSimulatorResourceAttribute();
271 if (null == attribute) {
275 // CellEditor is not required as the automation is in progress.
276 if (attributeElement.isAutoUpdateInProgress()) {
280 final AttributeValue val = attribute.value();
285 final TypeInfo type = val.typeInfo();
286 if (type.mBaseType == ValueType.RESOURCEMODEL) {
290 synchronized (syncValueChange) {
291 syncValueChange.setValueChangeInProgress(false);
295 if (type.mType == ValueType.ARRAY) {
296 editor = new TextCellEditor(viewer.getTree());
297 editor.setStyle(SWT.READ_ONLY);
298 final Text txt = (Text) editor.getControl();
299 txt.addModifyListener(new ModifyListener() {
301 public void modifyText(ModifyEvent e) {
302 String currentAttValue = txt.getText();
303 UpdatePrimitiveArrayAttributeDialog dialog = new UpdatePrimitiveArrayAttributeDialog(
304 Display.getDefault().getActiveShell(),
306 if (dialog.open() == Window.OK) {
307 updateAttributeValue(attributeElement, attribute,
308 dialog.getNewValueObj());
310 if (callNativeUpdateValue) {
311 ResourceManager resourceManager;
312 resourceManager = Activator.getDefault()
313 .getResourceManager();
315 Resource resource = resourceManager
316 .getCurrentResourceInSelection();
318 SimulatorResourceAttribute result = getResultantAttribute(attributeElement);
319 if (null == result) {
323 .log(Level.ERROR.ordinal(),
325 "There is an error while updating the attribute value.\n");
329 boolean updated = resourceManager
330 .attributeValueUpdated(
331 (SingleResource) resource,
332 result.name(), result.value());
335 updateAttributeValue(attributeElement,
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 (AttributeElement) 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,
496 (AttributeElement) element, att, type);
497 ((AttributeElement) element).setEditLock(false);
503 public void compareAndUpdateAttribute(String oldValue, String newValue,
504 AttributeElement attElement, SimulatorResourceAttribute att,
506 if (null == oldValue || null == newValue || null == attElement
507 || null == att || null == type) {
511 synchronized (syncValueChange) {
512 syncValueChange.setValueChangeInProgress(true);
515 if (!oldValue.equals(newValue)) {
516 boolean invalid = false;
518 // Get the AttriuteValue from the string
519 AttributeValue attValue = null;
521 attValue = AttributeValueBuilder.build(newValue,
523 } catch (Exception e) {
527 .log(Level.ERROR.ordinal(),
529 "There is an error while creating the new attribute value.\n"
530 + Utility.getSimulatorErrorString(
534 if (null == attValue) {
537 TypeInfo resTypeInfo = attValue.typeInfo();
538 if (type.mDepth != resTypeInfo.mDepth
539 || type.mType != resTypeInfo.mType
540 || type.mBaseType != resTypeInfo.mBaseType) {
545 MessageBox dialog = new MessageBox(viewer.getTree()
546 .getShell(), SWT.ICON_ERROR | SWT.OK);
547 dialog.setText("Invalid Value");
548 dialog.setMessage("Given value is invalid");
552 // To show the new value till decision made.
553 updateAttributeValue(attElement, att, attValue);
554 viewer.update(attElement, null);
556 if (callNativeUpdateValue) {
557 MessageBox dialog = new MessageBox(viewer.getTree()
558 .getShell(), SWT.ICON_QUESTION | SWT.OK
560 dialog.setText("Confirm action");
561 dialog.setMessage("Do you want to modify the value?");
562 int retval = dialog.open();
563 if (retval != SWT.OK) {
565 attValue = AttributeValueBuilder.build(
566 oldValue, type.mBaseType);
567 updateAttributeValue(attElement, att, attValue);
568 } catch (Exception e) {
572 .log(Level.ERROR.ordinal(),
574 "There is an error while updating the attribute value.\n"
576 .getSimulatorErrorString(
580 ResourceManager resourceManager;
581 resourceManager = Activator.getDefault()
582 .getResourceManager();
584 Resource resource = resourceManager
585 .getCurrentResourceInSelection();
587 SimulatorResourceAttribute result = getResultantAttribute(attElement);
588 if (null == result) {
592 .log(Level.ERROR.ordinal(), new Date(),
593 "There is an error while updating the attribute value.\n");
597 boolean updated = resourceManager
598 .attributeValueUpdated(
599 (SingleResource) resource,
600 result.name(), result.value());
603 attValue = AttributeValueBuilder.build(
604 oldValue, type.mBaseType);
605 updateAttributeValue(attElement, att,
607 } catch (Exception e) {
611 .log(Level.ERROR.ordinal(),
613 "There is an error while updating the attribute value.\n"
615 .getSimulatorErrorString(
620 .openInformation(Display.getDefault()
623 "Failed to update the attribute value.");
629 viewer.update(attElement, null);
632 public void updateAttributeValue(AttributeElement attributeElement,
633 SimulatorResourceAttribute att, AttributeValue value) {
634 if (null == attributeElement || null == att || null == value) {
638 attributeElement.getSimulatorResourceAttribute().setValue(value);
640 Object parent = attributeElement.getParent();
641 if (null != parent && parent instanceof AttributeElement) {
642 AttributeElement parentElement = (AttributeElement) parent;
644 parentElement.deepSetChildValue(att);
645 } catch (InvalidArgsException e) {
649 .log(Level.ERROR.ordinal(),
651 "There is an error while updating the attribute value.\n"
652 + Utility.getSimulatorErrorString(
658 public SimulatorResourceAttribute getResultantAttribute(
659 AttributeElement attElement) {
660 if (null == attElement) {
664 SimulatorResourceAttribute result;
665 Object parent = attElement.getParent();
667 while (parent != null && parent instanceof AttributeElement) {
668 attElement = (AttributeElement) parent;
669 parent = ((AttributeElement) parent).getParent();
671 result = attElement.getSimulatorResourceAttribute();
677 private static class AutomationEditor extends EditingSupport {
679 private final TreeViewer viewer;
681 public AutomationEditor(TreeViewer viewer) {
683 this.viewer = viewer;
687 protected boolean canEdit(Object arg0) {
692 protected CellEditor getCellEditor(Object element) {
693 // CellEditor is not required as the automation is in progress.
694 ResourceManager resourceManager = Activator.getDefault()
695 .getResourceManager();
696 Resource resource = resourceManager.getCurrentResourceInSelection();
698 if (null == resource) {
702 if (!(resource instanceof SingleResource)) {
705 if (((SingleResource) resource).isResourceAutomationInProgress()) {
709 SimulatorResourceAttribute att = null;
710 if (element instanceof AttributeElement) {
711 att = ((AttributeElement) element)
712 .getSimulatorResourceAttribute();
719 AttributeValue val = att.value();
724 TypeInfo type = val.typeInfo();
726 if (type.mType == ValueType.RESOURCEMODEL
727 || type.mType == ValueType.ARRAY) {
731 Object parent = ((AttributeElement) element).getParent();
732 if (null != parent && !(parent instanceof ResourceRepresentation)) {
736 if (((AttributeElement) element).isReadOnly()) {
740 return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
744 protected Object getValue(Object element) {
745 if (element instanceof AttributeElement) {
746 return ((AttributeElement) element).isAutoUpdateInProgress();
753 protected void setValue(Object element, Object value) {
754 if (!(element instanceof AttributeElement)) {
758 ResourceManager resourceManager = Activator.getDefault()
759 .getResourceManager();
760 // As automation depends on the current resource in selection, its
761 // presence is being checked.
762 Resource resource = resourceManager.getCurrentResourceInSelection();
763 if (null == resource) {
767 AttributeElement att = (AttributeElement) element;
768 boolean checked = (Boolean) value;
770 // Start the automation
771 // Fetch the settings data
772 List<AutomationSettingHelper> automationSettings;
773 automationSettings = AutomationSettingHelper
774 .getAutomationSettings(att);
776 // Open the settings dialog
777 AutomationSettingDialog dialog = new AutomationSettingDialog(
778 viewer.getTree().getShell(), automationSettings);
780 if (dialog.open() == Window.OK) {
781 String automationType = dialog.getAutomationType();
782 String updateFreq = dialog.getUpdateFrequency();
784 AutoUpdateType autoType = AutoUpdateType
785 .valueOf(automationType);
786 int updFreq = Utility
787 .getUpdateIntervalFromString(updateFreq);
788 int autoId = resourceManager.startAutomation(
789 (SingleResource) resource, att, autoType, updFreq);
791 MessageDialog.openInformation(Display.getDefault()
792 .getActiveShell(), "Automation Status",
793 "Automation start failed!!");
797 // Stop the automation
798 resourceManager.stopAutomation((SingleResource) resource, att,
799 att.getAutoUpdateId());