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 // CellEditor is not required as the automation is in progress.
84 if (attributeInSelection.isAutomationInProgress()) {
88 String values[] = null;
89 List<String> valueSet = attributeInSelection.getAttValues();
90 values = convertListToStringArray(valueSet);
92 ComboBoxCellEditor comboEditor = new ComboBoxCellEditor(
93 viewer.getTable(), values, SWT.READ_ONLY);
94 comboBox = (CCombo) comboEditor.getControl();
95 comboBox.addModifyListener(new ModifyListener() {
98 public void modifyText(ModifyEvent event) {
99 String oldValue = String.valueOf(attributeInSelection
100 .getAttributeValue());
101 String newValue = comboBox.getText();
102 if (!oldValue.equals(newValue)) {
103 attributeInSelection.setAttributeValue(newValue);
104 MessageBox dialog = new MessageBox(viewer.getTable()
105 .getShell(), SWT.ICON_QUESTION | SWT.OK
107 dialog.setText("Confirm action");
108 dialog.setMessage("Do you want to modify the value?");
109 int retval = dialog.open();
110 if (retval != SWT.OK) {
111 attributeInSelection.setAttributeValue(oldValue);
113 ResourceManager resourceManager;
114 resourceManager = Activator.getDefault()
115 .getResourceManager();
116 SimulatorResource resource = resourceManager
117 .getCurrentResourceInSelection();
118 resourceManager.attributeValueUpdated(resource,
119 attributeInSelection.getAttributeName(),
122 viewer.update(attributeInSelection, null);
123 comboBox.setVisible(false);
131 protected Object getValue(Object element) {
133 LocalResourceAttribute att = (LocalResourceAttribute) element;
134 String valueString = String.valueOf(att.getAttributeValue());
135 List<String> valueSet = att.getAttValues();
136 if (null != valueSet) {
137 indexOfItem = valueSet.indexOf(valueString);
139 if (indexOfItem == -1) {
146 protected void setValue(Object element, Object value) {
147 Object valueObj = attributeInSelection.getAttributeValue();
148 if (null == valueObj)
150 String attValue = String.valueOf(valueObj);
151 ((LocalResourceAttribute) element).setAttributeValue(attValue);
152 viewer.update(element, null);
155 public String[] convertListToStringArray(List<String> valueList) {
157 if (null != valueList && valueList.size() > 0) {
158 strArr = valueList.toArray(new String[1]);
160 strArr = new String[1];
166 class AutomationEditor extends EditingSupport {
168 private final TableViewer viewer;
170 public AutomationEditor(TableViewer viewer) {
172 this.viewer = viewer;
176 protected boolean canEdit(Object arg0) {
181 protected CellEditor getCellEditor(Object element) {
182 // CellEditor is not required as the automation is in progress.
183 ResourceManager resourceManager = Activator.getDefault()
184 .getResourceManager();
185 SimulatorResource resource = resourceManager
186 .getCurrentResourceInSelection();
187 if (null != resource && resource.isResourceAutomationInProgress()) {
190 return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
194 protected Object getValue(Object element) {
195 LocalResourceAttribute att = (LocalResourceAttribute) element;
196 return att.isAutomationInProgress();
200 protected void setValue(Object element, Object value) {
201 ResourceManager resourceManager = Activator.getDefault()
202 .getResourceManager();
203 // As automation depends on the current resource in selection, its
204 // presence is being checked.
205 SimulatorResource resource = resourceManager
206 .getCurrentResourceInSelection();
207 if (null == resource) {
211 LocalResourceAttribute att = (LocalResourceAttribute) element;
212 boolean checked = (Boolean) value;
214 // Start the automation
215 // Fetch the settings data
216 List<AutomationSettingHelper> automationSettings;
217 automationSettings = AutomationSettingHelper
218 .getAutomationSettings(att);
220 // Open the settings dialog
221 AutomationSettingDialog dialog = new AutomationSettingDialog(
222 viewer.getTable().getShell(), automationSettings);
224 if (dialog.open() == Window.OK) {
225 String automationType = dialog.getAutomationType();
226 String updateFreq = dialog.getUpdateFrequency();
228 AutomationType autoType = AutomationType
229 .valueOf(automationType);
230 int updFreq = Utility
231 .getUpdateIntervalFromString(updateFreq);
232 int autoId = resourceManager.startAutomation(resource, att,
235 MessageDialog.openInformation(Display.getDefault()
236 .getActiveShell(), "Automation Status",
237 "Automation start failed!!");
239 viewer.update(element, null);
243 // Stop the automation
244 resourceManager.stopAutomation(resource, att,
245 att.getAutomationId());
246 MessageDialog.openInformation(Display.getDefault()
247 .getActiveShell(), "Automation Status",
248 "Automation stopped.");
249 viewer.update(element, null);