shapeseditor.java

来自「mywork是rcp开发的很好的例子」· Java 代码 · 共 407 行 · 第 1/2 页

JAVA
407
字号
/******************************************************************************* * Copyright (c) 2004, 2005 Elias Volanakis and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Elias Volanakis - initial API and implementation�*******************************************************************************/package net.sf.freenote;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.net.URL;import java.util.ArrayList;import java.util.EventObject;import java.util.List;import net.sf.freenote.action.ChangeColorAction;import net.sf.freenote.action.ChangeConnectionStyleAction;import net.sf.freenote.action.ChangePropertiesAction;import net.sf.freenote.action.ChangeZoomLevelAction;import net.sf.freenote.action.ExportShapeAction;import net.sf.freenote.action.FileChooseAction;import net.sf.freenote.action.FileSaveAsAction;import net.sf.freenote.action.FitImageAction;import net.sf.freenote.action.OpenShapeAction;import net.sf.freenote.action.SelectSameAction;import net.sf.freenote.action.TextDirectEditAction;import net.sf.freenote.mindmap.ChangeLayoutAction;import net.sf.freenote.mindmap.ExpandAction;import net.sf.freenote.mindmap.model.BranchShape;import net.sf.freenote.mindmap.model.RootShape;import net.sf.freenote.model.CircleShape;import net.sf.freenote.model.Connection;import net.sf.freenote.model.EllipticalShape;import net.sf.freenote.model.FileShape;import net.sf.freenote.model.ImageFileShape;import net.sf.freenote.model.LinkFileShape;import net.sf.freenote.model.ModelElement;import net.sf.freenote.model.RectangularShape;import net.sf.freenote.model.Shape;import net.sf.freenote.model.ShapesDiagram;import net.sf.freenote.model.TextShape;import net.sf.freenote.parts.ShapesEditPartFactory;import net.sf.freenote.uml.model.ClassShape;import net.sf.freenote.uml.model.SystemShape;import net.sf.freenote.uml.model.UmlShape;import net.sf.freenote.uml.model.UseCaseShape;import net.sf.freenote.uml.model.UserRoleShape;import net.sf.util.StringUtil;import org.eclipse.core.runtime.FileLocator;import org.eclipse.core.runtime.IProgressMonitor;import org.eclipse.core.runtime.Path;import org.eclipse.draw2d.PositionConstants;import org.eclipse.draw2d.geometry.Dimension;import org.eclipse.draw2d.geometry.Point;import org.eclipse.gef.ContextMenuProvider;import org.eclipse.gef.DefaultEditDomain;import org.eclipse.gef.GraphicalViewer;import org.eclipse.gef.KeyHandler;import org.eclipse.gef.KeyStroke;import org.eclipse.gef.dnd.TemplateTransferDragSourceListener;import org.eclipse.gef.dnd.TemplateTransferDropTargetListener;import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;import org.eclipse.gef.editparts.ZoomManager;import org.eclipse.gef.palette.PaletteRoot;import org.eclipse.gef.requests.CreationFactory;import org.eclipse.gef.requests.SimpleFactory;import org.eclipse.gef.ui.actions.ActionRegistry;import org.eclipse.gef.ui.actions.AlignmentAction;import org.eclipse.gef.ui.actions.GEFActionConstants;import org.eclipse.gef.ui.actions.MatchHeightAction;import org.eclipse.gef.ui.actions.MatchWidthAction;import org.eclipse.gef.ui.actions.ZoomInAction;import org.eclipse.gef.ui.actions.ZoomOutAction;import org.eclipse.gef.ui.palette.PaletteViewer;import org.eclipse.gef.ui.palette.PaletteViewerProvider;import org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette;import org.eclipse.gef.ui.parts.GraphicalViewerKeyHandler;import org.eclipse.jface.action.IAction;import org.eclipse.jface.dialogs.MessageDialog;import org.eclipse.jface.util.TransferDropTargetListener;import org.eclipse.swt.SWT;import org.eclipse.swt.graphics.RGB;import org.eclipse.swt.widgets.FileDialog;import org.eclipse.ui.IEditorInput;import org.eclipse.ui.IEditorPart;import org.eclipse.ui.IPathEditorInput;import org.eclipse.ui.IWorkbenchPart;import org.eclipse.ui.actions.ActionFactory;import org.eclipse.ui.internal.SaveAllAction;import org.eclipse.ui.internal.SaveAsAction;import org.eclipse.ui.internal.part.NullEditorInput;import com.thoughtworks.xstream.XStream;import com.thoughtworks.xstream.io.xml.DomDriver;/** * A graphical editor with flyout palette that can edit .shapes files. The * binding between the .shapes file extension and this editor is done in * plugin.xml *  * @author Elias Volanakis */public class ShapesEditor extends GraphicalEditorWithFlyoutPalette {	public static final String ID = "net.sf.freenote.ShapesEditor";	/** This is the root of the editor's model. */	private ShapesDiagram diagram;	/** Palette component, holding the tools and shapes. */	private static PaletteRoot PALETTE_MODEL;	private KeyHandler sharedKeyHandler;	private static XStream xstream = new XStream(new DomDriver());	//优化一下xstream	static{		//查找所有ModelElment的子类,都简化一下		Class[] clazz=new Class[]{ModelElement.class,Connection.class,ShapesDiagram.class,Shape.class,				CircleShape.class,EllipticalShape.class,FileShape.class,ImageFileShape.class,LinkFileShape.class,RectangularShape.class,TextShape.class,				BranchShape.class,RootShape.class,				UmlShape.class,ClassShape.class,SystemShape.class,UseCaseShape.class,UserRoleShape.class				};		for(Class c:clazz){			xstream.alias(c.getSimpleName(), c);		}		xstream.useAttributeFor(RGB.class,"red");		xstream.useAttributeFor(RGB.class,"green");		xstream.useAttributeFor(RGB.class,"blue");		xstream.useAttributeFor(Point.class, "x");		xstream.useAttributeFor(Point.class, "y");		xstream.useAttributeFor(Dimension.class,"width");		xstream.useAttributeFor(Dimension.class,"height");				xstream.addImplicitCollection(ShapesDiagram.class, "children");		xstream.addImplicitCollection(BranchShape.class, "children");		xstream.addImplicitCollection(SystemShape.class, "children");	}	/** Create a new ShapesEditor instance. This is called by the Workspace. */	public ShapesEditor() {		setEditDomain(new DefaultEditDomain(this));	}	/**	 * Configure the graphical viewer before it receives contents.	 * <p>	 * This is the place to choose an appropriate RootEditPart and	 * EditPartFactory for your editor. The RootEditPart determines the behavior	 * of the editor's "work-area". For example, GEF includes zoomable and	 * scrollable root edit parts. The EditPartFactory maps model elements to	 * edit parts (controllers).	 * </p>	 * 	 * @see org.eclipse.gef.ui.parts.GraphicalEditor#configureGraphicalViewer()	 */	protected void configureGraphicalViewer() {		super.configureGraphicalViewer();		GraphicalViewer viewer = getGraphicalViewer();		viewer.setEditPartFactory(new ShapesEditPartFactory());		ScalableFreeformRootEditPart rootPart = new ScalableFreeformRootEditPart();		viewer.setRootEditPart(rootPart);		viewer.setKeyHandler(new GraphicalViewerKeyHandler(viewer).setParent(getCommonKeyHandler()));		//定制zoommanager		ZoomManager zoomManager = rootPart.getZoomManager();		double[] levels=new double[]{0.1,0.25,0.5,0.75,1.0,1.25,1.5,2.0,3.0,5.0,10.0,20.0};		zoomManager.setZoomLevels(levels);		List<String> list=new ArrayList<String>();		list.add(FreeNoteConstants.ZOOM_SEPARATOR);		list.add(ZoomManager.FIT_ALL);		list.add(ZoomManager.FIT_HEIGHT);		list.add(ZoomManager.FIT_WIDTH);		zoomManager.setZoomLevelContributions(list);				//注册缩放的action		IAction action=new ZoomInAction(zoomManager);		getActionRegistry().registerAction(action);		action=new ZoomOutAction(zoomManager);		getActionRegistry().registerAction(action);		// configure the context menu provider		ContextMenuProvider cmProvider = new ShapesEditorContextMenuProvider(viewer, getActionRegistry());		viewer.setContextMenu(cmProvider);		getSite().registerContextMenu(cmProvider, viewer);	}	protected void createActions() {		super.createActions();		ActionRegistry ar = getActionRegistry();		//一组继承自selectionaction的action		IAction[] actions = new IAction[] { new TextDirectEditAction(this), // 直接编辑				new ChangeColorAction(this, FreeNoteConstants.BACKCOLOR), // 更改背景色				new ChangeColorAction(this, FreeNoteConstants.FORECOLOR), // 更改前景色				new FileChooseAction(this), // 选文件				new FileSaveAsAction(this), // 存文件				new FitImageAction(this), //适应图片				new AlignmentAction((IWorkbenchPart)this,PositionConstants.LEFT), //一组对齐				new AlignmentAction((IWorkbenchPart)this,PositionConstants.CENTER),

⌨️ 快捷键说明

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