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.resource.AutomationSettingHelper;
24 import oic.simulator.serviceprovider.resource.LocalResourceAttribute;
25 import oic.simulator.serviceprovider.resource.SimulatorResource;
26 import oic.simulator.serviceprovider.utils.Utility;
27 import oic.simulator.serviceprovider.view.dialogs.AutomationSettingDialog;
29 import org.eclipse.jface.dialogs.MessageDialog;
30 import org.eclipse.jface.viewers.CellEditor;
31 import org.eclipse.jface.viewers.CheckboxCellEditor;
32 import org.eclipse.jface.viewers.ComboBoxCellEditor;
33 import org.eclipse.jface.viewers.EditingSupport;
34 import org.eclipse.jface.viewers.TableViewer;
35 import org.eclipse.jface.window.Window;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.custom.CCombo;
38 import org.eclipse.swt.events.ModifyEvent;
39 import org.eclipse.swt.events.ModifyListener;
40 import org.eclipse.swt.widgets.Display;
41 import org.eclipse.swt.widgets.MessageBox;
42 import org.oic.simulator.serviceprovider.AutomationType;
45 * This class provides editing support to the resources attributes table in the
48 public class AttributeEditingSupport {
50 private AttributeValueEditor attValueEditor;
51 private AutomationEditor automationEditor;
53 public AttributeValueEditor createAttributeValueEditor(TableViewer viewer) {
54 attValueEditor = new AttributeValueEditor(viewer);
55 return attValueEditor;
58 public AutomationEditor createAutomationEditor(TableViewer viewer) {
59 automationEditor = new AutomationEditor(viewer);
60 return automationEditor;
63 class AttributeValueEditor extends EditingSupport {
65 private final TableViewer viewer;
66 private LocalResourceAttribute attributeInSelection;
67 private CCombo comboBox;
69 public AttributeValueEditor(TableViewer viewer) {
75 protected boolean canEdit(Object arg0) {
80 protected CellEditor getCellEditor(Object element) {
81 attributeInSelection = (LocalResourceAttribute) element;
83 ResourceManager resourceManager = Activator.getDefault()
84 .getResourceManager();
86 .isAttHasRangeOrAllowedValues(attributeInSelection)) {
90 // CellEditor is not required as the automation is in progress.
91 if (attributeInSelection.isAutomationInProgress()) {
95 String values[] = null;
96 List<String> valueSet = attributeInSelection.getAttValues();
97 values = convertListToStringArray(valueSet);
99 ComboBoxCellEditor comboEditor = new ComboBoxCellEditor(
100 viewer.getTable(), values, SWT.READ_ONLY);
101 comboBox = (CCombo) comboEditor.getControl();
102 comboBox.addModifyListener(new ModifyListener() {
105 public void modifyText(ModifyEvent event) {
106 String oldValue = String.valueOf(attributeInSelection
107 .getAttributeValue());
108 String newValue = comboBox.getText();
109 if (!oldValue.equals(newValue)) {
110 attributeInSelection.setAttributeValue(newValue);
111 MessageBox dialog = new MessageBox(viewer.getTable()
112 .getShell(), SWT.ICON_QUESTION | SWT.OK
114 dialog.setText("Confirm action");
115 dialog.setMessage("Do you want to modify the value?");
116 int retval = dialog.open();
117 if (retval != SWT.OK) {
118 attributeInSelection.setAttributeValue(oldValue);
120 ResourceManager resourceManager;
121 resourceManager = Activator.getDefault()
122 .getResourceManager();
123 SimulatorResource resource = resourceManager
124 .getCurrentResourceInSelection();
125 resourceManager.attributeValueUpdated(resource,
126 attributeInSelection.getAttributeName(),
129 viewer.update(attributeInSelection, null);
130 comboBox.setVisible(false);
138 protected Object getValue(Object element) {
140 LocalResourceAttribute att = (LocalResourceAttribute) element;
141 String valueString = String.valueOf(att.getAttributeValue());
142 List<String> valueSet = att.getAttValues();
143 if (null != valueSet) {
144 indexOfItem = valueSet.indexOf(valueString);
146 if (indexOfItem == -1) {
153 protected void setValue(Object element, Object value) {
154 Object valueObj = attributeInSelection.getAttributeValue();
155 if (null == valueObj)
157 String attValue = String.valueOf(valueObj);
158 ((LocalResourceAttribute) element).setAttributeValue(attValue);
159 viewer.update(element, null);
162 public String[] convertListToStringArray(List<String> valueList) {
164 if (null != valueList && valueList.size() > 0) {
165 strArr = valueList.toArray(new String[1]);
167 strArr = new String[1];
173 class AutomationEditor extends EditingSupport {
175 private final TableViewer viewer;
177 public AutomationEditor(TableViewer viewer) {
179 this.viewer = viewer;
183 protected boolean canEdit(Object arg0) {
188 protected CellEditor getCellEditor(Object element) {
189 // CellEditor is not required as the automation is in progress.
190 ResourceManager resourceManager = Activator.getDefault()
191 .getResourceManager();
192 LocalResourceAttribute att = (LocalResourceAttribute) element;
193 if (!resourceManager.isAttHasRangeOrAllowedValues(att)) {
197 SimulatorResource resource = resourceManager
198 .getCurrentResourceInSelection();
199 if (null != resource && resource.isResourceAutomationInProgress()) {
202 return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
206 protected Object getValue(Object element) {
207 LocalResourceAttribute att = (LocalResourceAttribute) element;
208 return att.isAutomationInProgress();
212 protected void setValue(Object element, Object value) {
213 ResourceManager resourceManager = Activator.getDefault()
214 .getResourceManager();
215 // As automation depends on the current resource in selection, its
216 // presence is being checked.
217 SimulatorResource resource = resourceManager
218 .getCurrentResourceInSelection();
219 if (null == resource) {
223 LocalResourceAttribute att = (LocalResourceAttribute) element;
224 boolean checked = (Boolean) value;
226 // Start the automation
227 // Fetch the settings data
228 List<AutomationSettingHelper> automationSettings;
229 automationSettings = AutomationSettingHelper
230 .getAutomationSettings(att);
232 // Open the settings dialog
233 AutomationSettingDialog dialog = new AutomationSettingDialog(
234 viewer.getTable().getShell(), automationSettings);
236 if (dialog.open() == Window.OK) {
237 String automationType = dialog.getAutomationType();
238 String updateFreq = dialog.getUpdateFrequency();
240 AutomationType autoType = AutomationType
241 .valueOf(automationType);
242 int updFreq = Utility
243 .getUpdateIntervalFromString(updateFreq);
244 int autoId = resourceManager.startAutomation(resource, att,
247 MessageDialog.openInformation(Display.getDefault()
248 .getActiveShell(), "Automation Status",
249 "Automation start failed!!");
251 viewer.update(element, null);
255 // Stop the automation
256 resourceManager.stopAutomation(resource, att,
257 att.getAutomationId());
258 MessageDialog.openInformation(Display.getDefault()
259 .getActiveShell(), "Automation Status",
260 "Automation stopped.");
261 viewer.update(element, null);