Resolved issues and concerns found during overall functionality testing.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / dialogs / UpdatePrimitiveArrayAttributeDialog.java
1 /*
2  * Copyright 2016 Samsung Electronics All Rights Reserved.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package oic.simulator.serviceprovider.view.dialogs;
18
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;
31
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;
39
40 import oic.simulator.serviceprovider.utils.AttributeValueBuilder;
41 import oic.simulator.serviceprovider.utils.AttributeValueStringConverter;
42 import oic.simulator.serviceprovider.utils.Utility;
43
44 /**
45  * This class manages and shows the automation settings dialog from the
46  * attribute view.
47  */
48 public class UpdatePrimitiveArrayAttributeDialog extends TitleAreaDialog {
49
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;
60
61     private AttributeValue             newAttValueObj;
62
63     public UpdatePrimitiveArrayAttributeDialog(Shell parentShell,
64             SimulatorResourceAttribute attribute) {
65         super(parentShell);
66         this.attribute = attribute;
67     }
68
69     @Override
70     public void create() {
71         super.create();
72         setTitle("Modify Attribute's Value");
73         setMessage("Change the value of the array type attribute");
74     }
75
76     @Override
77     protected Control createDialogArea(Composite parent) {
78         Composite compLayout = (Composite) super.createDialogArea(parent);
79
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);
86
87         GridData gd;
88
89         Label attNameLbl = new Label(container, SWT.NONE);
90         attNameLbl.setText("Attribute Name");
91
92         attNameTxt = new Text(container, SWT.READ_ONLY | SWT.BORDER);
93         attNameTxt.setBackground(container.getBackground());
94         gd = new GridData();
95         gd.grabExcessHorizontalSpace = true;
96         gd.horizontalAlignment = SWT.FILL;
97         attNameTxt.setLayoutData(gd);
98
99         Group subGroup = new Group(container, SWT.NONE);
100         subGroup.setText("Array Properties");
101         layout = new GridLayout(2, true);
102         subGroup.setLayout(layout);
103         gd = new GridData();
104         gd.grabExcessHorizontalSpace = true;
105         gd.horizontalAlignment = SWT.FILL;
106         gd.horizontalSpan = 2;
107         subGroup.setLayoutData(gd);
108
109         Composite minRangeContainer = new Composite(subGroup, SWT.NONE);
110         gd = new GridData();
111         gd.grabExcessHorizontalSpace = true;
112         gd.horizontalAlignment = SWT.FILL;
113         minRangeContainer.setLayoutData(gd);
114         layout = new GridLayout(2, true);
115         minRangeContainer.setLayout(layout);
116
117         Label minRangeLbl = new Label(minRangeContainer, SWT.NONE);
118         minRangeLbl.setText("Minimum Items");
119
120         minRangeTxt = new Text(minRangeContainer, SWT.BORDER | SWT.READ_ONLY);
121         gd = new GridData();
122         gd.grabExcessHorizontalSpace = true;
123         gd.horizontalAlignment = SWT.FILL;
124         minRangeTxt.setLayoutData(gd);
125         minRangeTxt.setBackground(container.getBackground());
126
127         Composite maxRangeContainer = new Composite(subGroup, SWT.NONE);
128         gd = new GridData();
129         gd.grabExcessHorizontalSpace = true;
130         gd.horizontalAlignment = SWT.FILL;
131         maxRangeContainer.setLayoutData(gd);
132         layout = new GridLayout(2, true);
133         maxRangeContainer.setLayout(layout);
134
135         Label maxRangeLbl = new Label(maxRangeContainer, SWT.NONE);
136         maxRangeLbl.setText("Maximum Items");
137
138         maxRangeTxt = new Text(maxRangeContainer, SWT.BORDER | SWT.READ_ONLY);
139         gd = new GridData();
140         gd.grabExcessHorizontalSpace = true;
141         gd.horizontalAlignment = SWT.FILL;
142         maxRangeTxt.setLayoutData(gd);
143         maxRangeTxt.setBackground(container.getBackground());
144
145         Composite uniqueContainer = new Composite(subGroup, SWT.NONE);
146         gd = new GridData();
147         gd.grabExcessHorizontalSpace = true;
148         gd.horizontalAlignment = SWT.FILL;
149         uniqueContainer.setLayoutData(gd);
150         layout = new GridLayout(2, true);
151         uniqueContainer.setLayout(layout);
152
153         Label uniqueLbl = new Label(uniqueContainer, SWT.NONE);
154         uniqueLbl.setText("Allow Duplicates");
155
156         allowDuplicatesTxt = new Text(uniqueContainer, SWT.BORDER
157                 | SWT.READ_ONLY);
158         gd = new GridData();
159         gd.grabExcessHorizontalSpace = true;
160         gd.horizontalAlignment = SWT.FILL;
161         allowDuplicatesTxt.setLayoutData(gd);
162         allowDuplicatesTxt.setBackground(container.getBackground());
163
164         Composite addlItemsContainer = new Composite(subGroup, SWT.NONE);
165         gd = new GridData();
166         gd.grabExcessHorizontalSpace = true;
167         gd.horizontalAlignment = SWT.FILL;
168         addlItemsContainer.setLayoutData(gd);
169         layout = new GridLayout(2, true);
170         addlItemsContainer.setLayout(layout);
171
172         Label addlItemsLbl = new Label(addlItemsContainer, SWT.NONE);
173         addlItemsLbl.setText("Allow Extra Items");
174
175         additionalItemsTxt = new Text(addlItemsContainer, SWT.BORDER
176                 | SWT.READ_ONLY);
177         gd = new GridData();
178         gd.grabExcessHorizontalSpace = true;
179         gd.horizontalAlignment = SWT.FILL;
180         additionalItemsTxt.setLayoutData(gd);
181         additionalItemsTxt.setBackground(container.getBackground());
182
183         Group elementPropGroup = new Group(container, SWT.NONE);
184         elementPropGroup.setText("Element Property");
185         layout = new GridLayout(2, false);
186         elementPropGroup.setLayout(layout);
187         gd = new GridData();
188         gd.grabExcessHorizontalSpace = true;
189         gd.horizontalAlignment = SWT.FILL;
190         gd.horizontalSpan = 2;
191         elementPropGroup.setLayoutData(gd);
192
193         Label allowedValuesLbl = new Label(elementPropGroup, SWT.NONE);
194         allowedValuesLbl.setText("Allowed Values");
195
196         allowedValuesTxt = new Text(elementPropGroup, SWT.MULTI | SWT.READ_ONLY
197                 | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
198         allowedValuesTxt.setBackground(container.getBackground());
199         gd = new GridData();
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);
206
207         Label curValueLbl = new Label(container, SWT.NONE);
208         curValueLbl.setText("Current Value");
209
210         currentValueTxt = new Text(container, SWT.MULTI | SWT.READ_ONLY
211                 | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
212         currentValueTxt.setBackground(container.getBackground());
213         gd = new GridData();
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);
220
221         Label attNewValue = new Label(container, SWT.NONE);
222         attNewValue.setText("New Value");
223
224         newValueTxt = new Text(container, SWT.MULTI | SWT.BORDER | SWT.WRAP
225                 | SWT.V_SCROLL);
226         gd = new GridData();
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();
234
235         Group hintGroup = new Group(container, SWT.NONE);
236         hintGroup.setText("Note");
237         layout = new GridLayout();
238         hintGroup.setLayout(layout);
239         gd = new GridData();
240         gd.grabExcessHorizontalSpace = true;
241         gd.horizontalAlignment = SWT.FILL;
242         gd.horizontalSpan = 2;
243         hintGroup.setLayoutData(gd);
244
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]]\"");
248         gd = new GridData();
249         gd.grabExcessHorizontalSpace = true;
250         gd.horizontalAlignment = SWT.FILL;
251         gd.horizontalSpan = 2;
252         gd.horizontalIndent = 40;
253         hint.setLayoutData(gd);
254
255         populateData();
256
257         return compLayout;
258     }
259
260     private void populateData() {
261         // Set the Attribute Name.
262         attNameTxt.setText(attribute.name());
263
264         // Set the allowed values.
265         ArrayProperty arrProp = attribute.property().asArray();
266         AttributeProperty elementProp = arrProp.getElementProperty();
267
268         String values = "";
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++) {
276                     values += arr[i];
277                     if (i + 1 < arr.length) {
278                         values += ", ";
279                     }
280                 }
281
282                 if (!values.isEmpty()) {
283                     values = "[" + values + "]";
284                 }
285             }
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++) {
293                     values += arr[i];
294                     if (i + 1 < arr.length) {
295                         values += ", ";
296                     }
297                 }
298
299                 if (!values.isEmpty()) {
300                     values = "[" + values + "]";
301                 }
302             }
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++) {
310                     values += arr[i];
311                     if (i + 1 < arr.length) {
312                         values += ", ";
313                     }
314                 }
315
316                 if (!values.isEmpty()) {
317                     values = "[" + values + "]";
318                 }
319             }
320         }
321         allowedValuesTxt.setText(values);
322
323         // Set the current value.
324         currentValueTxt.setText(new AttributeValueStringConverter(attribute
325                 .value()).toString());
326
327         minRangeTxt.setText(String.valueOf(arrProp.minItems()));
328
329         maxRangeTxt.setText(String.valueOf(arrProp.maxItems()));
330
331         allowDuplicatesTxt.setText(!arrProp.isUnique() ? "Yes" : "No");
332
333         additionalItemsTxt.setText(arrProp.isVariable() ? "Yes" : "No");
334     }
335
336     public String getNewValue() {
337         return newValue;
338     }
339
340     public AttributeValue getNewValueObj() {
341         return newAttValueObj;
342     }
343
344     @Override
345     protected void okPressed() {
346         // Validate the value
347         newValue = newValueTxt.getText();
348         if (null == newValue || newValue.isEmpty()) {
349             return;
350         }
351
352         ArrayProperty arrProp = attribute.property().asArray();
353         try {
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)) {
359                     close();
360                     return;
361                 }
362             }
363         } catch (Exception e) {
364         }
365
366         MessageBox dialog = new MessageBox(Display.getDefault()
367                 .getActiveShell(), SWT.ICON_ERROR | SWT.OK);
368         dialog.setText("Invalid Value");
369         dialog.setMessage("Given value is invalid.\nEither the entered value(s) are not in the allowed values or in improper format.\n"
370                 + "Format: Array values should be comma-seperated and surrounded by square brackets.\n"
371                 + "Ex: \"[value]\", \"[value1,value2]\", \"[[value1], [value2]]\"");
372         dialog.open();
373
374         newValueTxt.setFocus();
375     }
376
377     @Override
378     protected boolean isResizable() {
379         return true;
380     }
381
382     @Override
383     public boolean isHelpAvailable() {
384         return false;
385     }
386 }