Changeset 20 in josm for src/org/openstreetmap


Ignore:
Timestamp:
2005-10-13T08:27:12+02:00 (19 years ago)
Author:
imi
Message:

Added bookmarks for importing and fixed password dialog

Location:
src/org/openstreetmap/josm
Files:
1 added
5 edited

Legend:

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

    r18 r20  
    22
    33import java.awt.GridBagLayout;
     4import java.awt.GridLayout;
    45import java.awt.event.ActionEvent;
     6import java.awt.event.ActionListener;
    57import java.awt.event.KeyEvent;
    68
    79import javax.swing.AbstractAction;
     10import javax.swing.DefaultListModel;
     11import javax.swing.JButton;
     12import javax.swing.JCheckBox;
    813import javax.swing.JLabel;
    914import javax.swing.JOptionPane;
    1015import javax.swing.JPanel;
     16import javax.swing.JScrollPane;
    1117import javax.swing.JTextField;
     18import javax.swing.event.ListSelectionEvent;
     19import javax.swing.event.ListSelectionListener;
    1220
    1321import org.openstreetmap.josm.data.GeoPoint;
    1422import org.openstreetmap.josm.data.osm.DataSet;
     23import org.openstreetmap.josm.gui.BookmarkList;
    1524import org.openstreetmap.josm.gui.GBC;
    1625import org.openstreetmap.josm.gui.ImageProvider;
     
    1827import org.openstreetmap.josm.gui.MapFrame;
    1928import org.openstreetmap.josm.gui.MapView;
     29import org.openstreetmap.josm.gui.BookmarkList.Bookmark;
    2030import org.openstreetmap.josm.gui.layer.Layer;
    2131import org.openstreetmap.josm.gui.layer.LayerFactory;
     
    3444public class OpenOsmServerAction extends AbstractAction {
    3545
     46        private JTextField[] latlon = new JTextField[]{
     47                        new JTextField(9),
     48                        new JTextField(9),
     49                        new JTextField(9),
     50                        new JTextField(9)};
     51        private JCheckBox rawGps = new JCheckBox("Open as raw gps data", false);
     52
    3653        public OpenOsmServerAction() {
    3754                super("Connect to OSM", ImageProvider.get("connectosm"));
     
    4461                dlg.add(new JLabel("Bounding box"), GBC.eol());
    4562
    46                 JTextField minLat = new JTextField(9);
    47                 JTextField minLon = new JTextField(9);
    48                 JTextField maxLat = new JTextField(9);
    49                 JTextField maxLon = new JTextField(9);
    50 
    5163                dlg.add(new JLabel("min lat"), GBC.std().insets(10,0,5,0));
    52                 dlg.add(minLat, GBC.std());
     64                dlg.add(latlon[0], GBC.std());
    5365                dlg.add(new JLabel("max lat"), GBC.std().insets(10,0,5,0));
    54                 dlg.add(maxLat, GBC.eol());
     66                dlg.add(latlon[1], GBC.eol());
    5567                dlg.add(new JLabel("min lon"), GBC.std().insets(10,0,5,0));
    56                 dlg.add(minLon, GBC.std());
     68                dlg.add(latlon[2], GBC.std());
    5769                dlg.add(new JLabel("max lon"), GBC.std().insets(10,0,5,0));
    58                 dlg.add(maxLon, GBC.eol());
     70                dlg.add(latlon[3], GBC.eop());
    5971
    6072                if (Main.main.getMapFrame() != null) {
     
    6476                        GeoPoint bottomLeft = mv.getPoint(0, h, true);
    6577                        GeoPoint topRight = mv.getPoint(w, 0, true);
    66                         minLat.setText(""+bottomLeft.lat);
    67                         minLon.setText(""+bottomLeft.lon);
    68                         maxLat.setText(""+topRight.lat);
    69                         maxLon.setText(""+topRight.lon);
    70                        
    71                         minLat.setCaretPosition(0);
    72                         minLon.setCaretPosition(0);
    73                         maxLat.setCaretPosition(0);
    74                         maxLon.setCaretPosition(0);
     78                        latlon[0].setText(""+bottomLeft.lat);
     79                        latlon[1].setText(""+bottomLeft.lon);
     80                        latlon[2].setText(""+topRight.lat);
     81                        latlon[3].setText(""+topRight.lon);
     82                        for (JTextField f : latlon)
     83                                f.setCaretPosition(0);
     84                        rawGps.setSelected(!mv.getActiveLayer().isEditable());
    7585                }
    7686
     87                dlg.add(rawGps, GBC.eop());
     88               
     89                // load bookmarks
     90                dlg.add(new JLabel("Bookmarks"), GBC.eol());
     91                final BookmarkList bookmarks = new BookmarkList();
     92                bookmarks.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
     93                        public void valueChanged(ListSelectionEvent e) {
     94                                Bookmark b = (Bookmark)bookmarks.getSelectedValue();
     95                                for (int i = 0; i < 4; ++i) {
     96                                        latlon[i].setText(b == null ? "" : ""+b.latlon[i]);
     97                                        latlon[i].setCaretPosition(0);
     98                                }
     99                                rawGps.setSelected(b == null ? false : b.rawgps);
     100                        }
     101                });
     102                dlg.add(new JScrollPane(bookmarks), GBC.eol().fill());
     103
     104                JPanel buttons = new JPanel(new GridLayout(1,2));
     105                JButton add = new JButton("Add");
     106                add.addActionListener(new ActionListener(){
     107                        public void actionPerformed(ActionEvent e) {
     108                                Bookmark b = readBookmark();
     109                                if (b == null) {
     110                                        JOptionPane.showMessageDialog(Main.main, "Please enter the desired coordinates first.");
     111                                        return;
     112                                }
     113                                b.name = JOptionPane.showInputDialog(Main.main, "Please enter a name for the location.");
     114                                if (!b.name.equals("")) {
     115                                        ((DefaultListModel)bookmarks.getModel()).addElement(b);
     116                                        bookmarks.save();
     117                                }
     118                        }
     119                });
     120                buttons.add(add);
     121                JButton remove = new JButton("Remove");
     122                remove.addActionListener(new ActionListener(){
     123                        public void actionPerformed(ActionEvent e) {
     124                                Object sel = bookmarks.getSelectedValue();
     125                                if (sel == null) {
     126                                        JOptionPane.showMessageDialog(Main.main, "Select a bookmark first.");
     127                                        return;
     128                                }
     129                                ((DefaultListModel)bookmarks.getModel()).removeElement(sel);
     130                                bookmarks.save();
     131                        }
     132                });
     133                buttons.add(remove);
     134                dlg.add(buttons, GBC.eop().fill(GBC.HORIZONTAL));
     135               
    77136                int r = JOptionPane.showConfirmDialog(Main.main, dlg, "Choose an area",
    78137                                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
     
    80139                        return;
    81140
     141                Bookmark b = readBookmark();
     142                if (b == null) {
     143                        JOptionPane.showMessageDialog(Main.main, "Please enter the desired coordinates or click on a bookmark.");
     144                        return;
     145                }
    82146                OsmReader osmReader = new OsmReader(Main.pref.osmDataServer,
    83                                 Main.pref.osmDataUsername, Main.pref.osmDataPassword,
    84                                 Double.parseDouble(minLat.getText()),
    85                                 Double.parseDouble(minLon.getText()),
    86                                 Double.parseDouble(maxLat.getText()),
    87                                 Double.parseDouble(maxLon.getText()));
     147                                rawGps.isSelected(), b.latlon[0], b.latlon[1], b.latlon[2], b.latlon[3]);
    88148                try {
    89149                        DataSet dataSet = osmReader.parse();
     150                        if (dataSet == null)
     151                                return; // user cancelled download
     152                        if (dataSet.nodes.isEmpty())
     153                                JOptionPane.showMessageDialog(Main.main, "No data imported.");
    90154
    91                         String name = minLat.getText()+" "+minLon.getText()+" x "+
    92                                         maxLat.getText()+" "+maxLon.getText();
     155                        String name = latlon[0].getText()+" "+latlon[1].getText()+" x "+
     156                                        latlon[2].getText()+" "+latlon[3].getText();
    93157                       
    94                         Layer layer = LayerFactory.create(dataSet, name, false);
     158                        Layer layer = LayerFactory.create(dataSet, name, rawGps.isSelected());
    95159
    96160                        if (Main.main.getMapFrame() == null)
     
    106170                }
    107171        }
     172       
     173        /**
     174         * Read a bookmark from the current set edit fields. If one of the fields is
     175         * empty or contain illegal chars, <code>null</code> is returned.
     176         * The name of the bookmark is <code>null</code>.
     177         * @return A bookmark containing information from the edit fields and rawgps
     178         *              checkbox.
     179         */
     180        private Bookmark readBookmark() {
     181                try {
     182                        Bookmark b = new Bookmark();
     183                        for (int i = 0; i < 4; ++i) {
     184                                if (latlon[i].getText().equals(""))
     185                                        return null;
     186                                b.latlon[i] = Double.parseDouble(latlon[i].getText());
     187                        }
     188                        b.rawgps = rawGps.isSelected();
     189                        return b;
     190                } catch (NumberFormatException x) {
     191                        return null;
     192                }
     193        }
    108194}
  • src/org/openstreetmap/josm/data/Preferences.java

    r18 r20  
    66import java.io.FileReader;
    77import java.io.FileWriter;
     8import java.io.IOException;
    89import java.util.Collection;
    910import java.util.LinkedList;
     
    7980         * Return the location of the preferences file
    8081         */
    81         public static String getPreferencesFile() {
    82                 return System.getProperty("user.home")+"/.josm-preferences";
     82        public static String getPreferencesDir() {
     83                return System.getProperty("user.home")+"/.josm/";
    8384        }
    8485       
     
    9192                        super(message, cause);
    9293                }
     94                public PreferencesException(String message) {
     95                        super(message);
     96                }
    9397        }
    9498        /**
     
    97101         */
    98102        public void load() throws PreferencesException {
    99                 File file = new File(System.getProperty("user.home")+"/.josm-preferences");
     103                File file = new File(getPreferencesDir()+"/preferences");
    100104                Element root;
    101105                try {
     
    159163
    160164                try {
    161                         final FileWriter file = new FileWriter(getPreferencesFile());
     165                        File prefDir = new File(getPreferencesDir());
     166                        if (prefDir.exists() && !prefDir.isDirectory())
     167                                throw new PreferencesException("Preferences directory "+getPreferencesDir()+" is not a directory.");
     168                        if (!prefDir.exists())
     169                                prefDir.mkdirs();
     170
     171                        FileWriter file = new FileWriter(getPreferencesDir()+"/preferences");
    162172                        new XMLOutputter(Format.getPrettyFormat()).output(root, file);
    163173                        file.close();
    164                 } catch (Exception e) {
     174                } catch (IOException e) {
    165175                        throw new PreferencesException("Could not write preferences", e);
    166176                }
  • src/org/openstreetmap/josm/gui/Main.java

    r19 r20  
    126126                } catch (PreferencesException e1) {
    127127                        e1.printStackTrace();
    128                         errMsg = "Preferences could not be loaded. Write default preference file to '"+Preferences.getPreferencesFile()+"'.";
     128                        errMsg = "Preferences could not be loaded. Write default preference file to '"+Preferences.getPreferencesDir()+"preferences'.";
    129129                        try {
    130130                                pref.save();
  • src/org/openstreetmap/josm/gui/PreferenceDialog.java

    r18 r20  
    189189
    190190               
    191                 // tooltips
    192191                osmDataServer.setToolTipText("The base URL to the OSM server (REST API)");
    193192                osmDataUsername.setToolTipText("Login name (email) to the OSM account.");
     
    197196                forceRawGpsLines.setToolTipText("Force drawing of lines if the imported data contain no line information.");
    198197                forceRawGpsLines.setSelected(Main.pref.isForceRawGpsLines());
     198                forceRawGpsLines.setEnabled(drawRawGpsLines.isSelected());
    199199                mergeNodes.setToolTipText("When importing GPX data, all nodes with exact the same lat/lon are merged.");
    200200                mergeNodes.setSelected(Main.pref.mergeNodes);
  • src/org/openstreetmap/josm/io/OsmReader.java

    r18 r20  
    11package org.openstreetmap.josm.io;
    22
     3import java.awt.Font;
     4import java.awt.GridBagLayout;
    35import java.io.IOException;
    46import java.io.InputStreamReader;
     
    911import java.net.URL;
    1012
     13import javax.swing.JLabel;
     14import javax.swing.JOptionPane;
     15import javax.swing.JPanel;
     16import javax.swing.JPasswordField;
     17import javax.swing.JTextField;
     18
    1119import org.openstreetmap.josm.data.osm.DataSet;
     20import org.openstreetmap.josm.gui.GBC;
     21import org.openstreetmap.josm.gui.Main;
    1222
    1323/**
     
    2232         */
    2333        private String urlStr;
     34        /**
     35         * Whether importing the raw trackpoints or the regular osm map information
     36         */
     37        private boolean rawGps;
     38        /**
     39         * Whether the user cancelled the password dialog
     40         */
     41        private boolean cancelled = false;
     42        /**
     43         * Set to true, when the autenticator tried the password once.
     44         */
     45        private boolean passwordtried = false;
    2446
    2547        /**
    2648         * Construct the reader and store the information for attaching
    2749         */
    28         public OsmReader(String server, final String username, final String password,
     50        public OsmReader(String server, boolean rawGps,
    2951                        double lat1, double lon1, double lat2, double lon2) {
     52                this.rawGps = rawGps;
    3053                urlStr = server.endsWith("/") ? server : server+"/";
    31                 urlStr += "map?bbox="+lon1+","+lat1+","+lon2+","+lat2;
     54                urlStr += rawGps?"trackpoints" : "map";
     55                urlStr += "?bbox="+lon1+","+lat1+","+lon2+","+lat2;
     56                if (rawGps)
     57                        urlStr += "&page=";
    3258               
    3359                HttpURLConnection.setFollowRedirects(true);
     
    3561                        @Override
    3662                        protected PasswordAuthentication getPasswordAuthentication() {
     63                                String username = Main.pref.osmDataUsername;
     64                                String password = Main.pref.osmDataPassword;
     65                                if (passwordtried || "".equals(username) || password == null || "".equals(password)) {
     66                                        JPanel p = new JPanel(new GridBagLayout());
     67                                        p.add(new JLabel("Username"), GBC.std().insets(0,0,10,0));
     68                                        JTextField usernameField = new JTextField("".equals(username) ? "" : username, 20);
     69                                        p.add(usernameField, GBC.eol());
     70                                        p.add(new JLabel("Password"), GBC.std().insets(0,0,10,0));
     71                                        JPasswordField passwordField = new JPasswordField(password == null ? "" : password, 20);
     72                                        p.add(passwordField, GBC.eol());
     73                                        JLabel warning = new JLabel("Warning: The password is transferred unencrypted.");
     74                                        warning.setFont(warning.getFont().deriveFont(Font.ITALIC));
     75                                        p.add(warning, GBC.eol());
     76                                        int choice = JOptionPane.showConfirmDialog(Main.main, p, "Enter Password", JOptionPane.OK_CANCEL_OPTION);
     77                                        if (choice == JOptionPane.CANCEL_OPTION) {
     78                                                cancelled = true;
     79                                                return null;
     80                                        }
     81                                        username = usernameField.getText();
     82                                        password = String.valueOf(passwordField.getPassword());
     83                                        if ("".equals(username))
     84                                                return null;
     85                                }
     86                                passwordtried = true;
    3787                                return new PasswordAuthentication(username, password.toCharArray());
    3888                        }
     
    4494                Reader in;
    4595                try {
     96                        if (rawGps) {
     97                                DataSet ds = new DataSet();
     98                                for (int i = 0;;++i) {
     99                                        URL url = new URL(urlStr+i);
     100                                        HttpURLConnection con = (HttpURLConnection)url.openConnection();
     101                                        con.setConnectTimeout(20000);
     102                                        if (con.getResponseCode() == 401 && cancelled)
     103                                                return null;
     104                                        in = new InputStreamReader(con.getInputStream());
     105                                        DataSet currentData = new GpxReader(in, true).parse();
     106                                        if (currentData.nodes.isEmpty())
     107                                                return ds;
     108                                        ds.mergeFrom(currentData, true);
     109                                }
     110                        }
    46111                        URL url = new URL(urlStr);
    47112                        HttpURLConnection con = (HttpURLConnection)url.openConnection();
    48                         con.setDoInput(true);
    49113                        con.setConnectTimeout(20000);
    50                         con.setRequestMethod("GET");
    51                         con.connect();
     114                        if (con.getResponseCode() == 401 && cancelled)
     115                                return null;
    52116                        in = new InputStreamReader(con.getInputStream());
     117                        return new GpxReader(in, false).parse();
    53118                } catch (IOException e) {
    54119                        throw new ConnectionException("Failed to open server connection\n"+e.getMessage(), e);
    55120                }
    56                 GpxReader reader = new GpxReader(in, false);
    57                 return reader.parse();
    58121        }
    59122}
Note: See TracChangeset for help on using the changeset viewer.