1 package oic.simulator.serviceprovider.view;
5 import oic.simulator.serviceprovider.Activator;
6 import oic.simulator.serviceprovider.manager.ResourceManager;
7 import oic.simulator.serviceprovider.resource.AutomationSettingHelper;
8 import oic.simulator.serviceprovider.resource.LocalResourceAttribute;
9 import oic.simulator.serviceprovider.resource.SimulatorResource;
10 import oic.simulator.serviceprovider.utils.Utility;
11 import oic.simulator.serviceprovider.view.dialogs.AutomationSettingDialog;
13 import org.eclipse.jface.dialogs.MessageDialog;
14 import org.eclipse.jface.viewers.CellEditor;
15 import org.eclipse.jface.viewers.CheckboxCellEditor;
16 import org.eclipse.jface.viewers.ComboBoxCellEditor;
17 import org.eclipse.jface.viewers.EditingSupport;
18 import org.eclipse.jface.viewers.TableViewer;
19 import org.eclipse.jface.window.Window;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.custom.CCombo;
22 import org.eclipse.swt.events.ModifyEvent;
23 import org.eclipse.swt.events.ModifyListener;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.swt.widgets.MessageBox;
26 import org.oic.simulator.AutomationType;
28 public class AttributeEditingSupport {
30 private AttributeValueEditor attValueEditor;
31 private AutomationEditor automationEditor;
33 public AttributeValueEditor createAttributeValueEditor(TableViewer viewer) {
34 attValueEditor = new AttributeValueEditor(viewer);
35 return attValueEditor;
38 public AutomationEditor createAutomationEditor(TableViewer viewer) {
39 automationEditor = new AutomationEditor(viewer);
40 return automationEditor;
43 class AttributeValueEditor extends EditingSupport {
45 private final TableViewer viewer;
46 private LocalResourceAttribute attributeInSelection;
47 private CCombo comboBox;
49 public AttributeValueEditor(TableViewer viewer) {
55 protected boolean canEdit(Object arg0) {
60 protected CellEditor getCellEditor(Object element) {
61 attributeInSelection = (LocalResourceAttribute) element;
63 // CellEditor is not required as the automation is in progress.
64 if (attributeInSelection.isAutomationInProgress()) {
68 String values[] = null;
69 List<String> valueSet = attributeInSelection.getAttValues();
70 values = convertListToStringArray(valueSet);
72 ComboBoxCellEditor comboEditor = new ComboBoxCellEditor(
73 viewer.getTable(), values, SWT.READ_ONLY);
74 comboBox = (CCombo) comboEditor.getControl();
75 comboBox.addModifyListener(new ModifyListener() {
78 public void modifyText(ModifyEvent event) {
79 String oldValue = String.valueOf(attributeInSelection
80 .getAttributeValue());
81 String newValue = comboBox.getText();
82 if (!oldValue.equals(newValue)) {
83 attributeInSelection.setAttributeValue(newValue);
84 MessageBox dialog = new MessageBox(viewer.getTable()
85 .getShell(), SWT.ICON_QUESTION | SWT.OK
87 dialog.setText("Confirm action");
88 dialog.setMessage("Do you want to modify the value?");
89 int retval = dialog.open();
90 if (retval != SWT.OK) {
91 attributeInSelection.setAttributeValue(oldValue);
93 ResourceManager resourceManager;
94 resourceManager = Activator.getDefault()
95 .getResourceManager();
96 SimulatorResource resource = resourceManager
97 .getCurrentResourceInSelection();
98 resourceManager.attributeValueUpdated(resource,
99 attributeInSelection.getAttributeName(),
102 viewer.update(attributeInSelection, null);
103 comboBox.setVisible(false);
111 protected Object getValue(Object element) {
113 LocalResourceAttribute att = (LocalResourceAttribute) element;
114 String valueString = String.valueOf(att.getAttributeValue());
115 List<String> valueSet = att.getAttValues();
116 if (null != valueSet) {
117 indexOfItem = valueSet.indexOf(valueString);
119 if (indexOfItem == -1) {
126 protected void setValue(Object element, Object value) {
127 Object valueObj = attributeInSelection.getAttributeValue();
128 if (null == valueObj)
130 String attValue = String.valueOf(valueObj);
131 ((LocalResourceAttribute) element).setAttributeValue(attValue);
132 viewer.update(element, null);
135 public String[] convertListToStringArray(List<String> valueList) {
137 if (null != valueList && valueList.size() > 0) {
138 strArr = valueList.toArray(new String[1]);
140 strArr = new String[1];
146 class AutomationEditor extends EditingSupport {
148 private final TableViewer viewer;
150 public AutomationEditor(TableViewer viewer) {
152 this.viewer = viewer;
156 protected boolean canEdit(Object arg0) {
161 protected CellEditor getCellEditor(Object element) {
162 // CellEditor is not required as the automation is in progress.
163 ResourceManager resourceManager = Activator.getDefault()
164 .getResourceManager();
165 SimulatorResource resource = resourceManager
166 .getCurrentResourceInSelection();
167 if (null != resource && resource.isResourceAutomationInProgress()) {
170 return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
174 protected Object getValue(Object element) {
175 LocalResourceAttribute att = (LocalResourceAttribute) element;
176 return att.isAutomationInProgress();
180 protected void setValue(Object element, Object value) {
181 ResourceManager resourceManager = Activator.getDefault()
182 .getResourceManager();
183 // As automation depends on the current resource in selection, its
184 // presence is being checked.
185 SimulatorResource resource = resourceManager
186 .getCurrentResourceInSelection();
187 if (null == resource) {
191 LocalResourceAttribute att = (LocalResourceAttribute) element;
192 boolean checked = (Boolean) value;
194 // Start the automation
195 // Fetch the settings data
196 List<AutomationSettingHelper> automationSettings;
197 automationSettings = AutomationSettingHelper
198 .getAutomationSettings(att);
200 // Open the settings dialog
201 AutomationSettingDialog dialog = new AutomationSettingDialog(
202 viewer.getTable().getShell(), automationSettings);
204 if (dialog.open() == Window.OK) {
205 String automationType = dialog.getAutomationType();
206 String updateFreq = dialog.getUpdateFrequency();
208 AutomationType autoType = AutomationType
209 .valueOf(automationType);
210 int updFreq = Utility
211 .getUpdateIntervalFromString(updateFreq);
212 int autoId = resourceManager.startAutomation(resource, att,
215 MessageDialog.openInformation(Display.getDefault()
216 .getActiveShell(), "Automation Status",
217 "Automation start failed!!");
219 viewer.update(element, null);
223 // Stop the automation
224 resourceManager.stopAutomation(resource, att,
225 att.getAutomationId());
226 MessageDialog.openInformation(Display.getDefault()
227 .getActiveShell(), "Automation Status",
228 "Automation stopped.");
229 viewer.update(element, null);