1 package prefuse.controls; 2 3 import java.awt.Cursor ; 4 import java.awt.event.MouseEvent ; 5 import java.awt.geom.Point2D ; 6 7 import javax.swing.SwingUtilities ; 8 9 import prefuse.Display; 10 import prefuse.data.Table; 11 import prefuse.data.event.EventConstants; 12 import prefuse.data.event.TableListener; 13 import prefuse.visual.VisualItem; 14 15 16 24 public class DragControl extends ControlAdapter implements TableListener { 25 26 private VisualItem activeItem; 27 protected String action; 28 protected Point2D down = new Point2D.Double (); 29 protected Point2D temp = new Point2D.Double (); 30 protected boolean dragged, wasFixed, resetItem; 31 private boolean fixOnMouseOver = true; 32 protected boolean repaint = true; 33 34 38 public DragControl() { 39 } 40 41 49 public DragControl(boolean repaint) { 50 this.repaint = repaint; 51 } 52 53 63 public DragControl(boolean repaint, boolean fixOnMouseOver) { 64 this.repaint = repaint; 65 this.fixOnMouseOver = fixOnMouseOver; 66 } 67 68 72 public DragControl(String action) { 73 this.repaint = false; 74 this.action = action; 75 } 76 77 83 public DragControl(String action, boolean fixOnMouseOver) { 84 this.repaint = false; 85 this.fixOnMouseOver = fixOnMouseOver; 86 this.action = action; 87 } 88 89 95 public void setFixPositionOnMouseOver(boolean s) { 96 fixOnMouseOver = s; 97 } 98 99 102 public void itemEntered(VisualItem item, MouseEvent e) { 103 Display d = (Display)e.getSource(); 104 d.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 105 activeItem = item; 106 if ( fixOnMouseOver ) { 107 wasFixed = item.isFixed(); 108 resetItem = true; 109 item.setFixed(true); 110 item.getTable().addTableListener(this); 111 } 112 } 113 114 117 public void itemExited(VisualItem item, MouseEvent e) { 118 if ( activeItem == item ) { 119 activeItem = null; 120 item.getTable().removeTableListener(this); 121 if ( resetItem ) item.setFixed(wasFixed); 122 } 123 Display d = (Display)e.getSource(); 124 d.setCursor(Cursor.getDefaultCursor()); 125 } 127 130 public void itemPressed(VisualItem item, MouseEvent e) { 131 if (!SwingUtilities.isLeftMouseButton(e)) return; 132 if ( !fixOnMouseOver ) { 133 wasFixed = item.isFixed(); 134 resetItem = true; 135 item.setFixed(true); 136 item.getTable().addTableListener(this); 137 } 138 dragged = false; 139 Display d = (Display)e.getComponent(); 140 d.getAbsoluteCoordinate(e.getPoint(), down); 141 } 142 143 146 public void itemReleased(VisualItem item, MouseEvent e) { 147 if (!SwingUtilities.isLeftMouseButton(e)) return; 148 if ( dragged ) { 149 activeItem = null; 150 item.getTable().removeTableListener(this); 151 if ( resetItem ) item.setFixed(wasFixed); 152 dragged = false; 153 } 154 } 155 156 159 public void itemDragged(VisualItem item, MouseEvent e) { 160 if (!SwingUtilities.isLeftMouseButton(e)) return; 161 dragged = true; 162 Display d = (Display)e.getComponent(); 163 d.getAbsoluteCoordinate(e.getPoint(), temp); 164 double dx = temp.getX()-down.getX(); 165 double dy = temp.getY()-down.getY(); 166 double x = item.getX(); 167 double y = item.getY(); 168 169 item.setStartX(x); item.setStartY(y); 170 item.setX(x+dx); item.setY(y+dy); 171 item.setEndX(x+dx); item.setEndY(y+dy); 172 173 if ( repaint ) 174 item.getVisualization().repaint(); 175 176 down.setLocation(temp); 177 if ( action != null ) 178 d.getVisualization().run(action); 179 } 180 181 184 public void tableChanged(Table t, int start, int end, int col, int type) { 185 if ( activeItem == null || type != EventConstants.UPDATE 186 || col != t.getColumnNumber(VisualItem.FIXED) ) 187 return; 188 int row = activeItem.getRow(); 189 if ( row >= start && row <= end ) 190 resetItem = false; 191 } 192 193 } | Popular Tags |