changeconnectionstyleaction.java

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

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

import net.sf.freenote.FreeNoteConstants;
import net.sf.freenote.commands.ModelPropertyCommand;
import net.sf.freenote.model.Connection;
import net.sf.freenote.parts.ConnectionEditPart;
import net.sf.util.StringUtil;

import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.ui.actions.SelectionAction;
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.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowPulldownDelegate;
import org.eclipse.ui.internal.WorkbenchWindow;

/**
 * 改变连接线的样式,箭头和线型
 * @author levin
 * @since 2008-2-19 下午10:12:19
 */
public class ChangeConnectionStyleAction extends SelectionAction implements IWorkbenchWindowPulldownDelegate {
	//几种样式,样式代码为三位,第一位线型,第二位始端修饰,第三位终端修饰
	private static String[][] styles=new String[][]{
		{"000","实线"},
		{"200","虚线"},
		{"001","实线终端箭头"},
		{"201","虚线终端箭头"},
		{"011","实线两端箭头"},
		{"211","虚线两端箭头"},
		{"003","实线终端菱形"}
	};
	private Menu toolbarMenu;
	public ChangeConnectionStyleAction(IWorkbenchPart part) {
		super(part);
		setText("改变连接线的样式");
		setToolTipText("(批量)改变连接线的样式");
		setId(FreeNoteConstants.CHANGE_CONNECTION_STYLE);
	}

	@Override
	protected boolean calculateEnabled() {
		//不为空,且有ConnectionEditPart
		if(getSelectedObjects().isEmpty())
			return false;
		for(Object o:getSelectedObjects())
			if(o instanceof ConnectionEditPart)
				return true;
		return false;
	}

	@Override
	public void run() {
		WorkbenchWindow window=(WorkbenchWindow) getWorkbenchPart().getSite().getWorkbenchWindow();
		getMenu(window.getCoolBarManager().getControl()).setVisible(true);
	}

	@Override
	public Menu getMenu(Control parent) {
		if (toolbarMenu == null) {
			toolbarMenu = new Menu(parent);
			fillMenu(toolbarMenu);
		}
		return toolbarMenu;
	}

	private void fillMenu(Menu menu) {
		for(int i=0;i<styles.length;i++){
			if(FreeNoteConstants.ZOOM_SEPARATOR.equals(styles[i][1])){
				new MenuItem(menu,SWT.SEPARATOR);
				continue;
			}
			MenuItem choice = new MenuItem(menu,SWT.PUSH);
			choice.setText(styles[i][1]);
			choice.setData(styles[i][0]);
			choice.addSelectionListener(new SelectionAdapter(){
				@Override
				public void widgetSelected(SelectionEvent se) {
					String style =(String) ((MenuItem)(se.getSource())).getData();
					changeStyle(style);
				}});
		}

	}

	private void changeStyle(String style) {
		//依据style来设置连接线的属性
		if(style == null || style.length() != 3) return;
		int lineStyle=StringUtil.parseInt(style.substring(0,1));
		int sourceDecoration=StringUtil.parseInt(style.substring(1,2));
		int targetDecoration=StringUtil.parseInt(style.substring(2));
		CompoundCommand cc=new CompoundCommand();
		for(Object part:getSelectedObjects()){
			if(part instanceof ConnectionEditPart){
				Connection model = ((ConnectionEditPart)part).getCastedModel();
				if(model.getLineStyle().intValue() != lineStyle)
					cc.add(new ModelPropertyCommand(model,lineStyle,Connection.LINESTYLE_PROP));
				if(model.getSourceDecoration().intValue() != sourceDecoration)
					cc.add(new ModelPropertyCommand(model,sourceDecoration,Connection.SOURCE_DECORATION_PROP));
				if(model.getTargetDecoration().intValue() != targetDecoration)
					cc.add(new ModelPropertyCommand(model,targetDecoration,Connection.TARGET_DECORATION_PROP));
			}
		}
		this.getCommandStack().execute(cc);
	}

	@Override
	public void init(IWorkbenchWindow window) {
		
	}

	@Override
	public void run(IAction action) {
		
	}

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

⌨️ 快捷键说明

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