Fix for klocwork issues.
[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         if (elementProp.isInteger()) {
274             IntegerProperty intProp = elementProp.asInteger();
275             if (intProp.hasRange()) {
276                 values.append(intProp.min() + " - " + intProp.max());
277             } else if (intProp.hasValues()) {
278                 int[] arr = intProp.getValues();
279                 for (int i = 0; i < arr.length; i++) {
280                     values.append(arr[i]);
281                     if (i + 1 < arr.length) {
282                         values.append(", ");
283                     }
284                 }
285
286                 if (!values.toString().isEmpty()) {
287                     values.append("[" + values + "]");
288                 }
289             }
290         } else if (elementProp.isDouble()) {
291             DoubleProperty dblProp = elementProp.asDouble();
292             if (dblProp.hasRange()) {
293                 values.append(dblProp.min() + " - " + dblProp.max());
294             } else if (dblProp.hasValues()) {
295                 double[] arr = dblProp.getValues();
296                 for (int i = 0; i < arr.length; i++) {
297                     values.append(arr[i]);
298                     if (i + 1 < arr.length) {
299                         values.append(", ");
300                     }
301                 }
302
303                 if (!values.toString().isEmpty()) {
304                     values.append("[" + values + "]");
305                 }
306             }
307         } else if (elementProp.isBoolean()) {
308             values.append("[true, false]");
309         } else if (elementProp.isString()) {
310             StringProperty strProp = elementProp.asString();
311             if (strProp.hasValues()) {
312                 String[] arr = strProp.getValues();
313                 for (int i = 0; i < arr.length; i++) {
314                     values.append(arr[i]);
315                     if (i + 1 < arr.length) {
316                         values.append(", ");
317                     }
318                 }
319
320                 if (!values.toString().isEmpty()) {
321                     values.append("[" + values + "]");
322                 }
323             }
324         }
325         allowedValuesTxt.setText(values.toString());
326
327         // Set the current value.
328         currentValueTxt.setText(new AttributeValueStringConverter(attribute
329                 .value()).toString());
330
331         minRangeTxt.setText(String.valueOf(arrProp.minItems()));
332
333         maxRangeTxt.setText(String.valueOf(arrProp.maxItems()));
334
335         allowDuplicatesTxt.setText(!arrProp.isUnique() ? "Yes" : "No");
336
337         additionalItemsTxt.setText(arrProp.isVariable() ? "Yes" : "No");
338     }
339
340     public String getNewValue() {
341         return newValue;
342     }
343
344     public AttributeValue getNewValueObj() {
345         return newAttValueObj;
346     }
347
348     @Override
349     protected void okPressed() {
350         // Validate the value
351         newValue = newValueTxt.getText();
352         if (null == newValue || newValue.isEmpty()) {
353             return;
354         }
355
356         ArrayProperty arrProp = attribute.property().asArray();
357         try {
358             String value = Utility.removeWhiteSpacesInArrayValues(newValue);
359             if (null != value && value.startsWith("[") && value.endsWith("]")) {
360                 newAttValueObj = AttributeValueBuilder.build(value, attribute
361                         .value().typeInfo().mBaseType);
362                 if (null != newAttValueObj && arrProp.validate(newAttValueObj)) {
363                     close();
364                     return;
365                 }
366             }
367         } catch (Exception e) {
368             Activator
369                     .getDefault()
370                     .getLogManager()
371                     .log(Level.ERROR.ordinal(),
372                             new Date(),
373                             "There is an error while building the new attribute value.\n"
374                                     + Utility.getSimulatorErrorString(e, null));
375         }
376
377         MessageBox dialog = new MessageBox(Display.getDefault()
378                 .getActiveShell(), SWT.ICON_ERROR | SWT.OK);
379         dialog.setText("Invalid Value");
380         dialog.setMessage("Given value is invalid.\nEither the entered value(s) are not in the allowed values or in improper format.\n"
381                 + "Format: Array values should be comma-seperated and surrounded by square brackets.\n"
382                 + "Ex: \"[value]\", \"[value1,value2]\", \"[[value1], [value2]]\"");
383         dialog.open();
384
385         newValueTxt.setFocus();
386     }
387
388     @Override
389     protected boolean isResizable() {
390         return true;
391     }
392
393     @Override
394     public boolean isHelpAvailable() {
395         return false;
396     }
397 }