📄 basicgrapheditor.java
字号:
// Uncomment the following if mousewheel should zoom //graphComponent.getGraphControl().addMouseWheelListener(wheelTracker); // Installs the popup menu in the outline graphOutline.addMouseListener(new MouseAdapter() { /** * */ public void mousePressed(MouseEvent e) { // Handles context menu on the Mac where the trigger is on mousepressed mouseReleased(e); } /** * */ public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { showOutlinePopupMenu(e); } } }); // Installs the popup menu in the graph component graphComponent.getGraphControl().addMouseListener(new MouseAdapter() { /** * */ public void mousePressed(MouseEvent e) { // Handles context menu on the Mac where the trigger is on mousepressed mouseReleased(e); } /** * */ public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { showGraphPopupMenu(e); } } }); // Installs a mouse motion listener to display the mouse location graphComponent.getGraphControl().addMouseMotionListener( new MouseMotionListener() { /* * (non-Javadoc) * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent) */ public void mouseDragged(MouseEvent e) { mouseLocationChanged(e); } /* * (non-Javadoc) * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent) */ public void mouseMoved(MouseEvent e) { mouseDragged(e); } }); } /** * */ public void setCurrentFile(File file) { File oldValue = currentFile; currentFile = file; firePropertyChange("currentFile", oldValue, file); if (oldValue != file) { updateTitle(); } } /** * */ public File getCurrentFile() { return currentFile; } /** * * @param modified */ public void setModified(boolean modified) { boolean oldValue = this.modified; this.modified = modified; firePropertyChange("modified", oldValue, modified); if (oldValue != modified) { updateTitle(); } } /** * * @return */ public boolean isModified() { return modified; } /** * */ public mxGraphComponent getGraphComponent() { return graphComponent; } /** * */ public mxGraphOutline getGraphOutline() { return graphOutline; } /** * */ public mxUndoManager getUndoManager() { return undoManager; } /** * * @param name * @param action * @return */ public Action bind(String name, final Action action) { return bind(name, action, null); } /** * * @param name * @param action * @return */ public Action bind(String name, final Action action, String iconUrl) { return new AbstractAction(name, (iconUrl != null) ? new ImageIcon( BasicGraphEditor.class.getResource(iconUrl)) : null) { public void actionPerformed(ActionEvent e) { action.actionPerformed(new ActionEvent(getGraphComponent(), e .getID(), e.getActionCommand())); } }; } /** * * @param msg */ public void status(String msg) { statusBar.setText(msg); } /** * */ public void updateTitle() { JFrame frame = (JFrame) SwingUtilities.windowForComponent(this); if (frame != null) { String title = (currentFile != null) ? currentFile .getAbsolutePath() : mxResources.get("newDiagram"); if (modified) { title += "*"; } frame.setTitle(title + " - " + appTitle); } } /** * */ public void about() { JFrame frame = (JFrame) SwingUtilities.windowForComponent(this); if (frame != null) { EditorAboutFrame about = new EditorAboutFrame(frame); about.setModal(true); // Centers inside the application frame int x = frame.getX() + (frame.getWidth() - about.getWidth()) / 2; int y = frame.getY() + (frame.getHeight() - about.getHeight()) / 2; about.setLocation(x, y); // Shows the modal dialog and waits about.setVisible(true); } } /** * */ public void exit() { JFrame frame = (JFrame) SwingUtilities.windowForComponent(this); if (frame != null) { frame.dispose(); } } /** * */ public void setLookAndFeel(String clazz) { JFrame frame = (JFrame) SwingUtilities.windowForComponent(this); if (frame != null) { try { UIManager.setLookAndFeel(clazz); SwingUtilities.updateComponentTreeUI(frame); // Needs to assign the key bindings again keyboardHandler = new EditorKeyboardHandler(graphComponent); } catch (Exception e1) { e1.printStackTrace(); } } } /** * */ public JFrame createFrame(JMenuBar menuBar) { JFrame frame = new JFrame(); frame.getContentPane().add(this); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setJMenuBar(menuBar); frame.setSize(870, 640); // Updates the frame title updateTitle(); return frame; } /** * Placeholder for executing layouts in the commercial version. Layouts are * not shipped with the free version. This is overridden in the commercial * graph editor example. * * @param key Key to be used for getting the label from mxResources and also * to create the layout instance for the commercial graph editor example. * @return */ public Action graphLayout(final String key) { final mxIGraphLayout layout = createLayout(key); if (layout != null) { return new AbstractAction(mxResources.get(key)) { public void actionPerformed(ActionEvent e) { if (layout != null) { Object cell = graphComponent.getGraph() .getSelectionCell(); if (cell == null || graphComponent.getGraph().getModel() .getChildCount(cell) == 0) { cell = graphComponent.getGraph().getDefaultParent(); } long t0 = System.currentTimeMillis(); layout.execute(cell); status("Layout: " + (System.currentTimeMillis() - t0) + " ms"); } } }; } else { return new AbstractAction(mxResources.get(key)) { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(graphComponent, mxResources .get("noLayout")); } }; } } /** * Creates a layout instance for the given identifier. */ protected mxIGraphLayout createLayout(String ident) { if (ident != null) { mxGraph graph = graphComponent.getGraph(); if (ident.equals("verticalPartition")) { return new mxPartitionLayout(graph, false) { /** * Overrides the empty implementation to return the size of the * graph control. */ public mxRectangle getContainerSize() { return graphComponent.getLayoutAreaSize(); } }; } else if (ident.equals("horizontalPartition")) { return new mxPartitionLayout(graph, true) { /** * Overrides the empty implementation to return the size of the * graph control. */ public mxRectangle getContainerSize() { return graphComponent.getLayoutAreaSize(); } }; } else if (ident.equals("verticalStack")) { return new mxStackLayout(graph, false) { /** * Overrides the empty implementation to return the size of the * graph control. */ public mxRectangle getContainerSize() { return graphComponent.getLayoutAreaSize(); } }; } else if (ident.equals("horizontalStack")) { return new mxStackLayout(graph, true) { /** * Overrides the empty implementation to return the size of the * graph control. */ public mxRectangle getContainerSize() { return graphComponent.getLayoutAreaSize(); } }; } else if (ident.equals("circleLayout")) { return new mxCircleLayout(graph); } } return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -