📄 mxutils.java
字号:
Rectangle bg = new Rectangle(x, y, width, height); try { if (g.getClipBounds() != null) { bg = bg.intersection(g.getClipBounds()); } } catch (Exception e) { // FIXME: Getting clipbounds sometimes throws an NPE } g.fillRect(bg.x, bg.y, bg.width, bg.height); } /** * Creates a new list of new points obtained by translating the points in * the given list by the given vector. Elements that are not mxPoints are * added to the result as-is. */ public static List translatePoints(List pts, double dx, double dy) { List result = null; if (pts != null) { result = new ArrayList(pts.size()); Iterator it = pts.iterator(); while (it.hasNext()) { Object obj = it.next(); if (obj instanceof mxPoint) { mxPoint point = (mxPoint) ((mxPoint) obj).clone(); point.setX(point.getX() + dx); point.setY(point.getY() + dy); result.add(point); } else { result.add(obj); } } } return result; } /** * Returns the intersection of two lines as an mxPoint. * * @param x0 X-coordinate of the first line's startpoint. * @param y0 Y-coordinate of the first line's startpoint. * @param x1 X-coordinate of the first line's endpoint. * @param y1 Y-coordinate of the first line's endpoint. * @param x2 X-coordinate of the second line's startpoint. * @param y2 Y-coordinate of the second line's startpoint. * @param x3 X-coordinate of the second line's endpoint. * @param y3 Y-coordinate of the second line's endpoint. * @return Returns the intersection between the two lines. */ public static mxPoint intersection(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3) { double denom = ((y3 - y2) * (x1 - x0)) - ((x3 - x2) * (y1 - y0)); double nume_a = ((x3 - x2) * (y0 - y2)) - ((y3 - y2) * (x0 - x2)); double nume_b = ((x1 - x0) * (y0 - y2)) - ((y1 - y0) * (x0 - x2)); double ua = nume_a / denom; double ub = nume_b / denom; if (ua >= 0.0 && ua <= 1.0 && ub >= 0.0 && ub <= 1.0) { // Get the intersection point double intersectionX = x0 + ua * (x1 - x0); double intersectionY = y0 + ua * (y1 - y0); return new mxPoint(intersectionX, intersectionY); } // No intersection return null; } /** * Sorts the given cells according to the order in the cell hierarchy. */ public static Object[] sortCells(Object[] cells, final boolean ascending) { return sortCells(Arrays.asList(cells), ascending).toArray(); } /** * Sorts the given cells according to the order in the cell hierarchy. */ public static Collection sortCells(Collection cells, final boolean ascending) { SortedSet result = new TreeSet(new Comparator() { public int compare(Object o1, Object o2) { String acp = mxCellPath.create((mxICell) o1); String bcp = mxCellPath.create((mxICell) o2); int comp = acp.compareTo(bcp); return (comp == 0) ? comp : (((comp > 0) == ascending) ? 1 : -1); } }); result.addAll(cells); return result; } /** * Returns the stylename in a style of the form stylename[;key=value] or an * empty string if the given style does not contain a stylename. * * @param style String of the form stylename[;key=value]. * @return Returns the stylename from the given formatted string. */ public static String getStylename(String style) { if (style != null) { String[] pairs = style.split(";"); String stylename = pairs[0]; if (stylename.indexOf("=") < 0) { return stylename; } } return ""; } /** * Returns the stylenames in a style of the form stylename[;key=value] or an * empty array if the given style does not contain any stylenames. * * @param style String of the form stylename[;stylename][;key=value]. * @return Returns the stylename from the given formatted string. */ public static String[] getStylenames(String style) { List result = new ArrayList(); if (style != null) { String[] pairs = style.split(";"); for (int i = 0; i < pairs.length; i++) { if (pairs[i].indexOf("=") < 0) { result.add(pairs[i]); } } } return (String[]) result.toArray(); } /** * Returns the index of the given stylename in the given style. This * returns -1 if the given stylename does not occur (as a stylename) in the * given style, otherwise it returns the index of the first character. */ public static int indexOfStylename(String style, String stylename) { if (style != null && stylename != null) { String[] tokens = style.split(";"); int pos = 0; for (int i = 0; i < tokens.length; i++) { if (tokens[i].equals(stylename)) { return pos; } pos += tokens[i].length() + 1; } } return -1; } /** * Adds the specified stylename to the given style if it does not already * contain the stylename. */ public String addStylename(String style, String stylename) { if (indexOfStylename(style, stylename) < 0) { if (style == null) { style = ""; } else if (style.length() > 0 && style.charAt(style.length() - 1) != ';') { style += ';'; } style += stylename; } return style; } /** * Removes all occurrences of the specified stylename in the given style * and returns the updated style. Trailing semicolons are preserved. */ public String removeStylename(String style, String stylename) { StringBuffer buffer = new StringBuffer(); if (style != null) { String[] tokens = style.split(";"); for (int i = 0; i < tokens.length; i++) { if (!tokens[i].equals(stylename)) { buffer.append(tokens[i] + ";"); } } } return (buffer.length() > 1) ? buffer.substring(0, buffer.length() - 1) : buffer.toString(); } /** * Removes all stylenames from the given style and returns the updated * style. */ public static String removeAllStylenames(String style) { StringBuffer buffer = new StringBuffer(); if (style != null) { String[] tokens = style.split(";"); for (int i = 0; i < tokens.length; i++) { if (tokens[i].indexOf('=') >= 0) { buffer.append(tokens[i] + ";"); } } } return (buffer.length() > 1) ? buffer.substring(0, buffer.length() - 1) : buffer.toString(); } /** * Assigns the value for the given key in the styles of the given cells, or * removes the key from the styles if the value is null. * * @param model Model to execute the transaction in. * @param cells Array of cells to be updated. * @param key Key of the style to be changed. * @param value New value for the given key. */ public static void setCellStyles(mxIGraphModel model, Object[] cells, String key, String value) { if (cells != null && cells.length > 0) { model.beginUpdate(); try { for (int i = 0; i < cells.length; i++) { if (cells[i] != null) { String style = setStyle(model.getStyle(cells[i]), key, value); model.setStyle(cells[i], style); } } } finally { model.endUpdate(); } } } /** * Adds or removes the given key, value pair to the style and returns the * new style. If value is null or zero length then the key is removed from * the style. * * @param style String of the form <code>stylename[;key=value]</code>. * @param key Key of the style to be changed. * @param value New value for the given key. * @return Returns the new style. */ public static String setStyle(String style, String key, String value) { boolean isValue = value != null && value.length() > 0; if (style == null || style.length() == 0) { if (isValue) { style = key + "=" + value; } } else { int index = style.indexOf(key + "="); if (index < 0) { if (isValue) { String sep = (style.endsWith(";")) ? "" : ";"; style = style + sep + key + '=' + value; } } else { String tmp = (isValue) ? key + "=" + value : ""; int cont = style.indexOf(";", index); style = style.substring(0, index) + tmp + ((cont >= 0) ? style.substring(cont) : ""); } } return style; } /** * Sets or toggles the flag bit for the given key in the cell's styles. * If value is null then the flag is toggled. * * <code> * mxUtils.setCellStyleFlags(graph.getModel(), * cells, * mxConstants.STYLE_FONTSTYLE, * mxConstants.FONT_BOLD, null); * </code> * * Toggles the bold font style. * * @param model Model that contains the cells. * @param cells Array of cells to change the style for. * @param key Key of the style to be changed. * @param flag Integer for the bit to be changed. * @param value Optional boolean value for the flag. */ public static void setCellStyleFlags(mxIGraphModel model, Object[] cells, String key, int flag, Boolean value) { if (cells != null && cells.length > 0) { model.beginUpdate(); try { for (int i = 0; i < cells.length; i++) { if (cells[i] != null) { String style = setStyleFlag(model.getStyle(cells[i]), key, flag, value); model.setStyle(cells[i], style); } } } finally { model.endUpdate(); } } } /** * Sets or removes the given key from the specified style and returns the * new style. If value is null then the flag is toggled. * * @param style String of the form stylename[;key=value]. * @param key Key of the style to be changed. * @param flag Integer for the bit to be changed. * @param value Optional boolean value for the given flag. */ public static String setStyleFlag(String style, String key, int flag, Boolean value) { if (style == null || style.length() == 0) { if (value == null || value.booleanValue()) { style = key + "=" + flag; } else { style = key + "=0"; } } else { int index = style.indexOf(key + "="); if (index < 0) { String sep = (style.endsWith(";")) ? "" : ";"; if (value == null || value.booleanValue()) { style = style + sep + key + "=" + flag; } else { style = style + sep + key + "=0"; } } else { int cont = style.indexOf(";", index); String tmp = ""; int result = 0; if (cont < 0) { tmp = style.substring(index + key.length() + 1); } else { tmp = style.substring(index + key.length() + 1, cont); } if (value == null) { result = Integer.parseInt(tmp) ^ flag; } else if (value.booleanValue()) { result = Integer.parseInt(tmp) | flag; } else { result = Integer.parseInt(tmp) & ~flag; } style = style.substring(0, index) + key + "=" + result + ((cont >= 0) ? style.substring(cont) : ""); } } return style; } public static boolean intersectsHotspot(mxCellState state, int x, int y, double hotspot) { return intersectsHotspot(state, x, y, hotspot, 0, 0); } /** * Returns true if the dictionary contains true for the given key or * false if no value is defined for the key. * * @param dict Dictionary that contains the key, value pairs. * @param key Key whose value should be returned. * @return Returns the boolean value for key in dict. */ public static boolean intersectsHotspot(mxCellState state, int x, int y, double hotspot, int min, int max) { if (hotspot > 0) { int cx = (int) Math.round(state.getCenterX()); int cy = (int) Math.round(state.getCenterY()); int width = (int) Math.round(state.getWidth()); int height = (int) Math.round(state.getHeight()); int start = mxUtils.getInt(state.getStyle(), mxConstants.STYLE_STARTSIZE); if (start > 0) { if (mxUtils.isTrue(state.getStyle(), mxConstants.STYLE_HORIZONTAL, true)) { cy = (int) Math.round(state.getY() + start / 2); height = start; } else { cx = (int) Math.round(state.getX() + start / 2); width = start; } } int w = (int) Math.max(min, width * hotspot); int h = (int) Math.max(min, height * hotspot); if (max > 0) { w = Math.min(w, max); h = Math.min(h, max); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -