Changeset 61 in josm


Ignore:
Timestamp:
2006-03-15T23:16:04+01:00 (18 years ago)
Author:
imi
Message:
  • added search dialog in selection menu
  • changed way mapmode to switch to selection mode after way creation
  • changed way mapmode to ask what to do, if an other way was selected
  • fixed html encoding for tag attributes in OSM data export
Files:
1 added
9 edited
2 moved

Legend:

Unmodified
Added
Removed
  • data/empty.xml

    r49 r61  
    1 <?xml version="1.0" encoding="UTF-8"?>
    2 <osm />
    3 
     1<osm version='0.3' generator='JOSM'>
     2</osm>
  • src/org/openstreetmap/josm/actions/DownloadAction.java

    r58 r61  
    3232import org.openstreetmap.josm.data.Bounds;
    3333import org.openstreetmap.josm.data.GeoPoint;
     34import org.openstreetmap.josm.data.Preferences.PreferencesException;
    3435import org.openstreetmap.josm.data.osm.DataSet;
    3536import org.openstreetmap.josm.gui.BookmarkList;
     
    5556public class DownloadAction extends JosmAction {
    5657
     58    private enum DownloadStatus {FINISHED, REDISPLAY}
     59
    5760        JTextField[] latlon = new JTextField[]{
    5861                        new JTextField(9),
     
    6871
    6972        public void actionPerformed(ActionEvent e) {
     73               
     74                //TODO: Remove this in later versions (temporary only)
     75                if (Main.pref.osmDataServer.endsWith("/0.2") || Main.pref.osmDataServer.endsWith("/0.2/")) {
     76                        int answer = JOptionPane.showConfirmDialog(Main.main,
     77                                        "You seem to have an outdated server entry in your preferences.\n" +
     78                                        "\n" +
     79                                        "As of JOSM Release 1.2, you must no longer specify the API version in\n" +
     80                                        "the osm url. For the OSM standard server, use http://www.openstreetmap.org/api" +
     81                                        "\n" +
     82                                        "Fix settings and continue?", "Outdated server url detected.", JOptionPane.YES_NO_OPTION);
     83                        if (answer != JOptionPane.YES_OPTION)
     84                                return;
     85                        int cutPos = Main.pref.osmDataServer.endsWith("/0.2") ? 4 : 5;
     86                        Main.pref.osmDataServer = Main.pref.osmDataServer.substring(0, Main.pref.osmDataServer.length()-cutPos);
     87                        try {
     88                                Main.pref.save();
     89                        } catch (PreferencesException x) {
     90                                x.printStackTrace();
     91                                JOptionPane.showMessageDialog(Main.main, "Could not save the preferences chane:\n" +
     92                                                x.getMessage());
     93                        }
     94                }
     95
    7096                JPanel dlg = new JPanel(new GridBagLayout());
    7197
     
    208234               
    209235                // Finally: the dialog
    210                 int r = JOptionPane.showConfirmDialog(Main.main, dlg, "Choose an area",
    211                                 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
    212                 if (r != JOptionPane.OK_OPTION)
    213                         return;
    214 
    215                 startDownload();
     236                while(true) {
     237                        int r = JOptionPane.showConfirmDialog(Main.main, dlg, "Choose an area",
     238                                        JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
     239                        if (r != JOptionPane.OK_OPTION)
     240                                return;
     241                        if (startDownload() == DownloadStatus.FINISHED)
     242                                break;
     243                }
    216244        }
    217245
     
    219247         * Read the values from the edit fields and start the download.
    220248         */
    221         private void startDownload() {
     249        private DownloadStatus startDownload() {
    222250                Bookmark b = readBookmark();
    223251                if (b == null) {
    224252                        JOptionPane.showMessageDialog(Main.main, "Please enter the desired coordinates or click on a bookmark.");
    225                         return;
    226                 }
    227                
     253                        return DownloadStatus.REDISPLAY;
     254                }
     255
    228256                final OsmServerReader osmReader = new OsmServerReader(b.latlon[0], b.latlon[1], b.latlon[2], b.latlon[3]);
    229257                new Thread(new PleaseWaitRunnable("Downloading data"){
     
    275303                        }
    276304                }).start();
     305                return DownloadStatus.FINISHED;
    277306        }
    278307       
  • src/org/openstreetmap/josm/actions/mapmode/AddWayAction.java

    r58 r61  
    11package org.openstreetmap.josm.actions.mapmode;
    22
    3 import java.awt.Rectangle;
    43import java.awt.event.ActionEvent;
    54import java.awt.event.KeyEvent;
     
    76import java.util.Iterator;
    87import java.util.LinkedList;
     8
     9import javax.swing.JOptionPane;
    910
    1011import org.openstreetmap.josm.Main;
     
    1415import org.openstreetmap.josm.data.osm.Track;
    1516import org.openstreetmap.josm.gui.MapFrame;
    16 import org.openstreetmap.josm.gui.SelectionManager;
    17 import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded;
    1817
    1918/**
     
    3433 *
    3534 */
    36 public class AddTrackAction extends MapMode implements SelectionEnded {
     35public class AddWayAction extends MapMode {
    3736
    38         /**
    39          * The selection manager for this action, keeping track of all selections.
    40          */
    41         SelectionManager selectionManager;
     37        private MapMode followMode;
    4238       
    4339        /**
    44          * Create a new AddTrackAction.
     40         * Create a new AddWayAction.
    4541         * @param mapFrame The MapFrame this action belongs to.
     42         * @param followMode The mode to go into when finished creating a way.
    4643         */
    47         public AddTrackAction(MapFrame mapFrame) {
    48                 super("Add Way", "addtrack", "Combine line segments to a new way.", KeyEvent.VK_W, mapFrame);
    49                 this.selectionManager = new SelectionManager(this, false, mv);
     44        public AddWayAction(MapFrame mapFrame, MapMode followMode) {
     45                super("Add Way", "addway", "Combine line segments to a new way.", KeyEvent.VK_W, mapFrame);
     46                this.followMode = followMode;
    5047        }
    5148
    52         @Override
    53         public void registerListener() {
    54                 super.registerListener();
    55                 selectionManager.register(mv);
    56         }
    57 
    58         @Override
    59         public void unregisterListener() {
    60                 super.unregisterListener();
    61                 selectionManager.unregister(mv);
    62         }
    63 
    64        
    6549        @Override
    6650        public void actionPerformed(ActionEvent e) {
    6751                makeTrack();
    6852                super.actionPerformed(e);
    69         }
    70 
    71         /**
    72          * If Shift is pressed, only add the selected line segments to the selection.
    73          * If Ctrl is pressed, only remove the selected line segments from the selection.
    74          * If both, Shift and Ctrl is pressed, do nothing.
    75          *
    76          * Else, form a new track out of all line segments in the selection and
    77          * clear the selection afterwards.
    78          *
    79          * If Alt is pressed, consider all linesegments of all tracks a selected
    80          * line segment is part of. Also consider all line segments that cross the
    81          * selecting rectangle, instead only those that are fully within.
    82          *
    83          */
    84         public void selectionEnded(Rectangle r, boolean alt, boolean shift, boolean ctrl) {
    85                 if (shift && ctrl)
    86                         return; // not allowed together
    87 
    88                 if (!ctrl && !shift)
    89                         Main.main.ds.clearSelection(); // new selection will replace the old.
    90 
    91                 Collection<OsmPrimitive> selectionList = selectionManager.getObjectsInRectangle(r,alt);
    92                 for (OsmPrimitive osm : selectionList)
    93                         osm.setSelected(!ctrl);
    94 
    95                 mv.repaint(); // from now on, the map has to be repainted.
    96 
    97                 if (ctrl || shift)
    98                         return; // no new track yet.
    99                
    100                 makeTrack();
     53                mapFrame.selectMapMode(followMode);
    10154        }
    10255
     
    11164                // form a new track
    11265                LinkedList<LineSegment> lineSegments = new LinkedList<LineSegment>();
     66                int numberOfSelectedWays = 0;
    11367                for (OsmPrimitive osm : selection) {
    11468                        if (osm instanceof Track)
    115                                 lineSegments.addAll(((Track)osm).segments);
     69                                numberOfSelectedWays++;
    11670                        else if (osm instanceof LineSegment)
    11771                                lineSegments.add((LineSegment)osm);
     72                }
     73               
     74                if (numberOfSelectedWays > 0) {
     75                        String ways = "way" + (numberOfSelectedWays==1?"":"s");
     76                        int answer = JOptionPane.showConfirmDialog(Main.main, numberOfSelectedWays+" "+ways+" have been selected.\n" +
     77                                        "Do you wish to select all segments belonging to the "+ways+" instead?");
     78                        if (answer == JOptionPane.CANCEL_OPTION)
     79                                return;
     80                        if (answer == JOptionPane.YES_OPTION) {
     81                                for (OsmPrimitive osm : selection)
     82                                        if (osm instanceof Track)
     83                                                lineSegments.addAll(((Track)osm).segments);
     84                        }
    11885                }
    11986               
  • src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r58 r61  
    145145                keys.put(key, value);
    146146        }
     147       
     148        public String get(Key key) {
     149                return (keys == null) ? null : keys.get(key);
     150        }
    147151}
  • src/org/openstreetmap/josm/gui/MapFrame.java

    r30 r61  
    1717import org.openstreetmap.josm.actions.mapmode.AddLineSegmentAction;
    1818import org.openstreetmap.josm.actions.mapmode.AddNodeAction;
    19 import org.openstreetmap.josm.actions.mapmode.AddTrackAction;
     19import org.openstreetmap.josm.actions.mapmode.AddWayAction;
    2020import org.openstreetmap.josm.actions.mapmode.DeleteAction;
    2121import org.openstreetmap.josm.actions.mapmode.MapMode;
     
    8181                toolBarActions.setFloatable(false);
    8282                toolBarActions.add(new IconToggleButton(this, new ZoomAction(this)));
    83                 toolBarActions.add(new IconToggleButton(this, new SelectionAction(this)));
     83                final SelectionAction selectionAction = new SelectionAction(this);
     84                toolBarActions.add(new IconToggleButton(this, selectionAction));
    8485                toolBarActions.add(new IconToggleButton(this, new MoveAction(this)));
    8586                toolBarActions.add(new IconToggleButton(this, new AddNodeAction(this)));
    8687                toolBarActions.add(new IconToggleButton(this, new AddLineSegmentAction(this)));
    87                 toolBarActions.add(new IconToggleButton(this, new AddTrackAction(this)));
     88                toolBarActions.add(new IconToggleButton(this, new AddWayAction(this, selectionAction)));
    8889                toolBarActions.add(new IconToggleButton(this, new DeleteAction(this)));
    8990
  • src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java

    r39 r61  
    33import java.awt.BorderLayout;
    44import java.awt.Dimension;
     5import java.awt.GridLayout;
    56import java.awt.event.ActionEvent;
    67import java.awt.event.ActionListener;
     
    910import java.awt.event.MouseEvent;
    1011import java.util.Collection;
     12import java.util.Map.Entry;
    1113
    1214import javax.swing.DefaultListModel;
    1315import javax.swing.JButton;
    1416import javax.swing.JList;
     17import javax.swing.JOptionPane;
     18import javax.swing.JPanel;
    1519import javax.swing.JScrollPane;
    1620import javax.swing.ListSelectionModel;
     
    1822import org.openstreetmap.josm.Main;
    1923import org.openstreetmap.josm.data.SelectionChangedListener;
     24import org.openstreetmap.josm.data.osm.Key;
    2025import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2126import org.openstreetmap.josm.gui.ImageProvider;
     
    6166                add(new JScrollPane(displaylist), BorderLayout.CENTER);
    6267
     68                JPanel buttonPanel = new JPanel(new GridLayout(1,2));
     69               
    6370                JButton button = new JButton("Select", ImageProvider.get("mapmode", "selection"));
    6471                button.setToolTipText("Set the selected elements on the map to the selected items in the list above.");
     
    6875                        }
    6976                });
    70                 add(button, BorderLayout.SOUTH);
     77                buttonPanel.add(button);
    7178
     79                button = new JButton("Search", ImageProvider.get("dialogs", "search"));
     80                button.setToolTipText("Search for objects.");
     81                button.addActionListener(new ActionListener(){
     82                        public void actionPerformed(ActionEvent e) {
     83                                String search = JOptionPane.showInputDialog(Main.main, "Please enter a search string", "Search", JOptionPane.INFORMATION_MESSAGE);
     84                                if (search == null)
     85                                        return;
     86                                Main.main.ds.clearSelection();
     87                                for (OsmPrimitive osm : Main.main.ds.allNonDeletedPrimitives()) {
     88                                        if (osm.keys != null) {
     89                                                for (Entry<Key, String> ent : osm.keys.entrySet()) {
     90                                                        if (search.indexOf(ent.getKey().name) != -1 || search.indexOf(ent.getValue()) != -1) {
     91                                                                osm.setSelected(true);
     92                                                                break;
     93                                                        }
     94                                                }
     95                                        }
     96                                }
     97                                selectionChanged(Main.main.ds.getSelected());
     98                                Main.main.getMapFrame().repaint();
     99                        }
     100                });
     101                buttonPanel.add(button);
     102               
     103                add(buttonPanel, BorderLayout.SOUTH);
    72104                selectionChanged(Main.main.ds.getSelected());
    73105        }
  • src/org/openstreetmap/josm/io/OsmReader.java

    r60 r61  
    1616import org.xml.sax.Attributes;
    1717import org.xml.sax.SAXException;
     18import org.xml.sax.SAXParseException;
    1819
    1920import uk.co.wilson.xml.MinML2;
     
    9394                                readCommon(atts);
    9495                        } else if (qName.equals("seg")) {
    95                                 if (current instanceof Track)
    96                                         ((Track)current).segments.add(lineSegments.get(getLong(atts, "id")));
     96                                if (current instanceof Track) {
     97                                        LineSegment ls = lineSegments.get(getLong(atts, "id"));
     98                                        if (ls == null)
     99                                                fatalError(new SAXParseException("Line segment "+getLong(atts, "id")+"has not been transfered before.", null));
     100                                        ((Track)current).segments.add(ls);
     101                                }
    97102                        } else if (qName.equals("tag")) {
    98103                                current.put(Key.get(atts.getValue("k")), atts.getValue("v"));
     
    102107                } catch (NullPointerException x) {
    103108                        throw new SAXException("NullPointerException. Possible some missing tags.", x);
     109                }
     110        }
     111
     112       
     113        @Override
     114        public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
     115                if (qName.equals("node") || qName.equals("segment") || qName.equals("way") || qName.equals("area")) {
     116                        current.visit(adder);
    104117                }
    105118        }
     
    122135        }
    123136
    124         @Override
    125         public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
    126                 if (qName.equals("node") || qName.equals("segment") || qName.equals("way") || qName.equals("area")) {
    127                         current.visit(adder);
    128                 }
    129         }
    130 
    131137        private double getDouble(Attributes atts, String value) {
    132138                return Double.parseDouble(atts.getValue(value));
  • src/org/openstreetmap/josm/io/OsmServerWriter.java

    r58 r61  
    66import java.io.InputStreamReader;
    77import java.io.OutputStreamWriter;
     8import java.io.StringWriter;
    89import java.io.Writer;
    910import java.net.HttpURLConnection;
     
    166167                                OsmWriter.outputSingle(out, osm, true);
    167168                                out.close();
     169                               
     170                                StringWriter o = new StringWriter();
     171                                OsmWriter.outputSingle(o, osm, true);
     172                                System.out.println(o.getBuffer().toString());
    168173                        }
    169174
     
    172177                                osm.id = readId(con.getInputStream());
    173178                        System.out.println("got return: "+retCode+" with id "+osm.id);
     179                        String retMsg = con.getResponseMessage();
    174180                        con.disconnect();
     181                        if (retCode != 200)
     182                                throw new RuntimeException(retCode+" "+retMsg);
    175183                } catch (UnknownHostException e) {
    176184                        throw new RuntimeException("Unknown host: "+e.getMessage(), e);
  • src/org/openstreetmap/josm/io/OsmWriter.java

    r59 r61  
    1313import org.openstreetmap.josm.data.osm.Track;
    1414import org.openstreetmap.josm.data.osm.visitor.Visitor;
     15import org.xml.sax.SAXException;
    1516
    1617/**
     
    2021 */
    2122public class OsmWriter implements Visitor {
     23
     24        private class RuntimeEncodingException extends RuntimeException {
     25                public RuntimeEncodingException(Throwable t) {
     26                        super(t);
     27                }
     28                public RuntimeEncodingException() {
     29                }
     30        }
    2231
    2332        /**
     
    3847        private final boolean osmConform;
    3948
     49        private final static HashMap<Character, String> encoding = new HashMap<Character, String>();
     50        static {
     51                encoding.put('<', "&lt;");
     52                encoding.put('>', "&gt;");
     53                encoding.put('"', "&quot;");
     54                encoding.put('\'', "&apos;");
     55                encoding.put('&', "&amp;");
     56                encoding.put('\n', "&#xA;");
     57                encoding.put('\r', "&#xD;");
     58                encoding.put('\t', "&#x9;");
     59        }
     60       
    4061        /**
    4162         * Output the data to the stream
     
    4667        public static void output(Writer out, DataSet ds, boolean osmConform) {
    4768                OsmWriter writer = new OsmWriter(out, osmConform);
     69                writer.out.println("<?xml version='1.0' encoding='UTF-8'?>");
    4870                writer.out.println("<osm version='0.3' generator='JOSM'>");
    4971                for (Node n : ds.nodes)
     
    5678        }
    5779
    58         public static void outputSingle(Writer out, OsmPrimitive osm, boolean osmConform) {
    59                 OsmWriter writer = new OsmWriter(out, osmConform);
    60                 writer.out.println("<osm version='0.3' generator='JOSM'>");
    61                 osm.visit(writer);
    62                 writer.out.println("</osm>");
     80        public static void outputSingle(Writer out, OsmPrimitive osm, boolean osmConform) throws SAXException {
     81                try {
     82                        OsmWriter writer = new OsmWriter(out, osmConform);
     83                        writer.out.println("<?xml version='1.0' encoding='UTF-8'?>");
     84                        writer.out.println("<osm version='0.3' generator='JOSM'>");
     85                        osm.visit(writer);
     86                        writer.out.println("</osm>");
     87                } catch (RuntimeEncodingException e) {
     88                        throw new SAXException("Your Java installation does not support the required UTF-8 encoding", (Exception)e.getCause());
     89                }               
    6390        }
    64        
     91
    6592        private OsmWriter(Writer out, boolean osmConform) {
    6693                if (out instanceof PrintWriter)
     
    7097                this.osmConform = osmConform;
    7198        }
    72        
     99
    73100        public void visit(Node n) {
    74101                addCommon(n, "node");
     
    112139                                out.println(">");
    113140                        for (Entry<Key, String> e : osm.keys.entrySet())
    114                                 out.println("    <tag k='"+e.getKey().name+"' v='"+e.getValue()+"' />");
    115                         out.println("  </"+tagname+">");
     141                                out.println("    <tag k='"+ encode(e.getKey().name) +
     142                                                "' v='"+encode(e.getValue())+ "' />");
     143                        out.println("  </" + tagname + ">");
    116144                } else if (tagOpen)
    117145                        out.println(" />");
    118146                else
    119                         out.println("  </"+tagname+">");
     147                        out.println("  </" + tagname + ">");
    120148        }
     149
     150        /**
     151         * Encode the given string in XML1.0 format.
     152         * Optimized to fast pass strings that don't need encoding (normal case).
     153         */
     154        public String encode(String unencoded) {
     155                StringBuilder buffer = null;
     156                for (int i = 0; i < unencoded.length(); ++i) {
     157                        String encS = encoding.get(unencoded.charAt(i));
     158                        if (encS != null) {
     159                                if (buffer == null)
     160                                        buffer = new StringBuilder(unencoded.substring(0,i));
     161                                buffer.append(encS);
     162                        } else if (buffer != null)
     163                                buffer.append(unencoded.charAt(i));
     164                }
     165                return (buffer == null) ? unencoded : buffer.toString();
     166        }
     167
    121168
    122169        /**
     
    140187        }
    141188}
    142 
  • test/org/openstreetmap/josm/test/OsmWriterTest.java

    r58 r61  
    1010import junit.framework.TestCase;
    1111
     12import org.jdom.Attribute;
    1213import org.jdom.Element;
    1314import org.jdom.JDOMException;
     
    5758        }
    5859
     60        @Bug(59)
     61        public void testSpecialChars() throws Exception {
     62                StringBuilder sb = new StringBuilder();
     63                for (int i = 32; i < 0xd800; ++i)
     64                        sb.append((char)i);
     65                String s = sb.toString();
     66                n1.put(Key.get(s), s);
     67                reparse();
     68                assertEquals(1, nodes.get(0).getChildren().size());
     69                Attribute key = ((Element)nodes.get(0).getChildren().get(0)).getAttribute("k");
     70                assertEquals(s, key.getValue());
     71                Attribute value = ((Element)nodes.get(0).getChildren().get(0)).getAttribute("v");
     72                assertEquals(s, value.getValue());
     73        }
    5974       
    6075        public void testLineSegment() throws Exception {
     
    110125         */
    111126        @Bug(47)
    112         public void testDeleteNewDoesReallyRemove() throws IOException, JDOMException {
     127        public void testDeleteNewDoesReallyRemove() throws Exception {
    113128                ds.tracks.iterator().next().setDeleted(true);
    114129                reparse();
     
    120135         * Verify that action tag is set correctly.
    121136         */
    122         public void testActionTag() throws IOException, JDOMException {
     137        public void testActionTag() throws Exception {
    123138                int id = 1;
    124139                for (OsmPrimitive osm : ds.allPrimitives())
Note: See TracChangeset for help on using the changeset viewer.