2 * Copyright 2015 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 java.util.Iterator;
22 import oic.simulator.serviceprovider.model.AttributeHelper;
23 import oic.simulator.serviceprovider.utils.Constants;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.jface.dialogs.TitleAreaDialog;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.custom.CCombo;
30 import org.eclipse.swt.events.SelectionAdapter;
31 import org.eclipse.swt.events.SelectionEvent;
32 import org.eclipse.swt.graphics.Point;
33 import org.eclipse.swt.graphics.Rectangle;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.swt.widgets.Event;
41 import org.eclipse.swt.widgets.Group;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.List;
44 import org.eclipse.swt.widgets.Listener;
45 import org.eclipse.swt.widgets.Shell;
46 import org.eclipse.swt.widgets.Text;
47 import org.oic.simulator.AttributeProperty.Type;
49 public class AddAttributeDialog extends TitleAreaDialog {
51 private Text attNameTxt;
52 private Text minRangeTxt;
53 private Text maxRangeTxt;
54 private CCombo attTypeCmb;
55 private Text dflValueTxt;
56 private Button rangeBtn;
57 private Button cusValuesBtn;
58 private Button noneBtn;
59 private Button addBtn;
60 private Button remBtn;
63 private List customValuesList;
66 private AttributeHelper attHelper;
68 private AttributeHelper attClone;
70 private Set<AttributeHelper> attributes;
72 private final String defaultMessage = "Name, Type, and Default Value fields "
73 + "are mandatory.\n\nRange and custom fields allow to set the valid "
74 + "values of the attribute.\n\n";
75 private final String msgForBoolType = "Possible attribute values of Bool are "
76 + "true and false.\nSo range and custom options are disabled.";
77 private final String msgForIntType = "Valid values for Int type can either be "
78 + "of range type (Ex: 1 - 10) or custom values (Ex: 10, 20, 50, and 100).\n";
79 private final String msgForDoubleType = "Valid values for Double type can either be "
80 + "of range type (Ex: 18.0 - 22.0) or custom values (Ex: 1.5, 2.5, 3.9, 4.8, etc).\n";
81 private final String msgForStringType = "For String type, range option is not"
82 + "applicable. Hence it is disabled.\n\n"
83 + "Custom option is available to provide the valid values.\n\n"
84 + "Ex: low, mid, high, etc.";
86 private Set<String> attValueTypes;
88 private boolean editOperation;
90 public AddAttributeDialog(Shell parentShell, AttributeHelper att,
91 Set<String> attValueTypes, Set<AttributeHelper> attributes) {
94 att = new AttributeHelper();
96 attClone = att.clone();
100 this.attValueTypes = attValueTypes;
101 this.attributes = attributes;
105 public void create() {
107 setTitle("Add Attribute");
108 setMessage("Fill the details for creating an attribute");
112 protected Control createDialogArea(Composite parent) {
113 Composite compLayout = (Composite) super.createDialogArea(parent);
114 Composite container = new Composite(compLayout, SWT.NONE);
115 container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
116 GridLayout layout = new GridLayout(5, false);
117 container.setLayout(layout);
119 Label attNameLbl = new Label(container, SWT.NULL);
120 attNameLbl.setText("Attribute Name:");
122 attNameTxt = new Text(container, SWT.BORDER);
123 GridData gd = new GridData();
124 gd.horizontalAlignment = SWT.FILL;
125 gd.horizontalSpan = 4;
126 gd.grabExcessHorizontalSpace = true;
127 attNameTxt.setLayoutData(gd);
129 Label attTypeLbl = new Label(container, SWT.NULL);
130 attTypeLbl.setText("Attribute Type:");
132 attTypeCmb = new CCombo(container, SWT.READ_ONLY | SWT.BORDER);
134 gd.grabExcessHorizontalSpace = true;
135 gd.horizontalSpan = 4;
136 gd.horizontalAlignment = SWT.FILL;
137 attTypeCmb.setLayoutData(gd);
140 Group valuesGrp = new Group(container, SWT.NULL);
141 valuesGrp.setText("Attribute Values");
143 gd.verticalIndent = 10;
144 gd.horizontalSpan = 3;
145 gd.grabExcessHorizontalSpace = true;
146 gd.horizontalAlignment = SWT.FILL;
147 gd.grabExcessVerticalSpace = true;
148 gd.verticalAlignment = SWT.FILL;
149 valuesGrp.setLayoutData(gd);
150 layout = new GridLayout(4, false);
151 valuesGrp.setLayout(layout);
153 rangeBtn = new Button(valuesGrp, SWT.RADIO);
154 rangeBtn.setText("Range");
156 gd.horizontalSpan = 4;
157 rangeBtn.setLayoutData(gd);
159 minLbl = new Label(valuesGrp, SWT.NONE);
160 minLbl.setText("Min:");
162 gd.horizontalIndent = 25;
163 minLbl.setLayoutData(gd);
165 minRangeTxt = new Text(valuesGrp, SWT.BORDER);
168 minRangeTxt.setLayoutData(gd);
170 maxLbl = new Label(valuesGrp, SWT.NONE);
171 maxLbl.setText("Max:");
173 gd.horizontalIndent = 25;
174 maxLbl.setLayoutData(gd);
176 maxRangeTxt = new Text(valuesGrp, SWT.BORDER);
179 maxRangeTxt.setLayoutData(gd);
181 cusValuesBtn = new Button(valuesGrp, SWT.RADIO);
182 cusValuesBtn.setText("Custom");
184 gd.horizontalSpan = 4;
185 cusValuesBtn.setLayoutData(gd);
187 Composite cusValuesComp = new Composite(valuesGrp, SWT.NONE);
188 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
189 gd.horizontalSpan = 4;
190 cusValuesComp.setLayoutData(gd);
191 layout = new GridLayout(2, false);
192 cusValuesComp.setLayout(layout);
194 customValuesList = new List(cusValuesComp, SWT.BORDER | SWT.MULTI
196 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
198 gd.horizontalIndent = 25;
199 customValuesList.setLayoutData(gd);
201 Composite cusValuesActionsComp = new Composite(cusValuesComp, SWT.NONE);
202 layout = new GridLayout();
203 cusValuesActionsComp.setLayout(layout);
205 gd.verticalAlignment = SWT.TOP;
206 cusValuesActionsComp.setLayoutData(gd);
208 addBtn = new Button(cusValuesActionsComp, SWT.PUSH);
209 addBtn.setText("Add");
212 addBtn.setLayoutData(gd);
214 remBtn = new Button(cusValuesActionsComp, SWT.PUSH);
215 remBtn.setText("Remove");
218 remBtn.setLayoutData(gd);
219 remBtn.setEnabled(false);
221 noneBtn = new Button(valuesGrp, SWT.RADIO);
222 noneBtn.setText("None");
224 gd.horizontalSpan = 4;
225 noneBtn.setLayoutData(gd);
227 Composite detailsComp = new Composite(container, SWT.NULL);
228 detailsComp.setLayout(new GridLayout());
229 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
230 gd.horizontalSpan = 2;
232 detailsComp.setLayoutData(gd);
234 Label lbl = new Label(detailsComp, SWT.NULL);
235 lbl.setText("Details");
237 Group detailsGrp = new Group(detailsComp, SWT.NULL);
238 detailsGrp.setLayout(new GridLayout());
239 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
240 detailsGrp.setLayoutData(gd);
242 detail = new Text(detailsGrp, SWT.MULTI | SWT.READ_ONLY | SWT.BORDER
243 | SWT.WRAP | SWT.V_SCROLL);
244 detail.setBackground(detailsGrp.getBackground());
245 detail.setText(defaultMessage);
246 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
247 detail.setLayoutData(gd);
249 Label dflValueLbl = new Label(container, SWT.NULL);
250 dflValueLbl.setText("Default value");
252 dflValueTxt = new Text(container, SWT.BORDER);
254 gd.horizontalSpan = 4;
255 gd.grabExcessHorizontalSpace = true;
256 gd.horizontalAlignment = SWT.FILL;
257 dflValueTxt.setLayoutData(gd);
263 attNameTxt.setFocus();
265 setInitialSelection();
271 private void initTypes() {
272 if (null != attValueTypes && attValueTypes.size() > 0) {
273 Iterator<String> itr = attValueTypes.iterator();
274 while (itr.hasNext()) {
275 attTypeCmb.add(itr.next());
280 private void setInitialSelection() {
282 rangeOptionSelected(false);
283 customOptionSelected(false);
286 private void initData() {
288 // Populate the UI controls with the data.
289 attNameTxt.setText(attHelper.getAttributeName());
290 attTypeCmb.select(attTypeCmb.indexOf(attHelper.getAttributeType()));
292 dflValueTxt.setText(attHelper.getAttributeDflValue());
293 Type valuesType = attHelper.getValidValuesType();
294 if (valuesType == Type.RANGE) {
295 rangeBtn.setSelection(true);
296 noneBtn.setSelection(false);
297 rangeOptionSelected(true);
298 minRangeTxt.setText(attHelper.getMin());
299 maxRangeTxt.setText(attHelper.getMax());
300 } else if (valuesType == Type.VALUESET) {
301 cusValuesBtn.setSelection(true);
302 noneBtn.setSelection(false);
303 customOptionSelected(true);
304 Set<String> allowedValues = attHelper.getAllowedValues();
305 customValuesList.setItems(allowedValues.toArray(new String[1]));
310 private void setUiListeners() {
311 rangeBtn.addSelectionListener(new SelectionAdapter() {
313 public void widgetSelected(SelectionEvent e) {
314 rangeOptionSelected(true);
315 customOptionSelected(false);
316 minRangeTxt.setFocus();
320 cusValuesBtn.addSelectionListener(new SelectionAdapter() {
322 public void widgetSelected(SelectionEvent e) {
324 rangeOptionSelected(false);
325 customOptionSelected(true);
329 noneBtn.addSelectionListener(new SelectionAdapter() {
331 public void widgetSelected(SelectionEvent e) {
332 rangeOptionSelected(false);
333 customOptionSelected(false);
337 attTypeCmb.addSelectionListener(new SelectionAdapter() {
339 public void widgetSelected(SelectionEvent e) {
344 minRangeTxt.addListener(SWT.Verify, new Listener() {
346 public void handleEvent(Event e) {
347 String string = e.text;
348 char[] chars = new char[string.length()];
349 string.getChars(0, chars.length, chars, 0);
350 for (int i = 0; i < chars.length; i++) {
351 if (!(('0' <= chars[i] && chars[i] <= '9')
352 || chars[i] == '-' || chars[i] == '+')) {
353 if (attTypeCmb.getText().equals(Constants.INT)) {
356 } else if (attTypeCmb.getText()
357 .equals(Constants.DOUBLE)) {
358 if (!(chars[i] == '.')) {
368 maxRangeTxt.addListener(SWT.Verify, new Listener() {
370 public void handleEvent(Event e) {
371 String string = e.text;
372 char[] chars = new char[string.length()];
373 string.getChars(0, chars.length, chars, 0);
374 for (int i = 0; i < chars.length; i++) {
375 if (!(('0' <= chars[i] && chars[i] <= '9')
376 || chars[i] == '-' || chars[i] == '+')) {
377 if (attTypeCmb.getText().equals(Constants.INT)) {
380 } else if (attTypeCmb.getText()
381 .equals(Constants.DOUBLE)) {
382 if (!(chars[i] == '.')) {
392 addBtn.addSelectionListener(new SelectionAdapter() {
394 public void widgetSelected(SelectionEvent e) {
395 SingleTextInputDialog dialog = new SingleTextInputDialog(
396 getShell(), "Add Attribute Value", "Attribute Value");
397 if (dialog.open() == Window.OK) {
398 String value = dialog.getValue();
399 String type = attTypeCmb.getText();
400 if (!attHelper.isValueValid(value, type)) {
401 MessageDialog.openError(getParentShell(),
402 "Invalid value", "Attribute value is invalid.");
403 } else if (attHelper.isAllowedValueExist(
404 customValuesList.getItems(), value)) {
405 MessageDialog.openError(getParentShell(),
407 "Attribute value already exists.");
409 customValuesList.add(value);
410 customValuesList.deselectAll();
411 customValuesList.select(customValuesList.getItemCount() - 1);
412 customValuesList.showSelection();
413 remBtn.setEnabled(true);
419 remBtn.addSelectionListener(new SelectionAdapter() {
421 public void widgetSelected(SelectionEvent e) {
422 int[] selection = customValuesList.getSelectionIndices();
423 if (null != selection && selection.length > 0) {
424 customValuesList.remove(selection);
427 changeRemBtnVisibility();
431 customValuesList.addSelectionListener(new SelectionAdapter() {
433 public void widgetSelected(SelectionEvent e) {
434 changeRemBtnVisibility();
439 private void updateControls() {
440 rangeBtn.setSelection(false);
441 cusValuesBtn.setSelection(false);
442 noneBtn.setSelection(true);
444 cleanRangeAndCustomValues();
445 dflValueTxt.setText("");
447 String selected = attTypeCmb.getText();
449 if (selected.equals(Constants.INT)) {
451 rangeOptionSelected(false);
452 customOptionSelected(false);
453 detail.setText(defaultMessage + msgForIntType);
454 } else if (selected.equals(Constants.DOUBLE)) {
456 rangeOptionSelected(false);
457 customOptionSelected(false);
458 detail.setText(defaultMessage + msgForDoubleType);
459 } else if (selected.equals(Constants.BOOL)) {
461 rangeOptionSelected(false);
462 customOptionSelected(false);
463 detail.setText(defaultMessage + msgForBoolType);
464 } else if (selected.equals(Constants.STRING)) {
465 rangeBtn.setEnabled(false);
466 rangeOptionSelected(false);
467 cusValuesBtn.setEnabled(true);
468 noneBtn.setEnabled(true);
469 detail.setText(defaultMessage + msgForStringType);
472 rangeOptionSelected(false);
473 customOptionSelected(false);
474 detail.setText(defaultMessage);
478 private void changeRemBtnVisibility() {
479 if (cusValuesBtn.isEnabled() && cusValuesBtn.getSelection()) {
480 int[] selection = customValuesList.getSelectionIndices();
481 if (null != selection && selection.length > 0) {
482 remBtn.setEnabled(true);
484 remBtn.setEnabled(false);
487 remBtn.setEnabled(false);
491 private void enable(boolean enable) {
492 rangeBtn.setEnabled(enable);
493 cusValuesBtn.setEnabled(enable);
494 noneBtn.setEnabled(enable);
497 private void rangeOptionSelected(boolean enable) {
498 minRangeTxt.setEnabled(enable);
499 maxRangeTxt.setEnabled(enable);
500 minLbl.setEnabled(enable);
501 maxLbl.setEnabled(enable);
504 private void customOptionSelected(boolean enable) {
505 customValuesList.setEnabled(enable);
506 addBtn.setEnabled(enable);
507 changeRemBtnVisibility();
510 private void cleanRangeAndCustomValues() {
515 private void cleanRangeValues() {
516 minRangeTxt.setText("");
517 maxRangeTxt.setText("");
520 private void cleanCustomValues() {
521 customValuesList.removeAll();
524 public AttributeHelper getAttHelper() {
528 public AttributeHelper getAttClone() {
533 protected void okPressed() {
535 String attName = attNameTxt.getText();
536 if (null == attName || attName.isEmpty()) {
537 MessageDialog.openError(getParentShell(),
538 "Mandatory field is empty",
539 "Please fill the attribute name.");
540 attNameTxt.setFocus();
543 attName = attName.trim();
544 if (attName.length() < 1) {
545 MessageDialog.openError(getParentShell(),
546 "Mandatory field is empty", "Attribute name is invalid.");
547 attNameTxt.setFocus();
550 attHelper.setAttributeName(attName);
552 // Duplicate check for attribute name if this is not editing operation
553 if (!editOperation) {
554 if (!attributes.isEmpty()) {
555 Iterator<AttributeHelper> itr = attributes.iterator();
557 while (itr.hasNext()) {
559 if (att.getAttributeName().equals(attName)) {
563 "Duplicate Attribute",
564 "Another attribute with the same attribute name exist. Please enter a new attribute name.");
565 attNameTxt.setFocus();
573 String attType = attTypeCmb.getText();
574 if (null == attType || attType.isEmpty()) {
575 MessageDialog.openError(getParentShell(),
576 "Mandatory field is empty",
577 "Please select an attribute type.");
580 attHelper.setAttributeType(attType);
583 if (rangeBtn.isEnabled() && rangeBtn.getSelection()) { // Range option
584 String min = minRangeTxt.getText();
585 String max = maxRangeTxt.getText();
586 if (null == min && null == max) {
588 .openError(getParentShell(), "Range is empty",
589 "Please enter the minimum and maximum value of the range.");
590 minRangeTxt.setFocus();
593 if (null == min || null == max) {
595 msg = "Please enter the "
596 + ((null == min) ? "minimum value" : "maximum value")
599 .openError(getParentShell(), "Range is empty", msg);
600 ((null == min) ? minRangeTxt : maxRangeTxt).setFocus();
603 if (min.isEmpty() && max.isEmpty()) {
605 .openError(getParentShell(), "Range is empty",
606 "Please enter the minimum and maximum value of the range.");
607 minRangeTxt.setFocus();
611 MessageDialog.openError(getParentShell(), "Range is empty",
612 "Please enter the minimum value for the range.");
613 minRangeTxt.setFocus();
617 MessageDialog.openError(getParentShell(), "Range is empty",
618 "Please enter the maximum value for the range.");
619 maxRangeTxt.setFocus();
623 if (!min.isEmpty() && !max.isEmpty()
624 && !attHelper.isRangeValid(min, max, attType)) {
625 MessageDialog.openError(getParentShell(), "Invalid range",
626 "Range is invalid.");
627 maxRangeTxt.setFocus();
630 attHelper.setValidValuesType(Type.RANGE);
631 attHelper.setMin(min);
632 attHelper.setMax(max);
635 // Remove all existing custom values
636 attHelper.setAllowedValues(null);
638 } else if (cusValuesBtn.isEnabled() && cusValuesBtn.getSelection()) {
639 String[] cusItems = customValuesList.getItems();
640 if (null == cusItems || cusItems.length < 1) {
641 MessageDialog.openError(getParentShell(),
642 "Custom list is empty.",
643 "No values are added to the custom list.");
644 maxRangeTxt.setFocus();
647 attHelper.setValidValuesType(Type.VALUESET);
648 attHelper.setAllowedValuesByArray(cusItems);
651 // Remove min and max values
652 attHelper.setMin(null);
653 attHelper.setMax(null);
655 } else if (noneBtn.isEnabled() && noneBtn.getSelection()) {
656 attHelper.setValidValuesType(Type.UNKNOWN);
658 // Remove min, max and custom values
659 attHelper.setAllowedValues(null);
660 attHelper.setMin(null);
661 attHelper.setMax(null);
665 // Attribute Default Value
666 String attDflValue = dflValueTxt.getText();
667 if (null == attDflValue || attDflValue.isEmpty()) {
668 MessageDialog.openError(getParentShell(),
669 "Mandatory field is empty",
670 "Please enter a default value for the attribute.");
671 dflValueTxt.setFocus();
674 attDflValue = attDflValue.trim();
675 if (attDflValue.length() < 1
676 || !attHelper.isValueValid(attDflValue, attType)) {
677 MessageDialog.openError(getParentShell(), "Invalid value",
678 "Default value is invalid.");
679 dflValueTxt.setFocus();
682 if (!attHelper.isDefaultValueValid(attDflValue)) {
683 MessageDialog.openError(getParentShell(), "Invalid value",
684 "Default value is not in the valid values.");
685 dflValueTxt.setFocus();
689 attHelper.setAttributeDflValue(attDflValue.toLowerCase());
695 public boolean isHelpAvailable() {
700 protected Point getInitialLocation(Point initialSize) {
701 Rectangle shellBounds = Display.getDefault().getActiveShell()
703 Point dialogSize = getInitialSize();
706 shellBounds.x + (shellBounds.width - dialogSize.x) / 2,
707 shellBounds.y + (shellBounds.height - dialogSize.y) / 2);