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

📄 floweditor.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.ui.editors;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.util.EventObject;

import net.orthanc.flow4j.designer.core.Flow4JPlugin;
import net.orthanc.flow4j.designer.core.JavaTaskFlowletClassLoader;
import net.orthanc.flow4j.designer.editparts.Flow4JEditPartFactory;
import net.orthanc.flow4j.designer.model.DesignerModelDigester;
import net.orthanc.flow4j.designer.model.FlowDiagramModelPart;
import net.orthanc.flow4j.designer.ui.dnd.FlowTemplateTransferDropTargetListener;
import net.orthanc.flow4j.designer.ui.dnd.TaskTransferDropTargetListener;
import net.orthanc.flow4j.flows.updateflowfile.UpdateFlowFileConsts;
import net.orthanc.flow4j.model.bind.BindingHandler;
import net.orthanc.flow4j.model.bind.FlowModelBind;
import net.orthanc.flow4j.runtime.Flow4JRuntimeException;
import net.orthanc.flow4j.runtime.FlowDictionary;
import net.orthanc.flow4j.runtime.FlowManager;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.gef.ContextMenuProvider;
import org.eclipse.gef.DefaultEditDomain;
import org.eclipse.gef.EditDomain;
import org.eclipse.gef.Tool;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.editparts.ZoomManager;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.gef.ui.actions.CopyTemplateAction;
import org.eclipse.gef.ui.actions.GEFActionConstants;
import org.eclipse.gef.ui.actions.ZoomInAction;
import org.eclipse.gef.ui.actions.ZoomOutAction;
import org.eclipse.gef.ui.parts.GraphicalEditorWithPalette;
import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
import org.eclipse.gef.ui.stackview.CommandStackInspectorPage;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.ui.IEditorActionBarContributor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.part.EditorActionBarContributor;
import org.eclipse.ui.part.FileEditorInput;

/**
 * @author agreif
 *
 * TODO
 * 
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class FlowEditor extends GraphicalEditorWithPalette {

	private PaletteRoot root;
	private FlowDiagramModelPart flowDiagram = new FlowDiagramModelPart();
	//private KeyHandler sharedKeyHandler;
	private boolean savePreviouslyNeeded = false;
	//private ResourceTracker resourceListener = new ResourceTracker();

	/**
	 * Creates a new flow editor and sets it's edit domain,
	 */
	public FlowEditor() {
		setEditDomain(new DefaultEditDomain(this));
	}

	public void dispose() {
		CopyTemplateAction copy = (CopyTemplateAction)getActionRegistry().getAction(GEFActionConstants.COPY);
		getPaletteViewer().removeSelectionChangedListener(copy);
		//getSite().getWorkbenchWindow().getPartService().removePartListener(partListener);
		//partListener = null;
		//((FileEditorInput)getEditorInput()).getFile().getWorkspace().removeResourceChangeListener(resourceListener);
		super.dispose();
	}

	/**
	 * @see org.eclipse.gef.ui.parts.GraphicalEditorWithPalette#getPaletteRoot()
	 */
	protected PaletteRoot getPaletteRoot() {
		if (root == null)
			root = PaletteHelper.createPalette();

		return root;
	}

//	/**
//	 * Returns the palette root of this editor.
//	 * @return the palette root of this editor.
//	 */
//	public PaletteRoot getEditorPaletteRoot() {
//		return getPaletteRoot();
//	}
//
//	/**
//	 * returns the editor's edit domain
//	 * @return the editor's edit domain
//	 */
//	public DefaultEditDomain getEditorEditDomain() {
//		return getEditDomain();
//	}

	public void activateDefaultTool() {
		EditDomain editDomain = getEditDomain();
		Tool defaultTool = getPaletteRoot().getDefaultEntry().createTool();
		//editDomain.setActiveTool(defaultTool);
		getPaletteViewer().setActiveTool(getPaletteRoot().getDefaultEntry());
	}



	/**
	 * Get the desktop's StatusLineManager
	 */
	public IStatusLineManager getStatusLineManager() {
		IEditorActionBarContributor contributor= getEditorSite().getActionBarContributor();
		if (contributor instanceof EditorActionBarContributor) {
			return ((EditorActionBarContributor) contributor).getActionBars().getStatusLineManager();
		}
		return null;
	}	






	/**
	 * Returns the editor's flow diagram instance
	 * @return the editor's flow diagram instance
	 */
	public FlowDiagramModelPart getFlowDiagram() {
		return flowDiagram;
	}

	/**
	 * TODO
	 * @param diagram
	 */
	public void setFlowDiagram(FlowDiagramModelPart diagram) {
		flowDiagram = diagram;
	}


	/**
	 * Returns the File which corresponds with this Editor
	 * @return the File which corresponds with this Editor
	 */
	public IFile getFlowFile() {
		return ((FileEditorInput)getEditorInput()).getFile();
	}


	/**
	 * @see org.eclipse.gef.ui.parts.GraphicalEditor#configureGraphicalViewer()
	 */
	protected void configureGraphicalViewer() {
		super.configureGraphicalViewer();
		ScrollingGraphicalViewer viewer = (ScrollingGraphicalViewer)getGraphicalViewer();

		ScalableFreeformRootEditPart root = new ScalableFreeformRootEditPart();

		IAction zoomIn = new ZoomInAction(root.getZoomManager());
		IAction zoomOut = new ZoomOutAction(root.getZoomManager());
		getActionRegistry().registerAction(zoomIn);
		getActionRegistry().registerAction(zoomOut);
		getSite().getKeyBindingService().registerAction(zoomIn);
		getSite().getKeyBindingService().registerAction(zoomOut);

		viewer.setRootEditPart(root);

		viewer.setEditPartFactory(new Flow4JEditPartFactory(getFlowFile()));
		ContextMenuProvider provider = new Flow4JContextMenuProvider(viewer, getActionRegistry());
		viewer.setContextMenu(provider);
		getSite().registerContextMenu("net.orthanc.flow4j.designer.ui.editors.contextmenu", //$NON-NLS-1$
		provider, viewer);
		/**		viewer.setKeyHandler(new GraphicalViewerKeyHandler(viewer)
					.setParent(getCommonKeyHandler()));
		*/
	}

	/**
	 * @see org.eclipse.gef.ui.parts.GraphicalEditor#initializeGraphicalViewer()
	 */
	protected void initializeGraphicalViewer() {
		getGraphicalViewer().setContents(getFlowDiagram());
		getGraphicalViewer().addDropTargetListener(new FlowTemplateTransferDropTargetListener(getGraphicalViewer()));
		/*		getGraphicalViewer().addDropTargetListener(
					new TextTransferDropTargetListener(getGraphicalViewer(), TextTransfer.getInstance()));
		*/
		
		getGraphicalViewer().addDropTargetListener(
			   new TaskTransferDropTargetListener(getGraphicalViewer()));

	}

	/**
	 * Returns the KeyHandler with common bindings for both the Outline and Graphical Views.
	 * For example, delete is a common action.
	 */
	/*	protected KeyHandler getCommonKeyHandler(){
			if (sharedKeyHandler == null){
				sharedKeyHandler = new KeyHandler();
				sharedKeyHandler.put(
					KeyStroke.getPressed(SWT.DEL, 127, 0),
					getActionRegistry().getAction(GEFActionConstants.DELETE));
				sharedKeyHandler.put(
					KeyStroke.getPressed(SWT.F2, 0),
					getActionRegistry().getAction(GEFActionConstants.DIRECT_EDIT));
			}
			return sharedKeyHandler;
		}
	*/

	public Object getAdapter(Class type) {
		if (type == CommandStackInspectorPage.class)
			return new CommandStackInspectorPage(getCommandStack());
		/*if (type == IContentOutlinePage.class)
			return new OutlinePage(new TreeViewer());*/
		if (type == ZoomManager.class)
			return ((ScalableFreeformRootEditPart)getGraphicalViewer().getRootEditPart()).getZoomManager();

		return super.getAdapter(type);
	}

	/**
	 * @see org.eclipse.gef.commands.CommandStackListener#commandStackChanged(java.util.EventObject)
	 */
	public void commandStackChanged(EventObject event) {
		if (isDirty()) {
			if (!savePreviouslyNeeded()) {
				setSavePreviouslyNeeded(true);
				firePropertyChange(IEditorPart.PROP_DIRTY);
			}
		} else {
			setSavePreviouslyNeeded(false);
			firePropertyChange(IEditorPart.PROP_DIRTY);
		}
		super.commandStackChanged(event);
	}

	/**
	 * TODO
	 * @return
	 */
	private boolean savePreviouslyNeeded() {
		return savePreviouslyNeeded;
	}

	/**
	 * TODO
	 * @param value
	 */
	private void setSavePreviouslyNeeded(boolean value) {
		savePreviouslyNeeded = value;
	}

	/**
	 * saves the marshalled model.
	 * Afterwords the Fileresource is written.
	 * This is necessary to inform the workspace that the filecontent has changed.
	 * @see org.eclipse.ui.ISaveablePart#doSave(org.eclipse.core.runtime.IProgressMonitor)
	 */
	public void doSave(IProgressMonitor monitor) {
		IFile file = ((IFileEditorInput)getEditorInput()).getFile();
		try {
			FlowModelBind flowModelBind = new DesignerModelDigester(getFlowDiagram()).createFlowModelBind();
			ByteArrayInputStream bin = BindingHandler.getInstance().saveFlowModel(flowModelBind);
			file.setContents(bin, true, true, monitor);
			bin.close();
			getCommandStack().markSaveLocation();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.ISaveablePart#doSaveAs()
	 */
	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IEditorPart#gotoMarker(org.eclipse.core.resources.IMarker)
	 */
	public void gotoMarker(IMarker marker) {
		// TODO Auto-generated method stub

	}

	/**
	 * @see org.eclipse.ui.ISaveablePart#isDirty()
	 */
	public boolean isDirty() {
		return isSaveOnCloseNeeded();
	}

	/**
	 * @see org.eclipse.ui.ISaveablePart#isSaveOnCloseNeeded()
	 */
	public boolean isSaveOnCloseNeeded() {
		return getCommandStack().isDirty();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
	 */
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * @see org.eclipse.gef.ui.parts.GraphicalEditor#createActions()
	 */
	protected void createActions() {
		super.createActions();
		ActionRegistry registry = getActionRegistry();
		IAction action;

	}

	/**
	 * TODO
	 * @param input
	 */
	private void superSetInput(IEditorInput input) {
		// The workspace never changes for an editor.  So, removing and re-adding the 
		// resourceListener is not necessary.  But it is being done here for the sake
		// of proper implementation.  Plus, the resourceListener needs to be added 
		// to the workspace the first time around.
		/*if (getEditorInput() != null) {
			IFile file = ((FileEditorInput)getEditorInput()).getFile();
			file.getWorkspace().removeResourceChangeListener(resourceListener);
		}*/

		super.setInput(input);

		if (getEditorInput() != null) {
			IFile file = ((FileEditorInput)getEditorInput()).getFile();
			//file.getWorkspace().addResourceChangeListener(resourceListener);
			setTitle(file.getName());
		}
	}

	/**
	 * @see org.eclipse.ui.part.EditorPart#setInput(org.eclipse.ui.IEditorInput)
	 */
	public void setInput(IEditorInput input) {
		superSetInput(input);

		IFile file = ((IFileEditorInput)input).getFile();
		try {
			FlowDictionary dictionary = new FlowDictionary();
			dictionary.put(UpdateFlowFileConsts.KEY_FLOW_FILE, file.getLocation().toFile());
			try {
				FlowManager.executeFlow("UpdateFlowFile", "Start", dictionary);
			} catch(Flow4JRuntimeException e) {
				//	file was corrupt, reset it
				Flow4JPlugin.message( "The file was an invalid flow file. It will get reset to an empty flow file. (file:" + file.getLocation() + " cause: " + e.getMessage() + ")"); 
				String fileName = file.getName();
				String flowName = fileName.substring(0, fileName.length()-file.getFullPath().getFileExtension().length()-1);
				FlowModelBind flowModelBind = new FlowModelBind(flowName);
				
				FileOutputStream fout = new FileOutputStream(file.getLocation().toFile());
				BindingHandler.getInstance().saveFlowModel(flowModelBind, fout);
				fout.close();

				
//				FlowModelBind.saveFlowDiagram(flowDiagramBind, file.getLocation().toFile());
			}
			JavaTaskFlowletClassLoader taskFlowletClassLoader = Flow4JPlugin.getTaskFlowletClassLoader(file);
//			FlowModelBind flowDiagramBind = FlowModelBind.loadFlowDiagram(file.getLocation().toFile(), taskFlowletClassLoader);
	
			net.orthanc.flow4j.model.bind.FlowModelBind flowModelBind = BindingHandler.getInstance().loadFlowModel(file.getLocation().toFile());

			FlowDiagramModelPart flowDiagramModelPart = new FlowDiagramModelPart();
//			DesignerModelDigester digester = new DesignerModelDigester(flowDiagramModelPart);
			DesignerModelDigester digester = new DesignerModelDigester(flowDiagramModelPart);
			digester.digest(flowModelBind);
			setFlowDiagram(flowDiagramModelPart);
			digester.getCreateCommand().execute();
		} catch (Flow4JRuntimeException e) {
			Flow4JPlugin.log(e);
			Flow4JPlugin.message("Problems while updating flow file: " + e.getMessage());
		} catch (Exception e) {
			Flow4JPlugin.log(e);
			Flow4JPlugin.message("Problems while opening flow file: " + e.getMessage());
		}
	}

}

⌨️ 快捷键说明

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