filechooseaction.java
来自「mywork是rcp开发的很好的例子」· Java 代码 · 共 55 行
JAVA
55 行
package net.sf.freenote.action;
import net.sf.freenote.FreeNoteConstants;
import net.sf.freenote.model.FileShape;
import net.sf.freenote.model.ImageFileShape;
import net.sf.freenote.parts.ShapeEditPart;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IWorkbenchPart;
/**
* 为文件形体设置文件
* @author levin
* @since 2008-1-20 下午09:42:37
*/
public class FileChooseAction extends SelectionAction {
public FileChooseAction(IWorkbenchPart part) {
super(part);
setText("选择文件...");
setToolTipText("更换文件或图片");
setId(FreeNoteConstants.FILE_CHOOSE);
}
@Override
protected boolean calculateEnabled() {
if (getSelectedObjects().size() == 1 && (getSelectedObjects().get(0) instanceof ShapeEditPart)) {
ShapeEditPart part = (ShapeEditPart) getSelectedObjects().get(0);
if(part.getModel() instanceof FileShape)
return true;
}
return false;
}
@Override
public void run() {
FileShape shape =(FileShape) ((ShapeEditPart) getSelectedObjects().get(0)).getModel();
FileDialog fd = new FileDialog(getWorkbenchPart().getSite().getShell(), SWT.OPEN|SWT.SINGLE);
fd.setText("请选择一个文件");
if(shape instanceof ImageFileShape){
String suffixs="*.bmp;*.jpg;*.jpeg;*.png;*.ico;*.tif;*.gif";
fd.setFilterExtensions(new String[]{suffixs,"*.*"});
fd.setFilterNames(new String[]{"图片文件("+suffixs+")","所有文件(*.*)"});
}
fd.open();
String fname = fd.getFileName();
if (fname.equals(""))
return;
fname = fd.getFilterPath() + "/" + fname;
shape.setFilePath(fname);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?