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

📄 jgraph.java

📁 用JGraph编的软件
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * @(#)JGraph.java	1.0 1/1/02 * * Copyright (c) 2001-2004, Gaudenz Alder * 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 JGraph 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 org.jgraph;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.Graphics;import java.awt.Point;import java.awt.Rectangle;import java.awt.event.MouseEvent;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.util.Map;import java.util.Set;import java.util.Vector;import javax.accessibility.Accessible;import javax.swing.BorderFactory;import javax.swing.JComponent;import javax.swing.JViewport;import javax.swing.Scrollable;import javax.swing.SwingConstants;import javax.swing.SwingUtilities;import javax.swing.tree.DefaultMutableTreeNode;import org.jgraph.event.GraphSelectionEvent;import org.jgraph.event.GraphSelectionListener;import org.jgraph.graph.BasicMarqueeHandler;import org.jgraph.graph.CellMapper;import org.jgraph.graph.CellView;import org.jgraph.graph.CellViewFactory;import org.jgraph.graph.DefaultGraphModel;import org.jgraph.graph.DefaultGraphSelectionModel;import org.jgraph.graph.Edge;import org.jgraph.graph.EdgeView;import org.jgraph.graph.GraphConstants;import org.jgraph.graph.GraphLayoutCache;import org.jgraph.graph.GraphModel;import org.jgraph.graph.GraphSelectionModel;import org.jgraph.graph.Port;import org.jgraph.graph.PortView;import org.jgraph.graph.VertexView;import org.jgraph.plaf.GraphUI;/** * A control that displays a network of related objects using the * well-known paradigm of a graph. * <p> * A JGraph object doesn't actually contain your data; it simply provides * a view of the data. Like any non-trivial Swing component, the graph gets * data by querying its data model. * <p> * JGraph displays its data by drawing individual elements. Each element * displayed by the graph contains exactly one item of data, which is * called a cell. A cell may either be a vertex or an edge. Vertices may * have neighbours or not, and edges may have source and target vertices * or not, depending on whether they are connected. * <p> * <strong>Creating a Graph</strong> * <p> * The following code creates a JGraph object:<p> * JGraph graph = new JGraph();<br> * ...<br> * JScrollPane graphLayoutCache = new JScrollPane(graph) * <p> * The code creates an instance of JGraph and puts it in a scroll pane. * JGraphs constructor is called with no arguments in this example, which * causes the constructor to create a sample model. * <p> * <strong>Editing</strong> * <p> * JGraph supports in-place editing of text and shapes. These features can * be disabled using the setEnabled() method, which blocks all features, or * individually, using the following methods: * <p> * setEditable() controls in-place editing of cells. Moving, cloning, sizing, * and bending, connection and disconnection of edges may also be disabled * using the respective methods, namemly setMoveable(), setCloneable(), * setSizeable(), setBendable(), setConnectable() and setDisconnectable(). * <p> * The model offers fainer control of connection establishment based * on the passed-in edge and port. The individual cells offer yet * another level of control in that they may allow/disallow being edited, * moved, cloned, resized, and shaped, or connected/disconnected to or * from other cells. * <p> * <strong>Keyboard Bindings</strong> * <p> * JGraph defines the following set of keyboard bindings: * <p><ul> * <li>Alt-Click forces marquee selection if over a cell. * <li>Shift- or Ctrl-Select extends or toggles the selection. * <li>Shift-Drag constrains the offset to one direction. * <li>Ctrl-Drag clones the selection. * <li>Doubleclick/F2 starts editing a cell. * </ul> * You can change the number of clicks that triggers editing using * setEditClickCount(). * <p> * <strong>Customization</strong> * <p> * There are a number of additional methods that customize JGraph. * For example, setMinimumMove() defines the minimum amount of * pixels before a move operation is initiated. setSnapSize() defines * the maximum distance for a cell to be selected. setFloatEnabled() * enables/disables port floating. * <p> * With setDisconnectOnMove() you can indicate if the selected subgraph * should be disconnected from the unselected rest when a move operation * is initiated. setDragEnabled() enables/disables the use of Drag And * Drop, and setDropEnabled() sets if the graph accepts Drops from * external sources. * <p> * <strong>Customizing a graphs display</strong> * <p> * JGraph performs some look-and-feel specific painting. You can * customize this painting in a limited way. For example, you can modify the * grid using setGridColor() and setGridSize(), and you can change the handle * colors using setHandleColor() and setLockedHandleColor(). * <p> * If you want finer control over the rendering, you can subclass one of the * default renderers, and extend its paint()-method. A renderer is a * Component-extension that paints a cell based on its attributes. Thus, * neither the JGraph nor its look-and-feel-specific implementation actually * contain the code that paints the cell. Instead, the graph uses the cell * renderers painting code. * <p> * <strong>Selection</strong> * <p> * Apart from the single-cell and marquee-selection, JGraphs selection * model also allows to "step-into" groups, and select children. This * feature can be disabled using the setAllowsChildSelection() method * of the selection model instance. * <p> * If you are interested in knowing when the selection changes implement * the <code>GraphSelectionListener</code> interface and add the instance * using the method <code>addGraphSelectionListener</code>. * <code>valueChanged</code> will be invoked when the * selection changes, that is if the user clicks twice on the same * vertex <code>valueChanged</code> will only be invoked once. * <p> * <strong>Change Notification</strong> * <p> * If you are interested in handling modifications, implement * the <code>GraphEventHandler</code> interface and add the instance * using the method <code>addGraphEventHandler</code>. * <p> * For detection of double-clicks or when a user clicks on a cell, * regardless of whether or not it was selected, I recommend you * implement a MouseListener and use <code>getFirstCellForLocation</code>. * <p> * <strong>Undo Support</strong> * <p> * To enable Undo-Support, a <code>GraphUndoManager</code> must be added * using <code>addGraphSelectionListener</code>. The GraphUndoManager * is an extension of Swing's <code>GraphUndoManager</code> that maintains * a command history in the context of multiple views. In this setup, a * cell may have a set of attributes in each view attached to the model. * <p> * For example, consider a position that is stored separately in each view. * If a node is inserted, the change will be visible in all attached views, * resulting in a new node that pops-up at the initial position. * If the node is subsequently moved, say, in view1, this does not constitute * a change in view2. If view2 does an "undo", the move <i>and</i> the * insertion must be undone, whereas an "undo" in view1 will only undo * the previous move operation. * <p> * Like all <code>JComponent</code> classes, you can use {@link javax.swing.InputMap} and * {@link javax.swing.ActionMap} to associate an {@link javax.swing.Action} object with a * {@link javax.swing.KeyStroke} and execute the action under specified conditions. * * @author Gaudenz Alder * @version 2.1 16/03/03 * */public class JGraph// DO NOT REMOVE OR MODIFY THIS LINE!extends JComponent // JAVA13: org.jgraph.plaf.basic.TransferHandler.JAdapterComponentimplements CellViewFactory, Scrollable, Accessible, Serializable {	public static final String VERSION = "JGraph (v3.1)";	public static final int DOT_GRID_MODE = 0;	public static final int CROSS_GRID_MODE = 1;	public static final int LINE_GRID_MODE = 2;	/**	 * @see #getUIClassID	 * @see #readObject	 */	private static final String uiClassID = "GraphUI";	/** Creates a new event and passes it off the <code>selectionListeners</code>. */	protected transient GraphSelectionRedirector selectionRedirector;	//	// Bound Properties	//	/** The model that defines the graph displayed by this object. Bound property. */	transient protected GraphModel graphModel;	/** The view that defines the display properties of the model. Bound property. */	transient protected GraphLayoutCache graphLayoutCache;	/** Handler for marquee selection. */	transient protected BasicMarqueeHandler marquee;	/** Models the set of selected objects in this graph. Bound property. */	transient protected GraphSelectionModel selectionModel;	/** Scale of the graph. Default is 1. Bound property. */	protected double scale = 1.0;	/** True if the graph is anti-aliased. Default is false. Bound property. */	protected boolean antiAliased = false;	/** True if the graph allows editing the value of a cell. Bound property. */	protected boolean editable = true;	/** True if the grid is visible. Bound property. */	protected boolean gridVisible = false;	/** The size of the grid in points.  Default is 10. Bound property.*/	protected int gridSize = 10;	/** The style of the grid. Use one of the _GRID_MODE constants. */	protected int gridMode = DOT_GRID_MODE;	/** True if the ports are visible. Bound property. */	protected boolean portsVisible = false;	//	// Look-And-Feel dependent	//	/** Highlight Color. Changes when the Look-and-Feel changes. */	protected Color highlightColor = Color.green;	/** Color of the handles and locked handles.  Changes when the Look-and-Feel changes. */	protected Color handleColor, lockedHandleColor;	/** Color of the marquee. Changes when the Look-and-Feel changes. */	protected Color marqueeColor;	/** The color of the grid. Changes when the Look-and-Feel changes. */	protected Color gridColor;	//	// Datatransfer	//	/**	 * True if Drag-and-Drop should be used for move operations. Default is	 * false due to a JDK bug.	 */	protected boolean dragEnabled = false;	/**	 * True if the graph accepts transfers from other components (graphs).	 * This also affects the clipboard. Default is true.	 */	protected boolean dropEnabled = true;	//	// Unbound Properties	//	/** Number of clicks for editing to start. Default is 2 clicks.*/	protected int editClickCount = 2;	/** True if the graph allows interactions. Default is true. */	protected boolean enabled = true;	/** True if the snap method should be active (snap to grid). */	protected boolean gridEnabled = false;	/** Size of a handle. Default is 3 pixels.*/	protected int handleSize = 3;	/** Maximum distance between a cell and the mousepointer. Default is 4.*/	protected int tolerance = 4;	/** Minimum amount of pixels to start a move transaction. Default is 5. */	protected int minimumMove = 5;	/** True if inserted cells should be selected. Default is false. */	protected boolean selectNewCells = false;	/**	 * True if selected edges are disconnected from unselected vertices on move.	 * Default is true.	 */	protected boolean disconnectOnMove = false;	/** True if the graph allows move operations. Default is true. */	protected boolean moveable = true;	/** True if the graph allows "ctrl-drag" operations. Default is true. */	protected boolean cloneable = true;	/** True if the graph allows cells to be resized. Default is true. */	protected boolean sizeable = true;	/** True if the graph allows points to be midified/added/removed. Default is true. */	protected boolean bendable = true;	/** True if the graph allows new connections to be established. Default is true. */	protected boolean connectable = true;	/** True if the graph allows existing connections to be removed. Default is true.  */	protected boolean disconnectable = true;	/**	 * If true, when editing is to be stopped by way of selection changing,	 * data in graph changing or other means <code>stopCellEditing</code>	 * is invoked, and changes are saved. If false,	 * <code>cancelCellEditing</code> is invoked, and changes	 * are discarded. Default is false.	 */	protected boolean invokesStopCellEditing;	/**	 * This is set to true for the life of the setUI call.	 */	private boolean settingUI;	//	// Bound propery names	//	/**	 * Bound property name for <code>graphModel</code>.	 */	public final static String GRAPH_MODEL_PROPERTY = "model";	/**	 * Bound property name for <code>graphModel</code>.	 */	public final static String GRAPH_LAYOUT_CACHE_PROPERTY = "view";	/**	 * Bound property name for <code>graphModel</code>.	 */	public final static String MARQUEE_HANDLER_PROPERTY = "marquee";	/**	 * Bound property name for <code>editable</code>.	 */	public final static String EDITABLE_PROPERTY = "editable";	/**	 * Bound property name for <code>scale</code>.	 */	public final static String SCALE_PROPERTY = "scale";	/**	 * Bound property name for <code>antiAliased</code>.	 */	public final static String ANTIALIASED_PROPERTY = "antiAliased";	/**	 * Bound property name for <code>gridSize</code>.	 */	public final static String GRID_SIZE_PROPERTY = "gridSize";	/**	 * Bound property name for <code>gridVisible</code>.	 */	public final static String GRID_VISIBLE_PROPERTY = "gridVisible";	/**	 * Bound property name for <code>gridVisible</code>.	 */	public final static String PORTS_VISIBLE_PROPERTY = "portsVisible";	/**	 * Bound property name for <code>selectionModel</code>.	 */	public final static String SELECTION_MODEL_PROPERTY = "selectionModel";	/**	 * Bound property name for <code>messagesStopCellEditing</code>.	 */	public final static String INVOKES_STOP_CELL_EDITING_PROPERTY =		"invokesStopCellEditing";	/**	 * Creates and returns a default <code>GraphLayoutCache</code>.	 *	 * @return the default <code>GraphLayoutCache</code>	 */	protected static GraphLayoutCache createDefaultGraphView(JGraph graph) {		return new GraphLayoutCache(graph.getModel(), graph);	}	/**	 * Returns an attributeMap for the specified position and color.	 */	public static Map createBounds(int x, int y, Color c) {		Map map = GraphConstants.createMap();		GraphConstants.setBounds(map, new Rectangle(x, y, 90, 30));		GraphConstants.setBorder(map, BorderFactory.createRaisedBevelBorder());		GraphConstants.setBackground(map, c.darker());		GraphConstants.setForeground(map, Color.white);		GraphConstants.setFont(map, GraphConstants.defaultFont.deriveFont(Font.BOLD, 12));		GraphConstants.setOpaque(map, true);		return map;	}	/**	 * Returns a <code>JGraph</code> with a sample model.	 */	public JGraph() {		this(null);	}	/**	  * Returns an instance of <code>JGraph</code> which displays the	  * the specified data model.	  *	  * @param model  the <code>GraphModel</code> to use as the data model	  */	public JGraph(GraphModel model) {		this(model, (GraphLayoutCache) null);	}	/**	  * Returns an instance of <code>JGraph</code> which displays	  * the specified data model using the specified view.	  *	  * @param model  the <code>GraphModel</code> to use as the data model	  * @param view  the <code>GraphLayoutCache</code> to use as the view	  */	public JGraph(GraphModel model, GraphLayoutCache view) {		this(model, view, new BasicMarqueeHandler());	}	/**	  * Returns an instance of <code>JGraph</code> which displays	  * the specified data model using the specified view.	  *	  * @param model  the <code>GraphModel</code> to use as the data model	  * @param view  the <code>GraphLayoutCache</code> to use as the view	  */	public JGraph(GraphModel model, BasicMarqueeHandler mh) {		this(model, null, mh);	}	/**	  * Returns an instance of <code>JGraph</code> which displays	  * the specified data model using the specified view.	  *	  * @param model  the <code>GraphModel</code> to use as the data model	  * @param view  the <code>GraphLayoutCache</code> to use as the view	  */	public JGraph(		GraphModel model,		GraphLayoutCache view,		BasicMarqueeHandler mh) {		selectionModel = new DefaultGraphSelectionModel(this);		setLayout(null);		marquee = mh;		if (view == null)			view = createDefaultGraphView(this);		setGraphLayoutCache(view);		updateUI();		if (model == null) {			model = new DefaultGraphModel();			setModel(model);		} else			setModel(model);		setDoubleBuffered(true);	}	//	// UI-delegate (GraphUI)	//	/**	 * Returns the L&F object that renders this component.	 * @return the GraphUI object that renders this component	 */	public GraphUI getUI() {		return (GraphUI) ui;	}	/**	 * Sets the L&F object that renders this component.	 * @param ui the GraphUI L&F object	 * @see javax.swing.UIDefaults#getUI(JComponent)	 *	 */	public void setUI(GraphUI ui) {		if ((GraphUI) this.ui != ui) {			settingUI = true;			try {				super.setUI(ui);			} finally {				settingUI = false;			}		}	}	/**	 * Notification from the <code>UIManager</code> that the L&F has changed.	 * Replaces the current UI object with the latest version from the	 * <code>UIManager</code>. Subclassers can override this to support	 * different GraphUIs.

⌨️ 快捷键说明

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