在属性页中,我们提供了文本编辑器,Combo编辑器,还有Color编辑器,但是对话框的编辑器只有一个抽象类DialogCellEditor。下面我们来实现一个在属性页中打开文件对话框的功能:
效果如图显示:
<o:p></o:p>
<v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"></v:path><o:lock v:ext="edit" aspectratio="t"></o:lock></v:shapetype><v:shape id="_x0000_i1025" style="WIDTH: 7in; HEIGHT: 118.5pt" type="#_x0000_t75"><v:imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image001.png" o:title=""></v:imagedata></v:shape>
当点击树图的按钮时,弹出文件选择对话框,在eclipse以及gef的包中,没有关于FileDialogPropertyDescriptor之类的定义,要实现这个功能需要我们自己去实现。<o:p></o:p>
有两个比较关键的类:PropertyDescriptor、DialogCellEditor,我们需要分别继承这两个类。DialogCellEditor是jface包里的一个抽象类文件,它是用dialog实现了一个cell 编辑器。这类编辑器通常是左边有个label或在右边有个button,单击这个button会弹出一个对话框窗体(如color对话框,file对话框等)去改变cell editor的值。<o:p></o:p>
继承DialogCellEditor的子类需要实现以下三个方法:<o:p></o:p>
createButton: 创建cell editor的button控件<o:p></o:p>
openDialogBox: 当点击button时,打开对话框(我们需要在这里实现打开什么对话框)<o:p></o:p>
updateLabel: 修改 cell editor的label,当它的值被改变。<o:p></o:p>
我们实现的方法如下:<o:p></o:p>
package com.jctx.dnrm.gef.model;<o:p></o:p>
<o:p> </o:p>
import org.eclipse.jface.viewers.DialogCellEditor;<o:p></o:p>
import org.eclipse.swt.SWT;<o:p></o:p>
import org.eclipse.swt.widgets.Button;<o:p></o:p>
import org.eclipse.swt.widgets.Composite;<o:p></o:p>
import org.eclipse.swt.widgets.Control;<o:p></o:p>
import org.eclipse.swt.widgets.FileDialog;<o:p></o:p>
import com.jctx.dnrm.LogicPlugin;<o:p></o:p>
<o:p> </o:p>
public class FileDialogCellEditor extends DialogCellEditor {<o:p></o:p>
<o:p></o:p>
public FileDialogCellEditor(Composite parent){<o:p></o:p>
super(parent);<o:p></o:p>
}<o:p></o:p>
protected Object openDialogBox(Control cellEditorWindow) {<o:p></o:p>
FileDialog fileDialog = new FileDialog(cellEditorWindow.getShell(),SWT.OPEN);<o:p></o:p>
fileDialog.setFileName("选择图形文件");<o:p></o:p>
fileDialog.setFilterExtensions(new String[]{"*.gif"});<o:p></o:p>
//fileDialog.setFilterPath(getURLPath(LogicPlugin.class,"icons/gef/model"));<o:p></o:p>
fileDialog.setFilterPath(getURLPath(LogicPlugin.class,"icons/gef/model"));<o:p></o:p>
<o:p></o:p>
String path = fileDialog.open();<o:p></o:p>
return path;<o:p></o:p>
}<o:p></o:p>
<o:p></o:p>
protected Button createButton(Composite parent){<o:p></o:p>
Button result = new Button(parent, SWT.PUSH);<o:p></o:p>
result.setText("..."); //$NON-NLS-1$<o:p></o:p>
return result;<o:p></o:p>
}<o:p></o:p>
<o:p></o:p>
protected static String getURLPath(Class rsrcClass, String name){<o:p></o:p>
return rsrcClass.getResource(name).getPath();<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
大家,注意OpenDialogBox这个方法,是在这里打开的对话框。在这个方法中,我还可以通过String value = (String) getValue();来获取当前的属性值。
另外,我们需要继承一个PropertyDescriptor,命名为FileDialogPropertyDescriptor,在这个类中来定义FileDialogCellEditor,用来描述文件选择类的属性。子类需要重新实现一个getPropertyEditor方法,来提供一个cell editor,用来改变属性值。系统已经定义了以下三个:<o:p></o:p>
1)TextPropertyDescriptor –编辑文本用 TextCellEditor <o:p></o:p>
2)ComboBoxPropertyDescriptor – 编辑下拉框 ComboBoxCellEditor <o:p></o:p>
3)ColorPropertyDescriptor – 编辑颜色Editor用ColorCellEditor <o:p></o:p>
我的FileDialogCellEditor实现如下:<o:p></o:p>
public class FileDialogPropertyDescriptor extends PropertyDescriptor {<o:p></o:p>
<o:p> </o:p>
public FileDialogPropertyDescriptor(Object id, String displayName) {<o:p></o:p>
super(id, displayName); <o:p></o:p>
}<o:p></o:p>
public CellEditor createPropertyEditor(Composite parent) {<o:p></o:p>
CellEditor editor = new FileDialogCellEditor(parent);<o:p></o:p>
if (getValidator() != null)<o:p></o:p>
editor.setValidator(getValidator());<o:p></o:p>
return editor;<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
在IPropertyDescriptor[]中加入new FileDialogPropertyDescriptor(PROP_ICONNAME,"树图")就可以完成我们的需求了。