📄 basicgrapheditor.java
字号:
package com.mxgraph.swing.examples.editor;import java.awt.BorderLayout;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ComponentAdapter;import java.awt.event.ComponentEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionListener;import java.awt.event.MouseWheelEvent;import java.awt.event.MouseWheelListener;import java.io.File;import java.util.List;import javax.swing.AbstractAction;import javax.swing.Action;import javax.swing.BorderFactory;import javax.swing.ImageIcon;import javax.swing.JCheckBoxMenuItem;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenuBar;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JScrollPane;import javax.swing.JSplitPane;import javax.swing.JTabbedPane;import javax.swing.JToolBar;import javax.swing.SwingUtilities;import javax.swing.UIManager;import com.mxgraph.layout.mxCircleLayout;import com.mxgraph.layout.mxIGraphLayout;import com.mxgraph.layout.mxPartitionLayout;import com.mxgraph.layout.mxStackLayout;import com.mxgraph.swing.mxGraphComponent;import com.mxgraph.swing.mxGraphOutline;import com.mxgraph.swing.handler.mxKeyboardHandler;import com.mxgraph.swing.handler.mxRubberband;import com.mxgraph.util.mxEvent;import com.mxgraph.util.mxEventObject;import com.mxgraph.util.mxRectangle;import com.mxgraph.util.mxResources;import com.mxgraph.util.mxUndoManager;import com.mxgraph.util.mxUndoableEdit;import com.mxgraph.util.mxEventSource.mxIEventListener;import com.mxgraph.view.mxGraph;public class BasicGraphEditor extends JPanel{ /** * Adds required resources for i18n */ static { mxResources.add("com/mxgraph/swing/examples/resources/editor"); } /** * */ protected mxGraphComponent graphComponent; /** * */ protected mxGraphOutline graphOutline; /** * */ protected JTabbedPane libraryPane; /** * */ protected mxUndoManager undoManager; /** * */ protected String appTitle; /** * */ protected JLabel statusBar; /** * */ protected File currentFile; /** * */ protected boolean modified = false; /** * */ protected mxRubberband rubberband; /** * */ protected mxKeyboardHandler keyboardHandler; /** * */ protected mxIEventListener undoHandler = new mxIEventListener() { public void invoke(Object source, mxEventObject evt) { undoManager.undoableEditHappened((mxUndoableEdit) evt.getArgAt(0)); } }; /** * */ protected mxIEventListener changeTracker = new mxIEventListener() { public void invoke(Object source, mxEventObject evt) { setModified(true); } }; /** * */ public BasicGraphEditor(String appTitle, mxGraphComponent component) { // Stores and updates the frame title this.appTitle = appTitle; // Stores a reference to the graph and creates the command history graphComponent = component; final mxGraph graph = graphComponent.getGraph(); undoManager = new mxUndoManager(); // Updates the modified flag if the graph model changes graph.getModel().addListener(mxEvent.CHANGE, changeTracker); // Adds the command history to the model and view graph.getModel().addListener(mxEvent.UNDO, undoHandler); graph.getView().addListener(mxEvent.UNDO, undoHandler); // Keeps the selection in sync with the command history mxIEventListener selectionHandler = new mxIEventListener() { public void invoke(Object source, mxEventObject evt) { List changes = ((mxUndoableEdit) evt.getArgAt(0)).getChanges(); graph.setSelectionCells(graph .getSelectionCellsForChanges(changes)); } }; undoManager.addListener(mxEvent.UNDO, selectionHandler); undoManager.addListener(mxEvent.REDO, selectionHandler); // Creates the graph outline component graphOutline = new mxGraphOutline(graphComponent); // Creates the library pane that contains the tabs with the palettes libraryPane = new JTabbedPane(); // Creates the inner split pane that contains the library with the // palettes and the graph outline on the left side of the window JSplitPane inner = new JSplitPane(JSplitPane.VERTICAL_SPLIT, libraryPane, graphOutline); inner.setDividerLocation(320); inner.setResizeWeight(1); inner.setDividerSize(6); inner.setBorder(null); // Creates the outer split pane that contains the inner split pane and // the graph component on the right side of the window JSplitPane outer = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, inner, graphComponent); outer.setOneTouchExpandable(true); outer.setDividerLocation(200); outer.setDividerSize(6); outer.setBorder(null); // Creates the status bar statusBar = createStatusBar(); // Display some useful information about repaint events installRepaintListener(); // Puts everything together setLayout(new BorderLayout()); add(outer, BorderLayout.CENTER); add(statusBar, BorderLayout.SOUTH); installToolBar(); // Installs rubberband selection and handling for some special // keystrokes such as F2, Control-C, -V, X, A etc. installHandlers(); installListeners(); updateTitle(); } /** * */ protected void installHandlers() { rubberband = new mxRubberband(graphComponent); keyboardHandler = new EditorKeyboardHandler(graphComponent); } /** * */ protected void installToolBar() { add(new EditorToolBar(this, JToolBar.HORIZONTAL), BorderLayout.NORTH); } /** * */ protected JLabel createStatusBar() { JLabel statusBar = new JLabel(mxResources.get("ready")); statusBar.setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4)); return statusBar; } /** * */ protected void installRepaintListener() { graphComponent.getGraph().addListener(mxEvent.REPAINT, new mxIEventListener() { public void invoke(Object source, mxEventObject evt) { String buffer = (graphComponent.getTripleBuffer() != null) ? "" : " (unbuffered)"; if (evt.getArgCount() == 0 || evt.getArgAt(0) == null) { status("Repaint all" + buffer); } else { mxRectangle rect = (mxRectangle) evt.getArgAt(0); status("Repaint: x=" + (int) (rect.getX()) + " y=" + (int) (rect.getY()) + " w=" + (int) (rect.getWidth()) + " h=" + (int) (rect.getHeight()) + buffer); } } }); } /** * */ public EditorPalette insertPalette(String title) { final EditorPalette palette = new EditorPalette(); final JScrollPane scrollPane = new JScrollPane(palette); scrollPane .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); libraryPane.add(title, scrollPane); // Updates the widths of the palettes if the container size changes libraryPane.addComponentListener(new ComponentAdapter() { /** * */ public void componentResized(ComponentEvent e) { int w = scrollPane.getWidth() - scrollPane.getVerticalScrollBar().getWidth(); palette.setPreferredWidth(w); } }); return palette; } /** * */ protected void mouseWheelMoved(MouseWheelEvent e) { if (e.getWheelRotation() < 0) { graphComponent.zoomIn(); } else { graphComponent.zoomOut(); } status(mxResources.get("scale") + ": " + (int) (100 * graphComponent.getGraph().getView().getScale()) + "%"); } /** * */ protected void showOutlinePopupMenu(MouseEvent e) { Point pt = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), graphComponent); JCheckBoxMenuItem item = new JCheckBoxMenuItem(mxResources .get("magnifyPage")); item.setSelected(graphOutline.isFitPage()); item.addActionListener(new ActionListener() { /** * */ public void actionPerformed(ActionEvent e) { graphOutline.setFitPage(!graphOutline.isFitPage()); graphOutline.repaint(); } }); JCheckBoxMenuItem item2 = new JCheckBoxMenuItem(mxResources .get("showLabels")); item2.setSelected(graphOutline.isDrawLabels()); item2.addActionListener(new ActionListener() { /** * */ public void actionPerformed(ActionEvent e) { graphOutline.setDrawLabels(!graphOutline.isDrawLabels()); graphOutline.repaint(); } }); JCheckBoxMenuItem item3 = new JCheckBoxMenuItem(mxResources .get("buffering")); item3.setSelected(graphOutline.isTripleBuffered()); item3.addActionListener(new ActionListener() { /** * */ public void actionPerformed(ActionEvent e) { graphOutline .setTripleBuffered(!graphOutline.isTripleBuffered()); graphOutline.repaint(); } }); JPopupMenu menu = new JPopupMenu(); menu.add(item); menu.add(item2); menu.add(item3); menu.show(graphComponent, pt.x, pt.y); e.consume(); } /** * */ protected void showGraphPopupMenu(MouseEvent e) { Point pt = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), graphComponent); EditorPopupMenu menu = new EditorPopupMenu(BasicGraphEditor.this); menu.show(graphComponent, pt.x, pt.y); e.consume(); } /** * */ protected void mouseLocationChanged(MouseEvent e) { status(e.getX() + ", " + e.getY()); } /** * */ protected void installListeners() { // Installs mouse wheel listener for zooming MouseWheelListener wheelTracker = new MouseWheelListener() { /** * */ public void mouseWheelMoved(MouseWheelEvent e) { BasicGraphEditor.this.mouseWheelMoved(e); } }; graphOutline.addMouseWheelListener(wheelTracker);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -