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如下:

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

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

接著為繼承ApplicationWindow 後定義的相關method如下:
protected void configureShell(Shell shell) {
     super.configureShell(shell);

     // Set the title bar text
     shell.setText("Simple Stack Example");
   }


/**
    * Creates the main window's contents
    * 
    * @param parent the main window
    * @return Control
    */
   protected Control createContents(Composite parent) {
     Composite composite = new Composite(parent, SWT.NONE);
     composite.setLayout(new GridLayout(3, false));
     MessageBox alertDialog = new MessageBox(parent.getShell(), SWT.ERROR);
     
     Label label = new Label(composite, SWT.NONE);
     label.setText("PUSH:");
     
     Text pushText = new Text(composite, SWT.BORDER);
     GridData gridData = new GridData();
     gridData.horizontalAlignment = SWT.FILL;
     gridData.grabExcessHorizontalSpace = true;
     pushText.setLayoutData(gridData);

     Button push = new Button(composite, SWT.PUSH);
     push.setText("PUSH");
     
     
     Label label2 = new Label(composite, SWT.NONE);
     label2.setText("POP:");
     gridData = new GridData();
     gridData.horizontalAlignment = SWT.FILL;
     gridData.grabExcessHorizontalSpace = true;
     gridData.horizontalSpan = 2;
     label2.setLayoutData(gridData);
     
     Button pop = new Button(composite, SWT.PUSH);
     pop.setText("POP");
     
     
     Label stackLabel = new Label(composite, SWT.NONE);
     stackLabel.setText("Stack");
     gridData = new GridData();
     gridData.horizontalSpan = 3;
     stackLabel.setLayoutData(gridData);
  
     //display stack container
     List stackList = new List(composite, SWT.BORDER | SWT.MULTI);
     gridData = new GridData();
     gridData.horizontalSpan = 3;
     gridData.horizontalAlignment = SWT.FILL;
     gridData.grabExcessHorizontalSpace = true;
     gridData.verticalAlignment = SWT.FILL;
     gridData.grabExcessVerticalSpace = true;
     gridData.widthHint = 250;
     gridData.heightHint = 250;
     stackList.setLayoutData(gridData);
  
     //push button click event
     push.addSelectionListener(new SelectionAdapter() {
         public void widgetSelected(SelectionEvent event) {
          //檢查是否超過stack容量及空資料不push
          if(sp != (MaxSize-1) && !pushText.getText().isEmpty()){
           stackList.removeAll();
              stack[++sp] = pushText.getText();
           composeStack(stackList);
           label2.setText("POP:"+stack[sp]);
           pushText.setText("");
          }else{
           alertDialog.setMessage("超過Stack容量!!");
           alertDialog.open();
          }
         }
     });
     
     //pop button click event
     pop.addSelectionListener(new SelectionAdapter() {
         public void widgetSelected(SelectionEvent event) {
        if(sp != -1){
         stackList.removeAll();
         stack[sp--] = null; 
         composeStack(stackList);
         if(sp == -1)
          label2.setText("POP:");
         else
          label2.setText("POP:"+stack[sp]);
        }else{
         alertDialog.setMessage("目前Stack為空!!");
            alertDialog.open();
        }
         }
     });
      
     parent.pack();
     return composite;
 }
   
 public void composeStack(List list){
    System.out.println(sp);
    for(int i = sp ; i > -1 ; i--){
       list.add(stack[i]);
    }
 }

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

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

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

Demo如下:



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


總結

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

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


留言