2 * Copyright 2016 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.dialogs;
19 import org.eclipse.jface.dialogs.TitleAreaDialog;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Display;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.MessageBox;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.swt.widgets.Text;
31 import org.oic.simulator.ArrayProperty;
32 import org.oic.simulator.AttributeProperty;
33 import org.oic.simulator.AttributeValue;
34 import org.oic.simulator.DoubleProperty;
35 import org.oic.simulator.IntegerProperty;
36 import org.oic.simulator.SimulatorResourceAttribute;
37 import org.oic.simulator.StringProperty;
39 import oic.simulator.serviceprovider.utils.AttributeValueBuilder;
40 import oic.simulator.serviceprovider.utils.AttributeValueStringConverter;
41 import oic.simulator.serviceprovider.utils.Utility;
44 * This class manages and shows the automation settings dialog from the
47 public class UpdatePrimitiveArrayAttributeDialog extends TitleAreaDialog {
49 private Text attNameTxt;
50 private Text currentValueTxt;
51 private Text newValueTxt;
52 private Text allowedValuesTxt;
53 private SimulatorResourceAttribute attribute;
54 private String newValue;
56 private AttributeValue newAttValueObj;
58 public UpdatePrimitiveArrayAttributeDialog(Shell parentShell,
59 SimulatorResourceAttribute attribute) {
61 this.attribute = attribute;
65 public void create() {
67 setTitle("Modify Attribute's Value");
68 setMessage("Change the value of the array type attribute");
72 protected Control createDialogArea(Composite parent) {
73 Composite compLayout = (Composite) super.createDialogArea(parent);
75 Composite container = new Composite(compLayout, SWT.NONE);
76 container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
77 GridLayout layout = new GridLayout(2, false);
78 layout.verticalSpacing = 10;
79 layout.marginTop = 10;
80 container.setLayout(layout);
84 Label attNameLbl = new Label(container, SWT.NONE);
85 attNameLbl.setText("Attribute Name");
87 attNameTxt = new Text(container, SWT.READ_ONLY | SWT.BORDER);
88 attNameTxt.setBackground(container.getBackground());
90 gd.grabExcessHorizontalSpace = true;
91 gd.horizontalAlignment = SWT.FILL;
92 attNameTxt.setLayoutData(gd);
94 Label allowedValuesLbl = new Label(container, SWT.NONE);
95 allowedValuesLbl.setText("Allowed Values");
97 allowedValuesTxt = new Text(container, SWT.MULTI | SWT.READ_ONLY
98 | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
99 allowedValuesTxt.setBackground(container.getBackground());
101 gd.grabExcessHorizontalSpace = true;
102 gd.horizontalAlignment = SWT.FILL;
103 gd.grabExcessVerticalSpace = true;
104 gd.verticalAlignment = SWT.FILL;
105 gd.minimumHeight = 30;
106 allowedValuesTxt.setLayoutData(gd);
108 Label curValueLbl = new Label(container, SWT.NONE);
109 curValueLbl.setText("Current Value");
111 currentValueTxt = new Text(container, SWT.MULTI | SWT.READ_ONLY
112 | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
113 currentValueTxt.setBackground(container.getBackground());
115 gd.grabExcessHorizontalSpace = true;
116 gd.horizontalAlignment = SWT.FILL;
117 gd.grabExcessVerticalSpace = true;
118 gd.verticalAlignment = SWT.FILL;
119 gd.minimumHeight = 30;
120 currentValueTxt.setLayoutData(gd);
122 Label attNewValue = new Label(container, SWT.NONE);
123 attNewValue.setText("New Value");
125 newValueTxt = new Text(container, SWT.MULTI | SWT.BORDER | SWT.WRAP
128 gd.grabExcessHorizontalSpace = true;
129 gd.horizontalAlignment = SWT.FILL;
130 gd.grabExcessVerticalSpace = true;
131 gd.verticalAlignment = SWT.FILL;
132 gd.minimumHeight = 60;
133 newValueTxt.setLayoutData(gd);
134 newValueTxt.setFocus();
136 Label hintHeader = new Label(container, SWT.NULL);
137 hintHeader.setText("Note:-");
139 gd.grabExcessHorizontalSpace = true;
140 gd.horizontalAlignment = SWT.FILL;
141 gd.horizontalSpan = 2;
142 hintHeader.setLayoutData(gd);
144 Label hint = new Label(container, SWT.NULL);
145 hint.setText("Array values should be comma-seperated and surrounded by square brackets.\n"
146 + "Ex: \"[value]\", \"[value1,value2]\", \"[[value1], [value2]]\"");
148 gd.grabExcessHorizontalSpace = true;
149 gd.horizontalAlignment = SWT.FILL;
150 gd.horizontalSpan = 2;
151 gd.horizontalIndent = 40;
152 hint.setLayoutData(gd);
159 private void populateData() {
160 // Set the Attribute Name.
161 attNameTxt.setText(attribute.name());
163 // Set the allowed values.
164 ArrayProperty arrProp = attribute.property().asArray();
165 AttributeProperty elementProp = arrProp.getElementProperty();
168 if (elementProp.isInteger()) {
169 IntegerProperty intProp = elementProp.asInteger();
170 if (intProp.hasRange()) {
171 values = "From " + intProp.min() + " To " + intProp.max();
172 } else if (intProp.hasValues()) {
173 int[] arr = intProp.getValues();
174 for (int i = 0; i < arr.length; i++) {
176 if (i + 1 < arr.length) {
181 if (!values.isEmpty()) {
182 values = "[" + values + "]";
185 } else if (elementProp.isDouble()) {
186 DoubleProperty dblProp = elementProp.asDouble();
187 if (dblProp.hasRange()) {
188 values = "From " + dblProp.min() + " To " + dblProp.max();
189 } else if (dblProp.hasValues()) {
190 double[] arr = dblProp.getValues();
191 for (int i = 0; i < arr.length; i++) {
193 if (i + 1 < arr.length) {
198 if (!values.isEmpty()) {
199 values = "[" + values + "]";
202 } else if (elementProp.isBoolean()) {
203 values = "[true, false]";
204 } else if (elementProp.isString()) {
205 StringProperty strProp = elementProp.asString();
206 if (strProp.hasValues()) {
207 String[] arr = strProp.getValues();
208 for (int i = 0; i < arr.length; i++) {
210 if (i + 1 < arr.length) {
215 if (!values.isEmpty()) {
216 values = "[" + values + "]";
220 allowedValuesTxt.setText(values);
222 // Set the current value.
223 currentValueTxt.setText(new AttributeValueStringConverter(attribute
224 .value()).toString());
227 public String getNewValue() {
231 public AttributeValue getNewValueObj() {
232 return newAttValueObj;
236 protected void okPressed() {
237 // Validate the value
238 newValue = newValueTxt.getText();
239 if (null == newValue || newValue.isEmpty()) {
243 ArrayProperty arrProp = attribute.property().asArray();
245 String value = Utility.removeWhiteSpacesInArrayValues(newValue);
246 if (value.startsWith("[") && value.endsWith("]")) {
247 newAttValueObj = AttributeValueBuilder.build(value, attribute
248 .value().typeInfo().mBaseType);
249 if (null != newAttValueObj && arrProp.validate(newAttValueObj)) {
254 } catch (Exception e) {
257 MessageBox dialog = new MessageBox(Display.getDefault()
258 .getActiveShell(), SWT.ICON_ERROR | SWT.OK);
259 dialog.setText("Invalid Value");
260 dialog.setMessage("Given value is invalid.\nEither the entered value(s) are not in the allowed values or in improper format.\n"
261 + "Format: Array values should be comma-seperated and surrounded by square brackets.\n"
262 + "Ex: \"[value]\", \"[value1,value2]\", \"[[value1], [value2]]\"");
265 newValueTxt.setFocus();
269 protected boolean isResizable() {
274 public boolean isHelpAvailable() {