Fix for jira IOT-1042.
[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 java.util.Date;
33
34 import org.oic.simulator.ArrayProperty;
35 import org.oic.simulator.AttributeProperty;
36 import org.oic.simulator.AttributeValue;
37 import org.oic.simulator.DoubleProperty;
38 import org.oic.simulator.ILogger.Level;
39 import org.oic.simulator.IntegerProperty;
40 import org.oic.simulator.SimulatorResourceAttribute;
41 import org.oic.simulator.StringProperty;
42
43 import oic.simulator.serviceprovider.Activator;
44 import oic.simulator.serviceprovider.utils.AttributeValueBuilder;
45 import oic.simulator.serviceprovider.utils.AttributeValueStringConverter;
46 import oic.simulator.serviceprovider.utils.Utility;
47
48 /**
49  * This class manages and shows the automation settings dialog from the
50  * attribute view.
51  */
52 public class UpdatePrimitiveArrayAttributeDialog extends TitleAreaDialog {
53
54     private Text                       attNameTxt;
55     private Text                       currentValueTxt;
56     private Text                       newValueTxt;
57     private Text                       allowedValuesTxt;
58     private Text                       minRangeTxt;
59     private Text                       maxRangeTxt;
60     private Text                       allowDuplicatesTxt;
61     private Text                       additionalItemsTxt;
62     private SimulatorResourceAttribute attribute;
63     private String                     newValue;
64
65     private AttributeValue             newAttValueObj;
66
67     public UpdatePrimitiveArrayAttributeDialog(Shell parentShell,
68             SimulatorResourceAttribute attribute) {
69         super(parentShell);
70         this.attribute = attribute;
71     }
72
73     @Override
74     public void create() {
75         super.create();
76         setTitle("Modify Attribute's Value");
77         setMessage("Change the value of the array type attribute");
78     }
79
80     @Override
81     protected Control createDialogArea(Composite parent) {
82         Composite compLayout = (Composite) super.createDialogArea(parent);
83
84         Composite container = new Composite(compLayout, SWT.NONE);
85         container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
86         GridLayout layout = new GridLayout(2, false);
87         layout.verticalSpacing = 10;
88         layout.marginTop = 10;
89         container.setLayout(layout);
90
91         GridData gd;
92
93         Label attNameLbl = new Label(container, SWT.NONE);
94         attNameLbl.setText("Attribute Name");
95
96         attNameTxt = new Text(container, SWT.READ_ONLY | SWT.BORDER);
97         attNameTxt.setBackground(container.getBackground());
98         gd = new GridData();
99         gd.grabExcessHorizontalSpace = true;
100         gd.horizontalAlignment = SWT.FILL;
101         attNameTxt.setLayoutData(gd);
102
103         Group subGroup = new Group(container, SWT.NONE);
104         subGroup.setText("Array Properties");
105         layout = new GridLayout(2, true);
106         subGroup.setLayout(layout);
107         gd = new GridData();
108         gd.grabExcessHorizontalSpace = true;
109         gd.horizontalAlignment = SWT.FILL;
110         gd.horizontalSpan = 2;
111         subGroup.setLayoutData(gd);
112
113         Composite minRangeContainer = new Composite(subGroup, SWT.NONE);
114         gd = new GridData();
115         gd.grabExcessHorizontalSpace = true;
116         gd.horizontalAlignment = SWT.FILL;
117         minRangeContainer.setLayoutData(gd);
118         layout = new GridLayout(2, true);
119         minRangeContainer.setLayout(layout);
120
121         Label minRangeLbl = new Label(minRangeContainer, SWT.NONE);
122         minRangeLbl.setText("Minimum Items");
123
124         minRangeTxt = new Text(minRangeContainer, SWT.BORDER | SWT.READ_ONLY);
125         gd = new GridData();
126         gd.grabExcessHorizontalSpace = true;
127         gd.horizontalAlignment = SWT.FILL;
128         minRangeTxt.setLayoutData(gd);
129         minRangeTxt.setBackground(container.getBackground());
130
131         Composite maxRangeContainer = new Composite(subGroup, SWT.NONE);
132         gd = new GridData();
133         gd.grabExcessHorizontalSpace = true;
134         gd.horizontalAlignment = SWT.FILL;
135         maxRangeContainer.setLayoutData(gd);
136         layout = new GridLayout(2, true);
137         maxRangeContainer.setLayout(layout);
138
139         Label maxRangeLbl = new Label(maxRangeContainer, SWT.NONE);
140         maxRangeLbl.setText("Maximum Items");
141
142         maxRangeTxt = new Text(maxRangeContainer, SWT.BORDER | SWT.READ_ONLY);
143         gd = new GridData();
144         gd.grabExcessHorizontalSpace = true;
145         gd.horizontalAlignment = SWT.FILL;
146         maxRangeTxt.setLayoutData(gd);
147         maxRangeTxt.setBackground(container.getBackground());
148
149         Composite uniqueContainer = new Composite(subGroup, SWT.NONE);
150         gd = new GridData();
151         gd.grabExcessHorizontalSpace = true;
152         gd.horizontalAlignment = SWT.FILL;
153         uniqueContainer.setLayoutData(gd);
154         layout = new GridLayout(2, true);
155         uniqueContainer.setLayout(layout);
156
157         Label uniqueLbl = new Label(uniqueContainer, SWT.NONE);
158         uniqueLbl.setText("Allow Duplicates");
159
160         allowDuplicatesTxt = new Text(uniqueContainer, SWT.BORDER
161                 | SWT.READ_ONLY);
162         gd = new GridData();
163         gd.grabExcessHorizontalSpace = true;
164         gd.horizontalAlignment = SWT.FILL;
165         allowDuplicatesTxt.setLayoutData(gd);
166         allowDuplicatesTxt.setBackground(container.getBackground());
167
168         Composite addlItemsContainer = new Composite(subGroup, SWT.NONE);
169         gd = new GridData();
170         gd.grabExcessHorizontalSpace = true;
171         gd.horizontalAlignment = SWT.FILL;
172         addlItemsContainer.setLayoutData(gd);
173         layout = new GridLayout(2, true);
174         addlItemsContainer.setLayout(layout);
175
176         Label addlItemsLbl = new Label(addlItemsContainer, SWT.NONE);
177         addlItemsLbl.setText("Allow Extra Items");
178
179         additionalItemsTxt = new Text(addlItemsContainer, SWT.BORDER
180                 | SWT.READ_ONLY);
181         gd = new GridData();
182         gd.grabExcessHorizontalSpace = true;
183         gd.horizontalAlignment = SWT.FILL;
184         additionalItemsTxt.setLayoutData(gd);
185         additionalItemsTxt.setBackground(container.getBackground());
186
187         Group elementPropGroup = new Group(container, SWT.NONE);
188         elementPropGroup.setText("Element Property");
189         layout = new GridLayout(2, false);
190         elementPropGroup.setLayout(layout);
191         gd = new GridData();
192         gd.grabExcessHorizontalSpace = true;
193         gd.horizontalAlignment = SWT.FILL;
194         gd.horizontalSpan = 2;
195         elementPropGroup.setLayoutData(gd);
196
197         Label allowedValuesLbl = new Label(elementPropGroup, SWT.NONE);
198         allowedValuesLbl.setText("Allowed Values");
199
200         allowedValuesTxt = new Text(elementPropGroup, SWT.MULTI | SWT.READ_ONLY
201                 | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
202         allowedValuesTxt.setBackground(container.getBackground());
203         gd = new GridData();
204         gd.grabExcessHorizontalSpace = true;
205         gd.horizontalAlignment = SWT.FILL;
206         gd.grabExcessVerticalSpace = true;
207         gd.verticalAlignment = SWT.FILL;
208         gd.minimumHeight = 30;
209         allowedValuesTxt.setLayoutData(gd);
210
211         Label curValueLbl = new Label(container, SWT.NONE);
212         curValueLbl.setText("Current Value");
213
214         currentValueTxt = new Text(container, SWT.MULTI | SWT.READ_ONLY
215                 | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
216         currentValueTxt.setBackground(container.getBackground());
217         gd = new GridData();
218         gd.grabExcessHorizontalSpace = true;
219         gd.horizontalAlignment = SWT.FILL;
220         gd.grabExcessVerticalSpace = true;
221         gd.verticalAlignment = SWT.FILL;
222         gd.minimumHeight = 30;
223         currentValueTxt.setLayoutData(gd);
224
225         Label attNewValue = new Label(container, SWT.NONE);
226         attNewValue.setText("New Value");
227
228         newValueTxt = new Text(container, SWT.MULTI | SWT.BORDER | SWT.WRAP
229                 | SWT.V_SCROLL);
230         gd = new GridData();
231         gd.grabExcessHorizontalSpace = true;
232         gd.horizontalAlignment = SWT.FILL;
233         gd.grabExcessVerticalSpace = true;
234         gd.verticalAlignment = SWT.FILL;
235         gd.minimumHeight = 60;
236         newValueTxt.setLayoutData(gd);
237         newValueTxt.setFocus();
238
239         Group hintGroup = new Group(container, SWT.NONE);
240         hintGroup.setText("Note");
241         layout = new GridLayout();
242         hintGroup.setLayout(layout);
243         gd = new GridData();
244         gd.grabExcessHorizontalSpace = true;
245         gd.horizontalAlignment = SWT.FILL;
246         gd.horizontalSpan = 2;
247         hintGroup.setLayoutData(gd);
248
249         Label hint = new Label(hintGroup, SWT.NULL);
250         hint.setText("Array values should be comma-seperated and surrounded by square brackets.\n"
251                 + "Ex: \"[value]\", \"[value1,value2]\", \"[[value1], [value2]]\"");
252         gd = new GridData();
253         gd.grabExcessHorizontalSpace = true;
254         gd.horizontalAlignment = SWT.FILL;
255         gd.horizontalSpan = 2;
256         gd.horizontalIndent = 40;
257         hint.setLayoutData(gd);
258
259         populateData();
260
261         return compLayout;
262     }
263
264     private void populateData() {
265         // Set the Attribute Name.
266         attNameTxt.setText(attribute.name());
267
268         // Set the allowed values.
269         ArrayProperty arrProp = attribute.property().asArray();
270         AttributeProperty elementProp = arrProp.getElementProperty();
271
272         StringBuilder values = new StringBuilder();
273         String value = "";
274         if (elementProp.isInteger()) {
275             IntegerProperty intProp = elementProp.asInteger();
276             if (intProp.hasRange()) {
277                 value = intProp.min() + " - " + intProp.max();
278             } else if (intProp.hasValues()) {
279                 int[] arr = intProp.getValues();
280                 for (int i = 0; i < arr.length; i++) {
281                     values.append(arr[i]);
282                     if (i + 1 < arr.length) {
283                         values.append(", ");
284                     }
285                 }
286
287                 if (!values.toString().isEmpty()) {
288                     value = "[" + values + "]";
289                 }
290             }
291         } else if (elementProp.isDouble()) {
292             DoubleProperty dblProp = elementProp.asDouble();
293             if (dblProp.hasRange()) {
294                 value = dblProp.min() + " - " + dblProp.max();
295             } else if (dblProp.hasValues()) {
296                 double[] arr = dblProp.getValues();
297                 for (int i = 0; i < arr.length; i++) {
298                     values.append(arr[i]);
299                     if (i + 1 < arr.length) {
300                         values.append(", ");
301                     }
302                 }
303
304                 if (!values.toString().isEmpty()) {
305                     value = "[" + values + "]";
306                 }
307             }
308         } else if (elementProp.isBoolean()) {
309             value = "[true, false]";
310         } else if (elementProp.isString()) {
311             StringProperty strProp = elementProp.asString();
312             if (strProp.hasValues()) {
313                 String[] arr = strProp.getValues();
314                 for (int i = 0; i < arr.length; i++) {
315                     values.append(arr[i]);
316                     if (i + 1 < arr.length) {
317                         values.append(", ");
318                     }
319                 }
320
321                 if (!values.toString().isEmpty()) {
322                     value = "[" + values + "]";
323                 }
324             }
325         }
326
327         allowedValuesTxt.setText(value);
328
329         // Set the current value.
330         currentValueTxt.setText(new AttributeValueStringConverter(attribute
331                 .value()).toString());
332
333         minRangeTxt.setText(String.valueOf(arrProp.minItems()));
334
335         maxRangeTxt.setText(String.valueOf(arrProp.maxItems()));
336
337         allowDuplicatesTxt.setText(!arrProp.isUnique() ? "Yes" : "No");
338
339         additionalItemsTxt.setText(arrProp.isVariable() ? "Yes" : "No");
340     }
341
342     public String getNewValue() {
343         return newValue;
344     }
345
346     public AttributeValue getNewValueObj() {
347         return newAttValueObj;
348     }
349
350     @Override
351     protected void okPressed() {
352         // Validate the value
353         newValue = newValueTxt.getText();
354         if (null == newValue || newValue.isEmpty()) {
355             return;
356         }
357
358         ArrayProperty arrProp = attribute.property().asArray();
359         try {
360             String value = Utility.removeWhiteSpacesInArrayValues(newValue);
361             if (null != value && value.startsWith("[") && value.endsWith("]")) {
362                 newAttValueObj = AttributeValueBuilder.build(value, attribute
363                         .value().typeInfo().mBaseType);
364                 if (null != newAttValueObj && arrProp.validate(newAttValueObj)) {
365                     close();
366                     return;
367                 }
368             }
369         } catch (Exception e) {
370             Activator
371                     .getDefault()
372                     .getLogManager()
373                     .log(Level.ERROR.ordinal(),
374                             new Date(),
375                             "There is an error while building the new attribute value.\n"
376                                     + Utility.getSimulatorErrorString(e, null));
377         }
378
379         MessageBox dialog = new MessageBox(Display.getDefault()
380                 .getActiveShell(), SWT.ICON_ERROR | SWT.OK);
381         dialog.setText("Invalid Value");
382         dialog.setMessage("Given value is invalid.\nEither the entered value(s) are not in the allowed values or in improper format.\n"
383                 + "Format: Array values should be comma-seperated and surrounded by square brackets.\n"
384                 + "Ex: \"[value]\", \"[value1,value2]\", \"[[value1], [value2]]\"");
385         dialog.open();
386
387         newValueTxt.setFocus();
388     }
389
390     @Override
391     protected boolean isResizable() {
392         return true;
393     }
394
395     @Override
396     public boolean isHelpAvailable() {
397         return false;
398     }
399 }