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.clientcontroller.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.clientcontroller.Activator;
44 import oic.simulator.clientcontroller.utils.AttributeValueBuilder;
45 import oic.simulator.clientcontroller.utils.AttributeValueStringConverter;
46 import oic.simulator.clientcontroller.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();
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) {
287 if (!values.toString().isEmpty()) {
288 value = "[" + values + "]";
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) {
304 if (!values.toString().isEmpty()) {
305 value = "[" + values + "]";
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) {
321 if (!values.toString().isEmpty()) {
322 value = "[" + values + "]";
327 allowedValuesTxt.setText(value);
329 // Set the current value.
330 currentValueTxt.setText(new AttributeValueStringConverter(attribute
331 .value()).toString());
333 minRangeTxt.setText(String.valueOf(arrProp.minItems()));
335 maxRangeTxt.setText(String.valueOf(arrProp.maxItems()));
337 allowDuplicatesTxt.setText(!arrProp.isUnique() ? "Yes" : "No");
339 additionalItemsTxt.setText(arrProp.isVariable() ? "Yes" : "No");
342 public String getNewValue() {
346 public AttributeValue getNewValueObj() {
347 return newAttValueObj;
351 protected void okPressed() {
352 // Validate the value
353 newValue = newValueTxt.getText();
354 if (null == newValue || newValue.isEmpty()) {
359 String value = Utility.removeWhiteSpacesInArrayValues(newValue);
360 if (null != value && value.startsWith("[") && value.endsWith("]")) {
361 newAttValueObj = AttributeValueBuilder.build(value, attribute
362 .value().typeInfo().mBaseType);
363 if (null != newAttValueObj) {
368 } catch (Exception e) {
372 .log(Level.ERROR.ordinal(),
374 "There is an error while building the new attribute value.\n"
375 + Utility.getSimulatorErrorString(e, null));
378 MessageBox dialog = new MessageBox(Display.getDefault()
379 .getActiveShell(), SWT.ICON_ERROR | SWT.OK);
380 dialog.setText("Invalid Value");
381 dialog.setMessage("Either the value of its format is invalid.\n"
382 + "Format: Array values should be comma-seperated and surrounded by square brackets.\n"
383 + "Ex: \"[value]\", \"[value1,value2]\", \"[[value1], [value2]]\"");
386 newValueTxt.setFocus();
390 protected boolean isResizable() {
395 public boolean isHelpAvailable() {