GEF,EMF,RCP,Eclipse's plugin的几个问题(7) 创建自己的选择文件PropertyDescrptor自定义的FileNamePropertyDescriptor

  创建自己的选择文件PropertyDescrptor自定义的FileNamePropertyDescriptor
  并且,让该属性可以自己编辑,可以弹出选择文件对话框.
  具体如下:

  public class FileNamePropertyDescriptor extends PropertyDescriptor {
 
    private String fileName;
 
    public FileNamePropertyDescriptor(Object id, Object obj) {
  super(id,"FileName");
  this.fileName = obj.getFileName();//Object要具体为你的model
  this.setLabelProvider(new MyLabelProvider());
    }
 
    public CellEditor createPropertyEditor(Composite parent) {
 //EditableFileSelectEditor 为我们自己定义的一个Editor
 //继承自TextCellEditor,但是,又具有DialogCellEditor的部分特性
 //可以弹出对话框
 //也可以让直接编辑属性
      CellEditor cellEditor = new EditableFileSelectEditor(parent,fileName);
        return cellEditor;
    }
   
    //用于控制显示在properties view中右侧的value部分的text
    class MyLabelProvider extends LabelProvider {
     public Image getImage(Object element) {
      return null;
     }

     public String getText(Object element) {
      return fileName;
     }
    }
  }

public class EditableFileSelectEditor extends TextCellEditor {

    public static final String CELL_EDITOR_IMG_DOTS_BUTTON = "cell_editor_dots_button_image";//$NON-NLS-1$
    private Composite editor;
    private Control contents;
    private Button button;
 private String fileName;
 private String selectedFile;
    
    private class DialogCellLayout extends Layout {
        public void layout(Composite editor, boolean force) {
            Rectangle bounds = editor.getClientArea();
            Point size = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, force);
            if (contents != null)
                contents.setBounds(0, 0, bounds.width - size.x, bounds.height);
            button.setBounds(bounds.width - size.x, 0, size.x, bounds.height);
        }

        public Point computeSize(Composite editor, int wHint, int hHint,
                boolean force) {
            if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT)
                return new Point(wHint, hHint);
            Point contentsSize = contents.computeSize(SWT.DEFAULT, SWT.DEFAULT,
                    force);
            Point buttonSize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT,
                    force);
            // Just return the button width to ensure the button is not clipped
            // if the label is long.
            // The label will just use whatever extra width there is
            Point result = new Point(buttonSize.x, Math.max(contentsSize.y,
                    buttonSize.y));
            return result;
        }
    }

    private static final int defaultStyle = SWT.NONE;

    public EditableFileSelectEditor() {
        setStyle(defaultStyle);
    }

    public EditableFileSelectEditor(Composite parent,String fileName) {
        super(parent);
        this.fileName = fileName;
    }

    protected Button createButton(Composite parent) {
        Button result = new Button(parent, SWT.DOWN);
        result.setText("..."); //$NON-NLS-1$
        return result;
    }

    protected Control createControl(Composite parent) {
     
        Font font = parent.getFont();
        Color bg = parent.getBackground();

        editor = new Composite(parent, getStyle());
        editor.setFont(font);
        editor.setBackground(bg);
        editor.setLayout(new DialogCellLayout());

     super.createControl(editor);
        contents = text;

        button = createButton(editor);
        button.setFont(font);

        button.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                if (e.character == '/u001b') { // Escape
                    fireCancelEditor();
                }
            }
        });

        button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                Object newValue = openDialogBox(editor);
                if (newValue != null) {
                    boolean newValidState = isCorrect(newValue);
                    if (newValidState) {
                        markDirty();
                        doSetValue(newValue);
                    } else {
                        // try to insert the current value into the error message.
                        setErrorMessage(MessageFormat.format(getErrorMessage(),
                                new Object[] { newValue.toString() }));
                    }
                    fireApplyEditorValue();
                }
            }
        });

        setValueValid(true);

        return editor;
    }

    protected Object doGetValue() {
        return super.doGetValue();
    }

    protected void doSetValue(Object value) {
     super.doSetValue(value);
    }

    protected Object openDialogBox(Control cellEditorWindow){
  //此处为弹出文件选择对话框关键
  //如果要选择路径,用DirectoryDialog即可.
  FileDialog dialog = new FileDialog(cellEditorWindow.getShell(),SWT.OPEN);
  dialog.setFileName(fileName);
  selectedFile = dialog.open();
  //返回选择的文件名称或者路径名称
  return selectedFile;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值