📄 dragdrop.new
字号:
/* ---------------------------------------------------------------------- sf Control Panel - A management software for sf Firewall Copyright (C) 1997 Roland E. Schmid <schmid@acm.org> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Please address all correspondence concerning the software to firewall-bugs@switch.ch. ---------------------------------------------------------------------- */package sfclasses;import java.awt.*;import java.awt.event.*;import java.util.*;/** * This class is used for graphically displaying and drag-and-drop editing * of graphs. Vertex objects must implement DragDropObj. <br> * The drag and drop panel consists of a canvas to display and edit the * topology, two scrollbars and a status line. Object dependent methods * are defined in DragDropObj. <br> * This is a GUI class. * @see DragDropObj * @see Graph * @version 1.0 29 Nov 1996 * @author Roland E. Schmid */public class DragDrop extends Panel implements AdjustmentListener { /** * Create a canvas and two scrollbars and lay them out in the panel * @param g graph to store objects and edges * @param newObjectClass class for object creation * (must be public and implement DragDropObj) */ public DragDrop(Graph g, Class newObjectClass) { // implicit super() call here creates the panel createClass = newObjectClass; graph = g; canvas = new DragDropCanvas(); //hbar = new Scrollbar(Scrollbar.HORIZONTAL); //vbar = new Scrollbar(Scrollbar.VERTICAL); GridBagLayout gbl = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.BOTH; setLayout(gbl); sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS); hbar = sp.getHAdjustable(); hbar.addAdjustmentListener(this); vbar = sp.getVAdjustable(); vbar.addAdjustmentListener(this); //sp.setLayout(new BorderLayout(0,0)); sp.add(canvas); //sp.add("South", hbar); //sp.add("East", vbar); Utils.add_component(this, sp, gbl, gbc, 0, 0, 1, 1, 100, 100); status = new Label(""); Utils.add_component(this, status, gbl, gbc, 0, 1, 1, 1, 100, 0); mt = new MediaTracker(canvas); } /** * Create a canvas and two scrollbars and lay them out in the panel * @param g graph to store objects and edges * @param newObjectClass class for object creation * (must be public and implement DragDropObj) * @param edit enable / disable editing functions on startup */ public DragDrop(Graph g, Class newObjectClass, boolean edit) { this(g, newObjectClass); editEnabled = edit; } public void adjustmentValueChanged(AdjustmentEvent ae) { Object source = ae.getAdjustable(); int whatkind = ae.getAdjustmentType(); Graphics canvas_g = canvas.getGraphics(); if (canvas_g == null) return; canvas_g.setXORMode(canvas.getBackground()); if (source == hbar) { // horizontal scroll bar switch(whatkind) { case AdjustmentEvent.UNIT_INCREMENT: case AdjustmentEvent.UNIT_DECREMENT: case AdjustmentEvent.BLOCK_INCREMENT: case AdjustmentEvent.BLOCK_DECREMENT: case AdjustmentEvent.TRACK: offset_x = ae.getValue(); break; } canvas.repaint(); canvas_g.dispose(); return; } else if (source == vbar) { // vertical scroll bar switch(whatkind) { case AdjustmentEvent.UNIT_INCREMENT: case AdjustmentEvent.UNIT_DECREMENT: case AdjustmentEvent.BLOCK_INCREMENT: case AdjustmentEvent.BLOCK_DECREMENT: case AdjustmentEvent.TRACK: offset_y = ae.getValue(); break; } canvas.repaint(); canvas_g.dispose(); return; } } // adjustmentValueChanged /** * Event handler <br> * mouse click on background creates new object<br> * left mouse button to draw lines<br> * right mouse button (or button + META) to drag objects<br> * double click on object to call object's execute() method */ public boolean handleEvent(Event e) { Graphics canvas_g = canvas.getGraphics(); if (canvas_g == null) return super.handleEvent(e); canvas_g.setXORMode(canvas.getBackground()); if (e.target == canvas) { int x = e.x + offset_x; int y = e.y + offset_y; DragDropObj v = find(x, y, orig_v); switch(e.id) { case Event.MOUSE_DOWN: if ((selected) && (v != last_v)) { // deselect last_v last_v.highlight(canvas_g, offset_x, offset_y, false); selected = false; status.setText(""); } if (v != null) { // user clicked on object if (!selected) { v.highlight(canvas_g, offset_x, offset_y, true); selected = true; status.setText(v.getObjectID()); } if (!editEnabled) { if (e.clickCount > 1) // double click v.userAction(Utils.getParentFrame(this), canvas_g, offset_x, offset_y); last_v = v; canvas_g.dispose(); return true; } // assert(editEnabled) if (e.clickCount > 1) // double click v.execute(Utils.getParentFrame(this)); else { // single click if ((e.modifiers & Event.META_MASK) != 0) { // right button state = STATE_DRAG; orig_v = v; } else { state = STATE_DRAW; orig_v = v; orig_x = v.getX(); orig_y = v.getY(); } } last_x = v.getX(); last_y = v.getY(); } else { // v == null if (!editEnabled) { canvas_g.dispose(); return true; } if (e.modifiers == 0) { // left button // create new vertex try { v = (DragDropObj)createClass.newInstance(); v.initImage(canvas, mt); boolean interrupted = true; while (interrupted) { try { mt.waitForAll(); interrupted = false; } catch (Exception ex) { System.out.println("DragDrop.handleEvent: MediaTracker.waitForAll Exception "+ex); } } v.setCoordinates(x, y); v.draw(canvas_g, offset_x, offset_y); graph.insertVertex(v); } catch (IllegalAccessException e1) { System.out.println("Cannot create object, class not accessible: " +createClass); } catch (InstantiationException e2) { System.out.println("Cannot create object, class is interface or abstract: "+createClass); } catch (ClassCastException e3) { System.out.println("Cannot create object, class does not implement DragDropObj: "+createClass); } } last_x = x; last_y = y; } last_v = v; canvas_g.dispose(); return true; case Event.MOUSE_UP: if (!dragged) { state = STATE_NONE; orig_v = null; canvas_g.dispose(); return true; } dragged = false; if (state == STATE_DRAG) { // user is dragging object if (v == null) { moveto(canvas_g, last_v, x, y); } else { // don't place object on top of other object moveto(canvas_g, last_v, last_x, last_y); } state = STATE_NONE; } if (state == STATE_DRAW) { // user is drawing line if ((last_x != orig_x) || (last_y != orig_y)) { // erase line canvas_g.drawLine(orig_x - offset_x, orig_y - offset_y, last_x - offset_x, last_y - offset_y); } // deselect last_v if ((last_v != orig_v) && (last_v != null)) last_v.highlight(canvas_g, offset_x, offset_y, false); if (v != null) { // insert or delete edge (toggle) // draw line (XOR-Mode -> erase or delete) drawEdge(canvas_g,orig_v, v); if (graph.searchEdge(orig_v, v)) graph.deleteEdge(orig_v, v); else graph.insertEdge(orig_v, v); } state = STATE_NONE; last_v = orig_v; last_x = orig_x; last_y = orig_y; } orig_v = null; canvas_g.dispose(); return true; case Event.MOUSE_DRAG: if (state == STATE_DRAG) { // user is dragging object if (e.x < 0 || e.x >= canvas_width || e.y < 0 || e.y >= canvas_height) updateVirtual(e.x, e.y, true, false); moveto(canvas_g, last_v, x, y); dragged = true; } if (state == STATE_DRAW) { // user is drawing line dragged = true; if ((last_x != orig_x) || (last_y != orig_y)) { // erase line canvas_g.drawLine(orig_x - offset_x, orig_y - offset_y, last_x - offset_x, last_y - offset_y); } if (e.x < 0 || e.x >= canvas_width || e.y < 0 || e.y >= canvas_height) updateVirtual(e.x, e.y, false, true); if (last_v != v) { // deselect last_v if ((last_v != orig_v) && (last_v != null)) last_v.highlight(canvas_g, offset_x, offset_y, false); // select v if (v != null) v.highlight(canvas_g, offset_x, offset_y, true); } // draw new line canvas_g.drawLine(orig_x - offset_x, orig_y - offset_y, x - offset_x, y - offset_y); last_v = v; last_x = x; last_y = y; } canvas_g.dispose(); return true; case Event.KEY_PRESS: if (e.key == 27) { // Esc pressed if (state == STATE_DRAG) { // user is dragging object moveto(canvas_g, last_v, last_x, last_y); orig_v = null; dragged = false; state = STATE_NONE; } if (state == STATE_DRAW) { // user is drawing line if ((last_x != orig_x) || (last_y != orig_y)) { // erase line canvas_g.drawLine(orig_x - offset_x, orig_y - offset_y, last_x - offset_x, last_y - offset_y); } // deselect last_v if ((last_v != orig_v) && (last_v != null)) last_v.highlight(canvas_g, offset_x, offset_y, false); state = STATE_NONE; last_v = orig_v; last_x = orig_x; last_y = orig_y; orig_v = null; dragged = false; } } else if (e.key == 127) { // delete pressed -> delete highlighted vertex if (!editEnabled) { canvas_g.dispose(); return true; } if ((state == STATE_NONE) && (last_v != null) && selected) { // let's first check if the user has changed his mind if (!last_v.confirmDelete()) { canvas_g.dispose(); return true; } // deselect vertex last_v.highlight(canvas_g, offset_x, offset_y, false); selected = false; status.setText(""); // erase edges Enumeration neighbors = graph.findNeighbors(last_v); DragDropObj n; while (neighbors.hasMoreElements()) { n = (DragDropObj)neighbors.nextElement();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -