| 1 23 package org.objectweb.clif.scenario.util.isac.plugin.gui; 24 25 import java.util.EventListener ; 26 import java.util.Hashtable ; 27 import java.util.StringTokenizer ; 28 import java.util.Vector ; 29 30 import org.apache.log4j.Category; 31 import org.eclipse.swt.SWT; 32 import org.eclipse.swt.custom.TableEditor; 33 import org.eclipse.swt.events.ModifyListener; 34 import org.eclipse.swt.events.SelectionEvent; 35 import org.eclipse.swt.events.SelectionListener; 36 import org.eclipse.swt.layout.FillLayout; 37 import org.eclipse.swt.layout.GridData; 38 import org.eclipse.swt.layout.GridLayout; 39 import org.eclipse.swt.widgets.Button; 40 import org.eclipse.swt.widgets.Combo; 41 import org.eclipse.swt.widgets.Composite; 42 import org.eclipse.swt.widgets.Control; 43 import org.eclipse.swt.widgets.Group; 44 import org.eclipse.swt.widgets.Label; 45 import org.eclipse.swt.widgets.Table; 46 import org.eclipse.swt.widgets.TableColumn; 47 import org.eclipse.swt.widgets.TableItem; 48 import org.eclipse.swt.widgets.Text; 49 50 56 public class ParameterWidget implements SelectionListener { 57 58 static Category cat = Category.getInstance(ParameterWidget.class.getName()); 59 60 private static final String EMPTY = "to be edited..."; 61 62 65 private int style; 66 67 private Composite composite; 68 69 private String name; 70 71 private String labelValue; 72 73 private Text textField; 74 75 private Group radioGroup; 76 77 private Group checkBox; 78 79 private Group group; 80 81 private Vector buttons; 82 83 private Combo combo; 84 85 private EventListener listener; 86 87 private Vector nfield; 88 89 private Vector nfieldLabel; 90 91 private Button nfieldAdd; 92 93 private Button nfieldRemove; 94 95 private Group nfieldGroup; 96 97 private Composite parent; 98 99 private Table table; 100 101 private Vector cellEditors; 102 103 private int cellYAxis; 104 105 private Vector cellText; 106 107 private Vector tableColumns; 108 109 126 public ParameterWidget(int style, String name, String labelValue, 127 Hashtable params, Composite parent, EventListener listener) { 128 cat.debug("-> constructor"); 129 this.listener = listener; 130 this.style = style; 131 this.name = name; 132 if (labelValue == null) { 133 this.labelValue = name; 134 } else { 135 if (labelValue.equals("")) { 136 this.labelValue = name; 137 } else { 138 this.labelValue = labelValue; 139 } 140 } 141 this.textField = null; 143 this.radioGroup = null; 144 this.checkBox = null; 145 this.group = null; 146 this.buttons = null; 147 this.nfield = null; 148 this.nfieldLabel = null; 149 this.nfieldAdd = null; 150 this.nfieldRemove = null; 151 this.nfieldGroup = null; 152 this.table = null; 153 this.tableColumns = null; 154 this.parent = parent; 156 this.composite = new Composite(parent, SWT.FLAT); 157 this.composite.setBackground(parent.getDisplay().getSystemColor( 158 SWT.COLOR_WHITE)); 159 GridLayout gridLayout = new GridLayout(); 160 gridLayout.numColumns = 1; 161 this.composite.setLayout(gridLayout); 162 GridData gridData = new GridData(); 164 gridData.horizontalAlignment = GridData.FILL; 165 gridData.grabExcessHorizontalSpace = true; 166 this.composite.setLayoutData(gridData); 167 switch (style) { 169 case WidgetDescription.TEXT_FIELD: 170 initField(params, this.composite); 171 break; 172 case WidgetDescription.RADIO_GROUP: 173 initRadioGroup(params, this.composite); 174 break; 175 case WidgetDescription.CHECK_BOX: 176 initCheckBox(params, this.composite); 177 break; 178 case WidgetDescription.GROUP: 179 initGroup(params, this.composite); 180 break; 181 case WidgetDescription.COMBO: 182 initCombo(params, this.composite); 183 break; 184 case WidgetDescription.NFIELD: 185 initNField(params, this.composite); 186 break; 187 case WidgetDescription.TABLE: 188 initTable(params, this.composite); 189 break; 190 default: 191 cat.warn("UNKNOW WIDGET STYLE : " + style); 192 } 193 } 194 195 198 public String getValue() { 199 cat.debug("-> getValue"); 200 switch (this.style) { 201 case WidgetDescription.TEXT_FIELD: 202 return getValueField(); 203 case WidgetDescription.RADIO_GROUP: 204 return getValueRadioGroup(); 205 case WidgetDescription.CHECK_BOX: 206 return getValueCheckBox(); 207 case WidgetDescription.GROUP: 208 return getValueGroup(); 209 case WidgetDescription.COMBO: 210 return getValueCombo(); 211 case WidgetDescription.NFIELD: 212 return getValueNField(); 213 case WidgetDescription.TABLE: 214 return getValueTable(); 215 default: 216 cat.warn("UNKNOW WIDGET STYLE..."); 217 return null; 218 } 219 } 220 221 227 public void setValue(String value) { 228 cat.debug("-> setValue"); 229 removeListeners(); 232 switch (this.style) { 233 case WidgetDescription.TEXT_FIELD: 234 setValueField(value); 235 break; 236 case WidgetDescription.RADIO_GROUP: 237 setValueRadioGroup(value); 238 break; 239 case WidgetDescription.CHECK_BOX: 240 setValueCheckBox(value); 241 break; 242 case WidgetDescription.GROUP: 243 setValueGroup(value); 244 break; 245 case WidgetDescription.COMBO: 246 setValueCombo(value); 247 break; 248 case WidgetDescription.NFIELD: 249 setValueNField(value); 250 break; 251 case WidgetDescription.TABLE: 252 setValueTable(value); 253 break; 254 default: 255 cat.warn("UNKNOW WIDGET STYLE..."); 256 } 257 addListeners(); 258 } 259 260 266 public void setComboValues(Vector values) { 267 cat.debug("-> setComboValues"); 268 for (int i = 0; i < values.size(); i++) { 269 this.combo.add((String ) values.elementAt(i)); 270 } 271 } 272 273 279 public boolean addEmtyFieldForNField(Object source) { 280 cat.debug("-> addEmptyFieldForNField"); 281 if (source != this.nfieldAdd) 283 return false; 284 Label label = new Label(this.nfieldGroup, SWT.NONE); 286 label 287 .setBackground(parent.getDisplay().getSystemColor( 288 SWT.COLOR_WHITE)); 289 label.setText("field " + this.nfield.size() + " : "); 291 this.nfieldLabel.add(label); 293 Text text = new Text(this.nfieldGroup, SWT.BORDER); 295 text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 296 text.addModifyListener((ModifyListener) this.listener); 298 this.nfield.add(text); 300 this.nfieldGroup.layout(); 302 this.composite.layout(); 303 this.parent.layout(); 304 return true; 305 } 306 307 314 public boolean addEmtyEntryForTable(Object source) { 315 if (source != this.nfieldAdd) 316 return false; 317 TableItem temp = new TableItem(table, SWT.NONE); 318 String [] values = new String [this.tableColumns.size()]; 320 for (int i = 0; i < this.tableColumns.size(); i++) { 321 values[i] = new String (EMPTY); 322 } 323 temp.setText(values); 324 return true; 325 } 326 327 334 public boolean removeLastEntryForTable(Object source) { 335 if (source != this.nfieldRemove) 336 return false; 337 this.table.remove(this.table.getItemCount() - 1); 338 return true; 339 } 340 341 347 public boolean removeLastFieldForNField(Object source) { 348 cat.debug("-> removeLastFieldForNField"); 349 if (source != this.nfieldRemove) 351 return false; 352 if (this.nfield.isEmpty()) 353 return true; 354 Label lastLabel = (Label) this.nfieldLabel.elementAt(this.nfieldLabel 356 .size() - 1); 357 Text lastText = (Text) this.nfield.elementAt(this.nfield.size() - 1); 358 this.nfield.remove(lastText); 360 this.nfieldLabel.remove(lastLabel); 361 lastLabel.dispose(); 363 lastText.dispose(); 364 this.nfieldGroup.layout(); 366 this.composite.layout(); 367 this.parent.layout(); 368 return true; 369 } 370 371 375 378 public void removeListeners() { 379 switch (this.style) { 380 case WidgetDescription.TEXT_FIELD: 381 this.textField.removeModifyListener((ModifyListener) listener); 382 return; 383 case WidgetDescription.RADIO_GROUP: 384 this.removeButtonsListeners(); 385 return; 386 case WidgetDescription.CHECK_BOX: 387 this.removeButtonsListeners(); 388 return; 389 case WidgetDescription.COMBO: 390 this.combo.removeSelectionListener((SelectionListener) listener); 391 return; 392 case WidgetDescription.NFIELD: 393 this.removeNFieldListeners(); 394 return; 395 case WidgetDescription.TABLE: 396 return; 398 default: 399 cat.warn("UNKNOW WIDGET STYLE..."); 400 } 401 } 402 403 406 public void addListeners() { 407 switch (this.style) { 408 case WidgetDescription.TEXT_FIELD: 409 this.textField.addModifyListener((ModifyListener) listener); 410 return; 411 case WidgetDescription.RADIO_GROUP: 412 this.addButtonsListeners(); 413 return; 414 case WidgetDescription.CHECK_BOX: 415 this.addButtonsListeners(); 416 return; 417 case WidgetDescription.COMBO: 418 this.combo.addSelectionListener((SelectionListener) listener); 419 return; 420 case WidgetDescription.NFIELD: 421 this.addNFieldListeners(); 422 return; 423 case WidgetDescription.TABLE: 424 return; 426 default: 427 cat.warn("UNKNOW WIDGET STYLE..."); 428 } 429 } 430 431 435 private void removeButtonsListeners() { 436 for (int i = 0; i < this.buttons.size(); i++) 437 ((Button) this.buttons.elementAt(i)) 438 .removeSelectionListener((SelectionListener) listener); 439 } 440 441 private void addButtonsListeners() { 442 for (int i = 0; i < this.buttons.size(); i++) 443 ((Button) this.buttons.elementAt(i)) 444 .addSelectionListener((SelectionListener) listener); 445 } 446 447 450 private void removeNFieldListeners() { 451 for (int i = 0; i < this.nfield.size(); i++) { 452 ((Text) this.nfield.elementAt(i)) 453 .removeModifyListener((ModifyListener) this.listener); 454 } 455 } 456 457 460 private void addNFieldListeners() { 461 for (int i = 0; i < this.nfield.size(); i++) { 462 ((Text) this.nfield.elementAt(i)) 463 .addModifyListener((ModifyListener) this.listener); 464 } 465 } 466 467 471 474 private void initTable(Hashtable params, Composite parent) { 475 cat.debug("-> initTable, p=" + params); 476 this.nfieldGroup = new Group(parent, SWT.BORDER); 478 this.nfieldGroup.setBackground(parent.getDisplay().getSystemColor( 479 SWT.COLOR_WHITE)); 480 this.nfieldGroup.setText(this.labelValue + " : "); 481 this.nfieldGroup.setLayoutData(new GridData(GridData.FILL_BOTH)); 483 GridLayout gd = new GridLayout(); 485 gd.numColumns = 1; 486 this.nfieldGroup.setLayout(gd); 487 Composite cButtons = new Composite(this.nfieldGroup, SWT.FLAT); 488 GridLayout gdc = new GridLayout(); 489 gdc.numColumns = 2; 490 cButtons.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 491 cButtons.setBackground(parent.getDisplay().getSystemColor( 492 SWT.COLOR_WHITE)); 493 cButtons.setLayout(gdc); 494 this.nfieldAdd = new Button(cButtons, SWT.FLAT); 496 this.nfieldAdd.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 498 this.nfieldAdd.setText("Add entry"); 499 this.nfieldAdd.addSelectionListener((SelectionListener) this.listener); 500 this.nfieldRemove = new Button(cButtons, SWT.FLAT); 501 this.nfieldRemove.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 502 this.nfieldRemove.setText("Remove entry"); 503 this.nfieldRemove 504 .addSelectionListener((SelectionListener) this.listener); 505 this.table = new Table(this.nfieldGroup, SWT.BORDER); 507 this.table.setLayoutData(new GridData(GridData.FILL_BOTH)); 508 this.tableColumns = new Vector (); 510 String cols = (String ) params.get("cols"); 512 Vector colsNames = getRealTokens(";",cols); 513 for (int i=0;i<colsNames.size();i++) { 514 TableColumn col = new TableColumn(this.table, SWT.LEFT); 515 col.setText((String )colsNames.elementAt(i)); 516 col.setWidth(60); 517 this.tableColumns.add(col); 518 } 519 table.setHeaderVisible(true); 520 table.setLinesVisible(true); 521 table.addSelectionListener(this); 523 this.cellEditors = new Vector (); 525 this.cellText = new Vector (); 526 } 527 528 531 private void initLabel() { 532 cat.debug("-> initLabel"); 533 Label label = new Label(this.composite, SWT.RIGHT); 534 label.setBackground(composite.getDisplay().getSystemColor( 535 SWT.COLOR_WHITE)); 536 label.setText(this.labelValue + " : "); 537 } 538 539 547 private void initField(Hashtable params, Composite parent) { 548 cat.debug("-> initField"); 549 ((GridLayout) this.composite.getLayout()).numColumns = 2; 551 this.initLabel(); 553 this.textField = new Text(this.composite, SWT.SINGLE | SWT.BORDER); 555 this.textField.setBackground(parent.getDisplay().getSystemColor( 557 SWT.COLOR_WHITE)); 558 559 GridData gridData = new GridData(); 561 gridData.horizontalAlignment = GridData.FILL; 562 gridData.grabExcessHorizontalSpace = true; 563 this.textField.setLayoutData(gridData); 564 if (params != null) { 566 if (params.containsKey("text")) { 567 this.textField.setText((String ) params.get("text")); 568 } 569 } 570 this.textField.addModifyListener((ModifyListener) listener); 572 } 573 574 582 private void initNField(Hashtable params, Composite parent) { 583 cat.debug("-> initNField"); 584 this.nfieldGroup = new Group(parent, SWT.BORDER); 586 this.nfieldGroup.setBackground(parent.getDisplay().getSystemColor( 587 SWT.COLOR_WHITE)); 588 this.nfieldGroup.setText(this.labelValue + " : "); 589 this.nfieldGroup.setLayoutData(new GridData(GridData.FILL_BOTH)); 591 GridLayout gd = new GridLayout(); 593 gd.numColumns = 2; 594 this.nfieldGroup.setLayout(gd); 595 this.nfieldAdd = new Button(this.nfieldGroup, SWT.FLAT); 597 this.nfieldAdd.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 599 this.nfieldAdd.setText("Add field"); 600 this.nfieldAdd.addSelectionListener((SelectionListener) this.listener); 601 this.nfieldRemove = new Button(this.nfieldGroup, SWT.FLAT); 602 this.nfieldRemove.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 603 this.nfieldRemove.setText("Remove field"); 604 this.nfieldRemove 605 .addSelectionListener((SelectionListener) this.listener); 606 this.nfield = new Vector (); 608 this.nfieldLabel = new Vector (); 609 } 610 611 619 private void initRadioGroup(Hashtable params, Composite parent) { 620 cat.debug("-> initRadioGroup"); 621 this.radioGroup = new Group(parent, SWT.BORDER); 622 this.radioGroup.setBackground(parent.getDisplay().getSystemColor( 624 SWT.COLOR_WHITE)); 625 626 this.radioGroup.setText(this.labelValue); 627 GridData gridData = new GridData(); 629 gridData.horizontalAlignment = GridData.FILL; 630 gridData.grabExcessHorizontalSpace = true; 631 this.radioGroup.setLayoutData(gridData); 632 FillLayout fill = new FillLayout(); 633 fill.type = SWT.VERTICAL; 634 this.radioGroup.setLayout(fill); 635 636 this.buttons = new Vector (); 637 Vector choices = null; 639 if (params.containsKey("choices")) { 640 choices = (Vector ) params.get("choices"); 641 } 642 Vector selected = null; 643 if (params.containsKey("selected")) { 644 selected = (Vector ) params.get("selected"); 645 } 646 if (choices != null) { 648 for (int i = 0; i < choices.size(); i++) { 649 Button temp = new Button(this.radioGroup, SWT.RADIO); 650 temp.setBackground(parent.getDisplay().getSystemColor( 651 SWT.COLOR_WHITE)); 652 String buttonName = (String ) choices.elementAt(i); 653 temp.setText(buttonName); 654 temp.addSelectionListener((SelectionListener) listener); 656 this.buttons.add(temp); 657 if (selected.contains(buttonName)) 659 temp.setSelection(true); 660 } 661 } 662 } 663 664 672 private void initCheckBox(Hashtable params, Composite parent) { 673 cat.debug("-> initCheckBox"); 674 this.checkBox = new Group(parent, SWT.BORDER); 675 this.checkBox.setBackground(parent.getDisplay().getSystemColor( 677 SWT.COLOR_WHITE)); 678 679 this.checkBox.setText(this.labelValue); 680 GridData gridData = new GridData(); 682 gridData.horizontalAlignment = GridData.FILL; 683 gridData.grabExcessHorizontalSpace = true; 684 this.checkBox.setLayoutData(gridData); 685 FillLayout fill = new FillLayout(); 686 fill.type = SWT.VERTICAL; 687 this.checkBox.setLayout(fill); 688 689 this.buttons = new Vector (); 690 691 Vector choices = null; 693 if (params.containsKey("choices")) { 694 choices = (Vector ) params.get("choices"); 695 } 696 Vector selected = null; 697 if (params.containsKey("selected")) { 698 selected = (Vector ) params.get("selected"); 699 } 700 if (choices != null) { 702 for (int i = 0; i < choices.size(); i++) { 703 Button temp = new Button(this.checkBox, SWT.CHECK); 704 temp.setBackground(parent.getDisplay().getSystemColor( 705 SWT.COLOR_WHITE)); 706 String buttonName = (String ) choices.elementAt(i); 707 temp.setText(buttonName); 708 temp.addSelectionListener((SelectionListener) listener); 709 this.buttons.add(temp); 710 if (selected.contains(buttonName)) 712 temp.setSelection(true); 713 } 714 } 715 } 716 717 725 private void initCombo(Hashtable params, Composite parent) { 726 cat.debug("-> initCombo"); 727 ((GridLayout) this.composite.getLayout()).numColumns = 2; 729 this.initLabel(); 731 this.combo = new Combo(parent, SWT.BORDER | SWT.READ_ONLY); 732 this.combo.setBackground(parent.getDisplay().getSystemColor( 734 SWT.COLOR_WHITE)); 735 736 GridData gridData = new GridData(); 738 gridData.horizontalAlignment = GridData.FILL; 739 gridData.grabExcessHorizontalSpace = true; 740 this.combo.setLayoutData(gridData); 741 Vector choices = null; 743 if (params == null) 745 return; 746 if (params.containsKey("choices")) { 747 choices = (Vector ) params.get("choices"); 748 } 749 if (choices != null) { 750 for (int i = 0; i < choices.size(); i++) { 752 this.combo.add((String ) choices.elementAt(i)); 753 } 754 } 755 this.combo.addSelectionListener((SelectionListener) listener); 756 } 757 758 766 private void initGroup(Hashtable params, Composite parent) { 767 cat.debug("-> initGroup"); 768 this.group = new Group(parent, SWT.BORDER); 769 this.group.setBackground(parent.getDisplay().getSystemColor( 771 SWT.COLOR_WHITE)); 772 this.group.setText(this.labelValue); 773 GridData gridData = new GridData(); 775 gridData.horizontalAlignment = GridData.FILL; 776 gridData.grabExcessHorizontalSpace = true; 777 this.group.setLayoutData(gridData); 778 GridLayout groupLayout = new GridLayout(); 779 groupLayout.numColumns = 1; 780 this.group.setLayout(groupLayout); 781 } 782 783 |