📄 mxgraph.java
字号:
/** * Sets the graph model that contains the data, and fires an * mxEvent.CHANGE followed by an mxEvent.REPAINT event. * * @param model Model that contains the graph data */ public void setModel(mxIGraphModel model) { if (this.model != null) { this.model.removeListener(graphModelChangeHandler); } Object oldModel = this.model; this.model = model; if (view != null) { view.revalidate(); } model.addListener(mxEvent.CHANGE, graphModelChangeHandler); changeSupport.firePropertyChange("model", oldModel, model); fireEvent(mxEvent.REPAINT); } /** * Returns the view that contains the cell states. * * @return Returns the view that contains the cell states */ public mxGraphView getView() { return view; } /** * Sets the view that contains the cell states. * * @param view View that contains the cell states */ public void setView(mxGraphView view) { if (this.view != null) { this.view.removeListener(fullRepaintHandler); } Object oldView = this.view; this.view = view; if (this.view != null) { this.view.revalidate(); } // Listens to changes in the view view.addListener(mxEvent.SCALE, fullRepaintHandler); view.addListener(mxEvent.TRANSLATE, fullRepaintHandler); view.addListener(mxEvent.SCALE_AND_TRANSLATE, fullRepaintHandler); view.addListener(mxEvent.UP, fullRepaintHandler); view.addListener(mxEvent.DOWN, fullRepaintHandler); changeSupport.firePropertyChange("view", oldView, view); } /** * Returns the stylesheet that provides the style. * * @return Returns the stylesheet that provides the style. */ public mxStylesheet getStylesheet() { return stylesheet; } /** * Sets the stylesheet that provides the style. * * @param stylesheet Stylesheet that provides the style. */ public void setStylesheet(mxStylesheet stylesheet) { mxStylesheet oldValue = this.stylesheet; this.stylesheet = stylesheet; changeSupport.firePropertyChange("stylesheet", oldValue, stylesheet); } /** * Returns the cells to be selected for the given list of changes. */ public Object[] getSelectionCellsForChanges(List changes) { List cells = new ArrayList(); Iterator it = changes.iterator(); while (it.hasNext()) { Object change = it.next(); if (change instanceof mxChildChange) { cells.add(((mxChildChange) change).getChild()); } else if (change instanceof mxTerminalChange) { cells.add(((mxTerminalChange) change).getCell()); } else if (change instanceof mxValueChange) { cells.add(((mxValueChange) change).getCell()); } else if (change instanceof mxStyleChange) { cells.add(((mxStyleChange) change).getCell()); } else if (change instanceof mxGeometryChange) { cells.add(((mxGeometryChange) change).getCell()); } else if (change instanceof mxCollapseChange) { cells.add(((mxCollapseChange) change).getCell()); } else if (change instanceof mxVisibleChange) { mxVisibleChange vc = (mxVisibleChange) change; if (vc.isVisible()) { cells.add(((mxVisibleChange) change).getCell()); } } } return mxGraphModel.getTopmostCells(model, cells.toArray()); } /** * Called when the graph model changes. Invokes processChange on each * item of the given array to update the view accordingly. */ public mxRectangle graphModelChanged(mxIGraphModel sender, List changes) { mxRectangle dirty = processChanges(changes, true); view.validate(); mxRectangle tmp = processChanges(changes, false); if (tmp != null) { if (dirty == null) { dirty = tmp; } else { dirty.add(tmp); } } removeSelectionCells(getRemovedCellsForChanges(changes)); return dirty; } /** * Returns the cells that have been removed from the model. */ public Object[] getRemovedCellsForChanges(List changes) { List result = new ArrayList(); Iterator it = changes.iterator(); while (it.hasNext()) { Object change = it.next(); if (change instanceof mxRootChange) { break; } else if (change instanceof mxChildChange) { mxChildChange cc = (mxChildChange) change; if (cc.getParent() == null) { result.addAll(mxGraphModel.getDescendants(model, cc .getChild())); } } else if (change instanceof mxVisibleChange) { Object cell = ((mxVisibleChange) change).getCell(); result.addAll(mxGraphModel.getDescendants(model, cell)); } } return result.toArray(); } /** * Processes the changes and returns the minimal rectangle to be * repainted in the buffer. A return value of null means no repaint * is required. */ public mxRectangle processChanges(List changes, boolean invalidate) { mxRectangle bounds = null; Iterator it = changes.iterator(); while (it.hasNext()) { mxRectangle rect = processChange(it.next(), invalidate); if (bounds == null) { bounds = rect; } else { bounds.add(rect); } } return bounds; } /** * Processes the given change and invalidates the respective cached data * in <view>. This fires a <root> event if the root has changed in the * model. */ public mxRectangle processChange(Object change, boolean invalidate) { mxRectangle result = null; if (change instanceof mxRootChange) { result = getGraphBounds(); if (invalidate) { clearSelection(); removeStateForCell(((mxRootChange) change).getPrevious()); } } else if (change instanceof mxChildChange) { mxChildChange cc = (mxChildChange) change; // Repaints the parent area if it is a rendered cell (vertex or // edge) otherwise only the child area is repainted, same holds // if the parent and previous are the same object, in which case // only the child area needs to be repainted (change of order) if (cc.getParent() != cc.getPrevious()) { if (model.isVertex(cc.getParent()) || model.isEdge(cc.getParent())) { result = getBoundingBox(cc.getParent(), true, true); } if (model.isVertex(cc.getPrevious()) || model.isEdge(cc.getPrevious())) { if (result != null) { result .add(getBoundingBox(cc.getPrevious(), true, true)); } else { result = getBoundingBox(cc.getPrevious(), true, true); } } } if (result == null) { result = getBoundingBox(cc.getChild(), true, true); } if (invalidate) { if (cc.getParent() != null) { view.clear(cc.getChild(), false, true); } else { removeStateForCell(cc.getChild()); } } } else if (change instanceof mxTerminalChange) { Object cell = ((mxTerminalChange) change).getCell(); result = getBoundingBox(cell, true); if (invalidate) { view.invalidate(cell); } } else if (change instanceof mxValueChange) { Object cell = ((mxValueChange) change).getCell(); result = getBoundingBox(cell); if (invalidate) { view.clear(cell, false, false); } } else if (change instanceof mxStyleChange) { Object cell = ((mxStyleChange) change).getCell(); result = getBoundingBox(cell, true); if (invalidate) { // TODO: Add includeEdges argument to clear method for // not having to call invalidate in this case (where it // is possible that the perimeter has changed, which // means the connected edges need to be invalidated) view.clear(cell, false, false); view.invalidate(cell); } } else if (change instanceof mxGeometryChange) { Object cell = ((mxGeometryChange) change).getCell(); result = getBoundingBox(cell, true, true); if (invalidate) { view.invalidate(cell); } } else if (change instanceof mxCollapseChange) { Object cell = ((mxCollapseChange) change).getCell(); result = getBoundingBox(((mxCollapseChange) change).getCell(), true, true); if (invalidate) { removeStateForCell(cell); } } else if (change instanceof mxVisibleChange) { Object cell = ((mxVisibleChange) change).getCell(); result = getBoundingBox(((mxVisibleChange) change).getCell(), true, true); if (invalidate) { removeStateForCell(cell); } } return result; } /** * Removes all cached information for the given cell and its descendants. * This is called when a cell was removed from the model. * * @param cell Cell that was removed from the model. */ protected void removeStateForCell(Object cell) { int childCount = model.getChildCount(cell); for (int i = 0; i < childCount; i++) { removeStateForCell(model.getChildAt(cell, i)); } view.removeState(cell); } // // Cell styles // /** * Returns an array of key, value pairs representing the cell style for the * given cell. If no string is defined in the model that specifies the * style, then the default style for the cell is returned or <EMPTY_ARRAY>, * if not style can be found. * * @param cell Cell whose style should be returned. * @return Returns the style of the cell. */ public Hashtable getCellStyle(Object cell) { Hashtable style = (model.isEdge(cell)) ? stylesheet .getDefaultEdgeStyle() : stylesheet.getDefaultVertexStyle(); String name = model.getStyle(cell); if (name != null) { style = stylesheet.getCellStyle(name, style); } if (style == null) { style = mxStylesheet.EMPTY_STYLE; } return style; } /** * Sets the style of the selection cells to the given value. * * @param style String representing the new style of the cells. */ public Object[] setCellStyle(String style) { return setCellStyle(style, null); } /** * Sets the style of the specified cells. If no cells are given, then the * selection cells are changed. * * @param style String representing the new style of the cells. * @param cells Optional array of <mxCells> to set the style for. Default is the * selection cells. */ public Object[] setCellStyle(String style, Object[] cells) { if (cells == null) { cells = getSelectionCells(); } if (cells != null) { model.beginUpdate(); try { for (int i = 0; i < cells.length; i++) { model.setStyle(cells[i], style); } } finally { model.endUpdate(); } } return cells; } /** * Toggles the boolean value for the given key in the style of the * given cell. If no cell is specified then the selection cell is * used. * * @param key Key for the boolean value to be toggled. * @param defaultValue Default boolean value if no value is defined. * @param cell Cell whose style should be modified. */ public Object toggleCellStyle(String key, boolean defaultValue, Object cell) { return toggleCellStyles(key, defaultValue, new Object[] { cell })[0]; } /** * Toggles the boolean value for the given key in the style of the * selection cells. * * @param key Key for the boolean value to be toggled. * @param defaultValue Default boolean value if no value is defined. */ public Object[] toggleCellStyles(String key, boolean defaultValue) { return toggleCellStyles(key, defaultValue, null); } /** * Toggles the boolean value for the given key in the style of the given * cells. If no cells are specified, then the selection cells are used. For * example, this can be used to toggle mxConstants.STYLE_ROUNDED or any * other style with a boolean value. * * @param key String representing the key of the boolean style to be toggled. * @param defaultValue Default boolean value if no value is defined. * @param cells Cells whose styles should be modified. */ public Object[] toggleCellStyles(String key, boolean defaultValue, Object[] cells) { if (cells == null) { cells = getSelectionCells(); } if (cells != null && cells.length > 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -