Eclipse Plug-in - How to toggle SWT Composite by selecting Radio option

這邊主要是說明如何隱藏及顯示所建立的表單項目,如Label、Text等,透過在表單點選不同

的Radio來切換需要登打的項目! 而特別注意的是,需要當下一起隱藏的元件需附加在同一個

Composite,而這個Composite是另外建立的跟其他不須隱藏的元件分開之!

在這邊以Dialog來展示,以繼承TitleAreaDialog建立之

主程式如下:
  1. @Override
  2. protected Control createDialogArea(Composite parent) {
  3. super.createDialogArea(parent);
  4. setTitle("Form Display");
  5. setMessage("* Required input option");
  6. Composite form = new Composite(parent, SWT.NONE);
  7. form.setLayout(new GridLayout(3, false));
  8. GridData grid = new GridData(GridData.FILL_BOTH);
  9. grid.heightHint = 150;
  10. form.setLayoutData(grid);
  11. createLabel(form, "*Type:", 5);
  12. createRadioGroup(form, new String[]{"Option1", "Option2"});
  13. createComboboxGroup(form, new String[]{"Option1_1", "Option1_2"});
  14. createCheckBoxGroup(form, new String[]{"Option2_1", "Option2_2"});
  15. createLabel(form, "Common:", 5);
  16. createCommonText(form);
  17. return form;
  18. }
首先,先建立自己的Composite,並且劃分為三個column的排列,主因是從畫面可以知道

第一欄擺Label、再來主要以radio會有兩項,因此設大家的Composite為三欄

一、建立Type
  1. private Composite comboxGroup;
  2. private Composite checkGroup;
  3. private GridData createGridLayout(int hori, int verti){
  4. return new GridData(SWT.LEFT, SWT.CENTER, false, false, hori, verti);
  5. }
  6. private void createLabel(Composite comp, String name, int indent){
  7. Label label = new Label(comp, SWT.NONE);
  8. label.setText(name);
  9. GridData grid = createGridLayout(1, 1);
  10. grid.horizontalIndent = indent;
  11. label.setLayoutData(grid);
  12. }
  13. private void togglePart(Composite comp, boolean hide){
  14. GridData grid = (GridData) comp.getLayoutData();
  15. grid.exclude = hide;
  16. comp.setVisible(!hide);
  17. }
  18. private void createRadioGroup(Composite comp, String radio[]){
  19. for(int i = 0 ; i < radio.length ; i++){
  20. Button option = new Button(comp, SWT.RADIO);
  21. option.setText(radio[i]);
  22. option.setData(radio[i]);
  23. GridData grid = createGridLayout(1, 1);
  24. grid.horizontalIndent = 10;
  25. option.setLayoutData(grid);
  26. option.addSelectionListener(new SelectionAdapter(){
  27. @Override
  28. public void widgetSelected(SelectionEvent e) {
  29. String data = (String) e.widget.getData();
  30. Composite parent = ((Button) e.widget).getParent();
  31. if(data.equals("Option2")){
  32. togglePart(comboxGroup, true);
  33. togglePart(checkGroup, false);
  34. }else{
  35. togglePart(checkGroup, true);
  36. togglePart(comboxGroup, false);
  37. }
  38. parent.pack();
  39. }
  40. });
  41. }
  42. }
在這邊需要注意的是,radio option內的event事件

comboxGroup及checkGroup變數皆為全域變數,它們所對應到的分別為

當點選Option1時,顯示comboxGroup的項目、隱藏checkGroup項目;反之Option2剛好顛倒

而這兩個變數在呼叫createComboboxGroup()、createCheckBoxGroup()會建立之

至於togglePart method為這一篇的重點

當傳進來的第二個參數flag為true時,表示要針對第一個參數的composite進行hide

進行hide的時候需要呼叫setVisible,但是若僅呼叫此method會造成該composite雖然隱藏了

但是還會占用位置,因此勢必要設定該composite GridData的exclude為true

官方說明如下:

exclude informs the layout to ignore this control when sizing and positioning controls.

最後,還需要針對這個composite's parent進行pack(),重新resize目前的layout

二、建立Option1_1
  1. private void createComboboxGroup(Composite comp, String option[]){
  2. comboxGroup = new Composite(comp, SWT.NONE);
  3. comboxGroup.setLayout(new GridLayout(2, false));
  4. comboxGroup.setLayoutData(createGridLayout(3, 1));
  5. createLabel(comboxGroup, "*Option1_1:", 0);
  6. Combo combo = new Combo(comboxGroup, SWT.READ_ONLY);
  7. combo.setItems(option);
  8. GridData grid = createGridLayout(1, 1);
  9. grid.widthHint = 110;
  10. combo.setLayoutData(grid);
  11. }
這個項目的顯示會在點選Option1時顯示之

這裡需重新定義它自己的composite,如此一來利用這個新的composite來建立這個Label和

Combo後,操作這個composite的動作就會一起連動所有的項目!

三、建立Option2_1
  1. private void createCheckBoxGroup(Composite comp, String check[]){
  2. checkGroup = new Composite(comp, SWT.NONE);
  3. checkGroup.setLayout(new GridLayout(check.length + 1, false));
  4. checkGroup.setLayoutData(createGridLayout(3, 1));
  5. createLabel(checkGroup, "*Option2_1:", 0);
  6. for(int i = 0 ; i < check.length ; i++){
  7. Button option = new Button(checkGroup, SWT.CHECK);
  8. option.setText(check[i]);
  9. option.setData(check[i]);
  10. option.setLayoutData(createGridLayout(1, 1));
  11. }
  12. }
這個項目的顯示會在點選Option2時顯示之,過程同二

四、建立Common
  1. private void createCommonText(Composite comp){
  2. Text text = new Text(comp, SWT.BORDER | SWT.MULTI);
  3. GridData grid = createGridLayout(2, 1);
  4. grid.horizontalIndent = 10;
  5. grid.widthHint = 300;
  6. grid.heightHint = 40;
  7. text.setLayoutData(grid);
  8. }
這個項目的目的主要是要呈現當它上面的元件被隱藏或顯示時,它是否會有移動的效果!

DEMO如下



留言