⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flowletlabeleditpart.java

📁 一个java写的business process management系统
💻 JAVA
字号:
/*
 * Copyright (c) 2003, Alexander Greif
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Flow4J-Eclipse project nor the names of its
 *       contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package net.orthanc.flow4j.designer.editparts;

import java.beans.PropertyChangeEvent;
import java.util.Iterator;

import net.orthanc.flow4j.designer.editparts.policies.FlowletLabelDirectEditPolicy;
import net.orthanc.flow4j.designer.figures.Flow4FigureFactory;
import net.orthanc.flow4j.designer.figures.FlowletLabelFigure;
import net.orthanc.flow4j.designer.model.FlowletLabelModelPart;
import net.orthanc.flow4j.designer.model.FlowletWithLabelModelPart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.gef.tools.DirectEditManager;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Text;

/**
 * The flowlet label edit part.
 * @author agreif
 */
public class FlowletLabelEditPart extends FlowDiagramElementEditPart {

	private DirectEditManager manager;

	/**
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		super.createEditPolicies();
		//	should not be deletable on its own
		removeEditPolicy(EditPolicy.COMPONENT_ROLE);
		installEditPolicy(
			EditPolicy.DIRECT_EDIT_ROLE,
			new FlowletLabelDirectEditPolicy());
	}

	/**
	 * Creates the flowlet figure with the model's text.
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		if (getModel() == null)
			return null;

		//return Flow4FigureFactory.createFlowletLabelFigure(
		//	((FlowletLabelModelPart) getModel()).getText());

		FlowletLabelFigure flowletLabelFigure =
			(FlowletLabelFigure) Flow4FigureFactory.createFlowletLabelFigure(((FlowletLabelModelPart) getModel()).getText());
		adjustFlowletLabelFigure(flowletLabelFigure);
		return flowletLabelFigure;
	}



	protected void adjustFlowletLabelFigure(FlowletLabelFigure flowletLabelFigure) {
	}







	/**
	 * Returns the corresponding <code>FlowletWithLabelEditPart</code>.
	 * Scans through all editParts and returns one that's model 
	 * is the same as the flowlet of this instance. Returns <code>null</code>
	 * if not found.
	 * 
	 * @return the FlowletWithLabelEditPart of this instance
	 */
	protected FlowletWithLabelEditPart getFlowletWithLabelEditPart() {
		for (Iterator editPartsIter = getParent().getChildren().iterator();
			editPartsIter.hasNext();
			) {
			FlowDiagramElementEditPart editPart =
				(FlowDiagramElementEditPart) editPartsIter.next();
			if (editPart instanceof FlowletWithLabelEditPart) {
				FlowletWithLabelEditPart fwlep =
					(FlowletWithLabelEditPart) editPart;
				if (((FlowletWithLabelModelPart) fwlep.getModel())
					.getFlowletLabel()
					== getModel()) {
					return fwlep;
				}
			}
		}

		return null;
	}

	/**
	 * Performs a direct edit on the Flowlet Label.
	 */
	private void performDirectEdit() {
		if (manager == null)
			manager = getFlowletLabelEditManager();

		manager.show();
	}

	/**
	 * Returns the FlowletLabelEditManager for this type of EditPart.
	 * By defaualt the <code>forceDirty</code> flag is set to false.
	 * @return the FlowletLabelEditManager for tihis type of EditPart.
	 */
	protected FlowletLabelEditManager getFlowletLabelEditManager() {
		return new FlowletLabelEditManager(
			this,
			new LabelCellEditorLocator((Label) getFigure()),
			false);
	}

	/**
	 * @see org.eclipse.gef.EditPart#performRequest(org.eclipse.gef.Request)
	 */
	public void performRequest(Request request) {
		if (request.getType() == RequestConstants.REQ_DIRECT_EDIT)
			performDirectEdit();
	}

	/**
	 * Reacts on property change of the modelPart. Sets the figures text if
	 * the label's property was changed.
	 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
	 */
	public void propertyChange(PropertyChangeEvent event) {
		String propName = event.getPropertyName();
		if (FlowletLabelModelPart.PROP_LABEL.equals(propName))
			((FlowletLabelFigure) getFigure()).setText(
				(String) event.getNewValue());
		else
			super.propertyChange(event);
	}

	/**
	 * Returns the model of this as a FlowDiagramModelPart.
	 * @return  FlowDiagramModelPart of this.
	 */
	protected FlowletLabelModelPart getFlowletLabel() {
		return (FlowletLabelModelPart) getModel();
	}

	/**
	 * Relocates the flowlet label's editor 
	 * @author alex
	 */
	protected class LabelCellEditorLocator implements CellEditorLocator {

		private Label label;

		public LabelCellEditorLocator(Label label) {
			setLabel(label);
		}

		public void relocate(CellEditor celleditor) {
			Text text = (Text) celleditor.getControl();
			Point pref = text.computeSize(-1, -1);
			Rectangle rect = label.getTextBounds().getCopy();
			label.translateToAbsolute(rect);
			text.setBounds(rect.x - 4, rect.y - 1, pref.x + 1, pref.y + 1);
		}

		protected Label getLabel() {
			return label;
		}

		protected void setLabel(Label label) {
			this.label = label;
		}

	}

	/**
	 * The direct edit manager for the flowlet's label
	 * @author alex
	 */
	protected class FlowletLabelEditManager extends DirectEditManager {
		
		/**
		 * If set to <code>true</code>, then the sellEditor's value
		 * will always be reset even when the vlaue was not changed by the user.
		 * This is necessary in the case when display text and edit text differ.
		 */
		private boolean forceDirty;
		private LabelCellEditorLocator locator;

		/**
		 * Contructor
		 * @param source
		 * @param locator
		 * @param forceDirty
		 */
		public FlowletLabelEditManager(
			GraphicalEditPart source,
			LabelCellEditorLocator locator,
			boolean forceDirty) {
			super(source, TextCellEditor.class, locator);
			this.forceDirty = forceDirty;
			this.locator = locator;
		}

		/**
		 * Initializes the Cell Editor.
		 * Sets the editable Text which is accesible from the label Model Part
		 */
		protected void initCellEditor() {
			Text text = (Text) getCellEditor().getControl();
			FlowletLabelEditPart labelEditPart =
				(FlowletLabelEditPart) getEditPart();
			
			//	set the text which is accesible from the label
			FlowletLabelModelPart model =
				(FlowletLabelModelPart) labelEditPart.getModel();
			getCellEditor().setValue(model.getText());
			text.selectAll();
		}

		/**
		 * Sets the flowlets name if the classname did not change.
		 * @see org.eclipse.gef.tools.DirectEditManager#commit()
		 */
		protected void commit() {
			if (forceDirty && !isDirty()) {
				Text text = (Text) getCellEditor().getControl();
				//	set to the original Text
				getCellEditor().setValue(locator.getLabel().getText());
				setDirty(true);
			}
			super.commit();
		}

	}

}

⌨️ 快捷键说明

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