Ignore:
Timestamp:
2005-10-02T20:32:00+02:00 (19 years ago)
Author:
imi
Message:

added mapmodes for adding and combining stuff. Reorganized images

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/gui/SelectionManager.java

    r4 r7  
    1010import java.awt.event.MouseListener;
    1111import java.awt.event.MouseMotionListener;
     12import java.beans.PropertyChangeEvent;
     13import java.beans.PropertyChangeListener;
     14import java.util.Collection;
     15import java.util.LinkedList;
     16
     17import org.openstreetmap.josm.data.osm.LineSegment;
     18import org.openstreetmap.josm.data.osm.Node;
     19import org.openstreetmap.josm.data.osm.OsmPrimitive;
     20import org.openstreetmap.josm.data.osm.Track;
    1221
    1322/**
     
    3645 * @author imi
    3746 */
    38 public class SelectionManager implements MouseListener, MouseMotionListener {
     47public class SelectionManager implements MouseListener, MouseMotionListener, PropertyChangeListener {
    3948
    4049        /**
     
    4756                 * Called, when the left mouse button was released.
    4857                 * @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
    5161                 * @see InputEvent#getModifiersEx()
    5262                 */
    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);
    5475        }
    5576        /**
     
    6788        private Point mousePos;
    6889        /**
    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;
    7293        /**
    7394         * Whether the selection rectangle must obtain the aspect ratio of the
     
    83104         * @param aspectRatio If true, the selection window must obtain the aspect
    84105         *              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) {
    88109                this.selectionEndedListener = selectionEndedListener;
    89110                this.aspectRatio = aspectRatio;
    90                 this.drawComponent = drawComponent;
     111                this.mv = mapView;
    91112        }
    92113       
     
    98119                eventSource.addMouseListener(this);
    99120                eventSource.addMouseMotionListener(this);
     121                selectionEndedListener.addPropertyChangeListener(this);
    100122        }
    101123        /**
     
    108130                eventSource.removeMouseListener(this);
    109131                eventSource.removeMouseMotionListener(this);
     132                selectionEndedListener.removePropertyChangeListener(this);
    110133        }
    111134
     
    158181                mousePosStart = null;
    159182                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;
    160187                if ((e.getModifiersEx() & MouseEvent.BUTTON3_DOWN_MASK) == 0)
    161                         selectionEndedListener.selectionEnded(r, e.getModifiersEx());
     188                        selectionEndedListener.selectionEnded(r, alt, shift, ctrl);
    162189        }
    163190
     
    168195         */
    169196        private void paintRect() {
    170                 Graphics g = drawComponent.getGraphics();
     197                Graphics g = mv.getGraphics();
    171198                g.setColor(Color.BLACK);
    172199                g.setXORMode(Color.WHITE);
     
    196223                if (aspectRatio) {
    197224                        // keep the aspect ration by shrinking the rectangle
    198                         double aspectRatio = (double)drawComponent.getWidth()/drawComponent.getHeight();
     225                        double aspectRatio = (double)mv.getWidth()/mv.getHeight();
    199226                        if ((double)w/h > aspectRatio) {
    200227                                int neww = (int)(h*aspectRatio);
     
    212239                return new Rectangle(x,y,w,h);
    213240        }
     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        }
    214322       
     323       
    215324        /**
    216325         * Does nothing. Only to satisfy MouseListener
     
    229338         */
    230339        public void mouseMoved(MouseEvent e) {}
     340
    231341}
Note: See TracChangeset for help on using the changeset viewer.