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
183 if (!(res instanceof SingleResource)) {
187 final SimulatorResourceAttribute attribute;
188 if (!(element instanceof AttributeElement)) {
192 final AttributeElement attributeElement = ((AttributeElement) element);
193 attribute = attributeElement.getSimulatorResourceAttribute();
194 if (null == attribute) {
198 // CellEditor is not required as the automation is in progress.
199 if (attributeElement.isAutoUpdateInProgress()) {
203 final AttributeValue val = attribute.value();
208 final TypeInfo type = val.typeInfo();
209 if (type.mBaseType == ValueType.RESOURCEMODEL) {
213 AttributeProperty prop = attribute.property();
218 if (!resourceManager.isAttHasRangeOrAllowedValues(attribute)) {
222 String values[] = null;
223 List<String> valueSet = resourceManager
224 .getAllValuesOfAttribute(attribute);
225 values = convertListToStringArray(valueSet);
227 ComboBoxCellEditor comboEditor;
228 if (type.mType == ValueType.ARRAY) {
229 comboEditor = new ComboBoxCellEditor(viewer.getTree(), values);
231 comboEditor = new ComboBoxCellEditor(viewer.getTree(), values,
234 comboBox = (CCombo) comboEditor.getControl();
235 comboBox.addModifyListener(new ModifyListener() {
238 public void modifyText(ModifyEvent event) {
239 if (type.mType == ValueType.ARRAY) {
242 String oldValue = String.valueOf(Utility
243 .getAttributeValueAsString(val));
244 String newValue = comboBox.getText();
246 attributeElement.setEditLock(true);
247 compareAndUpdateAttribute(oldValue, newValue, element,
249 attributeElement.setEditLock(false);
251 comboBox.setVisible(false);
258 protected Object getValue(Object element) {
260 SimulatorResourceAttribute att = null;
262 if (element instanceof AttributeElement) {
263 att = ((AttributeElement) element)
264 .getSimulatorResourceAttribute();
271 String valueString = Utility.getAttributeValueAsString(att.value());
272 List<String> valueSet = Activator.getDefault().getResourceManager()
273 .getAllValuesOfAttribute(att);
274 if (null != valueSet) {
275 indexOfItem = valueSet.indexOf(valueString);
277 if (indexOfItem == -1) {
284 protected void setValue(Object element, Object value) {
287 public void compareAndUpdateAttribute(String oldValue, String newValue,
288 Object element, SimulatorResourceAttribute att, TypeInfo type) {
289 if (null == oldValue || null == newValue || null == element
290 || null == att || null == type) {
293 if (!oldValue.equals(newValue)) {
294 // Get the AttriuteValue from the string
295 AttributeValue attValue = AttributeValueBuilder.build(newValue,
297 boolean invalid = false;
298 if (null == attValue) {
301 TypeInfo resTypeInfo = attValue.typeInfo();
302 if (type.mDepth != resTypeInfo.mDepth
303 || type.mType != resTypeInfo.mType
304 || type.mBaseType != resTypeInfo.mBaseType) {
309 MessageBox dialog = new MessageBox(viewer.getTree()
310 .getShell(), SWT.ICON_ERROR | SWT.OK);
311 dialog.setText("Invalid Value");
312 dialog.setMessage("Given value is invalid");
315 MessageBox dialog = new MessageBox(viewer.getTree()
316 .getShell(), SWT.ICON_QUESTION | SWT.OK
318 dialog.setText("Confirm action");
319 dialog.setMessage("Do you want to modify the value?");
320 int retval = dialog.open();
321 if (retval != SWT.OK) {
322 attValue = AttributeValueBuilder.build(oldValue,
324 updateAttributeValue(att, attValue);
326 updateAttributeValue(att, attValue);
328 ResourceManager resourceManager;
329 resourceManager = Activator.getDefault()
330 .getResourceManager();
332 Resource resource = resourceManager
333 .getCurrentResourceInSelection();
335 SimulatorResourceAttribute result = getResultantAttribute();
337 boolean updated = resourceManager
338 .attributeValueUpdated(
339 (SingleResource) resource,
340 result.name(), result.value());
342 attValue = AttributeValueBuilder.build(oldValue,
344 updateAttributeValue(att, attValue);
345 MessageDialog.openInformation(Display.getDefault()
346 .getActiveShell(), "Operation failed",
347 "Failed to update the attribute value.");
352 viewer.update(element, null);
355 public String[] convertListToStringArray(List<String> valueList) {
357 if (null != valueList && valueList.size() > 0) {
358 strArr = valueList.toArray(new String[1]);
360 strArr = new String[1];
365 public void updateAttributeValue(SimulatorResourceAttribute att,
366 AttributeValue value) {
367 IStructuredSelection selection = (IStructuredSelection) viewer
369 if (null == selection) {
373 Object obj = selection.getFirstElement();
378 Tree t = viewer.getTree();
379 TreeItem item = t.getSelection()[0];
384 if (item.getData() instanceof AttributeElement) {
385 AttributeElement attributeElement = (AttributeElement) item
387 attributeElement.getSimulatorResourceAttribute()
390 TreeItem parent = item.getParentItem();
391 if (null != parent) {
392 Object data = parent.getData();
394 ((AttributeElement) data).deepSetChildValue(att);
395 } catch (InvalidArgsException e) {
402 public SimulatorResourceAttribute getResultantAttribute() {
403 IStructuredSelection selection = (IStructuredSelection) viewer
405 if (null == selection) {
409 Object obj = selection.getFirstElement();
414 Tree t = viewer.getTree();
415 TreeItem item = t.getSelection()[0];
420 SimulatorResourceAttribute result = null;
421 TreeItem parent = item.getParentItem();
422 if (null == parent) {
423 Object data = item.getData();
424 result = ((AttributeElement) data)
425 .getSimulatorResourceAttribute();
427 while (parent.getParentItem() != null) {
428 parent = parent.getParentItem();
431 // Parent will point to the top-level attribute of type
432 Object data = parent.getData();
433 result = ((AttributeElement) data)
434 .getSimulatorResourceAttribute();
441 class AutomationEditor extends EditingSupport {
443 private final TreeViewer viewer;
445 public AutomationEditor(TreeViewer viewer) {
447 this.viewer = viewer;
451 protected boolean canEdit(Object arg0) {
456 protected CellEditor getCellEditor(Object element) {
457 // CellEditor is not required as the automation is in progress.
458 ResourceManager resourceManager = Activator.getDefault()
459 .getResourceManager();
460 Resource resource = resourceManager.getCurrentResourceInSelection();
462 if (null == resource) {
466 if (!(resource instanceof SingleResource)) {
469 if (((SingleResource) resource).isResourceAutomationInProgress()) {
473 SimulatorResourceAttribute att = null;
474 if (element instanceof AttributeElement) {
475 att = ((AttributeElement) element)
476 .getSimulatorResourceAttribute();
483 AttributeValue val = att.value();
488 TypeInfo type = val.typeInfo();
490 if (type.mType == ValueType.RESOURCEMODEL
491 || type.mType == ValueType.ARRAY) {
495 Object parent = ((AttributeElement) element).getParent();
496 if (null != parent && !(parent instanceof ResourceRepresentation)) {
500 if (((AttributeElement) element).isReadOnly()) {
504 return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
508 protected Object getValue(Object element) {
509 if (element instanceof AttributeElement) {
510 return ((AttributeElement) element).isAutoUpdateInProgress();
517 protected void setValue(Object element, Object value) {
518 if (!(element instanceof AttributeElement)) {
522 ResourceManager resourceManager = Activator.getDefault()
523 .getResourceManager();
524 // As automation depends on the current resource in selection, its
525 // presence is being checked.
526 Resource resource = resourceManager.getCurrentResourceInSelection();
527 if (null == resource) {
531 AttributeElement att = (AttributeElement) element;
532 boolean checked = (Boolean) value;
534 // Start the automation
535 // Fetch the settings data
536 List<AutomationSettingHelper> automationSettings;
537 automationSettings = AutomationSettingHelper
538 .getAutomationSettings(att);
540 // Open the settings dialog
541 AutomationSettingDialog dialog = new AutomationSettingDialog(
542 viewer.getTree().getShell(), automationSettings);
544 if (dialog.open() == Window.OK) {
545 String automationType = dialog.getAutomationType();
546 String updateFreq = dialog.getUpdateFrequency();
548 AutoUpdateType autoType = AutoUpdateType
549 .valueOf(automationType);
550 int updFreq = Utility
551 .getUpdateIntervalFromString(updateFreq);
552 int autoId = resourceManager.startAutomation(
553 (SingleResource) resource, att, autoType, updFreq);
555 MessageDialog.openInformation(Display.getDefault()
556 .getActiveShell(), "Automation Status",
557 "Automation start failed!!");
561 // Stop the automation
562 resourceManager.stopAutomation((SingleResource) resource, att,
563 att.getAutoUpdateId());
564 MessageDialog.openInformation(Display.getDefault()
565 .getActiveShell(), "Automation Status",
566 "Automation stopped.");