📄 basicgraphui.java
字号:
/* * @(#)BasicGraphUI.java 1.0 29/2/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.plaf.basic;import java.awt.Color;import java.awt.Component;import java.awt.Cursor;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.Insets;import java.awt.Point;import java.awt.Rectangle;import java.awt.RenderingHints;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.Transferable;import java.awt.dnd.DropTarget;import java.awt.dnd.DropTargetDragEvent;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ComponentAdapter;import java.awt.event.ComponentEvent;import java.awt.event.ComponentListener;import java.awt.event.InputEvent;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.geom.AffineTransform;import java.awt.image.BufferedImage;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.io.Serializable;import java.util.ArrayList;import java.util.Map;import java.util.Observable;import java.util.Observer;import java.util.TooManyListenersException;import javax.swing.AbstractAction;import javax.swing.Action;import javax.swing.ActionMap;import javax.swing.CellRendererPane;import javax.swing.InputMap;import javax.swing.JComponent;import javax.swing.JScrollBar;import javax.swing.JScrollPane;import javax.swing.JViewport;import javax.swing.KeyStroke;import javax.swing.SwingUtilities;import javax.swing.Timer;import javax.swing.UIManager;import javax.swing.event.CellEditorListener;import javax.swing.event.ChangeEvent;import javax.swing.event.MouseInputListener;import javax.swing.plaf.ActionMapUIResource;import javax.swing.plaf.ComponentUI;import javax.swing.plaf.UIResource;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.AbstractCellView;import org.jgraph.graph.BasicMarqueeHandler;import org.jgraph.graph.CellHandle;import org.jgraph.graph.CellView;import org.jgraph.graph.ConnectionSet;import org.jgraph.graph.EdgeRenderer;import org.jgraph.graph.EdgeView;import org.jgraph.graph.GraphCell;import org.jgraph.graph.GraphCellEditor;import org.jgraph.graph.GraphConstants;import org.jgraph.graph.GraphContext;import org.jgraph.graph.GraphLayoutCache;import org.jgraph.graph.GraphModel;import org.jgraph.graph.GraphSelectionModel;import org.jgraph.graph.GraphTransferable;import org.jgraph.graph.ParentMap;import org.jgraph.plaf.GraphUI;// DO NOT REMOVE OR MODIFY THIS LINE!import javax.swing.TransferHandler; // JAVA13: org.jgraph.plaf.basic.TransferHandler;/** * The basic L&F for a graph data structure. * * @version 1.0 1/1/02 * @author Gaudenz Alder */public class BasicGraphUI extends GraphUI implements Serializable { /** Controls live-preview in dragEnabled mode. This is used to disable * live-preview in dragEnabled mode on Java 1.4.0 to workaround a bug that * cause the VM to hang during concurrent DnD and repaints. * Is this still required? */ public static final boolean DNDPREVIEW = System.getProperty("java.version").compareTo("1.4.0") < 0 || System.getProperty("java.version").compareTo("1.4.0") > 0; /** Border in pixels to scroll if marquee or dragging are active. */ public static int SCROLLBORDER = 18; /** Multiplicator for width and height when autoscrolling (=stepsize). */ public static float SCROLLSTEP = 0.05f; /** The maximum number of cells to paint when dragging. */ public static int MAXCELLS = 20; /** The maximum number of handles to paint individually. */ public static int MAXHANDLES = 20; /** Maximum number of cells to compute clipping bounds for. */ public static int MAXCLIPCELLS = 20; /** Minimum preferred size. */ protected Dimension preferredMinSize; /** Component that we're going to be drawing into. */ protected JGraph graph; /** Reference to the graph's view (geometric pattern). */ protected GraphLayoutCache graphLayoutCache; /** Current editor for the graph. */ protected GraphCellEditor cellEditor; /** Set to false when editing and shouldSelectCell() returns true meaning * the node should be selected before editing, used in completeEditing. */ protected boolean stopEditingInCompleteEditing; /** Used to paint the CellRenderer. */ protected CellRendererPane rendererPane; /** Size needed to completely display all the cells. */ protected Dimension preferredSize; /** Is the preferredSize valid? */ protected boolean validCachedPreferredSize; /** Used to determine what to display. */ protected GraphModel graphModel; /** Model maintaining the selection. */ protected GraphSelectionModel graphSelectionModel; /** Handle that we are going to use. */ protected CellHandle handle; /** Marquee that we are going to use. */ protected BasicMarqueeHandler marquee; // Following 4 ivars are only valid when editing. /** When editing, this will be the Component that is doing the actual * editing. */ protected Component editingComponent; /** The focused cell under the mousepointer. */ protected CellView focus; /** Path that is being edited. */ protected Object editingCell; /** Set to true if the editor has a different size than the renderer. */ protected boolean editorHasDifferentSize; /** Needed to exchange information between Transfer- and MouseListener. */ protected Point insertionLocation; /** Needed to exchange information between DropTargetHandler and TransferHandler. */ protected int dropAction = TransferHandler.NONE; /** * If ture, a the view under mousepointer will be snapped to the grid lines during * a drag operation. If snap-to-grid mode is disabled, views are moved by a snap * increment. */ protected boolean snapSelectedView = false; // Cached listeners /** Listens for JGraph property changes and updates display. */ protected PropertyChangeListener propertyChangeListener; /** Listens for Mouse events. */ protected MouseListener mouseListener; /** Listens for KeyListener events. */ protected KeyListener keyListener; /** Listens for Component events. */ protected ComponentListener componentListener; /** Listens for CellEditor events. */ protected CellEditorListener cellEditorListener = createCellEditorListener(); /** Updates the display when the selection changes. */ protected GraphSelectionListener graphSelectionListener; /** Is responsible for updating the view based on model events. */ protected GraphModelListener graphModelListener; /** Updates the display when the view has changed. */ protected Observer graphViewObserver; /** The default TransferHandler. */ protected TransferHandler defaultTransferHandler; /** The default DropTargetListener. */ protected GraphDropTargetListener defaultDropTargetListener; public static ComponentUI createUI(JComponent x) { return new BasicGraphUI(); } public BasicGraphUI() { super(); } // // Methods for configuring the behavior of the graph. None of them // push the value to the JGraph instance. You should really only // call these methods on the JGraph instance. // /** * Sets the GraphModel. This invokes <code>updateSize</code>. */ protected void setModel(GraphModel model) { cancelEditing(graph); if (graphModel != null && graphModelListener != null) graphModel.removeGraphModelListener(graphModelListener); graphModel = model; if (graphModel != null && graphModelListener != null) graphModel.addGraphModelListener(graphModelListener); if (graphModel != null) // jmv : to avoid NullPointerException updateSize(); } /** * Sets the GraphLayoutCache (geometric pattern). * This invokes <code>updateSize</code>. */ protected void setGraphLayoutCache(GraphLayoutCache view) { cancelEditing(graph); if (graphLayoutCache != null && graphViewObserver != null) graphLayoutCache.deleteObserver(graphViewObserver); graphLayoutCache = view; if (graphLayoutCache != null && graphViewObserver != null) graphLayoutCache.addObserver(graphViewObserver); updateSize(); } /** * Sets the marquee handler. */ protected void setMarquee(BasicMarqueeHandler marqueeHandler) { marquee = marqueeHandler; } /** * Resets the selection model. The appropriate listeners are installed * on the model. */ protected void setSelectionModel(GraphSelectionModel newLSM) { cancelEditing(graph); if (graphSelectionListener != null && graphSelectionModel != null) graphSelectionModel.removeGraphSelectionListener( graphSelectionListener); graphSelectionModel = newLSM; if (graphSelectionModel != null && graphSelectionListener != null) graphSelectionModel.addGraphSelectionListener( graphSelectionListener); if (graph != null) graph.repaint(); } // // GraphUI methods // /** * Returns the handle that is currently active, or null, if no * handle is currently active. Typically, the returned objects * are instances of the RootHandle inner class. */ public CellHandle getHandle(JGraph graph) { return handle; } /** * Returns the cell that has the focus. */ protected Object getFocusedCell() { if (focus != null) return focus.getCell(); return null; } /** Get the preferred Size for a cell view.*/ public Dimension getPreferredSize(JGraph graph, CellView view) { // Either label or icon if (view != null) { Object cell = view.getCell(); String valueStr = graph.convertValueToString(cell); boolean label = ((valueStr != null) && (valueStr.length() > 0)); boolean icon = GraphConstants.getIcon(view.getAllAttributes()) != null; if (label || icon) { boolean focus = (getFocusedCell() == cell) && graph.hasFocus(); // Only ever removed when UI changes, this is OK! Component component = view.getRendererComponent(graph, focus, false, false); if (component != null) { graph.add(component); component.validate(); return component.getPreferredSize(); } } return view.getBounds().getSize(); } return null; } // // Insertion Location // // Used to track the location of the mousepointer during Drag-and-Drop. // /** * Returns the current location of the Drag-and-Drop activity. */ public Point getInsertionLocation() { return insertionLocation; } /** * Sets the current location for Drag-and-Drop activity. Should be * set to null after a drop. Used from within DropTargetListener. */ public void setInsertionLocation(Point p) { insertionLocation = p; } // // Selection // /** * From GraphUI interface. */ public void selectCellsForEvent( JGraph graph, Object[] cells, MouseEvent event) { selectCellsForEvent(cells, event); } /** * Messaged to update the selection based on a MouseEvent for a group of * cells. If the event is a toggle selection event, the cells are either * selected, or deselected. Otherwise the cells are selected. */ public void selectCellsForEvent(Object[] cells, MouseEvent event) { if (cells == null) return; // Toggle selection if (isToggleSelectionEvent(event)) { for (int i = 0; i < cells.length; i++) toggleSelectionCellForEvent(cells[i], event); // Select cells } else if (isAddToSelectionEvent(event)) graph.addSelectionCells(cells); else graph.setSelectionCells(cells); } /** * Messaged to update the selection based on a MouseEvent over a * particular cell. If the event is a toggle selection event, the * cell is either selected, or deselected. Otherwise the cell is * selected. */ public void selectCellForEvent(Object cell, MouseEvent event) { // Toggle selection if (isToggleSelectionEvent(event)) toggleSelectionCellForEvent(cell, event); // Select cell else if (isAddToSelectionEvent(event)) graph.addSelectionCell(cell); else graph.setSelectionCell(cell); } /** * Messaged to update the selection based on a toggle selection * event, which means the cell's selection state is inverted. */ protected void toggleSelectionCellForEvent(Object cell, MouseEvent event) { if (graph.isCellSelected(cell)) graph.removeSelectionCell(cell); else graph.addSelectionCell(cell); } /** * Returning true signifies that cells are added to the selection. */ public boolean isAddToSelectionEvent(MouseEvent e) { return e.isShiftDown(); } /** * Returning true signifies a mouse event on the cell should toggle * the selection of only the cell under mouse. */ public boolean isToggleSelectionEvent(MouseEvent event) { return (event.isControlDown()); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -