📄 mxgraphcomponent.java
字号:
{ /* * (non-Javadoc) * @see java.awt.event.ComponentAdapter#componentResized(java.awt.event.ComponentEvent) */ public void componentResized(ComponentEvent e) { zoomAndCenter(); } }); setGraph(graph); // Adds the viewport view and initializes handlers setTransferHandler(new mxGraphTransferHandler()); setViewportView(graphControl); createHandlers(); // Adds handling of edit and stop-edit events after all // other handlers have been installed graphControl.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { if (!e.isConsumed() && isEditEvent(e)) { Object cell = getCellAt(e.getX(), e.getY(), false); if (cell != null && getGraph().isCellEditable(cell)) { startEditingAtCell(cell, e); } } else { // Other languages use focus traversal here, in Java // we explicitely stop editing after a click elsewhere stopEditing(!invokesStopCellEditing); } } }); } /** * */ protected mxCellEditor createCellEditor() { return new mxCellEditor(this); } /** * */ public void setGraph(mxGraph graph) { // Uninstalls listeners for existing graph if (this.graph != null) { this.graph.removeListener(repaintHandler); this.graph.getModel().removeListener(updateHandler); this.graph.getView().removeListener(updateHandler); this.graph.removePropertyChangeListener(viewChangeHandler); this.graph.getView().removeListener(scaleHandler); } this.graph = graph; // Updates the buffer if the model changes graph.addListener(mxEvent.REPAINT, repaintHandler); // Installs the update handler to sync the overlays and controls graph.getModel().addListener(mxEvent.CHANGE, updateHandler); // Repaint after the following events is handled via mxGraph.repaint-events // The respective handlers are installed in mxGraph.setView mxGraphView view = graph.getView(); view.addListener(mxEvent.SCALE, updateHandler); view.addListener(mxEvent.TRANSLATE, updateHandler); view.addListener(mxEvent.SCALE_AND_TRANSLATE, updateHandler); view.addListener(mxEvent.UP, updateHandler); view.addListener(mxEvent.DOWN, updateHandler); graph.addPropertyChangeListener(viewChangeHandler); // Resets the zoom policy if the scale changes graph.getView().addListener(mxEvent.SCALE, scaleHandler); graph.getView().addListener(mxEvent.SCALE_AND_TRANSLATE, scaleHandler); // Invoke the update handler once for initial state updateHandler.invoke(graph.getView(), null); } /** * * @return Returns the object that contains the graph. */ public mxGraph getGraph() { return graph; } /** * Creates the inner control that handles tooltips, preferred size and * can draw cells onto a canvas. */ protected mxGraphControl createGraphControl() { return new mxGraphControl(); } /** * * @return Returns the control that renders the graph. */ public mxGraphControl getGraphControl() { return graphControl; } /** * Create the connection-, panning and graphhandler (in this order). */ protected void createHandlers() { connectionHandler = createConnectionHandler(); panningHandler = createPanningHandler(); graphHandler = createGraphHandler(); } /** * */ protected mxGraphHandler createGraphHandler() { return new mxGraphHandler(this); } /** * */ public mxGraphHandler getGraphHandler() { return graphHandler; } /** * */ protected mxConnectionHandler createConnectionHandler() { return new mxConnectionHandler(this); } /** * */ public mxConnectionHandler getConnectionHandler() { return connectionHandler; } /** * */ protected mxPanningHandler createPanningHandler() { return new mxPanningHandler(this); } /** * */ public mxPanningHandler getPanningHandler() { return panningHandler; } /** * */ public mxICellEditor getCellEditor() { return cellEditor; } /** * */ public void setCellEditor(mxICellEditor editor) { mxICellEditor oldValue = this.cellEditor; this.cellEditor = editor; firePropertyChange("cellEditor", oldValue, editor); } /** * @return the tolerance */ public int getTolerance() { return tolerance; } /** * @param tolerance the tolerance to set */ public void setTolerance(int tolerance) { int oldValue = this.tolerance; this.tolerance = tolerance; firePropertyChange("tolerance", oldValue, tolerance); } /** * */ public PageFormat getPageFormat() { return pageFormat; } /** * */ public void setPageFormat(PageFormat pageFormat) { PageFormat oldValue = this.pageFormat; this.pageFormat = pageFormat; firePropertyChange("pageFormat", oldValue, pageFormat); } /** * */ public double getPageScale() { return pageScale; } /** * */ public void setPageScale(double pageScale) { double oldValue = this.pageScale; this.pageScale = pageScale; firePropertyChange("pageScale", oldValue, pageScale); } /** * Returns the size of the area that layouts can operate in. */ public mxRectangle getLayoutAreaSize() { if (pageVisible) { Dimension d = getPreferredSizeForPage(); return new mxRectangle(new Rectangle(d)); } else { return new mxRectangle(new Rectangle(graphControl.getSize())); } } /** * */ public ImageIcon getBackgroundImage() { return backgroundImage; } /** * */ public void setBackgroundImage(ImageIcon image) { ImageIcon oldValue = backgroundImage; backgroundImage = image; firePropertyChange("backgroundImage", oldValue, backgroundImage); } /** * @return the pageVisible */ public boolean isPageVisible() { return pageVisible; } /** * Fires a property change event for <code>pageVisible</code>. * zoomAndCenter should be called if this is set to true. * * @param pageVisible the pageVisible to set */ public void setPageVisible(boolean pageVisible) { boolean oldValue = this.pageVisible; this.pageVisible = pageVisible; firePropertyChange("pageVisible", oldValue, pageVisible); } /** * @return the preferPageSize */ public boolean isPreferPageSize() { return preferPageSize; } /** * Fires a property change event for <code>preferPageSize</code>. * * @param preferPageSize the preferPageSize to set */ public void setPreferPageSize(boolean preferPageSize) { boolean oldValue = this.preferPageSize; this.preferPageSize = preferPageSize; firePropertyChange("preferPageSize", oldValue, preferPageSize); } /** * @return the pageVisible */ public boolean isPageBreakVisible() { return pageBreakVisible; } /** * @param pageBreakVisible the pageBreakVisible to set */ public void setPageBreakVisible(boolean pageBreakVisible) { boolean oldValue = this.pageBreakVisible; this.pageBreakVisible = pageBreakVisible; firePropertyChange("pageBreakVisible", oldValue, pageVisible); } /** * @param horizontalPageCount the horizontalPageCount to set */ public void setHorizontalPageCount(int horizontalPageCount) { int oldValue = this.horizontalPageCount; this.horizontalPageCount = horizontalPageCount; firePropertyChange("horizontalPageCount", oldValue, horizontalPageCount); } /** * */ public int getHorizontalPageCount() { return horizontalPageCount; } /** * @param verticalPageCount the verticalPageCount to set */ public void setVerticalPageCount(int verticalPageCount) { int oldValue = this.verticalPageCount; this.verticalPageCount = verticalPageCount; firePropertyChange("verticalPageCount", oldValue, verticalPageCount); } /** * */ public int getVerticalPageCount() { return horizontalPageCount; } /** * @return the centerPage */ public boolean isCenterPage() { return centerPage; } /** * zoomAndCenter should be called if this is set to true. * * @param centerPage the centerPage to set */ public void setCenterPage(boolean centerPage) { boolean oldValue = this.centerPage; this.centerPage = centerPage; firePropertyChange("centerPage", oldValue, centerPage); } /** * @return the pageBackgroundColor */ public Color getPageBackgroundColor() { return pageBackgroundColor; } /** * Sets the color that appears behind the page. * * @param pageBackgroundColor the pageBackgroundColor to set */ public void setPageBackgroundColor(Color pageBackgroundColor) { Color oldValue = this.pageBackgroundColor; this.pageBackgroundColor = pageBackgroundColor; firePropertyChange("pageBackgroundColor", oldValue, pageBackgroundColor); } /** * @return the pageShadowColor */ public Color getPageShadowColor() { return pageShadowColor; } /** * @param pageShadowColor the pageShadowColor to set */ public void setPageShadowColor(Color pageShadowColor) { Color oldValue = this.pageShadowColor; this.pageShadowColor = pageShadowColor; firePropertyChange("pageShadowColor", oldValue, pageShadowColor); } /** * @return the pageShadowColor */ public Color getPageBorderColor() { return pageBorderColor; } /** * @param pageBorderColor the pageBorderColor to set */ public void setPageBorderColor(Color pageBorderColor) { Color oldValue = this.pageBorderColor; this.pageBorderColor = pageBorderColor; firePropertyChange("pageBorderColor", oldValue, pageBorderColor); } /** * @return the keepSelectionVisibleOnZoom */ public boolean isKeepSelectionVisibleOnZoom() { return keepSelectionVisibleOnZoom; } /** * @param keepSelectionVisibleOnZoom the keepSelectionVisibleOnZoom to set */ public void setKeepSelectionVisibleOnZoom(boolean keepSelectionVisibleOnZoom) { boolean oldValue = this.keepSelectionVisibleOnZoom; this.keepSelectionVisibleOnZoom = keepSelectionVisibleOnZoom; firePropertyChange("keepSelectionVisibleOnZoom", oldValue, keepSelectionVisibleOnZoom); } /** * @return the zoomFactor */ public double getZoomFactor() { return zoomFactor; } /** * @param zoomFactor the zoomFactor to set */ public void setZoomFactor(double zoomFactor) { double oldValue = this.zoomFactor; this.zoomFactor = zoomFactor; firePropertyChange("zoomFactor", oldValue, zoomFactor); } /** * @return the centerZoom */ public boolean isCenterZoom() { return centerZoom; } /** * @param centerZoom the centerZoom to set */ public void setCenterZoom(boolean centerZoom) { boolean oldValue = this.centerZoom; this.centerZoom = centerZoom; firePropertyChange("centerZoom", oldValue, centerZoom); } /** * */ public void setZoomPolicy(int zoomPolicy) { int oldValue = this.zoomPolicy; this.zoomPolicy = zoomPolicy; if (zoomPolicy != ZOOM_POLICY_NONE) { zoom(zoomPolicy == ZOOM_POLICY_PAGE, true); } firePropertyChange("zoomPolicy", oldValue, zoomPolicy); } /** * */ public int getZoomPolicy() { return zoomPolicy; } /** * Callback to process an escape keystroke. * * @param e */ public void escape(KeyEvent e) { cellEditor.stopEditing(true); graphHandler.reset(); connectionHandler.reset(); } /** * Clones and inserts the given cells into the graph using the move * method and returns the inserted cells. This shortcut is used if * cells are inserted via datatransfer. */ public Object[] importCells(Object[] cells, double dx, double dy, Object target, Point location) { return graph.moveCells(cells, dx, dy, true, target, location); } /** * Refreshes the display and handles. */ public void refresh() { graph.refresh(); graphHandler.refresh(); } /** * Returns an mxPoint representing the given event in the unscaled, * non-translated coordinate space and applies the grid. */ public mxPoint getPointForEvent(MouseEvent e) { double s = graph.getView().getScale(); mxPoint tr = graph.getView().getTranslate(); double x = graph.snap(e.getX() / s - tr.getX() - graph.getGridSize() / 2); double y = graph.snap(e.getY() / s - tr.getY() - graph.getGridSize() / 2); return new mxPoint(x, y);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -