網站介面

組件建構子中的引數一般稱為相依關係。組件相依關係取代各種配接器,並取得一些方法,例如,存取網站 IActionBars 的方法,元件型組件會在其建構子中採用 IActionBars 實例。

全新樣式網站提供介面的開放式設定。任何外掛程式都可以用 org.eclipse.core.component.types 延伸點來延伸網站介面的設定。雖然網站介面的設定可以延伸,但所有網站都需支援相同的設定,確定所有組件都可以插入任何網站。使用 PDE 外掛程式登錄視圖來檢視網站介面的完整設定,是可行的。

工作台提供下列網站介面:

介面
說明
IErrorContext
建構和記載 IStatus 訊息到外掛程式日誌。
軟體組
外掛程式軟體組包含組件的實作方式。這自動通知了其他元件的外掛程式,例如實作 IErrorContext。
IContainer
網站的儲存器。這個物件可以查詢其他網站的任何介面,如果組件想要把每個項目從它的網站,重新導向或多工到它的子項,這非常有用。
INameable
容許組件設定它的名稱、圖示、工具提示和內容說明。取代 IWorkbenchPart 的各種取得方法和接聽器。
組合
組件的母項組合。這個組合不與任何其他組件共用,且會和組件一起同時刪除。組件可以設定佈置和附加接聽器到這個組合。
ISecondaryId
當作為多重實例視圖時,此為傳回組件次要 ID 的介面。
ResourceManager
安全地配置和取消配置「影像」、「字型」、「色彩品質」和其他 SWT 資源。確保相同的資源在組件之間共用,且關閉組件,確定有清除任何洩漏現象。
IDirtyHandler
容許組件設定或清除它們變動過的狀態。
IMessageDialogs
將錯誤、警告和資訊對話框顯示給使用者看。
IActionBars
用途和 Eclipse 3.0 API 的 getViewSite().getActionBars() 一樣。
IMultiplexer
提供一個多工的元件,以及對其多工器和任何共用介面的存取權。
.
ISavedState
保留組件之前保存的狀態。
IPartFactory
容許組件建立巢狀視圖和編輯器。
IPartDescriptor
保留關於組件的 Meta 資訊,例如,組件 ID、標題、預設影像等等。
IEditorInput
保留編輯器的編輯器輸入。指向任何空白的編輯器輸入以檢視。
ISelectionHandler
處理選項變更。組件可以用這個來變更它們提供給工作台的選項。

由組件的內含環境定義來決定,組件是否要為每一個介面取得唯一的實例,或者是否要取得數個組件共用的物件。組件的建構子絕不會接收到空值引數。

 

使用具有現有組件的新網站介面

雖然建構子注入項目很方便,但不利於用來重新寫入每一個現有編輯器和視圖,以使用建構子注入項目來採用新網站介面的優點。因此,現有視圖也可以將所有新介面當成 IWorkbenchPartSite 上的配接器來使用。

以下是包含開啟訊息對話框之單一按鈕的視圖。

DependenciesViewOld 的影像

下列範例顯示使用新 IMessageDialogs 介面開啟對話框的全新樣式視圖程式碼。

/**
 * 示範如何在全新樣式的組件中使用元件相依關係
 *
 * @自 3.1
 */
public class DependenciesViewNew {
    // 相依關係
    private IMessageDialogs dialogs;
   
    /**
     * 元件建構子。不要直接呼叫。
     */
    public DependenciesViewNew(Composite parent, IMessageDialogs dialogs) {
        this.dialogs = dialogs;
       
        Button testButton = new Button(parent, SWT.PUSH);
        testButton.setText("Open a dialog");
        testButton.addSelectionListener(new SelectionAdapter() {
        /* (非 Javadoc)
         * @請參閱 org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
         */
        public void widgetSelected(SelectionEvent e) {
            openADialog();
        }
        });
    }
   
    private void openADialog() {
        dialogs.open(IStatus.INFO, "This is a message");
    }
}

本例顯示使用 IMessageDialogs 來開啟對話框的傳統視圖,並示範使用傳統工作台組件也是可行的。 紅色字型的指令行顯示如何初值設定 IMessageDialogs 介面,以及在每一個案例中要如何使用。

/**
 * 示範如何在舊式視圖中使用元件相依關係
 *
 * @自 3.1
 */
public class DependenciesViewOld extends ViewPart {
    // 相依關係
    private IMessageDialogs dialogs;
    
    // 主要小組件
    private Composite parent;
    
    /* (非 Javadoc)
     * @請參閱 org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
     */
    public void createPartControl(Composite parent) {
        this.parent = parent;
        this.dialogs = (IMessageDialogs)getSite().getAdapter(IMessageDialogs.class);
        
        Button testButton = new Button(parent, SWT.PUSH);
        testButton.setText("Open a dialog");
        testButton.addSelectionListener(new SelectionAdapter() {
        /* (非 Javadoc)
         * @請參閱 org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
         */
        public void widgetSelected(SelectionEvent e) {
            openADialog();
        }
        });
    }
    
    private void openADialog() {
        if (dialogs != null) {
           dialogs.open(IStatus.INFO, "This is a message");
        }
    }

    /* (非 Javadoc)
     * @請參閱 org.eclipse.ui.IWorkbenchPart#setFocus()
     */
    public void setFocus() {
        parent.setFocus();
    }
}