1 package oic.simulator.serviceprovider.view.dialogs;
3 import java.util.Iterator;
6 import oic.simulator.serviceprovider.model.AttributeHelper;
7 import oic.simulator.serviceprovider.utils.Constants;
9 import org.eclipse.jface.dialogs.MessageDialog;
10 import org.eclipse.jface.dialogs.TitleAreaDialog;
11 import org.eclipse.jface.window.Window;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.custom.CCombo;
14 import org.eclipse.swt.events.SelectionAdapter;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.graphics.Point;
17 import org.eclipse.swt.graphics.Rectangle;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.swt.widgets.Event;
25 import org.eclipse.swt.widgets.Group;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.List;
28 import org.eclipse.swt.widgets.Listener;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.swt.widgets.Text;
31 import org.oic.simulator.AttributeProperty.Type;
33 public class AddAttributeDialog extends TitleAreaDialog {
35 private Text attNameTxt;
36 private Text minRangeTxt;
37 private Text maxRangeTxt;
38 private CCombo attTypeCmb;
39 private Text dflValueTxt;
40 private Button rangeBtn;
41 private Button cusValuesBtn;
42 private Button noneBtn;
43 private Button addBtn;
44 private Button remBtn;
47 private List customValuesList;
50 private AttributeHelper attHelper;
52 private AttributeHelper attClone;
54 private Set<AttributeHelper> attributes;
56 private final String defaultMessage = "Name, Type, and Default Value fields "
57 + "are mandatory.\n\nRange and custom fields allow to set the valid "
58 + "values of the attribute.\n\n";
59 private final String msgForBoolType = "Possible attribute values of Bool are "
60 + "true and false.\nSo range and custom options are disabled.";
61 private final String msgForIntType = "Valid values for Int type can either be "
62 + "of range type (Ex: 1 - 10) or custom values (Ex: 10, 20, 50, and 100).\n";
63 private final String msgForDoubleType = "Valid values for Double type can either be "
64 + "of range type (Ex: 18.0 - 22.0) or custom values (Ex: 1.5, 2.5, 3.9, 4.8, etc).\n";
65 private final String msgForStringType = "For String type, range option is not"
66 + "applicable. Hence it is disabled.\n\n"
67 + "Custom option is available to provide the valid values.\n\n"
68 + "Ex: low, mid, high, etc.";
70 private Set<String> attValueTypes;
72 private boolean editOperation;
74 public AddAttributeDialog(Shell parentShell, AttributeHelper att,
75 Set<String> attValueTypes, Set<AttributeHelper> attributes) {
78 att = new AttributeHelper();
80 attClone = att.clone();
81 System.out.println("Cloned attribute's data:" + attClone);
85 this.attValueTypes = attValueTypes;
86 this.attributes = attributes;
90 public void create() {
92 setTitle("Add Attribute");
93 setMessage("Fill the details for creating an attribute");
97 protected Control createDialogArea(Composite parent) {
98 Composite compLayout = (Composite) super.createDialogArea(parent);
99 Composite container = new Composite(compLayout, SWT.NONE);
100 container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
101 GridLayout layout = new GridLayout(5, false);
103 * layout.verticalSpacing = 10; layout.marginTop = 10;
105 container.setLayout(layout);
107 Label attNameLbl = new Label(container, SWT.NULL);
108 attNameLbl.setText("Attribute Name:");
110 attNameTxt = new Text(container, SWT.BORDER);
111 GridData gd = new GridData();
112 gd.horizontalAlignment = SWT.FILL;
113 gd.horizontalSpan = 4;
114 gd.grabExcessHorizontalSpace = true;
115 attNameTxt.setLayoutData(gd);
117 Label attTypeLbl = new Label(container, SWT.NULL);
118 attTypeLbl.setText("Attribute Type:");
120 attTypeCmb = new CCombo(container, SWT.READ_ONLY | SWT.BORDER);
122 gd.grabExcessHorizontalSpace = true;
123 gd.horizontalSpan = 4;
124 gd.horizontalAlignment = SWT.FILL;
125 attTypeCmb.setLayoutData(gd);
128 Group valuesGrp = new Group(container, SWT.NULL);
129 valuesGrp.setText("Attribute Values");
131 gd.verticalIndent = 10;
132 gd.horizontalSpan = 3;
133 gd.grabExcessHorizontalSpace = true;
134 gd.horizontalAlignment = SWT.FILL;
135 gd.grabExcessVerticalSpace = true;
136 gd.verticalAlignment = SWT.FILL;
137 valuesGrp.setLayoutData(gd);
138 layout = new GridLayout(4, false);
139 valuesGrp.setLayout(layout);
141 rangeBtn = new Button(valuesGrp, SWT.RADIO);
142 rangeBtn.setText("Range");
144 gd.horizontalSpan = 4;
145 rangeBtn.setLayoutData(gd);
147 minLbl = new Label(valuesGrp, SWT.NONE);
148 minLbl.setText("Min:");
150 gd.horizontalIndent = 25;
151 minLbl.setLayoutData(gd);
153 minRangeTxt = new Text(valuesGrp, SWT.BORDER);
156 minRangeTxt.setLayoutData(gd);
158 maxLbl = new Label(valuesGrp, SWT.NONE);
159 maxLbl.setText("Max:");
161 gd.horizontalIndent = 25;
162 maxLbl.setLayoutData(gd);
164 maxRangeTxt = new Text(valuesGrp, SWT.BORDER);
167 maxRangeTxt.setLayoutData(gd);
169 cusValuesBtn = new Button(valuesGrp, SWT.RADIO);
170 cusValuesBtn.setText("Custom");
172 gd.horizontalSpan = 4;
173 cusValuesBtn.setLayoutData(gd);
175 Composite cusValuesComp = new Composite(valuesGrp, SWT.NONE);
176 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
177 gd.horizontalSpan = 4;
178 cusValuesComp.setLayoutData(gd);
179 layout = new GridLayout(2, false);
180 cusValuesComp.setLayout(layout);
182 customValuesList = new List(cusValuesComp, SWT.BORDER | SWT.MULTI
184 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
186 gd.horizontalIndent = 25;
187 customValuesList.setLayoutData(gd);
189 Composite cusValuesActionsComp = new Composite(cusValuesComp, SWT.NONE);
190 layout = new GridLayout();
191 cusValuesActionsComp.setLayout(layout);
193 gd.verticalAlignment = SWT.TOP;
194 cusValuesActionsComp.setLayoutData(gd);
196 addBtn = new Button(cusValuesActionsComp, SWT.PUSH);
197 addBtn.setText("Add");
200 addBtn.setLayoutData(gd);
202 remBtn = new Button(cusValuesActionsComp, SWT.PUSH);
203 remBtn.setText("Remove");
206 remBtn.setLayoutData(gd);
207 remBtn.setEnabled(false);
209 noneBtn = new Button(valuesGrp, SWT.RADIO);
210 noneBtn.setText("None");
212 gd.horizontalSpan = 4;
213 noneBtn.setLayoutData(gd);
215 Composite detailsComp = new Composite(container, SWT.NULL);
216 detailsComp.setLayout(new GridLayout());
217 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
218 gd.horizontalSpan = 2;
220 detailsComp.setLayoutData(gd);
222 Label lbl = new Label(detailsComp, SWT.NULL);
223 lbl.setText("Details");
225 Group detailsGrp = new Group(detailsComp, SWT.NULL);
226 detailsGrp.setLayout(new GridLayout());
227 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
228 detailsGrp.setLayoutData(gd);
230 detail = new Text(detailsGrp, SWT.MULTI | SWT.READ_ONLY | SWT.BORDER
231 | SWT.WRAP | SWT.V_SCROLL);
232 detail.setBackground(detailsGrp.getBackground());
233 detail.setText(defaultMessage);
234 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
235 detail.setLayoutData(gd);
237 Label dflValueLbl = new Label(container, SWT.NULL);
238 dflValueLbl.setText("Default value");
240 dflValueTxt = new Text(container, SWT.BORDER);
242 gd.horizontalSpan = 4;
243 gd.grabExcessHorizontalSpace = true;
244 gd.horizontalAlignment = SWT.FILL;
245 dflValueTxt.setLayoutData(gd);
251 attNameTxt.setFocus();
253 setInitialSelection();
259 private void initTypes() {
260 if (null != attValueTypes && attValueTypes.size() > 0) {
261 Iterator<String> itr = attValueTypes.iterator();
262 while (itr.hasNext()) {
263 attTypeCmb.add(itr.next());
268 private void setInitialSelection() {
270 rangeOptionSelected(false);
271 customOptionSelected(false);
274 private void initData() {
276 // Populate the UI controls with the data.
277 attNameTxt.setText(attHelper.getAttributeName());
278 attTypeCmb.select(attTypeCmb.indexOf(attHelper.getAttributeType()));
280 dflValueTxt.setText(attHelper.getAttributeDflValue());
281 Type valuesType = attHelper.getValidValuesType();
283 * if (valuesType == Type.UNKNOWN) { noneBtn.setSelection(true); }
285 */if (valuesType == Type.RANGE) {
286 rangeBtn.setSelection(true);
287 noneBtn.setSelection(false);
288 rangeOptionSelected(true);
289 minRangeTxt.setText(attHelper.getMin());
290 maxRangeTxt.setText(attHelper.getMax());
291 } else if (valuesType == Type.VALUESET) {
292 cusValuesBtn.setSelection(true);
293 noneBtn.setSelection(false);
294 customOptionSelected(true);
295 Set<String> allowedValues = attHelper.getAllowedValues();
296 customValuesList.setItems(allowedValues.toArray(new String[1]));
301 private void setUiListeners() {
302 rangeBtn.addSelectionListener(new SelectionAdapter() {
304 public void widgetSelected(SelectionEvent e) {
305 rangeOptionSelected(true);
306 customOptionSelected(false);
307 minRangeTxt.setFocus();
311 cusValuesBtn.addSelectionListener(new SelectionAdapter() {
313 public void widgetSelected(SelectionEvent e) {
315 rangeOptionSelected(false);
316 customOptionSelected(true);
320 noneBtn.addSelectionListener(new SelectionAdapter() {
322 public void widgetSelected(SelectionEvent e) {
323 rangeOptionSelected(false);
324 customOptionSelected(false);
328 attTypeCmb.addSelectionListener(new SelectionAdapter() {
330 public void widgetSelected(SelectionEvent e) {
335 minRangeTxt.addListener(SWT.Verify, new Listener() {
337 public void handleEvent(Event e) {
338 String string = e.text;
339 System.out.println(string);
340 char[] chars = new char[string.length()];
341 string.getChars(0, chars.length, chars, 0);
342 for (int i = 0; i < chars.length; i++) {
343 if (!(('0' <= chars[i] && chars[i] <= '9')
344 || chars[i] == '-' || chars[i] == '+')) {
345 if (attTypeCmb.getText().equals(Constants.INT)) {
348 } else if (attTypeCmb.getText()
349 .equals(Constants.DOUBLE)) {
350 if (!(chars[i] == '.')) {
357 // attHelper.setMin(minRangeTxt.getText() + string);
361 maxRangeTxt.addListener(SWT.Verify, new Listener() {
363 public void handleEvent(Event e) {
364 String string = e.text;
365 char[] chars = new char[string.length()];
366 string.getChars(0, chars.length, chars, 0);
367 for (int i = 0; i < chars.length; i++) {
368 if (!(('0' <= chars[i] && chars[i] <= '9')
369 || chars[i] == '-' || chars[i] == '+')) {
370 if (attTypeCmb.getText().equals(Constants.INT)) {
373 } else if (attTypeCmb.getText()
374 .equals(Constants.DOUBLE)) {
375 if (!(chars[i] == '.')) {
382 // attHelper.setMax(maxRangeTxt.getText() + string);
386 addBtn.addSelectionListener(new SelectionAdapter() {
388 public void widgetSelected(SelectionEvent e) {
389 SingleTextInputDialog dialog = new SingleTextInputDialog(
390 getShell(), "Add Attribute Value", "Attribute Value");
391 if (dialog.open() == Window.OK) {
392 System.out.println(dialog.getValue());
393 String value = dialog.getValue();
394 String type = attTypeCmb.getText();
395 if (!attHelper.isValueValid(value, type)) {
396 MessageDialog.openError(getParentShell(),
397 "Invalid value", "Attribute value is invalid.");
398 } else if (attHelper.isAllowedValueExist(
399 customValuesList.getItems(), value)) {
400 MessageDialog.openError(getParentShell(),
402 "Attribute value already exists.");
404 customValuesList.add(value);
405 customValuesList.deselectAll();
406 customValuesList.select(customValuesList.getItemCount() - 1);
407 customValuesList.showSelection();
408 remBtn.setEnabled(true);
409 // attHelper.addValueToAllowedValues(value);
415 remBtn.addSelectionListener(new SelectionAdapter() {
417 public void widgetSelected(SelectionEvent e) {
418 int[] selection = customValuesList.getSelectionIndices();
419 // String[] selectionStr = customValuesList.getSelection();
420 if (null != selection && selection.length > 0) {
421 customValuesList.remove(selection);
423 * for (String selected : selectionStr) {
424 * attHelper.removeValueFromAllowedValues(selected); }
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();
449 // attHelper.setAttributeType(selected);
451 if (selected.equals(Constants.INT)) {
453 rangeOptionSelected(false);
454 customOptionSelected(false);
455 detail.setText(defaultMessage + msgForIntType);
456 } else if (selected.equals(Constants.DOUBLE)) {
458 rangeOptionSelected(false);
459 customOptionSelected(false);
460 detail.setText(defaultMessage + msgForDoubleType);
461 } else if (selected.equals(Constants.BOOL)) {
462 // noneBtn.setSelection(true);
464 rangeOptionSelected(false);
465 customOptionSelected(false);
466 detail.setText(defaultMessage + msgForBoolType);
467 } else if (selected.equals(Constants.STRING)) {
468 rangeBtn.setEnabled(false);
469 rangeOptionSelected(false);
470 cusValuesBtn.setEnabled(true);
471 noneBtn.setEnabled(true);
472 // cusValuesBtn.setSelection(true);
473 detail.setText(defaultMessage + msgForStringType);
475 // noneBtn.setSelection(true);
477 rangeOptionSelected(false);
478 customOptionSelected(false);
479 detail.setText(defaultMessage);
483 private void changeRemBtnVisibility() {
484 if (cusValuesBtn.isEnabled() && cusValuesBtn.getSelection()) {
485 int[] selection = customValuesList.getSelectionIndices();
486 if (null != selection && selection.length > 0) {
487 remBtn.setEnabled(true);
489 remBtn.setEnabled(false);
492 remBtn.setEnabled(false);
496 private void enable(boolean enable) {
497 rangeBtn.setEnabled(enable);
498 cusValuesBtn.setEnabled(enable);
499 noneBtn.setEnabled(enable);
502 private void rangeOptionSelected(boolean enable) {
503 minRangeTxt.setEnabled(enable);
504 maxRangeTxt.setEnabled(enable);
505 minLbl.setEnabled(enable);
506 maxLbl.setEnabled(enable);
509 private void customOptionSelected(boolean enable) {
510 customValuesList.setEnabled(enable);
511 addBtn.setEnabled(enable);
512 changeRemBtnVisibility();
515 private void cleanRangeAndCustomValues() {
520 private void cleanRangeValues() {
521 minRangeTxt.setText("");
522 maxRangeTxt.setText("");
525 private void cleanCustomValues() {
526 customValuesList.removeAll();
529 public AttributeHelper getAttHelper() {
533 public AttributeHelper getAttClone() {
538 protected void okPressed() {
540 String attName = attNameTxt.getText();
541 if (null == attName || attName.isEmpty()) {
542 MessageDialog.openError(getParentShell(),
543 "Mandatory field is empty",
544 "Please fill the attribute name.");
545 attNameTxt.setFocus();
548 attName = attName.trim();
549 if (attName.length() < 1) {
550 MessageDialog.openError(getParentShell(),
551 "Mandatory field is empty", "Attribute name is invalid.");
552 attNameTxt.setFocus();
555 attHelper.setAttributeName(attName);
557 // Duplicate check for attribute name if this is not editing operation
558 if (!editOperation) {
559 if (!attributes.isEmpty()) {
560 Iterator<AttributeHelper> itr = attributes.iterator();
562 while (itr.hasNext()) {
564 if (att.getAttributeName().equals(attName)) {
568 "Duplicate Attribute",
569 "Another attribute with the same attribute name exist. Please enter a new attribute name.");
570 attNameTxt.setFocus();
578 String attType = attTypeCmb.getText();
579 if (null == attType || attType.isEmpty()) {
580 MessageDialog.openError(getParentShell(),
581 "Mandatory field is empty",
582 "Please select an attribute type.");
585 attHelper.setAttributeType(attType);
588 if (rangeBtn.isEnabled() && rangeBtn.getSelection()) { // Range option
589 String min = minRangeTxt.getText();
590 String max = maxRangeTxt.getText();
591 if (null == min && null == max) {
593 .openError(getParentShell(), "Range is empty",
594 "Please enter the minimum and maximum value of the range.");
595 minRangeTxt.setFocus();
598 if (null == min || null == max) {
600 msg = "Please enter the "
601 + ((null == min) ? "minimum value" : "maximum value")
604 .openError(getParentShell(), "Range is empty", msg);
605 ((null == min) ? minRangeTxt : maxRangeTxt).setFocus();
608 if (min.isEmpty() && max.isEmpty()) {
610 .openError(getParentShell(), "Range is empty",
611 "Please enter the minimum and maximum value of the range.");
612 minRangeTxt.setFocus();
616 MessageDialog.openError(getParentShell(), "Range is empty",
617 "Please enter the minimum value for the range.");
618 minRangeTxt.setFocus();
622 MessageDialog.openError(getParentShell(), "Range is empty",
623 "Please enter the maximum value for the range.");
624 maxRangeTxt.setFocus();
628 if (!min.isEmpty() && !max.isEmpty()
629 && !attHelper.isRangeValid(min, max, attType)) {
630 MessageDialog.openError(getParentShell(), "Invalid range",
631 "Range is invalid.");
632 maxRangeTxt.setFocus();
635 attHelper.setValidValuesType(Type.RANGE);
636 attHelper.setMin(min);
637 attHelper.setMax(max);
640 // Remove all existing custom values
641 attHelper.setAllowedValues(null);
643 } else if (cusValuesBtn.isEnabled() && cusValuesBtn.getSelection()) {
644 String[] cusItems = customValuesList.getItems();
645 if (null == cusItems || cusItems.length < 1) {
646 MessageDialog.openError(getParentShell(),
647 "Custom list is empty.",
648 "No values are added to the custom list.");
649 maxRangeTxt.setFocus();
652 attHelper.setValidValuesType(Type.VALUESET);
653 attHelper.setAllowedValuesByArray(cusItems);
656 // Remove min and max values
657 attHelper.setMin(null);
658 attHelper.setMax(null);
660 } else if (noneBtn.isEnabled() && noneBtn.getSelection()) {
661 attHelper.setValidValuesType(Type.UNKNOWN);
663 // Remove min, max and custom values
664 attHelper.setAllowedValues(null);
665 attHelper.setMin(null);
666 attHelper.setMax(null);
670 // Attribute Default Value
671 String attDflValue = dflValueTxt.getText();
672 if (null == attDflValue || attDflValue.isEmpty()) {
673 MessageDialog.openError(getParentShell(),
674 "Mandatory field is empty",
675 "Please enter a default value for the attribute.");
676 dflValueTxt.setFocus();
679 attDflValue = attDflValue.trim();
680 if (attDflValue.length() < 1
681 || !attHelper.isValueValid(attDflValue, attType)) {
682 MessageDialog.openError(getParentShell(), "Invalid value",
683 "Default value is invalid.");
684 dflValueTxt.setFocus();
687 if (!attHelper.isDefaultValueValid(attDflValue)) {
688 MessageDialog.openError(getParentShell(), "Invalid value",
689 "Default value is not in the valid values.");
690 dflValueTxt.setFocus();
694 attHelper.setAttributeDflValue(attDflValue.toLowerCase());
700 public boolean isHelpAvailable() {
705 * @Override protected Point getInitialSize() { Point curSize =
706 * super.getInitialSize(); return new Point(curSize.x, curSize.y + 200); }
710 protected Point getInitialLocation(Point initialSize) {
711 Rectangle shellBounds = Display.getDefault().getActiveShell()
713 Point dialogSize = getInitialSize();
716 shellBounds.x + (shellBounds.width - dialogSize.x) / 2,
717 shellBounds.y + (shellBounds.height - dialogSize.y) / 2);