Changeset 91 in josm


Ignore:
Timestamp:
2006-04-22T01:47:07+02:00 (18 years ago)
Author:
imi
Message:
  • added autoscale after selection, layer and conflict
  • added URL download for selection search
Files:
9 added
1 deleted
11 edited
3 moved

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/actions/AutoScaleAction.java

    r68 r91  
    33import java.awt.event.ActionEvent;
    44import java.awt.event.KeyEvent;
     5import java.util.Collection;
    56
    67import javax.swing.KeyStroke;
     8import javax.swing.MenuElement;
    79
     10import org.openstreetmap.josm.Main;
     11import org.openstreetmap.josm.data.SelectionChangedListener;
     12import org.openstreetmap.josm.data.coor.EastNorth;
     13import org.openstreetmap.josm.data.coor.LatLon;
     14import org.openstreetmap.josm.data.osm.OsmPrimitive;
     15import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
    816import org.openstreetmap.josm.gui.MapFrame;
    9 import org.openstreetmap.josm.gui.MapView;
     17import org.openstreetmap.josm.gui.layer.Layer;
    1018
    1119/**
     
    1321 * @author imi
    1422 */
    15 public class AutoScaleAction extends JosmAction {
    16         /**
    17          * The mapView this action operates on.
    18          */
    19         private final MapView mapView;
     23public class AutoScaleAction extends GroupAction {
     24
     25        private enum AutoScaleMode {data, selection, layer, conflict}
     26        private AutoScaleMode mode = AutoScaleMode.data;
     27        private final MapFrame mapFrame;
     28
     29        private class Action extends JosmAction {
     30                private final AutoScaleMode mode;
     31                public Action(AutoScaleMode mode) {
     32                super("Auto Scale: "+mode, "dialogs/autoscale/"+mode, "Auto zoom the view to "+mode+". Disabled if the view is moved.", "Alt-A", KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.ALT_MASK));
     33                        this.mode = mode;
     34        }
     35                public void actionPerformed(ActionEvent e) {
     36                        AutoScaleAction.this.mode = mode;
     37                        if (e.getSource() instanceof MenuElement)
     38                                setAutoScaleOnMapView();
     39                        else
     40                                mapFrame.mapView.setAutoScale(!mapFrame.mapView.isAutoScale());
     41        }
     42        }
    2043       
    21         public AutoScaleAction(MapFrame mapFrame) {
    22                 super("Auto Scale", "autoscale", "Zoom the view to show the whole layer. Disabled if the view is moved.", "Alt-A", KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.ALT_MASK));
    23                 mapView = mapFrame.mapView;
     44        public AutoScaleAction(final MapFrame mapFrame) {
     45                for (AutoScaleMode mode : AutoScaleMode.values())
     46                        actions.add(new Action(mode));
     47                setCurrent(0);
     48                this.mapFrame = mapFrame;
     49                Main.ds.addSelectionChangedListener(new SelectionChangedListener(){
     50                        public void selectionChanged(Collection<OsmPrimitive> newSelection) {
     51                                if (mode == AutoScaleMode.selection)
     52                                        mapFrame.mapView.recalculateCenterScale();
     53            }
     54                });
    2455        }
    2556
     57        public BoundingXYVisitor getBoundingBox() {
     58                BoundingXYVisitor v = new BoundingXYVisitor();
     59                switch (mode) {
     60                case data:
     61                        for (Layer l : mapFrame.mapView.getAllLayers())
     62                                l.visitBoundingBox(v);
     63                        break;
     64                case layer:
     65                        mapFrame.mapView.getActiveLayer().visitBoundingBox(v);
     66                        break;
     67                case selection:
     68                case conflict:
     69                        Collection<OsmPrimitive> sel = mode == AutoScaleMode.selection ? Main.ds.getSelected() : mapFrame.conflictDialog.conflicts.keySet();
     70                        for (OsmPrimitive osm : sel)
     71                                osm.visit(v);
     72                        // special case to zoom nicely to one single node
     73                        if (v.min != null && v.max != null && v.min.north() == v.max.north() && v.min.east() == v.max.east()) {
     74                                EastNorth en = Main.proj.latlon2eastNorth(new LatLon(0.02, 0.02));
     75                                v.min = new EastNorth(v.min.east()-en.east(), v.min.north()-en.north());
     76                                v.max = new EastNorth(v.max.east()+en.east(), v.max.north()+en.north());
     77                        }
     78                        break;
     79                }
     80                return v;
     81    }
    2682
    27         public void actionPerformed(ActionEvent e) {
    28                 mapView.setAutoScale(!mapView.isAutoScale());
    29         }
     83        private void setAutoScaleOnMapView() {
     84        if (mapFrame.mapView.isAutoScale())
     85                mapFrame.mapView.recalculateCenterScale();
     86        else
     87                mapFrame.mapView.setAutoScale(true);
     88    }
    3089}
  • src/org/openstreetmap/josm/actions/mapmode/MapMode.java

    r86 r91  
    6363        }
    6464
    65         /**
    66          * Does nothing. Only to subclass.
    67          */
     65        public void mouseReleased(MouseEvent e) {}
     66        public void mouseExited(MouseEvent e) {}
     67        public void mousePressed(MouseEvent e) {}
    6868        public void mouseClicked(MouseEvent e) {}
    69         /**
    70          * Does nothing. Only to subclass.
    71          */
    72         public void mousePressed(MouseEvent e) {}
    73         /**
    74          * Does nothing. Only to subclass.
    75          */
    76         public void mouseReleased(MouseEvent e) {}
    77         /**
    78          * Does nothing. Only to subclass.
    79          */
    8069        public void mouseEntered(MouseEvent e) {}
    81         /**
    82          * Does nothing. Only to subclass.
    83          */
    84         public void mouseExited(MouseEvent e) {}
    85         /**
    86          * Does nothing. Only to subclass.
    87          */
    8870        public void mouseMoved(MouseEvent e) {}
    89         /**
    90          * Does nothing. Only to subclass.
    91          */
    9271        public void mouseDragged(MouseEvent e) {}
    9372}
  • src/org/openstreetmap/josm/command/ConflictResolveCommand.java

    r90 r91  
    4444                        }
    4545                }
    46                 for (OsmPrimitive k : completed)
    47                         conflictDialog.conflicts.remove(k);
    48                 if (!completed.isEmpty())
     46                if (!completed.isEmpty()) {
     47                        for (OsmPrimitive k : completed)
     48                                conflictDialog.conflicts.remove(k);
    4949                        conflictDialog.rebuildList();
     50                        Main.main.getMapFrame().mapView.recalculateCenterScale(); // in case of auto-zoom
     51                }
    5052        }
    5153
  • src/org/openstreetmap/josm/data/osm/DataSet.java

    r86 r91  
    151151                listeners.remove(listener);
    152152    }
     153
     154        public void addAllSelectionListener(DataSet ds) {
     155                listeners.addAll(ds.listeners);
     156    }
    153157}
  • src/org/openstreetmap/josm/gui/IconToggleButton.java

    r90 r91  
    11package org.openstreetmap.josm.gui;
    22
     3import java.awt.event.MouseAdapter;
     4import java.awt.event.MouseEvent;
    35import java.beans.PropertyChangeEvent;
    46import java.beans.PropertyChangeListener;
     
    1517public class IconToggleButton extends JToggleButton implements PropertyChangeListener {
    1618
     19        public boolean groupbutton;
     20
    1721        /**
    1822         * Construct the toggle button with the given action.
     
    2529                if (o != null)
    2630                        setToolTipText(o.toString());
     31
     32                action.addPropertyChangeListener(this);
    2733               
    28                 action.addPropertyChangeListener(this);
     34                addMouseListener(new MouseAdapter(){
     35                        @Override public void mousePressed(MouseEvent e) {
     36                                groupbutton = e.getX() > getWidth()/3 && e.getY() > getHeight()/3;
     37            }
     38                });
    2939        }
    3040
  • src/org/openstreetmap/josm/gui/MapFrame.java

    r90 r91  
    1212import javax.swing.ButtonGroup;
    1313import javax.swing.JPanel;
    14 import javax.swing.JToggleButton;
    1514import javax.swing.JToolBar;
    1615
    1716import org.openstreetmap.josm.Main;
    1817import org.openstreetmap.josm.actions.AutoScaleAction;
     18import org.openstreetmap.josm.actions.mapmode.AddNodeAction;
    1919import org.openstreetmap.josm.actions.mapmode.AddSegmentAction;
    20 import org.openstreetmap.josm.actions.mapmode.AddNodeAction;
    2120import org.openstreetmap.josm.actions.mapmode.AddWayAction;
    2221import org.openstreetmap.josm.actions.mapmode.DeleteAction;
     
    7069                setLayout(new BorderLayout());
    7170
    72                 add(mapView = new MapView(layer), BorderLayout.CENTER);
     71                AutoScaleAction autoScaleAction = new AutoScaleAction(this);
     72                add(mapView = new MapView(autoScaleAction), BorderLayout.CENTER);
     73                mapView.addLayer(layer);
    7374
    7475                // toolbar
     
    9293                // autoScale
    9394                toolBarActions.addSeparator();
    94                 final JToggleButton autoScaleButton = new IconToggleButton(new AutoScaleAction(this));
     95                final IconToggleButton autoScaleButton = new IconToggleButton(autoScaleAction);
    9596                toolBarActions.add(autoScaleButton);
    9697                autoScaleButton.setText(null);
  • src/org/openstreetmap/josm/gui/MapView.java

    r86 r91  
    1212
    1313import org.openstreetmap.josm.Main;
     14import org.openstreetmap.josm.actions.AutoScaleAction;
    1415import org.openstreetmap.josm.data.Bounds;
    1516import org.openstreetmap.josm.data.SelectionChangedListener;
    1617import org.openstreetmap.josm.data.coor.EastNorth;
    1718import org.openstreetmap.josm.data.coor.LatLon;
    18 import org.openstreetmap.josm.data.osm.DataSet;
    1919import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2020import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
     
    7171         */
    7272        private Collection<LayerChangeListener> listeners = new LinkedList<LayerChangeListener>();
    73        
    74         /**
    75          * Construct a MapView.
    76          * @param layer The first layer in the view.
    77          */
    78         public MapView(Layer layer) {
     73
     74        private final AutoScaleAction autoScaleAction;
     75
     76        public MapView(AutoScaleAction autoScaleAction) {
     77                this.autoScaleAction = autoScaleAction;
    7978                addComponentListener(new ComponentAdapter(){
    8079                        @Override public void componentResized(ComponentEvent e) {
     
    8382                });
    8483                new MapMover(this);
    85                 addLayer(layer);
    86                
     84
    8785                // listend to selection changes to redraw the map
    8886                Main.ds.addSelectionChangedListener(new SelectionChangedListener(){
    8987                        public void selectionChanged(Collection<OsmPrimitive> newSelection) {
    9088                                repaint();
    91             }
     89                        }
    9290                });
    9391        }
     
    10199                        final OsmDataLayer dataLayer = (OsmDataLayer)layer;
    102100                        if (editLayer != null) {
    103                                 // merge the layer into the existing one
    104                                 if (!editLayer.isMergable(layer))
    105                                         throw new IllegalArgumentException("Cannot merge argument");
    106101                                editLayer.mergeFrom(layer);
    107102                                repaint();
     
    109104                        }
    110105                        editLayer = dataLayer;
     106                        dataLayer.data.addAllSelectionListener(Main.ds);
     107                        Main.ds = dataLayer.data;
    111108                        dataLayer.addModifiedListener(new ModifiedChangedListener(){
    112109                                public void modifiedChanged(boolean value, OsmDataLayer source) {
     
    117114
    118115                // add as a new layer
    119         if (layer instanceof WmsServerLayer)
    120             layers.add(layers.size(), layer);
    121         else
    122             layers.add(0, layer);
     116                if (layer instanceof WmsServerLayer)
     117                        layers.add(layers.size(), layer);
     118                else
     119                        layers.add(0, layer);
    123120
    124121                for (LayerChangeListener l : listeners)
     
    127124                // autoselect the new layer
    128125                setActiveLayer(layer);
    129                 recalculateCenterScale();
    130126        }
    131127
     
    207203         * scale, if in autoScale mode.
    208204         */
    209         void recalculateCenterScale() {
     205        public void recalculateCenterScale() {
    210206                if (autoScale) {
    211207                        // -20 to leave some border
     
    217213                                h = 20;
    218214
    219                         BoundingXYVisitor v = new BoundingXYVisitor();
    220                         for (Layer l : layers)
    221                                 l.visitBoundingBox(v);
     215                        BoundingXYVisitor v = autoScaleAction.getBoundingBox();
    222216
    223217                        boolean oldAutoScale = autoScale;
    224218                        EastNorth oldCenter = center;
    225219                        double oldScale = this.scale;
    226                        
     220
    227221                        if (v.min == null || v.max == null || v.min.equals(v.max)) {
    228222                                // no bounds means whole world
     
    238232                                scale = Math.max(scaleX, scaleY); // minimum scale to see all of the screen
    239233                        }
    240        
     234
    241235                        if (!center.equals(oldCenter))
    242236                                firePropertyChange("center", oldCenter, center);
     
    275269        /**
    276270         * Set the active selection to the given value and raise an layerchange event.
    277          * Also, swap the active dataset in Main.main if it is a datalayer.
    278271         */
    279272        public void setActiveLayer(Layer layer) {
     
    282275                Layer old = activeLayer;
    283276                activeLayer = layer;
    284                 if (layer instanceof OsmDataLayer)
    285                         Main.ds = ((OsmDataLayer)layer).data;
    286277                if (old != layer) {
    287278                        for (LayerChangeListener l : listeners)
     
    304295        public OsmDataLayer editLayer() {
    305296                if (editLayer == null)
    306                         addLayer(new OsmDataLayer(new DataSet(), "unnamed", false));
     297                        addLayer(new OsmDataLayer(Main.ds, "unnamed", false));
    307298                return editLayer;
    308299        }
     
    319310
    320311                super.zoomTo(newCenter, scale);
    321                
     312
    322313                recalculateCenterScale();
    323                
     314
    324315                if (!oldCenter.equals(center))
    325316                        firePropertyChange("center", oldCenter, center);
  • src/org/openstreetmap/josm/gui/dialogs/LayerList.java

    r86 r91  
    102102        JList layers = new JList(model);
    103103        /**
    104          * The invisible icon blended over invisible layers.
    105          */
    106         static final Icon invisible = ImageProvider.get("layer", "invisible");
    107 
    108         /**
    109104         * The merge action. This is only called, if the current selection and its
    110105         * item below are editable datasets and the merge button is clicked.
     
    139134                                Icon icon = layer.getIcon();
    140135                                if (!layer.visible)
    141                                         icon = ImageProvider.overlay(icon, invisible, OverlayPosition.SOUTHEAST);
     136                                        icon = ImageProvider.overlay(icon, "invisible", OverlayPosition.SOUTHEAST);
    142137                                label.setIcon(icon);
    143138                                label.setToolTipText(layer.getToolTipText());
  • src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java

    r90 r91  
    1010import java.awt.event.MouseAdapter;
    1111import java.awt.event.MouseEvent;
     12import java.io.IOException;
     13import java.io.InputStreamReader;
     14import java.io.Reader;
     15import java.net.MalformedURLException;
     16import java.net.URL;
    1217import java.util.Collection;
    1318import java.util.LinkedList;
     19import java.util.Map;
    1420
    1521import javax.swing.ButtonGroup;
     
    3036import org.openstreetmap.josm.gui.MapFrame;
    3137import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
     38import org.openstreetmap.josm.io.OsmIdReader;
    3239import org.openstreetmap.josm.tools.GBC;
    3340import org.openstreetmap.josm.tools.ImageProvider;
    3441import org.openstreetmap.josm.tools.SearchCompiler;
     42import org.xml.sax.SAXException;
    3543
    3644/**
     
    120128                                        return;
    121129                                lastSearch = input.getText();
    122                                 SearchCompiler.Match matcher = SearchCompiler.compile(lastSearch);
    123                                 Collection<OsmPrimitive> sel = Main.ds.getSelected();
    124                                 for (OsmPrimitive osm : Main.ds.allNonDeletedPrimitives()) {
    125                                         if (replace.isSelected()) {
    126                                                 if (matcher.match(osm))
     130                                Collection<OsmPrimitive> sel = null;
     131                                if (lastSearch.startsWith("http://"))
     132                                        sel = selectFromWebsite(lastSearch, replace.isSelected(), add.isSelected(), remove.isSelected());
     133                                if (sel == null) {
     134                                        sel = Main.ds.getSelected();
     135                                        SearchCompiler.Match matcher = SearchCompiler.compile(lastSearch);
     136                                        for (OsmPrimitive osm : Main.ds.allNonDeletedPrimitives()) {
     137                                                if (replace.isSelected()) {
     138                                                        if (matcher.match(osm))
     139                                                                sel.add(osm);
     140                                                        else
     141                                                                sel.remove(osm);
     142                                                } else if (add.isSelected() && !osm.selected && matcher.match(osm))
    127143                                                        sel.add(osm);
    128                                                 else
     144                                                else if (remove.isSelected() && osm.selected && matcher.match(osm))
    129145                                                        sel.remove(osm);
    130                                         } else if (add.isSelected() && !osm.selected && matcher.match(osm))
    131                                                 sel.add(osm);
    132                                         else if (remove.isSelected() && osm.selected && matcher.match(osm))
    133                                                 sel.remove(osm);
     146                                        }
    134147                                }
    135148                                Main.ds.setSelected(sel);
     
    141154                selectionChanged(Main.ds.getSelected());
    142155        }
     156
     157        private Collection<OsmPrimitive> selectFromWebsite(String url, boolean replace, boolean add, boolean remove) {
     158                Collection<OsmPrimitive> sel = replace ? new LinkedList<OsmPrimitive>() : Main.ds.allNonDeletedPrimitives();
     159                try {
     160                Reader in = new InputStreamReader(new URL(url).openStream());
     161                Map<Long, String> ids = OsmIdReader.parseIds(in);
     162                for (OsmPrimitive osm : Main.ds.allNonDeletedPrimitives()) {
     163                        if (ids.containsKey(osm.id) && osm.getClass().getName().toLowerCase().endsWith(ids.get(osm.id))) {
     164                                if (remove)
     165                                        sel.remove(osm);
     166                                else
     167                                        sel.add(osm);
     168                        }
     169                        }
     170        } catch (MalformedURLException e) {
     171                return null;
     172        } catch (IOException e) {
     173                e.printStackTrace();
     174                JOptionPane.showMessageDialog(Main.main, "Could not read from url: '"+url+"'");
     175        } catch (SAXException e) {
     176                e.printStackTrace();
     177                JOptionPane.showMessageDialog(Main.main, "Parsing error in url: '"+url+"'");
     178        }
     179        return sel;
     180    }
    143181
    144182        @Override public void setVisible(boolean b) {
  • src/org/openstreetmap/josm/io/OsmReader.java

    r86 r91  
    1111import org.openstreetmap.josm.data.coor.LatLon;
    1212import org.openstreetmap.josm.data.osm.DataSet;
    13 import org.openstreetmap.josm.data.osm.Segment;
    1413import org.openstreetmap.josm.data.osm.Node;
    1514import org.openstreetmap.josm.data.osm.OsmPrimitive;
     15import org.openstreetmap.josm.data.osm.Segment;
    1616import org.openstreetmap.josm.data.osm.Way;
    1717import org.openstreetmap.josm.data.osm.visitor.AddVisitor;
     
    113113                        throw new SAXException(x.getMessage(), x);
    114114                } catch (NullPointerException x) {
     115                        x.printStackTrace(); // SAXException does not chain correctly
    115116                        throw new SAXException("NullPointerException. Possible some missing tags.", x);
    116117                }
  • src/org/openstreetmap/josm/tools/ImageProvider.java

    r79 r91  
    2727         * @author imi
    2828         */
    29         public final static class OverlayPosition {
    30                 private OverlayPosition() {}
    31                 public static OverlayPosition NORTHWEST = new OverlayPosition();
    32                 public static OverlayPosition NORTHEAST = new OverlayPosition();
    33                 public static OverlayPosition SOUTHWEST = new OverlayPosition();
    34                 public static OverlayPosition SOUTHEAST = new OverlayPosition();
    35         }
    36        
     29        public static enum OverlayPosition {NORTHWEST, NORTHEAST, SOUTHWEST, SOUTHEAST}
    3730       
    3831        /**
     
    7164
    7265        /**
    73          * Return an icon that represent the overlay of the two given icons. The
     66         * @return an icon that represent the overlay of the two given icons. The
    7467         * second icon is layed on the first relative to the given position.
    75          *
    76          * @param ground The ground icon (base)
    77          * @param overlay The icon to put on top of the ground (overlay)
    78          * @return The merged icon.
    7968         */
    80         public static Icon overlay(Icon ground, Icon overlay, OverlayPosition pos) {
     69        public static Icon overlay(Icon ground, String overlayImage, OverlayPosition pos) {
    8170                GraphicsConfiguration conf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    8271                int w = ground.getIconWidth();
    8372                int h = ground.getIconHeight();
     73                ImageIcon overlay = ImageProvider.get("overlay",overlayImage);
    8474                int wo = overlay.getIconWidth();
    8575                int ho = overlay.getIconHeight();
     
    8878                ground.paintIcon(null, g, 0, 0);
    8979                int x = 0, y = 0;
    90                 if (pos == OverlayPosition.NORTHWEST) {
     80                switch (pos) {
     81                case NORTHWEST:
    9182                        x = 0;
    9283                        y = 0;
    93                 } else if (pos == OverlayPosition.NORTHEAST) {
     84                        break;
     85                case NORTHEAST:
    9486                        x = w-wo;
    9587                        y = 0;
    96                 } else if (pos == OverlayPosition.SOUTHWEST) {
     88                        break;
     89                case SOUTHWEST:
    9790                        x = 0;
    9891                        y = h-ho;
    99                 } else if (pos == OverlayPosition.SOUTHEAST) {
     92                        break;
     93                case SOUTHEAST:
    10094                        x = w-wo;
    10195                        y = h-ho;
     96                        break;
    10297                }
    10398                overlay.paintIcon(null, g, x, y);
Note: See TracChangeset for help on using the changeset viewer.