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.model.AttributeHelper.ValidValuesType;
24 import oic.simulator.serviceprovider.utils.Constants;
26 import org.eclipse.jface.dialogs.MessageDialog;
27 import org.eclipse.jface.dialogs.TitleAreaDialog;
28 import org.eclipse.jface.window.Window;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.custom.CCombo;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.graphics.Point;
34 import org.eclipse.swt.graphics.Rectangle;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Button;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Display;
41 import org.eclipse.swt.widgets.Event;
42 import org.eclipse.swt.widgets.Group;
43 import org.eclipse.swt.widgets.Label;
44 import org.eclipse.swt.widgets.List;
45 import org.eclipse.swt.widgets.Listener;
46 import org.eclipse.swt.widgets.Shell;
47 import org.eclipse.swt.widgets.Text;
48 import org.oic.simulator.AttributeProperty.Type;
50 public class AddAttributeDialog extends TitleAreaDialog {
52 private Text attNameTxt;
53 private Text minRangeTxt;
54 private Text maxRangeTxt;
55 private CCombo attTypeCmb;
56 private Text dflValueTxt;
57 private Button rangeBtn;
58 private Button cusValuesBtn;
59 private Button noneBtn;
60 private Button addBtn;
61 private Button remBtn;
64 private List customValuesList;
67 private AttributeHelper attHelper;
69 private AttributeHelper attClone;
71 private Set<AttributeHelper> attributes;
73 private final String defaultMessage = "Name, Type, and Default Value fields "
74 + "are mandatory.\n\nRange and custom fields allow to set the valid "
75 + "values of the attribute.\n\n";
76 private final String msgForBoolType = "Possible attribute values of Bool are "
77 + "true and false.\nSo range and custom options are disabled.";
78 private final String msgForIntType = "Valid values for Int type can either be "
79 + "of range type (Ex: 1 - 10) or custom values (Ex: 10, 20, 50, and 100).\n";
80 private final String msgForDoubleType = "Valid values for Double type can either be "
81 + "of range type (Ex: 18.0 - 22.0) or custom values (Ex: 1.5, 2.5, 3.9, 4.8, etc).\n";
82 private final String msgForStringType = "For String type, range option is not"
83 + "applicable. Hence it is disabled.\n\n"
84 + "Custom option is available to provide the valid values.\n\n"
85 + "Ex: low, mid, high, etc.";
87 private Set<String> attValueTypes;
89 private boolean editOperation;
91 public AddAttributeDialog(Shell parentShell, AttributeHelper att,
92 Set<String> attValueTypes, Set<AttributeHelper> attributes) {
95 att = new AttributeHelper();
97 attClone = att.clone();
101 this.attValueTypes = attValueTypes;
102 this.attributes = attributes;
106 public void create() {
108 setTitle("Add Attribute");
109 setMessage("Fill the details for creating an attribute");
113 protected Control createDialogArea(Composite parent) {
114 Composite compLayout = (Composite) super.createDialogArea(parent);
115 Composite container = new Composite(compLayout, SWT.NONE);
116 container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
117 GridLayout layout = new GridLayout(5, false);
118 container.setLayout(layout);
120 Label attNameLbl = new Label(container, SWT.NULL);
121 attNameLbl.setText("Attribute Name:");
123 attNameTxt = new Text(container, SWT.BORDER);
124 GridData gd = new GridData();
125 gd.horizontalAlignment = SWT.FILL;
126 gd.horizontalSpan = 4;
127 gd.grabExcessHorizontalSpace = true;
128 attNameTxt.setLayoutData(gd);
130 Label attTypeLbl = new Label(container, SWT.NULL);
131 attTypeLbl.setText("Attribute Type:");
133 attTypeCmb = new CCombo(container, SWT.READ_ONLY | SWT.BORDER);
135 gd.grabExcessHorizontalSpace = true;
136 gd.horizontalSpan = 4;
137 gd.horizontalAlignment = SWT.FILL;
138 attTypeCmb.setLayoutData(gd);
141 Group valuesGrp = new Group(container, SWT.NULL);
142 valuesGrp.setText("Attribute Values");
144 gd.verticalIndent = 10;
145 gd.horizontalSpan = 3;
146 gd.grabExcessHorizontalSpace = true;
147 gd.horizontalAlignment = SWT.FILL;
148 gd.grabExcessVerticalSpace = true;
149 gd.verticalAlignment = SWT.FILL;
150 valuesGrp.setLayoutData(gd);
151 layout = new GridLayout(4, false);
152 valuesGrp.setLayout(layout);
154 rangeBtn = new Button(valuesGrp, SWT.RADIO);
155 rangeBtn.setText("Range");
157 gd.horizontalSpan = 4;
158 rangeBtn.setLayoutData(gd);
160 minLbl = new Label(valuesGrp, SWT.NONE);
161 minLbl.setText("Min:");
163 gd.horizontalIndent = 25;
164 minLbl.setLayoutData(gd);
166 minRangeTxt = new Text(valuesGrp, SWT.BORDER);
169 minRangeTxt.setLayoutData(gd);
171 maxLbl = new Label(valuesGrp, SWT.NONE);
172 maxLbl.setText("Max:");
174 gd.horizontalIndent = 25;
175 maxLbl.setLayoutData(gd);
177 maxRangeTxt = new Text(valuesGrp, SWT.BORDER);
180 maxRangeTxt.setLayoutData(gd);
182 cusValuesBtn = new Button(valuesGrp, SWT.RADIO);
183 cusValuesBtn.setText("Custom");
185 gd.horizontalSpan = 4;
186 cusValuesBtn.setLayoutData(gd);
188 Composite cusValuesComp = new Composite(valuesGrp, SWT.NONE);
189 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
190 gd.horizontalSpan = 4;
191 cusValuesComp.setLayoutData(gd);
192 layout = new GridLayout(2, false);
193 cusValuesComp.setLayout(layout);
195 customValuesList = new List(cusValuesComp, SWT.BORDER | SWT.MULTI
197 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
199 gd.horizontalIndent = 25;
200 customValuesList.setLayoutData(gd);
202 Composite cusValuesActionsComp = new Composite(cusValuesComp, SWT.NONE);
203 layout = new GridLayout();
204 cusValuesActionsComp.setLayout(layout);
206 gd.verticalAlignment = SWT.TOP;
207 cusValuesActionsComp.setLayoutData(gd);
209 addBtn = new Button(cusValuesActionsComp, SWT.PUSH);
210 addBtn.setText("Add");
213 addBtn.setLayoutData(gd);
215 remBtn = new Button(cusValuesActionsComp, SWT.PUSH);
216 remBtn.setText("Remove");
219 remBtn.setLayoutData(gd);
220 remBtn.setEnabled(false);
222 noneBtn = new Button(valuesGrp, SWT.RADIO);
223 noneBtn.setText("None");
225 gd.horizontalSpan = 4;
226 noneBtn.setLayoutData(gd);
228 Composite detailsComp = new Composite(container, SWT.NULL);
229 detailsComp.setLayout(new GridLayout());
230 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
231 gd.horizontalSpan = 2;
233 detailsComp.setLayoutData(gd);
235 Label lbl = new Label(detailsComp, SWT.NULL);
236 lbl.setText("Details");
238 Group detailsGrp = new Group(detailsComp, SWT.NULL);
239 detailsGrp.setLayout(new GridLayout());
240 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
241 detailsGrp.setLayoutData(gd);
243 detail = new Text(detailsGrp, SWT.MULTI | SWT.READ_ONLY | SWT.BORDER
244 | SWT.WRAP | SWT.V_SCROLL);
245 detail.setBackground(detailsGrp.getBackground());
246 detail.setText(defaultMessage);
247 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
248 detail.setLayoutData(gd);
250 Label dflValueLbl = new Label(container, SWT.NULL);
251 dflValueLbl.setText("Default value");
253 dflValueTxt = new Text(container, SWT.BORDER);
255 gd.horizontalSpan = 4;
256 gd.grabExcessHorizontalSpace = true;
257 gd.horizontalAlignment = SWT.FILL;
258 dflValueTxt.setLayoutData(gd);
264 attNameTxt.setFocus();
266 setInitialSelection();
272 private void initTypes() {
273 if (null != attValueTypes && attValueTypes.size() > 0) {
274 Iterator<String> itr = attValueTypes.iterator();
275 while (itr.hasNext()) {
276 attTypeCmb.add(itr.next());
281 private void setInitialSelection() {
283 rangeOptionSelected(false);
284 customOptionSelected(false);
287 private void initData() {
289 // Populate the UI controls with the data.
290 attNameTxt.setText(attHelper.getAttributeName());
291 attTypeCmb.select(attTypeCmb.indexOf(attHelper.getAttributeType()));
293 dflValueTxt.setText(attHelper.getAttributeDflValue());
294 ValidValuesType valuesType = attHelper.getValidValuesType();
295 if (valuesType == ValidValuesType.RANGE) {
296 rangeBtn.setSelection(true);
297 noneBtn.setSelection(false);
298 rangeOptionSelected(true);
299 minRangeTxt.setText(attHelper.getMin());
300 maxRangeTxt.setText(attHelper.getMax());
301 } else if (valuesType == ValidValuesType.VALUESET) {
302 cusValuesBtn.setSelection(true);
303 noneBtn.setSelection(false);
304 customOptionSelected(true);
305 Set<String> allowedValues = attHelper.getAllowedValues();
306 customValuesList.setItems(allowedValues.toArray(new String[1]));
311 private void setUiListeners() {
312 rangeBtn.addSelectionListener(new SelectionAdapter() {
314 public void widgetSelected(SelectionEvent e) {
315 rangeOptionSelected(true);
316 customOptionSelected(false);
317 minRangeTxt.setFocus();
321 cusValuesBtn.addSelectionListener(new SelectionAdapter() {
323 public void widgetSelected(SelectionEvent e) {
325 rangeOptionSelected(false);
326 customOptionSelected(true);
330 noneBtn.addSelectionListener(new SelectionAdapter() {
332 public void widgetSelected(SelectionEvent e) {
333 rangeOptionSelected(false);
334 customOptionSelected(false);
338 attTypeCmb.addSelectionListener(new SelectionAdapter() {
340 public void widgetSelected(SelectionEvent e) {
345 minRangeTxt.addListener(SWT.Verify, new Listener() {
347 public void handleEvent(Event e) {
348 String string = e.text;
349 char[] chars = new char[string.length()];
350 string.getChars(0, chars.length, chars, 0);
351 for (int i = 0; i < chars.length; i++) {
352 if (!(('0' <= chars[i] && chars[i] <= '9')
353 || chars[i] == '-' || chars[i] == '+')) {
354 if (attTypeCmb.getText().equals(Constants.INT)) {
357 } else if (attTypeCmb.getText()
358 .equals(Constants.DOUBLE)) {
359 if (!(chars[i] == '.')) {
369 maxRangeTxt.addListener(SWT.Verify, new Listener() {
371 public void handleEvent(Event e) {
372 String string = e.text;
373 char[] chars = new char[string.length()];
374 string.getChars(0, chars.length, chars, 0);
375 for (int i = 0; i < chars.length; i++) {
376 if (!(('0' <= chars[i] && chars[i] <= '9')
377 || chars[i] == '-' || chars[i] == '+')) {
378 if (attTypeCmb.getText().equals(Constants.INT)) {
381 } else if (attTypeCmb.getText()
382 .equals(Constants.DOUBLE)) {
383 if (!(chars[i] == '.')) {
393 addBtn.addSelectionListener(new SelectionAdapter() {
395 public void widgetSelected(SelectionEvent e) {
396 SingleTextInputDialog dialog = new SingleTextInputDialog(
397 getShell(), "Add Attribute Value", "Attribute Value");
398 if (dialog.open() == Window.OK) {
399 String value = dialog.getValue();
400 String type = attTypeCmb.getText();
401 if (!attHelper.isValueValid(value, type)) {
402 MessageDialog.openError(getParentShell(),
403 "Invalid value", "Attribute value is invalid.");
404 } else if (attHelper.isAllowedValueExist(
405 customValuesList.getItems(), value)) {
406 MessageDialog.openError(getParentShell(),
408 "Attribute value already exists.");
410 customValuesList.add(value);
411 customValuesList.deselectAll();
412 customValuesList.select(customValuesList.getItemCount() - 1);
413 customValuesList.showSelection();
414 remBtn.setEnabled(true);
420 remBtn.addSelectionListener(new SelectionAdapter() {
422 public void widgetSelected(SelectionEvent e) {
423 int[] selection = customValuesList.getSelectionIndices();
424 if (null != selection && selection.length > 0) {
425 customValuesList.remove(selection);
428 changeRemBtnVisibility();
432 customValuesList.addSelectionListener(new SelectionAdapter() {
434 public void widgetSelected(SelectionEvent e) {
435 changeRemBtnVisibility();
440 private void updateControls() {
441 rangeBtn.setSelection(false);
442 cusValuesBtn.setSelection(false);
443 noneBtn.setSelection(true);
445 cleanRangeAndCustomValues();
446 dflValueTxt.setText("");
448 String selected = attTypeCmb.getText();
450 if (selected.equals(Constants.INT)) {
452 rangeOptionSelected(false);
453 customOptionSelected(false);
454 detail.setText(defaultMessage + msgForIntType);
455 } else if (selected.equals(Constants.DOUBLE)) {
457 rangeOptionSelected(false);
458 customOptionSelected(false);
459 detail.setText(defaultMessage + msgForDoubleType);
460 } else if (selected.equals(Constants.BOOL)) {
462 rangeOptionSelected(false);
463 customOptionSelected(false);
464 detail.setText(defaultMessage + msgForBoolType);
465 } else if (selected.equals(Constants.STRING)) {
466 rangeBtn.setEnabled(false);
467 rangeOptionSelected(false);
468 cusValuesBtn.setEnabled(true);
469 noneBtn.setEnabled(true);
470 detail.setText(defaultMessage + msgForStringType);
473 rangeOptionSelected(false);
474 customOptionSelected(false);
475 detail.setText(defaultMessage);
479 private void changeRemBtnVisibility() {
480 if (cusValuesBtn.isEnabled() && cusValuesBtn.getSelection()) {
481 int[] selection = customValuesList.getSelectionIndices();
482 if (null != selection && selection.length > 0) {
483 remBtn.setEnabled(true);
485 remBtn.setEnabled(false);
488 remBtn.setEnabled(false);
492 private void enable(boolean enable) {
493 rangeBtn.setEnabled(enable);
494 cusValuesBtn.setEnabled(enable);
495 noneBtn.setEnabled(enable);
498 private void rangeOptionSelected(boolean enable) {
499 minRangeTxt.setEnabled(enable);
500 maxRangeTxt.setEnabled(enable);
501 minLbl.setEnabled(enable);
502 maxLbl.setEnabled(enable);
505 private void customOptionSelected(boolean enable) {
506 customValuesList.setEnabled(enable);
507 addBtn.setEnabled(enable);
508 changeRemBtnVisibility();
511 private void cleanRangeAndCustomValues() {
516 private void cleanRangeValues() {
517 minRangeTxt.setText("");
518 maxRangeTxt.setText("");
521 private void cleanCustomValues() {
522 customValuesList.removeAll();
525 public AttributeHelper getAttHelper() {
529 public AttributeHelper getAttClone() {
534 protected void okPressed() {
536 String attName = attNameTxt.getText();
537 if (null == attName || attName.isEmpty()) {
538 MessageDialog.openError(getParentShell(),
539 "Mandatory field is empty",
540 "Please fill the attribute name.");
541 attNameTxt.setFocus();
544 attName = attName.trim();
545 if (attName.length() < 1) {
546 MessageDialog.openError(getParentShell(),
547 "Mandatory field is empty", "Attribute name is invalid.");
548 attNameTxt.setFocus();
551 attHelper.setAttributeName(attName);
553 // Duplicate check for attribute name if this is not editing operation
554 if (!editOperation) {
555 if (!attributes.isEmpty()) {
556 Iterator<AttributeHelper> itr = attributes.iterator();
558 while (itr.hasNext()) {
560 if (att.getAttributeName().equals(attName)) {
564 "Duplicate Attribute",
565 "Another attribute with the same attribute name exist. Please enter a new attribute name.");
566 attNameTxt.setFocus();
574 String attType = attTypeCmb.getText();
575 if (null == attType || attType.isEmpty()) {
576 MessageDialog.openError(getParentShell(),
577 "Mandatory field is empty",
578 "Please select an attribute type.");
581 attHelper.setAttributeType(attType);
584 if (rangeBtn.isEnabled() && rangeBtn.getSelection()) { // Range option
585 String min = minRangeTxt.getText();
586 String max = maxRangeTxt.getText();
587 if (null == min && null == max) {
589 .openError(getParentShell(), "Range is empty",
590 "Please enter the minimum and maximum value of the range.");
591 minRangeTxt.setFocus();
594 if (null == min || null == max) {
596 msg = "Please enter the "
597 + ((null == min) ? "minimum value" : "maximum value")
600 .openError(getParentShell(), "Range is empty", msg);
601 ((null == min) ? minRangeTxt : maxRangeTxt).setFocus();
604 if (min.isEmpty() && max.isEmpty()) {
606 .openError(getParentShell(), "Range is empty",
607 "Please enter the minimum and maximum value of the range.");
608 minRangeTxt.setFocus();
612 MessageDialog.openError(getParentShell(), "Range is empty",
613 "Please enter the minimum value for the range.");
614 minRangeTxt.setFocus();
618 MessageDialog.openError(getParentShell(), "Range is empty",
619 "Please enter the maximum value for the range.");
620 maxRangeTxt.setFocus();
624 if (!min.isEmpty() && !max.isEmpty()
625 && !attHelper.isRangeValid(min, max, attType)) {
626 MessageDialog.openError(getParentShell(), "Invalid range",
627 "Range is invalid.");
628 maxRangeTxt.setFocus();
631 attHelper.setValidValuesType(ValidValuesType.RANGE);
632 attHelper.setMin(min);
633 attHelper.setMax(max);
636 // Remove all existing custom values
637 attHelper.setAllowedValues(null);
639 } else if (cusValuesBtn.isEnabled() && cusValuesBtn.getSelection()) {
640 String[] cusItems = customValuesList.getItems();
641 if (null == cusItems || cusItems.length < 1) {
642 MessageDialog.openError(getParentShell(),
643 "Custom list is empty.",
644 "No values are added to the custom list.");
645 maxRangeTxt.setFocus();
648 attHelper.setValidValuesType(ValidValuesType.VALUESET);
649 attHelper.setAllowedValuesByArray(cusItems);
652 // Remove min and max values
653 attHelper.setMin(null);
654 attHelper.setMax(null);
656 } else if (noneBtn.isEnabled() && noneBtn.getSelection()) {
657 attHelper.setValidValuesType(ValidValuesType.UNKNOWN);
659 // Remove min, max and custom values
660 attHelper.setAllowedValues(null);
661 attHelper.setMin(null);
662 attHelper.setMax(null);
666 // Attribute Default Value
667 String attDflValue = dflValueTxt.getText();
668 if (null == attDflValue || attDflValue.isEmpty()) {
669 MessageDialog.openError(getParentShell(),
670 "Mandatory field is empty",
671 "Please enter a default value for the attribute.");
672 dflValueTxt.setFocus();
675 attDflValue = attDflValue.trim();
676 if (attDflValue.length() < 1
677 || !attHelper.isValueValid(attDflValue, attType)) {
678 MessageDialog.openError(getParentShell(), "Invalid value",
679 "Default value is invalid.");
680 dflValueTxt.setFocus();
683 if (!attHelper.isDefaultValueValid(attDflValue)) {
684 MessageDialog.openError(getParentShell(), "Invalid value",
685 "Default value is not in the valid values.");
686 dflValueTxt.setFocus();
690 attHelper.setAttributeDflValue(attDflValue.toLowerCase());
696 public boolean isHelpAvailable() {
701 protected Point getInitialLocation(Point initialSize) {
702 Rectangle shellBounds = Display.getDefault().getActiveShell()
704 Point dialogSize = getInitialSize();
707 shellBounds.x + (shellBounds.width - dialogSize.x) / 2,
708 shellBounds.y + (shellBounds.height - dialogSize.y) / 2);