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.clientcontroller.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.Group;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.MessageBox;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.swt.widgets.Text;
32 import org.oic.simulator.ArrayProperty;
33 import org.oic.simulator.AttributeProperty;
34 import org.oic.simulator.AttributeValue;
35 import org.oic.simulator.DoubleProperty;
36 import org.oic.simulator.IntegerProperty;
37 import org.oic.simulator.SimulatorResourceAttribute;
38 import org.oic.simulator.StringProperty;
40 import oic.simulator.clientcontroller.utils.AttributeValueBuilder;
41 import oic.simulator.clientcontroller.utils.AttributeValueStringConverter;
42 import oic.simulator.clientcontroller.utils.Utility;
45 * This class manages and shows the automation settings dialog from the
48 public class UpdatePrimitiveArrayAttributeDialog extends TitleAreaDialog {
50 private Text attNameTxt;
51 private Text currentValueTxt;
52 private Text newValueTxt;
53 private Text allowedValuesTxt;
54 private Text minRangeTxt;
55 private Text maxRangeTxt;
56 private Text allowDuplicatesTxt;
57 private Text additionalItemsTxt;
58 private SimulatorResourceAttribute attribute;
59 private String newValue;
61 private AttributeValue newAttValueObj;
63 public UpdatePrimitiveArrayAttributeDialog(Shell parentShell,
64 SimulatorResourceAttribute attribute) {
66 this.attribute = attribute;
70 public void create() {
72 setTitle("Modify Attribute's Value");
73 setMessage("Change the value of the array type attribute");
77 protected Control createDialogArea(Composite parent) {
78 Composite compLayout = (Composite) super.createDialogArea(parent);
80 Composite container = new Composite(compLayout, SWT.NONE);
81 container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
82 GridLayout layout = new GridLayout(2, false);
83 layout.verticalSpacing = 10;
84 layout.marginTop = 10;
85 container.setLayout(layout);
89 Label attNameLbl = new Label(container, SWT.NONE);
90 attNameLbl.setText("Attribute Name");
92 attNameTxt = new Text(container, SWT.READ_ONLY | SWT.BORDER);
93 attNameTxt.setBackground(container.getBackground());
95 gd.grabExcessHorizontalSpace = true;
96 gd.horizontalAlignment = SWT.FILL;
97 attNameTxt.setLayoutData(gd);
99 Group subGroup = new Group(container, SWT.NONE);
100 subGroup.setText("Array Properties");
101 layout = new GridLayout(2, true);
102 subGroup.setLayout(layout);
104 gd.grabExcessHorizontalSpace = true;
105 gd.horizontalAlignment = SWT.FILL;
106 gd.horizontalSpan = 2;
107 subGroup.setLayoutData(gd);
109 Composite minRangeContainer = new Composite(subGroup, SWT.NONE);
111 gd.grabExcessHorizontalSpace = true;
112 gd.horizontalAlignment = SWT.FILL;
113 minRangeContainer.setLayoutData(gd);
114 layout = new GridLayout(2, true);
115 minRangeContainer.setLayout(layout);
117 Label minRangeLbl = new Label(minRangeContainer, SWT.NONE);
118 minRangeLbl.setText("Minimum Items");
120 minRangeTxt = new Text(minRangeContainer, SWT.BORDER | SWT.READ_ONLY);
122 gd.grabExcessHorizontalSpace = true;
123 gd.horizontalAlignment = SWT.FILL;
124 minRangeTxt.setLayoutData(gd);
125 minRangeTxt.setBackground(container.getBackground());
127 Composite maxRangeContainer = new Composite(subGroup, SWT.NONE);
129 gd.grabExcessHorizontalSpace = true;
130 gd.horizontalAlignment = SWT.FILL;
131 maxRangeContainer.setLayoutData(gd);
132 layout = new GridLayout(2, true);
133 maxRangeContainer.setLayout(layout);
135 Label maxRangeLbl = new Label(maxRangeContainer, SWT.NONE);
136 maxRangeLbl.setText("Maximum Items");
138 maxRangeTxt = new Text(maxRangeContainer, SWT.BORDER | SWT.READ_ONLY);
140 gd.grabExcessHorizontalSpace = true;
141 gd.horizontalAlignment = SWT.FILL;
142 maxRangeTxt.setLayoutData(gd);
143 maxRangeTxt.setBackground(container.getBackground());
145 Composite uniqueContainer = new Composite(subGroup, SWT.NONE);
147 gd.grabExcessHorizontalSpace = true;
148 gd.horizontalAlignment = SWT.FILL;
149 uniqueContainer.setLayoutData(gd);
150 layout = new GridLayout(2, true);
151 uniqueContainer.setLayout(layout);
153 Label uniqueLbl = new Label(uniqueContainer, SWT.NONE);
154 uniqueLbl.setText("Allow Duplicates");
156 allowDuplicatesTxt = new Text(uniqueContainer, SWT.BORDER
159 gd.grabExcessHorizontalSpace = true;
160 gd.horizontalAlignment = SWT.FILL;
161 allowDuplicatesTxt.setLayoutData(gd);
162 allowDuplicatesTxt.setBackground(container.getBackground());
164 Composite addlItemsContainer = new Composite(subGroup, SWT.NONE);
166 gd.grabExcessHorizontalSpace = true;
167 gd.horizontalAlignment = SWT.FILL;
168 addlItemsContainer.setLayoutData(gd);
169 layout = new GridLayout(2, true);
170 addlItemsContainer.setLayout(layout);
172 Label addlItemsLbl = new Label(addlItemsContainer, SWT.NONE);
173 addlItemsLbl.setText("Allow Extra Items");
175 additionalItemsTxt = new Text(addlItemsContainer, SWT.BORDER
178 gd.grabExcessHorizontalSpace = true;
179 gd.horizontalAlignment = SWT.FILL;
180 additionalItemsTxt.setLayoutData(gd);
181 additionalItemsTxt.setBackground(container.getBackground());
183 Group elementPropGroup = new Group(container, SWT.NONE);
184 elementPropGroup.setText("Element Property");
185 layout = new GridLayout(2, false);
186 elementPropGroup.setLayout(layout);
188 gd.grabExcessHorizontalSpace = true;
189 gd.horizontalAlignment = SWT.FILL;
190 gd.horizontalSpan = 2;
191 elementPropGroup.setLayoutData(gd);
193 Label allowedValuesLbl = new Label(elementPropGroup, SWT.NONE);
194 allowedValuesLbl.setText("Allowed Values");
196 allowedValuesTxt = new Text(elementPropGroup, SWT.MULTI | SWT.READ_ONLY
197 | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
198 allowedValuesTxt.setBackground(container.getBackground());
200 gd.grabExcessHorizontalSpace = true;
201 gd.horizontalAlignment = SWT.FILL;
202 gd.grabExcessVerticalSpace = true;
203 gd.verticalAlignment = SWT.FILL;
204 gd.minimumHeight = 30;
205 allowedValuesTxt.setLayoutData(gd);
207 Label curValueLbl = new Label(container, SWT.NONE);
208 curValueLbl.setText("Current Value");
210 currentValueTxt = new Text(container, SWT.MULTI | SWT.READ_ONLY
211 | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
212 currentValueTxt.setBackground(container.getBackground());
214 gd.grabExcessHorizontalSpace = true;
215 gd.horizontalAlignment = SWT.FILL;
216 gd.grabExcessVerticalSpace = true;
217 gd.verticalAlignment = SWT.FILL;
218 gd.minimumHeight = 30;
219 currentValueTxt.setLayoutData(gd);
221 Label attNewValue = new Label(container, SWT.NONE);
222 attNewValue.setText("New Value");
224 newValueTxt = new Text(container, SWT.MULTI | SWT.BORDER | SWT.WRAP
227 gd.grabExcessHorizontalSpace = true;
228 gd.horizontalAlignment = SWT.FILL;
229 gd.grabExcessVerticalSpace = true;
230 gd.verticalAlignment = SWT.FILL;
231 gd.minimumHeight = 60;
232 newValueTxt.setLayoutData(gd);
233 newValueTxt.setFocus();
235 Group hintGroup = new Group(container, SWT.NONE);
236 hintGroup.setText("Note");
237 layout = new GridLayout();
238 hintGroup.setLayout(layout);
240 gd.grabExcessHorizontalSpace = true;
241 gd.horizontalAlignment = SWT.FILL;
242 gd.horizontalSpan = 2;
243 hintGroup.setLayoutData(gd);
245 Label hint = new Label(hintGroup, SWT.NULL);
246 hint.setText("Array values should be comma-seperated and surrounded by square brackets.\n"
247 + "Ex: \"[value]\", \"[value1,value2]\", \"[[value1], [value2]]\"");
249 gd.grabExcessHorizontalSpace = true;
250 gd.horizontalAlignment = SWT.FILL;
251 gd.horizontalSpan = 2;
252 gd.horizontalIndent = 40;
253 hint.setLayoutData(gd);
260 private void populateData() {
261 // Set the Attribute Name.
262 attNameTxt.setText(attribute.name());
264 // Set the allowed values.
265 ArrayProperty arrProp = attribute.property().asArray();
266 AttributeProperty elementProp = arrProp.getElementProperty();
269 if (elementProp.isInteger()) {
270 IntegerProperty intProp = elementProp.asInteger();
271 if (intProp.hasRange()) {
272 values = intProp.min() + " - " + intProp.max();
273 } else if (intProp.hasValues()) {
274 int[] arr = intProp.getValues();
275 for (int i = 0; i < arr.length; i++) {
277 if (i + 1 < arr.length) {
282 if (!values.isEmpty()) {
283 values = "[" + values + "]";
286 } else if (elementProp.isDouble()) {
287 DoubleProperty dblProp = elementProp.asDouble();
288 if (dblProp.hasRange()) {
289 values = dblProp.min() + " - " + dblProp.max();
290 } else if (dblProp.hasValues()) {
291 double[] arr = dblProp.getValues();
292 for (int i = 0; i < arr.length; i++) {
294 if (i + 1 < arr.length) {
299 if (!values.isEmpty()) {
300 values = "[" + values + "]";
303 } else if (elementProp.isBoolean()) {
304 values = "[true, false]";
305 } else if (elementProp.isString()) {
306 StringProperty strProp = elementProp.asString();
307 if (strProp.hasValues()) {
308 String[] arr = strProp.getValues();
309 for (int i = 0; i < arr.length; i++) {
311 if (i + 1 < arr.length) {
316 if (!values.isEmpty()) {
317 values = "[" + values + "]";
321 allowedValuesTxt.setText(values);
323 // Set the current value.
324 currentValueTxt.setText(new AttributeValueStringConverter(attribute
325 .value()).toString());
327 minRangeTxt.setText(String.valueOf(arrProp.minItems()));
329 maxRangeTxt.setText(String.valueOf(arrProp.maxItems()));
331 allowDuplicatesTxt.setText(!arrProp.isUnique() ? "Yes" : "No");
333 additionalItemsTxt.setText(arrProp.isVariable() ? "Yes" : "No");
336 public String getNewValue() {
340 public AttributeValue getNewValueObj() {
341 return newAttValueObj;
345 protected void okPressed() {
346 // Validate the value
347 newValue = newValueTxt.getText();
348 if (null == newValue || newValue.isEmpty()) {
352 // ArrayProperty arrProp = attribute.property().asArray();
354 String value = Utility.removeWhiteSpacesInArrayValues(newValue);
355 if (value.startsWith("[") && value.endsWith("]")) {
356 newAttValueObj = AttributeValueBuilder.build(value, attribute
357 .value().typeInfo().mBaseType);
358 if (null != newAttValueObj/* && arrProp.validate(newAttValueObj) */) {
363 } catch (Exception e) {
366 MessageBox dialog = new MessageBox(Display.getDefault()
367 .getActiveShell(), SWT.ICON_ERROR | SWT.OK);
368 dialog.setText("Invalid Value");
369 dialog.setMessage("Either the value of its format is invalid.\n"
370 + "Format: Array values should be comma-seperated and surrounded by square brackets.\n"
371 + "Ex: \"[value]\", \"[value1,value2]\", \"[[value1], [value2]]\"");
374 newValueTxt.setFocus();
378 protected boolean isResizable() {
383 public boolean isHelpAvailable() {