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.clientcontroller.view;
19 import java.util.List;
21 import oic.simulator.clientcontroller.Activator;
22 import oic.simulator.clientcontroller.manager.ResourceManager;
23 import oic.simulator.clientcontroller.remoteresource.AttributeElement;
24 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
25 import oic.simulator.clientcontroller.utils.AttributeValueBuilder;
26 import oic.simulator.clientcontroller.utils.Utility;
27 import oic.simulator.clientcontroller.view.dialogs.PostRequestDialog;
29 import org.eclipse.jface.dialogs.TitleAreaDialog;
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.IStructuredSelection;
35 import org.eclipse.jface.viewers.TreeViewer;
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.MessageBox;
41 import org.eclipse.swt.widgets.Tree;
42 import org.eclipse.swt.widgets.TreeItem;
43 import org.oic.simulator.AttributeValue;
44 import org.oic.simulator.AttributeValue.TypeInfo;
45 import org.oic.simulator.AttributeValue.ValueType;
46 import org.oic.simulator.InvalidArgsException;
47 import org.oic.simulator.SimulatorResourceAttribute;
50 * This class provides editing support to the resources attributes table in the
53 public class AttributeEditingSupport {
55 private AttributeValueEditor attValueEditor;
56 private PostRequestEditor postReqEditor;
58 public AttributeValueEditor createAttributeValueEditor(TreeViewer viewer,
59 TitleAreaDialog dialog) {
60 attValueEditor = new AttributeValueEditor(viewer, dialog);
61 return attValueEditor;
64 public PostRequestEditor createAutomationEditor(TreeViewer viewer) {
65 postReqEditor = new PostRequestEditor(viewer);
69 class AttributeValueEditor extends EditingSupport {
71 private final TreeViewer viewer;
72 private CCombo comboBox;
73 private TitleAreaDialog dialog;
75 public AttributeValueEditor(TreeViewer viewer, TitleAreaDialog dialog) {
82 protected boolean canEdit(Object arg0) {
87 protected CellEditor getCellEditor(final Object element) {
88 ResourceManager resourceManager = Activator.getDefault()
89 .getResourceManager();
91 RemoteResource res = resourceManager
92 .getCurrentResourceInSelection();
97 final SimulatorResourceAttribute attribute;
98 if (!(element instanceof AttributeElement)) {
102 final AttributeElement attributeElement = ((AttributeElement) element);
103 attribute = attributeElement.getSimulatorResourceAttribute();
104 if (null == attribute) {
108 final AttributeValue val = attribute.value();
113 final TypeInfo type = val.typeInfo();
114 if (type.mBaseType == ValueType.RESOURCEMODEL) {
118 String values[] = null;
119 List<String> valueSet = resourceManager
120 .getAllValuesOfAttribute(attribute);
121 values = convertListToStringArray(valueSet);
123 ComboBoxCellEditor comboEditor;
124 comboEditor = new ComboBoxCellEditor(viewer.getTree(), values);
125 comboBox = (CCombo) comboEditor.getControl();
126 comboBox.addModifyListener(new ModifyListener() {
129 public void modifyText(ModifyEvent event) {
130 String newValue = comboBox.getText();
132 if (null != newValue && !newValue.isEmpty()) {
133 attributeElement.setPostState(true);
135 attributeElement.setPostState(false);
138 if (dialog instanceof PostRequestDialog) {
139 viewer.update(attributeElement, null);
147 protected Object getValue(Object element) {
149 SimulatorResourceAttribute att = null;
151 if (element instanceof AttributeElement) {
152 att = ((AttributeElement) element)
153 .getSimulatorResourceAttribute();
160 String valueString = Utility.getAttributeValueAsString(att.value());
161 List<String> valueSet = Activator.getDefault().getResourceManager()
162 .getAllValuesOfAttribute(att);
163 if (null != valueSet) {
164 indexOfItem = valueSet.indexOf(valueString);
166 if (indexOfItem == -1) {
173 protected void setValue(Object element, Object value) {
174 SimulatorResourceAttribute att = null;
175 if (element instanceof AttributeElement) {
176 att = ((AttributeElement) element)
177 .getSimulatorResourceAttribute();
184 AttributeValue val = att.value();
189 TypeInfo type = val.typeInfo();
191 String oldValue = String.valueOf(Utility
192 .getAttributeValueAsString(val));
193 String newValue = comboBox.getText();
194 if (!oldValue.equals(newValue)) {
195 // Get the AttriuteValue from the string
196 AttributeValue attValue = AttributeValueBuilder.build(newValue,
198 boolean invalid = false;
199 if (null == attValue) {
202 TypeInfo resTypeInfo = attValue.typeInfo();
203 if (type.mDepth != resTypeInfo.mDepth
204 || type.mType != resTypeInfo.mType
205 || type.mBaseType != resTypeInfo.mBaseType) {
210 MessageBox dialog = new MessageBox(viewer.getTree()
211 .getShell(), SWT.ICON_ERROR | SWT.OK);
212 dialog.setText("Invalid Value");
213 dialog.setMessage("Given value is invalid");
216 updateAttributeValue(att, attValue);
220 viewer.update(element, null);
223 public String[] convertListToStringArray(List<String> values) {
225 if (null != values && values.size() > 0) {
226 strArr = values.toArray(new String[1]);
228 strArr = new String[1];
233 public void updateAttributeValue(SimulatorResourceAttribute att,
234 AttributeValue value) {
235 IStructuredSelection selection = (IStructuredSelection) viewer
237 if (null == selection) {
241 Object obj = selection.getFirstElement();
246 Tree t = viewer.getTree();
247 TreeItem item = t.getSelection()[0];
252 TreeItem parent = item.getParentItem();
253 if (null != parent) {
254 while (parent.getParentItem() != null) {
255 parent = parent.getParentItem();
257 Object data = parent.getData();
258 ((AttributeElement) data).setPostState(true);
261 if (item.getData() instanceof AttributeElement) {
262 AttributeElement attributeElement = (AttributeElement) item
264 attributeElement.getSimulatorResourceAttribute()
267 parent = item.getParentItem();
268 if (null != parent) {
269 Object data = parent.getData();
271 ((AttributeElement) data).deepSetChildValue(att);
272 } catch (InvalidArgsException e) {
280 class PostRequestEditor extends EditingSupport {
282 private final TreeViewer viewer;
284 public PostRequestEditor(TreeViewer viewer) {
286 this.viewer = viewer;
290 protected boolean canEdit(Object arg0) {
295 protected CellEditor getCellEditor(Object element) {
296 SimulatorResourceAttribute att = null;
297 if (element instanceof AttributeElement) {
298 att = ((AttributeElement) element)
299 .getSimulatorResourceAttribute();
306 AttributeValue val = att.value();
311 TypeInfo type = val.typeInfo();
313 if (type.mType == ValueType.RESOURCEMODEL
314 || type.mBaseType == ValueType.RESOURCEMODEL) {
318 return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
322 protected Object getValue(Object element) {
323 if (element instanceof AttributeElement) {
324 return ((AttributeElement) element).getPostState();
331 protected void setValue(Object element, Object value) {
332 if (!(element instanceof AttributeElement)) {
335 boolean status = (Boolean) value;
336 ((AttributeElement) element).setPostState(status);
337 viewer.update(element, null);