openshapeaction.java

来自「mywork是rcp开发的很好的例子」· Java 代码 · 共 147 行

JAVA
147
字号
package net.sf.freenote.action;

import java.io.File;

import net.sf.component.config.ConfigHelper;
import net.sf.freenote.FreeNoteConstants;
import net.sf.freenote.PathEditorInput;
import net.sf.freenote.ShapesEditor;
import net.sf.util.StringUtil;

import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.IWorkbenchWindowPulldownDelegate;
import org.eclipse.ui.PartInitException;
/**
 * 打开文件的action
 * @author levin
 * @since 2008-1-26 下午05:40:27
 */
public class OpenShapeAction implements IWorkbenchWindowActionDelegate,IWorkbenchWindowPulldownDelegate {
	private IWorkbenchWindow window;
	private Menu mruMenu;
	@Override
	public void dispose() {
		if (mruMenu != null) {
			mruMenu.dispose();
			mruMenu = null;
		}
	}

	@Override
	public void init(IWorkbenchWindow window) {
		this.window=window;
	}

	@Override
	public void run(IAction action) {
		String path=openFileDialog();
		if(openShapeFile(path))
			addToMRU(path);
	}

	
	private boolean openShapeFile(String path) {
		if(path != null && new File(path).exists()){
			Path inputPath = new Path(path);
			IEditorInput input=new PathEditorInput(inputPath);
			try {
				window.getActivePage().openEditor(input, ShapesEditor.ID, true);
				return true;
			} catch (PartInitException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	private String openFileDialog() {
		FileDialog fd=new FileDialog(window.getShell(),SWT.OPEN);
		fd.setText("请选择一个文件");
		fd.setFilterExtensions(new String[]{"*.fn"});
		return fd.open();
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		
	}

	@Override
	public Menu getMenu(Control parent) {
		if (mruMenu != null) {
			mruMenu.dispose();
		}
		mruMenu = new Menu(parent);
		fillMenu(mruMenu);
		return mruMenu;
	}

	private void fillMenu(Menu menu) {
		String[] mrus=ConfigHelper.getStringArrayProperty(FreeNoteConstants.MRU);
		for(String s:mrus ){
			MenuItem choice = new MenuItem(menu, SWT.PUSH);
			choice.setData(s);
			choice.setText(s);
			choice.addSelectionListener(new SelectionAdapter(){
				@Override
				public void widgetSelected(SelectionEvent se) {
					String path = (String) ((MenuItem)(se.getSource())).getData();
					if(openShapeFile(path))
						addToMRU(path);
					else
						removeFromMRU(path);
				}});
		}		
	}
	

	//保存最近打开的文件并调频
	public static synchronized void addToMRU(String path) {
		String[] oldList=ConfigHelper.getStringArrayProperty(FreeNoteConstants.MRU);
		//清除已有的
		int hitIndex=-1;
		for(int i=0;i<oldList.length;i++)
			if(oldList[i].equals(path)){
				hitIndex=i;
				break;
			}
		if(hitIndex == 0)	//首位不处理
			return ;
		
		String[] newList=new String[0];
		if(hitIndex != -1){		
			newList=new String[oldList.length];
			newList[0]=path	;
			System.arraycopy(oldList, 0, newList, 1, hitIndex);
			System.arraycopy(oldList, hitIndex+1, newList, hitIndex+1, oldList.length-hitIndex-1);
		}else{ 
			//不存在,加1个,超过10个,减1个
			newList=new String[Math.min(oldList.length,9)+1];
			newList[0]=path	;
			System.arraycopy(oldList, 0, newList, 1, Math.min(oldList.length,9));
		}
		ConfigHelper.getPropList().setProperty(FreeNoteConstants.MRU, StringUtil.fromArray(newList, ","));
	}

	//偷个懒,调至首位,删之
	public static synchronized void removeFromMRU(String path){
		addToMRU(path);
		String[] oldList=ConfigHelper.getStringArrayProperty(FreeNoteConstants.MRU);
		String[] newList=new String[oldList.length -1];
		System.arraycopy(oldList, 1, newList, 0, newList.length);
		ConfigHelper.getPropList().setProperty(FreeNoteConstants.MRU, StringUtil.fromArray(newList, ","));
	}	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?