📄 mxgraphcomponent.java
字号:
/** * $Id: mxGraphComponent.java,v 1.65 2009/05/07 07:24:13 gaudenz Exp $ * Copyright (c) 2009, Gaudenz Alder */package com.mxgraph.swing;import java.awt.BasicStroke;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.Point;import java.awt.Rectangle;import java.awt.RenderingHints;import java.awt.Stroke;import java.awt.event.ComponentAdapter;import java.awt.event.ComponentEvent;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.image.BufferedImage;import java.awt.print.PageFormat;import java.awt.print.Printable;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.EventObject;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import java.util.Map;import javax.swing.BorderFactory;import javax.swing.BoundedRangeModel;import javax.swing.ImageIcon;import javax.swing.JComponent;import javax.swing.JPanel;import javax.swing.JScrollBar;import javax.swing.JScrollPane;import javax.swing.SwingUtilities;import javax.swing.ToolTipManager;import com.mxgraph.canvas.mxGraphics2DCanvas;import com.mxgraph.canvas.mxICanvas;import com.mxgraph.model.mxGraphModel;import com.mxgraph.model.mxIGraphModel;import com.mxgraph.model.mxGraphModel.Filter;import com.mxgraph.swing.handler.mxCellHandler;import com.mxgraph.swing.handler.mxConnectionHandler;import com.mxgraph.swing.handler.mxEdgeHandler;import com.mxgraph.swing.handler.mxElbowEdgeHandler;import com.mxgraph.swing.handler.mxGraphHandler;import com.mxgraph.swing.handler.mxGraphTransferHandler;import com.mxgraph.swing.handler.mxPanningHandler;import com.mxgraph.swing.handler.mxVertexHandler;import com.mxgraph.swing.util.mxCellOverlay;import com.mxgraph.swing.util.mxICellOverlay;import com.mxgraph.swing.view.mxCellEditor;import com.mxgraph.swing.view.mxICellEditor;import com.mxgraph.swing.view.mxInteractiveCanvas;import com.mxgraph.util.mxEvent;import com.mxgraph.util.mxEventObject;import com.mxgraph.util.mxEventSource;import com.mxgraph.util.mxPoint;import com.mxgraph.util.mxRectangle;import com.mxgraph.util.mxResources;import com.mxgraph.util.mxUtils;import com.mxgraph.util.mxEventSource.mxIEventListener;import com.mxgraph.view.mxCellState;import com.mxgraph.view.mxEdgeStyle;import com.mxgraph.view.mxGraph;import com.mxgraph.view.mxGraphView;import com.mxgraph.view.mxTemporaryCellStates;import com.mxgraph.view.mxEdgeStyle.mxEdgeStyleFunction;/** * For setting the preferred size of the viewport for scrolling, use * mxGraph.setMinimumGraphSize. */public class mxGraphComponent extends JScrollPane implements Printable{ /** * */ public static final int GRID_STYLE_DOT = 0; /** * */ public static final int GRID_STYLE_CROSS = 1; /** * */ public static final int GRID_STYLE_LINE = 2; /** * */ public static final int GRID_STYLE_DASHED = 3; /** * */ public static final int ZOOM_POLICY_NONE = 0; /** * */ public static final int ZOOM_POLICY_PAGE = 1; /** * */ public static final int ZOOM_POLICY_WIDTH = 2; /** * */ public static ImageIcon DEFAULT_EXPANDED_ICON = null; /** * */ public static ImageIcon DEFAULT_COLLAPSED_ICON = null; /** * */ public static ImageIcon DEFAULT_WARNING_ICON = null; /** * Specifies the default page scale. Default is 1.4 */ public static final double DEFAULT_PAGESCALE = 1.4; /** * Loads the collapse and expand icons. */ static { DEFAULT_EXPANDED_ICON = new ImageIcon(mxGraphComponent.class .getResource("/com/mxgraph/swing/images/expanded.gif")); DEFAULT_COLLAPSED_ICON = new ImageIcon(mxGraphComponent.class .getResource("/com/mxgraph/swing/images/collapsed.gif")); DEFAULT_WARNING_ICON = new ImageIcon(mxGraphComponent.class .getResource("/com/mxgraph/swing/images/warning.gif")); } /** * */ protected mxGraph graph; /** * */ protected mxGraphControl graphControl; /** * */ protected mxEventSource eventSource = new mxEventSource(this); /** * */ protected mxICellEditor cellEditor; /** * */ protected mxConnectionHandler connectionHandler; /** * */ protected mxPanningHandler panningHandler; /** * */ protected mxGraphHandler graphHandler; /** * Specifies the <mxImage> to be returned by <getBackgroundImage>. Default * is null. */ protected ImageIcon backgroundImage; /** * Background page format. */ protected PageFormat pageFormat = new PageFormat(); /** * */ protected mxInteractiveCanvas canvas; /** * */ protected BufferedImage tripleBuffer; /** * */ protected Graphics2D tripleBufferGraphics; /** * Defines the scaling for the background page metrics. Default is * {@link #DEFAULT_PAGESCALE}. */ protected double pageScale = DEFAULT_PAGESCALE; /** * Specifies if the background page should be visible. Default is false. */ protected boolean pageVisible = false; /** * If the pageFormat should be used to determine the minimal graph * bounds even if the page is not visible (see pageVisible). Default * is false. */ protected boolean preferPageSize = false; /** * Specifies if a dashed line should be drawn between multiple pages. */ protected boolean pageBreakVisible = true; /** * Specifies the number of pages in the horizontal direction. */ protected int horizontalPageCount = 1; /** * Specifies the number of pages in the vertical direction. */ protected int verticalPageCount = 1; /** * Specifies if the background page should be centered by automatically * setting the translate in the view. Default is true. This does only * apply if pageVisible is true. */ protected boolean centerPage = true; /** * Color of the background area if layout view. */ protected Color pageBackgroundColor = new Color(144, 153, 174); /** * */ protected Color pageShadowColor = new Color(110, 120, 140); /** * */ protected Color pageBorderColor = Color.black; /** * Specifies if the grid is visible. Default is false. */ protected boolean gridVisible = false; /** * */ protected Color gridColor = new Color(192, 192, 192); /** * */ protected boolean dragEnabled = true; /** * */ protected boolean importEnabled = true; /** * */ protected boolean exportEnabled = true; /** * Specifies if folding (collapse and expand via an image icon in the graph * should be enabled). Default is true. */ protected boolean foldingEnabled = true; /** * Specifies the tolerance for mouse clicks. Default is 4. */ protected int tolerance = 4; /** * Specifies if swimlanes are selected when the mouse is released over the * swimlanes content area. Default is true. */ protected boolean swimlaneSelectionEnabled = true; /** * Specifies if the content area should be transparent to events. Default is true. */ protected boolean transparentSwimlaneContent = true; /** * */ protected int gridStyle = GRID_STYLE_DOT; /** * */ protected ImageIcon expandedIcon = DEFAULT_EXPANDED_ICON; /** * */ protected ImageIcon collapsedIcon = DEFAULT_COLLAPSED_ICON; /** * */ protected ImageIcon warningIcon = DEFAULT_WARNING_ICON; /** * */ protected boolean antiAlias = true; /** * */ protected boolean textAntiAlias = true; /** * Specifies <escape> should be invoked when the escape key * is pressed. Default is true. */ protected boolean escapeEnabled = true; /** * If true, when editing is to be stopped by way of selection changing, * data in diagram changing or other means stopCellEditing is invoked, and * changes are saved. This is implemented in a mouse listener in this * class. Default is true. */ protected boolean invokesStopCellEditing = true; /** * If true, pressing the enter key without pressing control will stop * editing and accept the new value. This is used in <mxKeyHandler> to stop * cell editing. Default is false. */ protected boolean enterStopsCellEditing = false; /** * Specifies the zoom policy. Default is ZOOM_POLICY_PAGE. The zoom policy * does only apply if pageVisible is true. */ protected int zoomPolicy = ZOOM_POLICY_PAGE; /** * Internal flag to not reset zoomPolicy when zoom was set automatically. */ private transient boolean zooming = false; /** * Specifies the factor used for zoomIn and zoomOut. Default is 1.2 * (120%). */ protected double zoomFactor = 1.2; /** * Specifies if the viewport should automatically contain * the selection cells after a zoom operation. Default * is false. */ protected boolean keepSelectionVisibleOnZoom = false; /** * Specifies if the zoom operations should go into the center * of the actual diagram rather than going from top, left. * Default is true. */ protected boolean centerZoom = true; /** * Specifies if an image buffer should be used for painting the * component. Default is false. */ protected boolean tripleBuffered = false; /** * Used for debugging the dirty region. */ public boolean showDirtyRectangle = false; /** * Maps from cells to lists of heavyweights. */ protected Hashtable components = new Hashtable(); /** * Maps from cells to lists of overlays. */ protected Hashtable overlays = new Hashtable(); /** * Boolean flag to disable centering after the first time. */ private transient boolean centerOnResize = true; /** * Updates the heavyweight component structure after any changes. */ protected mxIEventListener updateHandler = new mxIEventListener() { public void invoke(Object sender, mxEventObject evt) { updateComponents(); graphControl.updatePreferredSize(); } }; /** * */ protected mxIEventListener repaintHandler = new mxIEventListener() { public void invoke(Object source, mxEventObject evt) { mxRectangle dirty = (evt.getArgCount() > 0) ? (mxRectangle) evt .getArgAt(0) : null; Rectangle rect = (dirty != null) ? dirty.getRectangle() : null; if (rect != null) { rect.grow(1, 1); } // Updates the triple buffer repaintTripleBuffer(rect); // Repaints the control using the optional triple buffer graphControl.repaint((rect != null) ? rect : getViewport() .getViewRect()); // ---------------------------------------------------------- // Shows the dirty region as a red rectangle (for debugging) JPanel panel = (JPanel) getClientProperty("dirty"); if (showDirtyRectangle) { if (panel == null) { panel = new JPanel(); panel.setOpaque(false); panel.setBorder(BorderFactory.createLineBorder(Color.RED)); putClientProperty("dirty", panel); graphControl.add(panel); } if (dirty != null) { panel.setBounds(dirty.getRectangle()); } panel.setVisible(dirty != null); } else if (panel != null && panel.getParent() != null) { panel.getParent().remove(panel); putClientProperty("dirty", null); repaint(); } // ---------------------------------------------------------- } }; /** * */ protected PropertyChangeListener viewChangeHandler = new PropertyChangeListener() { /** * */ public void propertyChange(PropertyChangeEvent evt) { if (evt.getPropertyName().equals("view")) { mxGraphView oldView = (mxGraphView) evt.getOldValue(); mxGraphView newView = (mxGraphView) evt.getNewValue(); if (oldView != null) { oldView.removeListener(updateHandler); } if (newView != null) { newView.addListener(mxEvent.SCALE, updateHandler); newView.addListener(mxEvent.TRANSLATE, updateHandler); newView.addListener(mxEvent.SCALE_AND_TRANSLATE, updateHandler); newView.addListener(mxEvent.UP, updateHandler); newView.addListener(mxEvent.DOWN, updateHandler); } } else if (evt.getPropertyName().equals("model")) { mxGraphModel oldModel = (mxGraphModel) evt.getOldValue(); mxGraphModel newModel = (mxGraphModel) evt.getNewValue(); if (oldModel != null) { oldModel.removeListener(updateHandler); } if (newModel != null) { newModel.addListener(mxEvent.CHANGE, updateHandler); } } } }; /** * Resets the zoom policy if the scale is changed manually. */ protected mxIEventListener scaleHandler = new mxIEventListener() { /** * */ public void invoke(Object sender, mxEventObject evt) { if (!zooming) { zoomPolicy = ZOOM_POLICY_NONE; } } }; /** * * @param graph */ public mxGraphComponent(mxGraph graph) { setCellEditor(createCellEditor()); this.canvas = createCanvas(); // Initializes the buffered view and installs a handler // to set the focus to the container graphControl = createGraphControl(); graphControl.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if (!hasFocus()) { requestFocus(); } } }); // Handles escape keystrokes addKeyListener(new KeyAdapter() { /** * * @param e * @return */ public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE && isEscapeEnabled()) { escape(e); } } }); // Applies the zoom policy if the size of the component changes addComponentListener(new ComponentAdapter()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -