shapeeditpart.java

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

JAVA
303
字号
/******************************************************************************* * 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.parts;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.util.List;import net.sf.freenote.FreeNoteConstants;import net.sf.freenote.commands.ConnectionCreateCommand;import net.sf.freenote.commands.ConnectionReconnectCommand;import net.sf.freenote.directedit.DescDirectEditPolicy;import net.sf.freenote.directedit.DirectEditable;import net.sf.freenote.directedit.ShapeDirectEditManager;import net.sf.freenote.directedit.ShapeCellEditorLocator;import net.sf.freenote.figure.FileFigure;import net.sf.freenote.figure.ImageFileFigure;import net.sf.freenote.figure.LinkFileFigure;import net.sf.freenote.figure.TextFigure;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.TextShape;import org.eclipse.draw2d.ChopboxAnchor;import org.eclipse.draw2d.ConnectionAnchor;import org.eclipse.draw2d.Ellipse;import org.eclipse.draw2d.EllipseAnchor;import org.eclipse.draw2d.IFigure;import org.eclipse.draw2d.RectangleFigure;import org.eclipse.draw2d.geometry.Rectangle;import org.eclipse.gef.ConnectionEditPart;import org.eclipse.gef.EditPolicy;import org.eclipse.gef.GraphicalEditPart;import org.eclipse.gef.NodeEditPart;import org.eclipse.gef.Request;import org.eclipse.gef.RequestConstants;import org.eclipse.gef.commands.Command;import org.eclipse.gef.editparts.AbstractGraphicalEditPart;import org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy;import org.eclipse.gef.requests.CreateConnectionRequest;import org.eclipse.gef.requests.ReconnectRequest;import org.eclipse.swt.graphics.Color;/** * EditPart used for Shape instances (more specific for EllipticalShape and * RectangularShape instances). * <p>This edit part must implement the PropertyChangeListener interface,  * so it can be notified of property changes in the corresponding model element. * </p> *  * @author Elias Volanakis */public class ShapeEditPart extends AbstractGraphicalEditPart 	implements PropertyChangeListener, NodeEditPart {	protected ConnectionAnchor anchor;/** * Upon activation, attach to the model element as a property change listener. */public void activate() {	if (!isActive()) {		super.activate();		((ModelElement) getModel()).addPropertyChangeListener(this);	}}/* (non-Javadoc) * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies() */protected void createEditPolicies() {	// allow removal of the associated model element	installEditPolicy(EditPolicy.COMPONENT_ROLE, new ShapeComponentEditPolicy());	// allow the creation of connections and 	// and the reconnection of connections between Shape instances	installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new GraphicalNodeEditPolicy() {		/* (non-Javadoc)		 * @see org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy#getConnectionCompleteCommand(org.eclipse.gef.requests.CreateConnectionRequest)		 */		protected Command getConnectionCompleteCommand(CreateConnectionRequest request) {			ConnectionCreateCommand cmd 				= (ConnectionCreateCommand) request.getStartCommand();			cmd.setTarget((Shape) getHost().getModel());			return cmd;		}		/* (non-Javadoc)		 * @see org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy#getConnectionCreateCommand(org.eclipse.gef.requests.CreateConnectionRequest)		 */		protected Command getConnectionCreateCommand(CreateConnectionRequest request) {			Shape source = (Shape) getHost().getModel();			int style = ((Integer) request.getNewObjectType()).intValue();			ConnectionCreateCommand cmd = new ConnectionCreateCommand(source, style);			request.setStartCommand(cmd);			return cmd;		}		/* (non-Javadoc)		 * @see org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy#getReconnectSourceCommand(org.eclipse.gef.requests.ReconnectRequest)		 */		protected Command getReconnectSourceCommand(ReconnectRequest request) {			Connection conn = (Connection) request.getConnectionEditPart().getModel();			Shape newSource = (Shape) getHost().getModel();			ConnectionReconnectCommand cmd = new ConnectionReconnectCommand(conn);			cmd.setNewSource(newSource);			return cmd;		}		/* (non-Javadoc)		 * @see org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy#getReconnectTargetCommand(org.eclipse.gef.requests.ReconnectRequest)		 */		protected Command getReconnectTargetCommand(ReconnectRequest request) {			Connection conn = (Connection) request.getConnectionEditPart().getModel();			Shape newTarget = (Shape) getHost().getModel();			ConnectionReconnectCommand cmd = new ConnectionReconnectCommand(conn);			cmd.setNewTarget(newTarget);			return cmd;		}	});	installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new DescDirectEditPolicy());}	/*(non-Javadoc) * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure() */protected IFigure createFigure() {	IFigure f = createFigureForModel();	f.setOpaque(true); // non-transparent figure	f.setBackgroundColor(new Color(null,getCastedModel().getBackColor()));	f.setForegroundColor(new Color(null,getCastedModel().getForeColor()));	if(f instanceof DirectEditable && getModel() instanceof DirectEditable){		((DirectEditable)f).setDesc(((DirectEditable)getModel()).getDesc());	}	return f;}/** * Return a IFigure depending on the instance of the current model element. * This allows this EditPart to be used for both sublasses of Shape.  */protected IFigure createFigureForModel() {	if (getModel() instanceof EllipticalShape) {		return new Ellipse();	} else if (getModel() instanceof RectangularShape) {		return new RectangleFigure();	} else if (getModel() instanceof CircleShape){		return new Ellipse();	} else if (getModel() instanceof TextShape){		return new TextFigure();	} else if (getModel() instanceof ImageFileShape){		ImageFileShape shape=(ImageFileShape) getModel();		ImageFileFigure figure = new ImageFileFigure();		figure.setFileEmbed(shape.getFileEmbed());		return figure;	} else if (getModel() instanceof LinkFileShape){		LinkFileShape shape=(LinkFileShape) getModel();		LinkFileFigure figure = new LinkFileFigure();		figure.setFileEmbed(shape.getFileEmbed());		return figure;	}else {		// if Shapes gets extended the conditions above must be updated		throw new IllegalArgumentException();	}}/** * Upon deactivation, detach from the model element as a property change listener. */public void deactivate() {	if (isActive()) {		super.deactivate();		((ModelElement) getModel()).removePropertyChangeListener(this);	}}public Shape getCastedModel() {	return (Shape) getModel();}protected ConnectionAnchor getConnectionAnchor() {	if (anchor == null) {		if (getModel() instanceof EllipticalShape)			anchor = new EllipseAnchor(getFigure());		else if (getModel() instanceof RectangularShape)			anchor = new ChopboxAnchor(getFigure());		else if (getModel() instanceof CircleShape)			anchor = new EllipseAnchor(getFigure());		else if (getModel() instanceof TextShape)			anchor = new ChopboxAnchor(getFigure());		else if (getModel() instanceof FileShape)			anchor = new ChopboxAnchor(getFigure());		else			// if Shapes gets extended the conditions above must be updated			throw new IllegalArgumentException("unexpected model");	}	return anchor;}/* * (non-Javadoc) * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getModelSourceConnections() */protected List getModelSourceConnections() {	return getCastedModel().getSourceConnections();}/* * (non-Javadoc) * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getModelTargetConnections() */protected List getModelTargetConnections() {	return getCastedModel().getTargetConnections();}/* * (non-Javadoc) * @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.ConnectionEditPart) */public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart connection) {	return getConnectionAnchor();}/* * (non-Javadoc) * @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.Request) */public ConnectionAnchor getSourceConnectionAnchor(Request request) {	return getConnectionAnchor();}/* * (non-Javadoc) * @see org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef.ConnectionEditPart) */public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart connection) {	return getConnectionAnchor();}/* * (non-Javadoc) * @see org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef.Request) */public ConnectionAnchor getTargetConnectionAnchor(Request request) {	return getConnectionAnchor();}/* (non-Javadoc) * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent) */public void propertyChange(PropertyChangeEvent evt) {	String prop = evt.getPropertyName();	if (Shape.SIZE_PROP.equals(prop) || Shape.LOCATION_PROP.equals(prop)) {		refreshVisuals();	}else if (FreeNoteConstants.BACKCOLOR.equals(prop)) {		getFigure().setBackgroundColor(new Color(null,getCastedModel().getBackColor()));	}else if (FreeNoteConstants.FORECOLOR.equals(prop)) {		getFigure().setForegroundColor(new Color(null,getCastedModel().getForeColor()));	} else if (Shape.SOURCE_CONNECTIONS_PROP.equals(prop)) {		refreshSourceConnections();	} else if (Shape.TARGET_CONNECTIONS_PROP.equals(prop)) {		refreshTargetConnections();	} else if (FreeNoteConstants.DESC_PROPERTY.equals(prop)) {		DirectEditable shape=(DirectEditable) getCastedModel();		((DirectEditable)getFigure()).setDesc(shape.getDesc());	}else if (FileShape.FILEPATH_PROP.equals(prop)){		FileShape shape=(FileShape)getCastedModel();		((FileFigure)getFigure()).setFileEmbed(shape.getFileEmbed());	}}protected void refreshVisuals() {	// notify parent container of changed position & location	// if this line is removed, the XYLayoutManager used by the parent container 	// (the Figure of the ShapesDiagramEditPart), will not know the bounds of this figure	// and will not draw it correctly.	Rectangle bounds = new Rectangle(getCastedModel().getLocation(),			getCastedModel().getSize());	((GraphicalEditPart) getParent()).setLayoutConstraint(this, getFigure(), bounds);}@Overridepublic void performRequest(Request req) {	if (req.getType() == RequestConstants.REQ_DIRECT_EDIT){		if(getModel() instanceof TextShape){			new ShapeDirectEditManager(this,new ShapeCellEditorLocator(getFigure())).show();		}	}}}

⌨️ 快捷键说明

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