📄 dragdrop.java
字号:
max_y += offset * (canvas_height / 2); } else max_y = vy; } } boolean interrupted = true; while (interrupted) { try { mt.waitForAll(); interrupted = false; } catch (Exception ex) { System.out.println("DragDrop.reshape: MediaTracker.waitForAll Exception "+ex); } } // initialize scrollbars updateScrollbars(width, height); } /** * Enable editing functions */ public void enableEdit() { if ((last_v != null) && selected) { Graphics canvas_g = canvas.getGraphics(); canvas_g.setXORMode(canvas.getBackground()); last_v.highlight(canvas_g, offset_x, offset_y, false); canvas_g.dispose(); selected = false; status.setText(""); } checkConsistency(); canvas.repaint(); editEnabled = true; } /** * Diable editing functions * @return true if successful, false if state != STATE_NONE */ public boolean disableEdit() { if (state != STATE_NONE) return false; checkConsistency(); if ((last_v != null) && selected) { Graphics canvas_g = canvas.getGraphics(); canvas_g.setXORMode(canvas.getBackground()); last_v.highlight(canvas_g, offset_x, offset_y, false); canvas_g.dispose(); selected = false; status.setText(""); } editEnabled = false; orig_v = null; return true; } /** * Check consistency of topology. * To be called when topology editor is left or a new * topology has been loaded. * @see checkConfig * @see checkConnections * @see checkForDuplicates */ public void checkConsistency() { graph.isConsistent = true; checkConfig(); checkForDuplicates(); } /** * Get highlighted object * @return selected object or null if no object has been selected */ public DragDropObj getSelected() { if ((state == STATE_NONE) && (last_v != null) && selected) return last_v; else return null; } /** * Set the class for creation of new objects * @param newObjectClass class for object creation */ public void setClass(Class newObjectClass) { createClass = newObjectClass; } /** * Get graph object; note that you have to call repaint() * after making changes in the graph topology manually * @return graph object * @see Graph */ public Graph getGraph() { return graph; } public Graphics getCanvasGraphics() { return canvas.getGraphics(); } // private methods /** * Checks the configuration of all vertices by calling * the checkConfig() methode. * Icons are updated accordingly to indicate wether the * specific configuration for an object represented by that icon * is valid or not. * @see DragDropObj */ private void checkConfig() { Enumeration en = graph.getAllVertices(); DragDropObj ddo = null; boolean interrupted = true; while (interrupted) { try { mt.waitForAll(); interrupted = false; } catch (Exception ex) { System.out.println("DragDrop.handleEvent: MediaTracker.waitForAll Exception "+ex); } } while (en.hasMoreElements()) { ddo = (DragDropObj)en.nextElement(); if (ddo instanceof Host) { if (((Host)ddo).checkConfig()) ((Host)ddo).setOKIcon(this); else { ((Host)ddo).setNOKIcon(this); graph.isConsistent = false; } } if (ddo instanceof Net) { if (((Net)ddo).checkConfig()) ((Net)ddo).setOKIcon(this); else { ((Net)ddo).setNOKIcon(this); graph.isConsistent = false; } } checkConnection(ddo); } } // checkConfig /** * Check if connections from Object ddo to all of its neighbours * are valid or not. (i.e. sub-net addresses are valid) * If a connection is not valid, the appropriate edge will be * marked as 'not valid' (i.e. red color edge). * @param DragDropObj Object to check. */ private void checkConnection(DragDropObj ddo) { Graphics canvas_g = canvas.getGraphics(); Enumeration neighbors = graph.findNeighbors(ddo); DragDropObj n; while (neighbors.hasMoreElements()) { n = (DragDropObj)neighbors.nextElement(); drawEdge(canvas_g, ddo, n); if (!checkNetAddresses(ddo, n)) { graph.setEdgeColor(ddo, n, Color.red); graph.isConsistent = false; } else graph.setEdgeColor(ddo, n, Color.black); drawEdge(canvas_g, ddo, n); } return; } // checkConnection /** * Checks wether the address of object V1 and V1 are in the same * sub-net or not. * @param DragDropObj V1 * @param DragDropObj V2 * @return true if objects are in the same sub-net, false otherwise */ private boolean checkNetAddresses(DragDropObj V1, DragDropObj V2) { if ((V1 instanceof Internet) || (V2 instanceof Internet)) return true; AddrList V1AddrList = null; AddrList V2AddrList = null; AddrObj ao1, ao2; Enumeration en1, en2; byte net1[] = new byte[4]; byte net2[] = new byte[4]; if (V1 instanceof Host) V1AddrList = ((Host)V1).HostAddresses; if (V1 instanceof Net) V1AddrList = ((Net)V1).NetAddresses; if (V2 instanceof Host) V2AddrList = ((Host)V2).HostAddresses; if (V2 instanceof Net) V2AddrList = ((Net)V2).NetAddresses; if ((V1AddrList == null) || (V2AddrList == null)) return true; en1 = V1AddrList.getAddresses().elements(); // outer loop while (en1.hasMoreElements()) { ao1 = (AddrObj)en1.nextElement(); en2 = V2AddrList.getAddresses().elements(); // inner loop while (en2.hasMoreElements()) { ao2 = (AddrObj)en2.nextElement(); net1 = AddrObj.And(ao1.getAddr(), ao2.getMask()); net2 = AddrObj.And(ao2.getAddr(), ao1.getMask()); if (AddrObj.equalIP(net1, net2)) return true; } //inner loop } // outer loop return false; } // checkNetAddresses /** * Check if all objects have unique addresses (i.e. no address * has been assigned twice). * If a duplicate has been found, the involved icons are set * accordingly to indicate, that the configuration of those * icons is not valid. */ private void checkForDuplicates() { Enumeration graphEnum; Enumeration en; Vector addrCollection = new Vector(3,3); Vector objCollection = new Vector(3,3); Vector ddoAddresses; AddrObj ao1, ao2; DragDropObj ddo; IconObject io; graphEnum = graph.getAllVertices(); while (graphEnum.hasMoreElements()) { ddo = (DragDropObj)graphEnum.nextElement(); if (ddo instanceof Host) ddoAddresses = ((Host)ddo).HostAddresses.getAddresses(); else if (ddo instanceof Net) ddoAddresses = ((Net)ddo).NetAddresses.getAddresses(); else ddoAddresses = null; if (ddoAddresses != null) { en = ddoAddresses.elements(); // for all addresses of object while (en.hasMoreElements()) { ao1 = (AddrObj)en.nextElement(); // for all addresses collected so far for (int i = 0; i < addrCollection.size(); i++) { ao2 = (AddrObj)addrCollection.elementAt(i); if (ao1.equals(ao2)) { ((IconObject)ddo).setNOKIcon(this); io = (IconObject)objCollection.elementAt(i); io.setNOKIcon(this); graph.isConsistent = false; } } addrCollection.addElement(ao1); objCollection.addElement(ddo); } } } } // checkForDuplicates private void updateScrollbars(int width, int height) { if (canvas_width >= 1 && canvas_height >= 1) { hbar.setValues(offset_x, canvas_width, 0, max_x - canvas_width); vbar.setValues(offset_y, canvas_height, 0, max_y - canvas_height); hbar.setBlockIncrement(canvas_width / 2); vbar.setBlockIncrement(canvas_height / 2); hbar.setUnitIncrement(canvas_width / 10); vbar.setUnitIncrement(canvas_height / 10); } } /** * Called to update the window when dragging objects or drawing lines * @param x current horizontal position * @param y current vertical position * @extend if true, the virtual size will be extended when moving out * @scroll if true, the panel scrolls so that the current position is visible */ private void updateVirtual(int x, int y, boolean extend, boolean scroll) { int offset; // extend virtual size if necessary if (extend && ((x + offset_x) > max_x || (y + offset_y) > max_y)) { if ((x+offset_x) > max_x) { offset = x + offset_x - max_x; if (canvas_width >= 2) { offset = (int)Math.ceil((double)offset / (double)(canvas_width / 2)); max_x += offset * (canvas_width / 2); } else max_x = x+offset_x+10; } if ((y+offset_y) > max_y) { offset = y + offset_y - max_y; if (canvas_height >= 2) { offset = (int)Math.ceil((double)offset / (double)(canvas_height / 2)); max_y += offset * (canvas_height / 2); } else max_y = y+offset_y+10; } updateScrollbars(canvas_width, canvas_height); } // scroll to current position if necessary if (scroll) { int oldx = offset_x; int oldy = offset_y; if (x < 0) { offset = ((-x) / (canvas_width / 10)) + 1; offset_x -= offset * (canvas_width / 10); if (offset_x < 0) offset_x = 0; } if (y < 0) { offset = ((-y) / (canvas_height / 10)) + 1; offset_y -= offset * (canvas_height / 10); if (offset_y < 0) offset_y = 0; } if (x >= canvas_width) { offset = ((x - canvas_width) / (canvas_width / 10)) + 1; offset_x += offset * (canvas_width / 10); if (offset_x > max_x - canvas_width) offset_x = max_x - canvas_width; } if (y >= canvas_height) { offset = ((y - canvas_height) / (canvas_height / 10)) + 1; offset_y += offset * (canvas_height / 10); if (offset_y > max_y - canvas_height) offset_y = max_y - canvas_height; } if (oldx != offset_x || oldy != offset_y) { hbar.setValue(offset_x); vbar.setValue(offset_y); Graphics canvas_g = canvas.getGraphics(); canvas.update(canvas_g); canvas_g.dispose(); } } } /** * Get object at the specified position. * @param x Current horizontal position * @param y Current vertical position * @param except Object to ignore */ private DragDropObj find(int x, int y, DragDropObj except) { Enumeration vertices = graph.getAllVertices(); DragDropObj v; while (vertices.hasMoreElements()) { v = (DragDropObj)vertices.nextElement(); if ((v != except) && (v.hasFocus(x,y))) return v; } return null; } /** * Move an object to a different position. * @param canvas_g Graphics context of canvas * @param obj Object to move * @param x New horizontal position * @param y New vertical position */ private void moveto(Graphics canvas_g, DragDropObj obj, int x, int y) { Enumeration neighbors = graph.findNeighbors(obj); DragDropObj n; while (neighbors.hasMoreElements()) { n = (DragDropObj)neighbors.nextElement(); drawEdge(canvas_g, obj, n); } obj.erase(canvas_g, offset_x, offset_y); obj.setCoordinates(x, y); obj.draw(canvas_g, offset_x, offset_y); neighbors = graph.findNeighbors(obj); while (neighbors.hasMoreElements()) { n = (DragDropObj)neighbors.nextElement(); drawEdge(canvas_g, obj, n); } } /** * Draw edge between two objects. * @param g Graphics context * @param V1 First object * @param V1 Second object */ protected void drawEdge(Graphics g, DragDropObj V1, DragDropObj V2) { Point v1_center = new Point(V1.getX(), V1.getY()); Point v2_center = new Point(V2.getX(), V2.getY()); Point v1_conn = V1.connectionCoord(v2_center); Point v2_conn = V2.connectionCoord(v1_center); Color ctxtcol = g.getColor(); Color edgecol = graph.getEdgeColor(V1, V2); if (edgecol != null) g.setColor(edgecol); g.drawLine(v1_conn.x-offset_x, v1_conn.y-offset_y, v2_conn.x-offset_x, v2_conn.y-offset_y); g.setColor(ctxtcol); } // constants private static final int STATE_NONE = 0; private static final int STATE_DRAW = 1; private static final int STATE_DRAG = 2; // data protected Graph graph; // graph to store vertices and edges protected int offset_x = 0; // current scroll position (x) protected int offset_y = 0; // current scroll position (y) private MediaTracker mt; // MediaTracker private Class createClass; // used to create new objects private DragDropCanvas canvas; // drag and drop area private Scrollbar hbar, vbar; // scrollbars private Panel sp; // South Panel (Scrollbar + Status) private Label status; // Status line private int last_x, last_y; // last mouse pointer position private DragDropObj last_v; // last selected object private int orig_x, orig_y; // origin position for line drawing private DragDropObj orig_v; // origin object for drawing and dragging private int max_x; // x size of drag and drop area private int max_y; // y size of drag and drop area private int canvas_width; // width of currently visible area private int canvas_height; // height of currently visible area private int state = STATE_NONE; // state (see constants above) private boolean selected = false; // is there any selected object? private boolean dragged = false; // did user move the mouse since mouse-down? private boolean editEnabled = false; // enable edit functions} // DragDrop// The class DragDropCanvas implements the drawing area of the panel.class DragDropCanvas extends Canvas { public synchronized void paint(Graphics g) { if (g==null) return; DragDrop dd = (DragDrop)getParent().getParent(); // erase screen Rectangle r = getBounds(); g.clearRect(r.x, r.y, r.width, r.height); g.setXORMode(getBackground()); // draw vertices Enumeration vertices = dd.graph.getAllVertices(); DragDropObj v; while (vertices.hasMoreElements()) { v = (DragDropObj)vertices.nextElement(); v.draw(g, dd.offset_x, dd.offset_y); } // draw edges Enumeration edges = dd.graph.getAllEdges(); Edge e; while (edges.hasMoreElements()) { e = (Edge)edges.nextElement(); dd.drawEdge(g, (DragDropObj)e.vertex1, (DragDropObj)e.vertex2); } // request input focus requestFocus(); } public void update(Graphics g) { paint(g); }/* public boolean gotFocus(Event evt, Object what) { return true; }*/} // DragDropCanvas
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -