Changeset 7 in josm for src/org/openstreetmap/josm/gui/SelectionManager.java
- Timestamp:
- 2005-10-02T20:32:00+02:00 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/gui/SelectionManager.java
r4 r7 10 10 import java.awt.event.MouseListener; 11 11 import java.awt.event.MouseMotionListener; 12 import java.beans.PropertyChangeEvent; 13 import java.beans.PropertyChangeListener; 14 import java.util.Collection; 15 import java.util.LinkedList; 16 17 import org.openstreetmap.josm.data.osm.LineSegment; 18 import org.openstreetmap.josm.data.osm.Node; 19 import org.openstreetmap.josm.data.osm.OsmPrimitive; 20 import org.openstreetmap.josm.data.osm.Track; 12 21 13 22 /** … … 36 45 * @author imi 37 46 */ 38 public class SelectionManager implements MouseListener, MouseMotionListener {47 public class SelectionManager implements MouseListener, MouseMotionListener, PropertyChangeListener { 39 48 40 49 /** … … 47 56 * Called, when the left mouse button was released. 48 57 * @param r The rectangle, that is currently the selection. 49 * @param modifiers The modifiers returned from the MouseEvent when 50 * the left mouse button was released. 58 * @param alt Whether the alt key was pressed 59 * @param shift Whether the shift key was pressed 60 * @param ctrl Whether the ctrl key was pressed 51 61 * @see InputEvent#getModifiersEx() 52 62 */ 53 public void selectionEnded(Rectangle r, int modifiers); 63 public void selectionEnded(Rectangle r, boolean alt, boolean shift, boolean ctrl); 64 /** 65 * Called to register the selection manager for "active" property. 66 * @param listener The listener to register 67 */ 68 public void addPropertyChangeListener(PropertyChangeListener listener); 69 /** 70 * Called to remove the selection manager from the listener list 71 * for "active" property. 72 * @param listener The listener to register 73 */ 74 public void removePropertyChangeListener(PropertyChangeListener listener); 54 75 } 55 76 /** … … 67 88 private Point mousePos; 68 89 /** 69 * The component, the selection rectangle is drawn onto.70 */ 71 private final Component drawComponent;90 * The MapView, the selection rectangle is drawn onto. 91 */ 92 private final MapView mv; 72 93 /** 73 94 * Whether the selection rectangle must obtain the aspect ratio of the … … 83 104 * @param aspectRatio If true, the selection window must obtain the aspect 84 105 * ratio of the drawComponent. 85 * @param drawComponent The component, the rectangle is drawn onto.86 */ 87 public SelectionManager(SelectionEnded selectionEndedListener, boolean aspectRatio, Component drawComponent) {106 * @param mapView The view, the rectangle is drawn onto. 107 */ 108 public SelectionManager(SelectionEnded selectionEndedListener, boolean aspectRatio, MapView mapView) { 88 109 this.selectionEndedListener = selectionEndedListener; 89 110 this.aspectRatio = aspectRatio; 90 this. drawComponent = drawComponent;111 this.mv = mapView; 91 112 } 92 113 … … 98 119 eventSource.addMouseListener(this); 99 120 eventSource.addMouseMotionListener(this); 121 selectionEndedListener.addPropertyChangeListener(this); 100 122 } 101 123 /** … … 108 130 eventSource.removeMouseListener(this); 109 131 eventSource.removeMouseMotionListener(this); 132 selectionEndedListener.removePropertyChangeListener(this); 110 133 } 111 134 … … 158 181 mousePosStart = null; 159 182 mousePos = null; 183 184 boolean shift = (e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0; 185 boolean alt = (e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) != 0; 186 boolean ctrl = (e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) != 0; 160 187 if ((e.getModifiersEx() & MouseEvent.BUTTON3_DOWN_MASK) == 0) 161 selectionEndedListener.selectionEnded(r, e.getModifiersEx());188 selectionEndedListener.selectionEnded(r, alt, shift, ctrl); 162 189 } 163 190 … … 168 195 */ 169 196 private void paintRect() { 170 Graphics g = drawComponent.getGraphics();197 Graphics g = mv.getGraphics(); 171 198 g.setColor(Color.BLACK); 172 199 g.setXORMode(Color.WHITE); … … 196 223 if (aspectRatio) { 197 224 // keep the aspect ration by shrinking the rectangle 198 double aspectRatio = (double) drawComponent.getWidth()/drawComponent.getHeight();225 double aspectRatio = (double)mv.getWidth()/mv.getHeight(); 199 226 if ((double)w/h > aspectRatio) { 200 227 int neww = (int)(h*aspectRatio); … … 212 239 return new Rectangle(x,y,w,h); 213 240 } 241 242 /** 243 * If the action goes inactive, remove the selection rectangle from screen 244 */ 245 public void propertyChange(PropertyChangeEvent evt) { 246 if (evt.getPropertyName() == "active" && !(Boolean)evt.getNewValue() && mousePosStart != null) { 247 paintRect(); 248 mousePosStart = null; 249 mousePos = null; 250 } 251 } 252 253 /** 254 * Return a list of all objects in the rectangle, respecting the different 255 * modifier. 256 * @param alt Whether the alt key was pressed, which means select all objects 257 * that are touched, instead those which are completly covered. Also 258 * select whole tracks instead of line segments. 259 */ 260 public Collection<OsmPrimitive> getObjectsInRectangle(Rectangle r, boolean alt) { 261 Collection<OsmPrimitive> selection = new LinkedList<OsmPrimitive>(); 262 263 // whether user only clicked, not dragged. 264 boolean clicked = r.width <= 2 && r.height <= 2; 265 Point center = new Point(r.x+r.width/2, r.y+r.height/2); 266 267 if (clicked) { 268 OsmPrimitive osm = mv.getNearest(center, alt); 269 if (osm != null) 270 selection.add(osm); 271 } else { 272 // nodes 273 for (Node n : mv.dataSet.nodes) { 274 if (r.contains(mv.getScreenPoint(n.coor))) 275 selection.add(n); 276 } 277 278 // pending line segments 279 for (LineSegment ls : mv.dataSet.pendingLineSegments) 280 if (rectangleContainLineSegment(r, alt, ls)) 281 selection.add(ls); 282 283 // tracks 284 for (Track t : mv.dataSet.tracks) { 285 boolean wholeTrackSelected = t.segments.size() > 0; 286 for (LineSegment ls : t.segments) 287 if (rectangleContainLineSegment(r, alt, ls)) 288 selection.add(ls); 289 else 290 wholeTrackSelected = false; 291 if (wholeTrackSelected) 292 selection.add(t); 293 } 294 295 // TODO areas 296 } 297 return selection; 298 } 299 300 /** 301 * Decide whether the line segment is in the rectangle Return 302 * <code>true</code>, if it is in or false if not. 303 * 304 * @param r The rectangle, in which the line segment has to be. 305 * @param alt Whether user pressed the Alt key 306 * @param ls The line segment. 307 * @return <code>true</code>, if the LineSegment was added to the selection. 308 */ 309 private boolean rectangleContainLineSegment(Rectangle r, boolean alt, LineSegment ls) { 310 if (alt) { 311 Point p1 = mv.getScreenPoint(ls.start.coor); 312 Point p2 = mv.getScreenPoint(ls.end.coor); 313 if (r.intersectsLine(p1.x, p1.y, p2.x, p2.y)) 314 return true; 315 } else { 316 if (r.contains(mv.getScreenPoint(ls.start.coor)) 317 && r.contains(mv.getScreenPoint(ls.end.coor))) 318 return true; 319 } 320 return false; 321 } 214 322 323 215 324 /** 216 325 * Does nothing. Only to satisfy MouseListener … … 229 338 */ 230 339 public void mouseMoved(MouseEvent e) {} 340 231 341 }
Note:
See TracChangeset
for help on using the changeset viewer.