Eclipse Plug-in - My First Eclipse Plugin Development

第一次接觸Eclipse Plug-in的開發是為了一面試題目,希望您在既有的UI上能夠呈現Stack or

Queue的操作。由於小弟對於Eclipse Plug-in的開發並不熟悉,因此安裝好開發plug-in套件後

,以Hello World的範例為主軸,想說透過既有的UI呈現來改寫!

Hello World的範例是在menu上開一個選項,當您點擊裡面的項目之後彈出一個訊息視窗!

而此時我碰到的問題是,如何點擊項目時要彈出的不是訊息類視窗,而是一般的應用空視

窗,如此一來就可以在裡面設計我要的項目,如InputText、Button等等

因此我參考了網路上的官網範例,自行又繼承了一ApplicationWindow (本來範例是

implemenes IWorkbenchWindowActionDelegate)

首先,plug-in的入口程式是Activator.java,裡面有定義start、stop method(都沿用無修改)

透過plugin.xml的設定檔可以設定當下你要設計在哪個UI位置,如下:


在既有的eclipse ui之下設定它當作自己的extension(範例是org.eclipse.ui.actionSet),如此一來

透過它本身設計的一些屬性,我們就填寫相關項目來設定該項目呈現的名稱及點選後會執行

的是哪一支程式。基本上,上面的設定都是範例幫我帶出來的,因為本身對於這個設定還不

太清楚只能延續使用囉!



PS. 原則上Eclipse就是很多plugin所組合而成,以上述的例子是沿用既有的UI當作自己的

extension,而它本身(actionSet)是設定為extension point可給予其他plugin做extension使用!

緊接著,SampleAction.java改寫後的code如下:

首先是進入點
  1. public void run(IAction action) {
  2. // Don't return from open() until window closes
  3. setBlockOnOpen(true);
  4. // Open the main window
  5. open();
  6. }

run method是一開始會執行到的method,以hello world的範例原本是
  1. MessageDialog.openInformation(
  2. window.getShell(),
  3. "Hello",
  4. "Hello, Eclipse world");

接著為繼承ApplicationWindow 後定義的相關method如下:
  1. protected void configureShell(Shell shell) {
  2. super.configureShell(shell);
  3. // Set the title bar text
  4. shell.setText("Simple Stack Example");
  5. }
  6. /**
  7. * Creates the main window's contents
  8. *
  9. * @param parent the main window
  10. * @return Control
  11. */
  12. protected Control createContents(Composite parent) {
  13. Composite composite = new Composite(parent, SWT.NONE);
  14. composite.setLayout(new GridLayout(3, false));
  15. MessageBox alertDialog = new MessageBox(parent.getShell(), SWT.ERROR);
  16. Label label = new Label(composite, SWT.NONE);
  17. label.setText("PUSH:");
  18. Text pushText = new Text(composite, SWT.BORDER);
  19. GridData gridData = new GridData();
  20. gridData.horizontalAlignment = SWT.FILL;
  21. gridData.grabExcessHorizontalSpace = true;
  22. pushText.setLayoutData(gridData);
  23. Button push = new Button(composite, SWT.PUSH);
  24. push.setText("PUSH");
  25. Label label2 = new Label(composite, SWT.NONE);
  26. label2.setText("POP:");
  27. gridData = new GridData();
  28. gridData.horizontalAlignment = SWT.FILL;
  29. gridData.grabExcessHorizontalSpace = true;
  30. gridData.horizontalSpan = 2;
  31. label2.setLayoutData(gridData);
  32. Button pop = new Button(composite, SWT.PUSH);
  33. pop.setText("POP");
  34. Label stackLabel = new Label(composite, SWT.NONE);
  35. stackLabel.setText("Stack");
  36. gridData = new GridData();
  37. gridData.horizontalSpan = 3;
  38. stackLabel.setLayoutData(gridData);
  39. //display stack container
  40. List stackList = new List(composite, SWT.BORDER | SWT.MULTI);
  41. gridData = new GridData();
  42. gridData.horizontalSpan = 3;
  43. gridData.horizontalAlignment = SWT.FILL;
  44. gridData.grabExcessHorizontalSpace = true;
  45. gridData.verticalAlignment = SWT.FILL;
  46. gridData.grabExcessVerticalSpace = true;
  47. gridData.widthHint = 250;
  48. gridData.heightHint = 250;
  49. stackList.setLayoutData(gridData);
  50. //push button click event
  51. push.addSelectionListener(new SelectionAdapter() {
  52. public void widgetSelected(SelectionEvent event) {
  53. //檢查是否超過stack容量及空資料不push
  54. if(sp != (MaxSize-1) && !pushText.getText().isEmpty()){
  55. stackList.removeAll();
  56. stack[++sp] = pushText.getText();
  57. composeStack(stackList);
  58. label2.setText("POP:"+stack[sp]);
  59. pushText.setText("");
  60. }else{
  61. alertDialog.setMessage("超過Stack容量!!");
  62. alertDialog.open();
  63. }
  64. }
  65. });
  66. //pop button click event
  67. pop.addSelectionListener(new SelectionAdapter() {
  68. public void widgetSelected(SelectionEvent event) {
  69. if(sp != -1){
  70. stackList.removeAll();
  71. stack[sp--] = null;
  72. composeStack(stackList);
  73. if(sp == -1)
  74. label2.setText("POP:");
  75. else
  76. label2.setText("POP:"+stack[sp]);
  77. }else{
  78. alertDialog.setMessage("目前Stack為空!!");
  79. alertDialog.open();
  80. }
  81. }
  82. });
  83. parent.pack();
  84. return composite;
  85. }
  86. public void composeStack(List list){
  87. System.out.println(sp);
  88. for(int i = sp ; i > -1 ; i--){
  89. list.add(stack[i]);
  90. }
  91. }

原則上重點放在createContents method,本身所需要設計的UI是定義在這個函數內,裡面設

計的也很簡單,參考官方的範例先設定排列方式,再依序設定Label、Text、Button,還有

push進去之後data存放的地方等等!!

Demo如下:



執行之後會另外開啟一Eclipse的視窗


總結

原則上這只是亂兜出來的plugin,本身對於plugin.xml的設定上還有不太清楚地部分,因此

在某些部分只能匆匆帶過了orz


留言