天天看點

在屬性頁中打開對話框

在屬性頁中,我們提供了文本編輯器,Combo編輯器,還有Color編輯器,但是對話框的編輯器隻有一個抽象類DialogCellEditor。下面我們來實作一個在屬性頁中打開檔案對話框的功能:

效果如圖顯示:

當點選樹圖的按鈕時,彈出檔案選擇對話框,在eclipse以及gef的包中,沒有關于FileDialogPropertyDescriptor之類的定義,要實作這個功能需要我們自己去實作。

有兩個比較關鍵的類:PropertyDescriptor、DialogCellEditor,我們需要分别繼承這兩個類。DialogCellEditor是jface包裡的一個抽象類檔案,它是用dialog實作了一個cell 編輯器。這類編輯器通常是左邊有個label或在右邊有個button,單擊這個button會彈出一個對話框窗體(如color對話框,file對話框等)去改變cell editor的值。

繼承DialogCellEditor的子類需要實作以下三個方法:

createButton: 建立cell editor的button控件

openDialogBox: 當點選button時,打開對話框(我們需要在這裡實作打開什麼對話框)

updateLabel: 修改 cell editor的label,當它的值被改變。

我們實作的方法如下:

package com.jctx.dnrm.gef.model;

import org.eclipse.jface.viewers.DialogCellEditor;

import org.eclipse.swt.SWT;

import org.eclipse.swt.widgets.Button;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Control;

import org.eclipse.swt.widgets.FileDialog;

import com.jctx.dnrm.LogicPlugin;

public class FileDialogCellEditor extends DialogCellEditor {

public FileDialogCellEditor(Composite parent){

super(parent);
           

}

protected Object openDialogBox(Control cellEditorWindow) {

FileDialog fileDialog = new FileDialog(cellEditorWindow.getShell(),SWT.OPEN);

   fileDialog.setFileName("選擇圖形檔案");

   fileDialog.setFilterExtensions(new String[]{"*.gif"});

   //fileDialog.setFilterPath(getURLPath(LogicPlugin.class,"icons/gef/model"));

   fileDialog.setFilterPath(getURLPath(LogicPlugin.class,"icons/gef/model"));

  

   String path = fileDialog.open();

   return path;
           

}

protected Button createButton(Composite parent){

Button result = new Button(parent, SWT.PUSH);

       result.setText("..."); //$NON-NLS-1$

       return result;
           

}

protected static String getURLPath(Class rsrcClass, String name){

return rsrcClass.getResource(name).getPath();
           

}

}

大家,注意OpenDialogBox這個方法,是在這裡打開的對話框。在這個方法中,我還可以通過String value = (String) getValue();來擷取目前的屬性值。

另外,我們需要繼承一個PropertyDescriptor,命名為FileDialogPropertyDescriptor,在這個類中來定義FileDialogCellEditor,用來描述檔案選擇類的屬性。子類需要重新實作一個getPropertyEditor方法,來提供一個cell editor,用來改變屬性值。系統已經定義了以下三個:

1)TextPropertyDescriptor –編輯文本用 TextCellEditor

2)ComboBoxPropertyDescriptor – 編輯下拉框 ComboBoxCellEditor

3)ColorPropertyDescriptor – 編輯顔色Editor用ColorCellEditor

我的FileDialogCellEditor實作如下:

public class FileDialogPropertyDescriptor extends PropertyDescriptor {

public FileDialogPropertyDescriptor(Object id, String displayName) {

super(id, displayName);
           

}

public CellEditor createPropertyEditor(Composite parent) {

    CellEditor editor = new FileDialogCellEditor(parent);

       if (getValidator() != null)

           editor.setValidator(getValidator());

       return editor;

   }
           

}

在IPropertyDescriptor[]中加入new FileDialogPropertyDescriptor(PROP_ICONNAME,“樹圖”)就可以完成我們的需求了。

————————————————

版權聲明:本文為CSDN部落客「jdenght」的原創文章,遵循 CC 4.0 BY-SA 版權協定,轉載請附上原文出處連結及本聲明。

原文連結:https://blog.csdn.net/jdenght/article/details/1017386

gef