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

📄 gpdocument.java

📁 用JGraph编的软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.jgraph.pad;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Observable;
import java.util.Observer;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.ToolTipManager;

import org.jgraph.GPGraphpad;
import org.jgraph.JGraph;
import org.jgraph.event.GraphModelEvent;
import org.jgraph.event.GraphModelListener;
import org.jgraph.event.GraphSelectionEvent;
import org.jgraph.event.GraphSelectionListener;
import org.jgraph.graph.CellView;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphLayoutCache;
import org.jgraph.graph.GraphModel;
import org.jgraph.graph.GraphUndoManager;
import org.jgraph.net.GraphNetworkModel;
import org.jgraph.pad.actions.FileSave;
import org.jgraph.pad.resources.Translator;
import org.jgraph.utils.Utilities;
import org.jgraph.utils.gui.GPSplitPane;

import org.jgpd.UI.JGpdPropertiesPanel;
import org.jgpd.io.*;
import org.jgpd.io.jbpm.*;

/**
 * A Document is a panel with a splitpane
 * the splitpane shows a library resource
 * and a
 *
 * @author sven.luzar
 *
 */
public class GPDocument
	extends JPanel
	implements
		GraphSelectionListener,
		ComponentListener,
		Printable,
		GraphModelListener,
		PropertyChangeListener,
		Observer {

	/**
	 */
	protected boolean enableTooltips;

	/** Filename for the current document.
	 * Null if never saved or opened. *
	 *
	 */
	protected URL file;

	/** A reference to the top level
	 *  component
	 */
	protected GPGraphpad graphpad;

	/** Splitpane between the
	 * libraries and the graph
	 */
	protected GPSplitPane splitPane;

	/** The left site of this document
	 *  Shows the libraries
	 *
	 */
//	protected GPLibraryPanel libraryPanel;

	/** Container for the graph so that you
	 *  can scroll over the graph
	 */
	protected JScrollPane scrollPane;

	/** The joint graph for this document
	 */
	protected GPGraph graph;

    /** The third party model, if present
     */
	protected ModelExportImpl exportModel;

	/** The overview Dialog for this document.
	 *  Can be <tt>null</tt>.
	 *
	 */
	protected JDialog overviewDialog;

	/** The column rule for the graph
	 */
	protected Rule columnRule;

	/** The row rule for the graph
	 */
	protected Rule rowRule;

	/** The graphUndoManager manager for the joint graph.
	 *
	 *  @see #graph
	 */
	protected GraphUndoManager graphUndoManager;

	/** The graphUndoManager handler for the current document.
	 * Each document has his own handler.
	 * So you can make an graphUndoManager seperate for each document.
	 *
	 */
	protected UndoHandler undoHandler;

	/** On the fly layout
	 *
	 */
	protected Touch touch;

	/** True if this documents graph model
	 *  was modified since last save. */
	protected boolean modified = false;

	/** true if the current graph is Metric.
	 *  default is true.
	 */
	protected static boolean isMetric = true;

	/** true if the library expand is expanded
	 *  default is true
	 */
	protected static boolean libraryExpanded = true;

	/** true if the ruler show is activated
	 */
	protected static boolean showRuler = true;

	/** Action used for fitting the size
	 */
	protected Action fitAction;

	/** contains the find pattern for this document
	 */
	protected String findPattern;

	/** contains the last found object
	 */
	protected Object lastFound;

	/** a reference to the internal Frame
	 */
	protected GPInternalFrame internalFrame;

	/** a reference to the graph model provider for
	 *  this document
	 */
	protected GraphModelProvider graphModelProvider;

	protected GraphNetworkModel networkModel;

	/** Static initializer.
	 *  Initializes some static variables.
	 */
	static {
		libraryExpanded =
			new Boolean(Translator.getString("LibraryExpanded")).booleanValue();
		isMetric = new Boolean(Translator.getString("IsMetric")).booleanValue();
		showRuler =
			new Boolean(Translator.getString("ShowRuler")).booleanValue();
	}

	/**
	 * Constructor for GPDocument.
	 */
	public GPDocument(
		GPGraphpad graphpad,
		URL file,
		GraphModelProvider graphModelProvider,
		GPGraph gpGraph,
		GraphModel model,
		GraphUndoManager undo) {
		super(true);

		this.file = file;
		this.graphpad = graphpad;
		this.graphModelProvider = graphModelProvider;

		undoHandler = new UndoHandler(this, true);
		this.graphUndoManager = undo == null ? createGraphUndoManager() : undo;

		setBorder(BorderFactory.createEtchedBorder());
		setLayout(new BorderLayout());

		// create the graph
		graph = gpGraph;
		touch = new Touch(graph);
		registerListeners(graph);

		// Add this as a listener for undoable edits.
		graph.getModel().addUndoableEditListener(undoHandler);
		networkModel = new GraphNetworkModel(graph.getModel());

		// FIXME todo db, make this any model
		// But make sure the export model is in place before the
		// center component (with the properties table model)
		// that depends on it.
		exportModel = new ModelExportJBpm();
		
		Component comp = this.createCenterComponent();
		this.add(comp);

		addComponentListener(this);

		setEnableTooltips(false);
		update();
	}

	/**
	 * Returns the filename.
	 * @return String
	 */
	public URL getFilename() {
		return file;
	}

	/**
	 * Sets the filename.
	 * @param filename The filename to set
	 */
	public void setFilename(URL filename) {
		this.file = filename;
		updateFrameTitle();
	}

	/**
	 * Create the center component of this panel.
	 * This creates a scroll-
	 * pane for the current graph variable and stores the scrollpane
	 * in the scrollPane variable.
	 */
	protected Component createCenterComponent() {
		Component mainPane = createScrollPane();

//      Properties pane added at bottom of main display
        JPanel propertiesPanel = JGpdPropertiesPanel.createPropPanel(getGraph(),
        															 this,
																	 this.graphpad);

		splitPane =
			new GPSplitPane(
				GPSplitPane.VERTICAL_SPLIT,
				mainPane,
				propertiesPanel);
		// Make the split 80% top and 20% bottom ( property panel )
		splitPane.setResizeWeight(0.15);
		splitPane.setName("DocumentMain");
		splitPane.setOneTouchExpandable(true);
		return splitPane;
	}

	protected Component createScrollPane() {
		scrollPane = new JScrollPane(graph);
		JViewport port = scrollPane.getViewport();
		try {
			String vpFlag = Translator.getString("ViewportBackingStore");
			Boolean bs = new Boolean(vpFlag);
			if (bs.booleanValue()) {
				port.setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE);
			} else {
				port.setScrollMode(JViewport.BLIT_SCROLL_MODE);
			}
			// deprecated
			// port.setBackingStoreEnabled(bs.booleanValue());
		} catch (MissingResourceException mre) {
			// just use the viewport default
		}
		columnRule = new Rule(Rule.HORIZONTAL, isMetric, graph);
		rowRule = new Rule(Rule.VERTICAL, isMetric, graph);
		if (showRuler) {
			scrollPane.setColumnHeaderView(columnRule);
			scrollPane.setRowHeaderView(rowRule);
		}
		return scrollPane;
	}

	protected GPLibraryPanel createLibrary() {
		return new GPLibraryPanel(
			Translator.getString("LibraryName"),
			Utilities.tokenize(Translator.getString("LoadLibraries")),
			new Integer(Translator.getString("EntrySize")).intValue());
	}

	protected GraphUndoManager createGraphUndoManager() {
		return new GraphUndoManager();
	}

	/**
	 * Fetch the editor contained in this panel
	 */
	public GPGraph getGraph() {
		return graph;
	}

	/** Returns the model of the graph
	 */
	public GraphModel getModel() {
		return graph.getModel();
	}

    /** Return the third party model set for export on this doc
     *
     * @return <code>ModelExportImpl</code> is the third party model
     */
    public ModelExportImpl getExportModel()
    {
    	return exportModel;
    }

    public void setExportModel(ModelExportImpl model)
    {
    	exportModel = model;
    }

	/** returns the GPGraph UI
	 */
	protected GPGraphUI getGraphUI() {
		return (GPGraphUI) graph.getUI();
	}

	/** Returns the view from the current graph
	 *
	 */
	public GraphLayoutCache getGraphLayoutCache() {
		return graph.getGraphLayoutCache();
	}

	/* Add this documents listeners to the specified graph. */
	protected void registerListeners(JGraph graph) {
		graph.getSelectionModel().addGraphSelectionListener(this);
		graph.getModel().addGraphModelListener(this);
		graph.getGraphLayoutCache().addObserver(this);
		graph.addPropertyChangeListener(this);
	}

	/* Remove this documents listeners from the specified graph. */
	protected void unregisterListeners(JGraph graph) {
		graph.getSelectionModel().removeGraphSelectionListener(this);
		graph.getModel().removeGraphModelListener(this);
		graph.removePropertyChangeListener(this);
	}

	public void setModified(boolean modified) {
		this.modified = modified;
		updateFrameTitle();
		graphpad.update();
	}

	/* Return the title of this document as a string. */
	protected String getDocumentTitle() {
		String s =
			(file != null) ? file.toString() : Translator.getString("NewGraph");
		if (modified)
			s = s + " *";
		return Translator.getString("Title") + " - " + s;
	}

	/* Return the status of this document as a string. */
	protected String getDocumentStatus() {
		String s = null;
		int n = graph.getSelectionCount();
		if (n > 0)
			s = n + " " + Translator.getString("Selected");
		else {
			int c = graph.getModel().getRootCount();
			if (c == 0) {
				s = Translator.getString("Empty");
			} else {
				s = c + " ";
				if (c > 1)
					s += Translator.getString("Cells");
				else
					s += Translator.getString("Cell");
				c = GPGraphpad.getGraphTools().getComponentCount(graph);
				s = s + " / " + c + " ";
				if (c > 1)
					s += Translator.getString("Components");
				else
					s += Translator.getString("Component");
			}
		}
		return s;
	}

	/* Return the scale of this document as a string. */
	protected String getDocumentScale() {
		return Integer.toString((int) (graph.getScale() * 100)) + "%";
	}

	/* Sets the attributes of the selected cells. */
	public void setSelectionAttributes(Map map) {
		Object[] cells =
			DefaultGraphModel
				.getDescendants(graph.getModel(), graph.getSelectionCells())
				.toArray();
		//Filter ports out
		java.util.List list = new ArrayList();
		for (int i = 0; i < cells.length; i++)
			if (!graph.isPort(cells[i]))
				list.add(cells[i]);
		cells = list.toArray();
		map = GraphConstants.cloneMap(map);
		map.remove(GraphConstants.BOUNDS);
		map.remove(GraphConstants.POINTS);

		Map nested = new Hashtable();
		for (int i = 0; i < cells.length; i++)
			nested.put(cells[i], GraphConstants.cloneMap(map));
		graph.getGraphLayoutCache().edit(nested, null, null, null);
	}

	/* Sets the attributes of the selected cells. */
	public void setFontSizeForSelection(float size) {
		Object[] cells =
			DefaultGraphModel
				.getDescendants(graph.getModel(), graph.getSelectionCells())
				.toArray();
		//Filter ports out
		java.util.List list = new ArrayList();
		for (int i = 0; i < cells.length; i++)
			if (!graph.isPort(cells[i]))
				list.add(cells[i]);
		cells = list.toArray();

		Map nested = new Hashtable();
		for (int i = 0; i < cells.length; i++) {
			CellView view = graph.getGraphLayoutCache().getMapping(cells[i], false);
			if (view != null) {
				Font font = GraphConstants.getFont(view.getAllAttributes());
				Map attr = GraphConstants.createMap();
				GraphConstants.setFont(attr, font.deriveFont(size));
				nested.put(cells[i], attr);
			}
		}
		graph.getGraphLayoutCache().edit(nested, null, null, null);
	}

⌨️ 快捷键说明

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