创建自己的选择文件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;
}
}