在属性页中打开对话框

 在属性页中,我们提供了文本编辑器,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>

有两个比较关键的类:PropertyDescriptorDialogCellEditor,我们需要分别继承这两个类。DialogCellEditorjface包里的一个抽象类文件,它是用dialog实现了一个cell 编辑器。这类编辑器通常是左边有个label或在右边有个button,单击这个button会弹出一个对话框窗体(如color对话框,file对话框等)去改变cell editor的值。<o:p></o:p>

继承DialogCellEditor的子类需要实现以下三个方法:<o:p></o:p>

createButton: 创建cell editorbutton控件<o:p></o:p>

openDialogBox: 当点击button时,打开对话框(我们需要在这里实现打开什么对话框)<o:p></o:p>

updateLabel: 修改 cell editorlabel,当它的值被改变。<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>

1TextPropertyDescriptor –编辑文本用 TextCellEditor <o:p></o:p>

2ComboBoxPropertyDescriptor – 编辑下拉框 ComboBoxCellEditor <o:p></o:p>

3ColorPropertyDescriptor – 编辑颜色EditorColorCellEditor <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,"树图")就可以完成我们的需求了。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值