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.serviceprovider.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 java.util.Date;
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;
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;
49 * This class manages and shows the automation settings dialog from the
52 public class UpdatePrimitiveArrayAttributeDialog extends TitleAreaDialog {
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;
65 private AttributeValue newAttValueObj;
67 public UpdatePrimitiveArrayAttributeDialog(Shell parentShell,
68 SimulatorResourceAttribute attribute) {
70 this.attribute = attribute;
74 public void create() {
76 setTitle("Modify Attribute's Value");
77 setMessage("Change the value of the array type attribute");
81 protected Control createDialogArea(Composite parent) {
82 Composite compLayout = (Composite) super.createDialogArea(parent);
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);
93 Label attNameLbl = new Label(container, SWT.NONE);
94 attNameLbl.setText("Attribute Name");
96 attNameTxt = new Text(container, SWT.READ_ONLY | SWT.BORDER);
97 attNameTxt.setBackground(container.getBackground());
99 gd.grabExcessHorizontalSpace = true;
100 gd.horizontalAlignment = SWT.FILL;
101 attNameTxt.setLayoutData(gd);
103 Group subGroup = new Group(container, SWT.NONE);
104 subGroup.setText("Array Properties");
105 layout = new GridLayout(2, true);
106 subGroup.setLayout(layout);
108 gd.grabExcessHorizontalSpace = true;
109 gd.horizontalAlignment = SWT.FILL;
110 gd.horizontalSpan = 2;
111 subGroup.setLayoutData(gd);
113 Composite minRangeContainer = new Composite(subGroup, SWT.NONE);
115 gd.grabExcessHorizontalSpace = true;
116 gd.horizontalAlignment = SWT.FILL;
117 minRangeContainer.setLayoutData(gd);
118 layout = new GridLayout(2, true);
119 minRangeContainer.setLayout(layout);
121 Label minRangeLbl = new Label(minRangeContainer, SWT.NONE);
122 minRangeLbl.setText("Minimum Items");
124 minRangeTxt = new Text(minRangeContainer, SWT.BORDER | SWT.READ_ONLY);
126 gd.grabExcessHorizontalSpace = true;
127 gd.horizontalAlignment = SWT.FILL;
128 minRangeTxt.setLayoutData(gd);
129 minRangeTxt.setBackground(container.getBackground());
131 Composite maxRangeContainer = new Composite(subGroup, SWT.NONE);
133 gd.grabExcessHorizontalSpace = true;
134 gd.horizontalAlignment = SWT.FILL;
135 maxRangeContainer.setLayoutData(gd);
136 layout = new GridLayout(2, true);
137 maxRangeContainer.setLayout(layout);
139 Label maxRangeLbl = new Label(maxRangeContainer, SWT.NONE);
140 maxRangeLbl.setText("Maximum Items");
142 maxRangeTxt = new Text(maxRangeContainer, SWT.BORDER | SWT.READ_ONLY);
144 gd.grabExcessHorizontalSpace = true;
145 gd.horizontalAlignment = SWT.FILL;
146 maxRangeTxt.setLayoutData(gd);
147 maxRangeTxt.setBackground(container.getBackground());
149 Composite uniqueContainer = new Composite(subGroup, SWT.NONE);
151 gd.grabExcessHorizontalSpace = true;
152 gd.horizontalAlignment = SWT.FILL;
153 uniqueContainer.setLayoutData(gd);
154 layout = new GridLayout(2, true);
155 uniqueContainer.setLayout(layout);
157 Label uniqueLbl = new Label(uniqueContainer, SWT.NONE);
158 uniqueLbl.setText("Allow Duplicates");
160 allowDuplicatesTxt = new Text(uniqueContainer, SWT.BORDER
163 gd.grabExcessHorizontalSpace = true;
164 gd.horizontalAlignment = SWT.FILL;
165 allowDuplicatesTxt.setLayoutData(gd);
166 allowDuplicatesTxt.setBackground(container.getBackground());
168 Composite addlItemsContainer = new Composite(subGroup, SWT.NONE);
170 gd.grabExcessHorizontalSpace = true;
171 gd.horizontalAlignment = SWT.FILL;
172 addlItemsContainer.setLayoutData(gd);
173 layout = new GridLayout(2, true);
174 addlItemsContainer.setLayout(layout);
176 Label addlItemsLbl = new Label(addlItemsContainer, SWT.NONE);
177 addlItemsLbl.setText("Allow Extra Items");
179 additionalItemsTxt = new Text(addlItemsContainer, SWT.BORDER
182 gd.grabExcessHorizontalSpace = true;
183 gd.horizontalAlignment = SWT.FILL;
184 additionalItemsTxt.setLayoutData(gd);
185 additionalItemsTxt.setBackground(container.getBackground());
187 Group elementPropGroup = new Group(container, SWT.NONE);
188 elementPropGroup.setText("Element Property");
189 layout = new GridLayout(2, false);
190 elementPropGroup.setLayout(layout);
192 gd.grabExcessHorizontalSpace = true;
193 gd.horizontalAlignment = SWT.FILL;
194 gd.horizontalSpan = 2;
195 elementPropGroup.setLayoutData(gd);
197 Label allowedValuesLbl = new Label(elementPropGroup, SWT.NONE);
198 allowedValuesLbl.setText("Allowed Values");
200 allowedValuesTxt = new Text(elementPropGroup, SWT.MULTI | SWT.READ_ONLY
201 | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
202 allowedValuesTxt.setBackground(container.getBackground());
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);
211 Label curValueLbl = new Label(container, SWT.NONE);
212 curValueLbl.setText("Current Value");
214 currentValueTxt = new Text(container, SWT.MULTI | SWT.READ_ONLY
215 | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
216 currentValueTxt.setBackground(container.getBackground());
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);
225 Label attNewValue = new Label(container, SWT.NONE);
226 attNewValue.setText("New Value");
228 newValueTxt = new Text(container, SWT.MULTI | SWT.BORDER | SWT.WRAP
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();
239 Group hintGroup = new Group(container, SWT.NONE);
240 hintGroup.setText("Note");
241 layout = new GridLayout();
242 hintGroup.setLayout(layout);
244 gd.grabExcessHorizontalSpace = true;
245 gd.horizontalAlignment = SWT.FILL;
246 gd.horizontalSpan = 2;
247 hintGroup.setLayoutData(gd);
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]]\"");
253 gd.grabExcessHorizontalSpace = true;
254 gd.horizontalAlignment = SWT.FILL;
255 gd.horizontalSpan = 2;
256 gd.horizontalIndent = 40;
257 hint.setLayoutData(gd);
264 private void populateData() {
265 // Set the Attribute Name.
266 attNameTxt.setText(attribute.name());
268 // Set the allowed values.
269 ArrayProperty arrProp = attribute.property().asArray();
270 AttributeProperty elementProp = arrProp.getElementProperty();
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) {
286 if (!values.toString().isEmpty()) {
287 values.append("[" + values + "]");
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) {
303 if (!values.toString().isEmpty()) {
304 values.append("[" + values + "]");
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) {
320 if (!values.toString().isEmpty()) {
321 values.append("[" + values + "]");
325 allowedValuesTxt.setText(values.toString());
327 // Set the current value.
328 currentValueTxt.setText(new AttributeValueStringConverter(attribute
329 .value()).toString());
331 minRangeTxt.setText(String.valueOf(arrProp.minItems()));
333 maxRangeTxt.setText(String.valueOf(arrProp.maxItems()));
335 allowDuplicatesTxt.setText(!arrProp.isUnique() ? "Yes" : "No");
337 additionalItemsTxt.setText(arrProp.isVariable() ? "Yes" : "No");
340 public String getNewValue() {
344 public AttributeValue getNewValueObj() {
345 return newAttValueObj;
349 protected void okPressed() {
350 // Validate the value
351 newValue = newValueTxt.getText();
352 if (null == newValue || newValue.isEmpty()) {
356 ArrayProperty arrProp = attribute.property().asArray();
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)) {
367 } catch (Exception e) {
371 .log(Level.ERROR.ordinal(),
373 "There is an error while building the new attribute value.\n"
374 + Utility.getSimulatorErrorString(e, null));
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]]\"");
385 newValueTxt.setFocus();
389 protected boolean isResizable() {
394 public boolean isHelpAvailable() {