Added UI Support for updating model arrays.
[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.Label;
27 import org.eclipse.swt.widgets.MessageBox;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.swt.widgets.Text;
30
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;
38
39 import oic.simulator.serviceprovider.utils.AttributeValueBuilder;
40 import oic.simulator.serviceprovider.utils.AttributeValueStringConverter;
41 import oic.simulator.serviceprovider.utils.Utility;
42
43 /**
44  * This class manages and shows the automation settings dialog from the
45  * attribute view.
46  */
47 public class UpdatePrimitiveArrayAttributeDialog extends TitleAreaDialog {
48
49     private Text                       attNameTxt;
50     private Text                       currentValueTxt;
51     private Text                       newValueTxt;
52     private Text                       allowedValuesTxt;
53     private SimulatorResourceAttribute attribute;
54     private String                     newValue;
55
56     private AttributeValue             newAttValueObj;
57
58     public UpdatePrimitiveArrayAttributeDialog(Shell parentShell,
59             SimulatorResourceAttribute attribute) {
60         super(parentShell);
61         this.attribute = attribute;
62     }
63
64     @Override
65     public void create() {
66         super.create();
67         setTitle("Modify Attribute's Value");
68         setMessage("Change the value of the array type attribute");
69     }
70
71     @Override
72     protected Control createDialogArea(Composite parent) {
73         Composite compLayout = (Composite) super.createDialogArea(parent);
74
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);
81
82         GridData gd;
83
84         Label attNameLbl = new Label(container, SWT.NONE);
85         attNameLbl.setText("Attribute Name");
86
87         attNameTxt = new Text(container, SWT.READ_ONLY | SWT.BORDER);
88         attNameTxt.setBackground(container.getBackground());
89         gd = new GridData();
90         gd.grabExcessHorizontalSpace = true;
91         gd.horizontalAlignment = SWT.FILL;
92         attNameTxt.setLayoutData(gd);
93
94         Label allowedValuesLbl = new Label(container, SWT.NONE);
95         allowedValuesLbl.setText("Allowed Values");
96
97         allowedValuesTxt = new Text(container, SWT.MULTI | SWT.READ_ONLY
98                 | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
99         allowedValuesTxt.setBackground(container.getBackground());
100         gd = new GridData();
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);
107
108         Label curValueLbl = new Label(container, SWT.NONE);
109         curValueLbl.setText("Current Value");
110
111         currentValueTxt = new Text(container, SWT.MULTI | SWT.READ_ONLY
112                 | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
113         currentValueTxt.setBackground(container.getBackground());
114         gd = new GridData();
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);
121
122         Label attNewValue = new Label(container, SWT.NONE);
123         attNewValue.setText("New Value");
124
125         newValueTxt = new Text(container, SWT.MULTI | SWT.BORDER | SWT.WRAP
126                 | SWT.V_SCROLL);
127         gd = new GridData();
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();
135
136         Label hintHeader = new Label(container, SWT.NULL);
137         hintHeader.setText("Note:-");
138         gd = new GridData();
139         gd.grabExcessHorizontalSpace = true;
140         gd.horizontalAlignment = SWT.FILL;
141         gd.horizontalSpan = 2;
142         hintHeader.setLayoutData(gd);
143
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]]\"");
147         gd = new GridData();
148         gd.grabExcessHorizontalSpace = true;
149         gd.horizontalAlignment = SWT.FILL;
150         gd.horizontalSpan = 2;
151         gd.horizontalIndent = 40;
152         hint.setLayoutData(gd);
153
154         populateData();
155
156         return compLayout;
157     }
158
159     private void populateData() {
160         // Set the Attribute Name.
161         attNameTxt.setText(attribute.name());
162
163         // Set the allowed values.
164         ArrayProperty arrProp = attribute.property().asArray();
165         AttributeProperty elementProp = arrProp.getElementProperty();
166
167         String values = "";
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++) {
175                     values += arr[i];
176                     if (i + 1 < arr.length) {
177                         values += ", ";
178                     }
179                 }
180
181                 if (!values.isEmpty()) {
182                     values = "[" + values + "]";
183                 }
184             }
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++) {
192                     values += arr[i];
193                     if (i + 1 < arr.length) {
194                         values += ", ";
195                     }
196                 }
197
198                 if (!values.isEmpty()) {
199                     values = "[" + values + "]";
200                 }
201             }
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++) {
209                     values += arr[i];
210                     if (i + 1 < arr.length) {
211                         values += ", ";
212                     }
213                 }
214
215                 if (!values.isEmpty()) {
216                     values = "[" + values + "]";
217                 }
218             }
219         }
220         allowedValuesTxt.setText(values);
221
222         // Set the current value.
223         currentValueTxt.setText(new AttributeValueStringConverter(attribute
224                 .value()).toString());
225     }
226
227     public String getNewValue() {
228         return newValue;
229     }
230
231     public AttributeValue getNewValueObj() {
232         return newAttValueObj;
233     }
234
235     @Override
236     protected void okPressed() {
237         // Validate the value
238         newValue = newValueTxt.getText();
239         if (null == newValue || newValue.isEmpty()) {
240             return;
241         }
242
243         ArrayProperty arrProp = attribute.property().asArray();
244         try {
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)) {
250                     close();
251                     return;
252                 }
253             }
254         } catch (Exception e) {
255         }
256
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]]\"");
263         dialog.open();
264
265         newValueTxt.setFocus();
266     }
267
268     @Override
269     protected boolean isResizable() {
270         return true;
271     }
272
273     @Override
274     public boolean isHelpAvailable() {
275         return false;
276     }
277 }