Changeset 4307 in osm for applications/editors/josm/plugins
- Timestamp:
- 2007-08-25T03:07:47+02:00 (17 years ago)
- Location:
- applications/editors/josm/plugins/namefinder
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/namefinder/README
r2925 r4307 1 A very simple firstimplementation of a Name Finder that will2 add a "place" tab to the download dialog . This is a "proof of concept"3 intended for others to build on ;-) 1 A very simple implementation of a Name Finder that will 2 add a "place" tab to the download dialog and connect to 3 David Earl's name finder service to get results. 4 4 5 Frederik Ramm <frederik@remote.org> 5 Author: Frederik Ramm <frederik@remote.org> 6 License: Public Domain 6 7 8 (but note, some JOSM components & MinML library are used 9 which may have different licensing.) -
applications/editors/josm/plugins/namefinder/build.xml
r3783 r4307 21 21 <classpath> 22 22 <pathelement path="${josm.prj.dir}/build"/> 23 <pathelement path="${josm.prj.dir}/src"/> 23 24 <fileset dir="${josm.prj.dir}/lib"> 24 25 <include name="**/*.jar"/> … … 34 35 <attribute name="Plugin-Class" value="namefinder.NameFinderPlugin" /> 35 36 <attribute name="Plugin-Description" value="Allows selection of download areas by name, using an external service" /> 37 <attribute name="Plugin-Version" value="0.9" /> 36 38 </manifest> 37 39 </jar> -
applications/editors/josm/plugins/namefinder/namefinder/PlaceSelection.java
r3067 r4307 4 4 5 5 import java.awt.Component; 6 import java.awt.Cursor; 6 7 import java.awt.Dimension; 7 8 import java.awt.GridBagLayout; 8 9 import java.awt.event.ActionEvent; 9 10 import java.awt.event.ActionListener; 11 import java.awt.event.MouseAdapter; 12 import java.awt.event.MouseEvent; 10 13 import java.io.InputStream; 11 14 import java.io.InputStreamReader; … … 13 16 import java.net.URL; 14 17 15 import javax.swing.DefaultListCellRenderer;16 import javax.swing.DefaultListModel;17 18 import javax.swing.JButton; 19 import javax.swing.JComponent; 18 20 import javax.swing.JLabel; 19 import javax.swing.JList;20 21 import javax.swing.JOptionPane; 21 22 import javax.swing.JPanel; 22 23 import javax.swing.JScrollPane; 24 import javax.swing.JTable; 23 25 import javax.swing.JTextField; 26 import javax.swing.ListSelectionModel; 24 27 import javax.swing.event.ListSelectionEvent; 25 28 import javax.swing.event.ListSelectionListener; 29 import javax.swing.table.DefaultTableCellRenderer; 30 import javax.swing.table.DefaultTableModel; 26 31 27 32 import org.openstreetmap.josm.Main; … … 38 43 private JTextField searchTerm = new JTextField(); 39 44 private JButton submitSearch = new JButton(tr("Search...")); 40 private DefaultListModel searchResults = new DefaultListModel(); 41 private JList searchResultDisplay = new JList(searchResults); 45 private DefaultTableModel searchResults = new DefaultTableModel() { 46 @Override public boolean isCellEditable(int row, int col) { return false; } 47 }; 48 private JTable searchResultDisplay = new JTable(searchResults); 42 49 private boolean updatingSelf; 43 50 … … 48 55 { 49 56 public String name; 57 public String type; 58 public String nearestPlace; 50 59 public String description; 51 60 public double lat; … … 75 84 if (qName.equals("searchresults")) 76 85 { 77 searchResults. clear();86 searchResults.setRowCount(0); 78 87 } 79 88 else if (qName.equals("named") && (depth == 2)) … … 81 90 currentResult = new PlaceSelection.SearchResult(); 82 91 currentResult.name = atts.getValue("name"); 92 currentResult.type = atts.getValue("info"); 83 93 currentResult.lat = Double.parseDouble(atts.getValue("lat")); 84 94 currentResult.lon = Double.parseDouble(atts.getValue("lon")); 85 95 currentResult.zoom = Integer.parseInt(atts.getValue("zoom")); 86 searchResults.add Element(currentResult);96 searchResults.addRow(new Object[] { currentResult, currentResult, currentResult, currentResult }); 87 97 } 88 98 else if (qName.equals("description") && (depth == 3)) 89 99 { 90 100 description = new StringBuffer(); 91 } 101 } 102 else if (qName.equals("named") && (depth == 4)) 103 { 104 // this is a "named" place in the nearest places list. 105 String info = atts.getValue("info"); 106 if ("city".equals(info) || "town".equals(info) || "village".equals(info)) { 107 currentResult.nearestPlace = atts.getValue("name"); 108 } 109 } 92 110 } 93 111 catch (NumberFormatException x) … … 135 153 * error handling improved. 136 154 */ 137 public void queryServer( )155 public void queryServer(final JComponent component) 138 156 { 139 try 140 { 141 URL url = new URL("http://www.frankieandshadow.com/osm/search.xml?find="+java.net.URLEncoder.encode(searchTerm.getText(), "UTF-8")); 142 HttpURLConnection activeConnection = (HttpURLConnection)url.openConnection(); 143 System.out.println("got return: "+activeConnection.getResponseCode()); 144 activeConnection.setConnectTimeout(15000); 145 InputStream inputStream = activeConnection.getInputStream(); 146 new Parser().parse(new InputStreamReader(inputStream, "UTF-8")); 147 } 148 catch (Exception x) 149 { 150 JOptionPane.showMessageDialog(Main.parent,tr("Cannot read place search results from server")); 151 x.printStackTrace(); 152 } 153 } 154 155 // add a new tab to the download dialog 157 final Cursor oldCursor = component.getCursor(); 158 159 // had to put this in a thread as it wouldn't update the cursor properly before. 160 Runnable r = new Runnable() { 161 public void run() { 162 try 163 { 164 component.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 165 component.repaint(); 166 URL url = new URL("http://www.frankieandshadow.com/osm/search.xml?find="+java.net.URLEncoder.encode(searchTerm.getText(), "UTF-8")); 167 HttpURLConnection activeConnection = (HttpURLConnection)url.openConnection(); 168 System.out.println("got return: "+activeConnection.getResponseCode()); 169 activeConnection.setConnectTimeout(15000); 170 InputStream inputStream = activeConnection.getInputStream(); 171 new Parser().parse(new InputStreamReader(inputStream, "UTF-8")); 172 } 173 catch (Exception x) 174 { 175 JOptionPane.showMessageDialog(Main.parent,tr("Cannot read place search results from server")); 176 x.printStackTrace(); 177 } 178 component.setCursor(oldCursor); 179 } 180 }; 181 new Thread(r).start(); 182 } 183 184 /** 185 * Adds a new tab to the download dialog in JOSM. 186 * 187 * This method is, for all intents and purposes, the constructor for this class. 188 */ 156 189 public void addGui(final DownloadDialog gui) { 157 190 JPanel panel = new JPanel(); 158 191 panel.setLayout(new GridBagLayout()); 159 panel.add(new JLabel(tr("Enter a place name to search for:")), GBC.eol()); 160 panel.add(searchTerm, GBC.std().fill(GBC.HORIZONTAL)); 161 panel.add(submitSearch, GBC.eol()); 162 163 GBC c = GBC.std().fill(); 192 193 // this is manually tuned so that it looks nice on a GNOME 194 // desktop - maybe needs some cross platform proofing. 195 panel.add(new JLabel(tr("Enter a place name to search for:")), GBC.eol().insets(5, 5, 5, 5)); 196 panel.add(searchTerm, GBC.std().fill(GBC.BOTH).insets(5, 0, 5, 4)); 197 panel.add(submitSearch, GBC.eol().insets(5, 0, 5, 5)); 198 Dimension btnSize = submitSearch.getPreferredSize(); 199 btnSize.setSize(btnSize.width, btnSize.height * 0.8); 200 submitSearch.setPreferredSize(btnSize); 201 202 GBC c = GBC.std().fill().insets(5, 0, 5, 5); 203 c.gridwidth = 2; 164 204 JScrollPane scrollPane = new JScrollPane(searchResultDisplay); 205 scrollPane.setPreferredSize(new Dimension(200,200)); 165 206 panel.add(scrollPane, c); 166 207 gui.tabpane.add(panel, tr("Places")); … … 171 212 submitSearch.addActionListener(new ActionListener() { 172 213 public void actionPerformed(ActionEvent e) { 173 queryServer(); 174 } 175 }); 176 177 // display search results in list just by name, and add tooltip 178 // for description. would also be possible to use a table model 179 // instead of list, and display lat/lon etc. 180 searchResultDisplay.setCellRenderer(new DefaultListCellRenderer() { 181 @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 182 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 214 queryServer(gui); 215 } 216 }); 217 218 searchTerm.addActionListener(new ActionListener() { 219 public void actionPerformed(ActionEvent e) { 220 queryServer(gui); 221 } 222 }); 223 224 searchResults.addColumn("name"); 225 searchResults.addColumn("type"); 226 searchResults.addColumn("near"); 227 searchResults.addColumn("zoom"); 228 229 // TODO - this is probably not the coolest way to set relative sizes? 230 searchResultDisplay.getColumn("name").setPreferredWidth(200); 231 searchResultDisplay.getColumn("type").setPreferredWidth(100); 232 searchResultDisplay.getColumn("near").setPreferredWidth(100); 233 searchResultDisplay.getColumn("zoom").setPreferredWidth(50); 234 searchResultDisplay.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 235 236 // display search results in a table. for simplicity, the table contains 237 // the same SearchResult object in each of the four columns, but it is rendered 238 // differently depending on the column. 239 searchResultDisplay.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() { 240 @Override public Component getTableCellRendererComponent(JTable table, Object value, 241 boolean isSelected, boolean hasFocus, int row, int column) { 242 super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 183 243 if (value != null) { 184 setText(((SearchResult)value).name); 244 SearchResult sr = (SearchResult) value; 245 switch(column) { 246 case 0: 247 setText(sr.name); 248 break; 249 case 1: 250 setText(sr.type); 251 break; 252 case 2: 253 setText(sr.nearestPlace); 254 break; 255 case 3: 256 setText(Integer.toString(sr.zoom)); 257 break; 258 } 185 259 setToolTipText("<html>"+((SearchResult)value).description+"</html>"); 186 260 } … … 190 264 191 265 // if item is selected in list, notify dialog 192 //searchResultDisplay.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 193 searchResultDisplay.addListSelectionListener(new ListSelectionListener() { 266 searchResultDisplay.getSelectionModel().addListSelectionListener(new ListSelectionListener() { 194 267 public void valueChanged(ListSelectionEvent lse) { 195 268 if (lse.getValueIsAdjusting()) return; … … 197 270 try 198 271 { 199 r = (SearchResult) searchResults.get ElementAt(lse.getFirstIndex());272 r = (SearchResult) searchResults.getValueAt(lse.getFirstIndex(), 0); 200 273 } 201 274 catch (Exception x) … … 216 289 } 217 290 }); 291 292 // TODO - we'd like to finish the download dialog upon double-click but 293 // don't know how to bypass the JOptionPane in which the whole thing is 294 // displayed. 295 searchResultDisplay.addMouseListener(new MouseAdapter() { 296 @Override public void mouseClicked(MouseEvent e) { 297 if (e.getClickCount() > 1) { 298 if (searchResultDisplay.getSelectionModel().getMinSelectionIndex() > -1) { 299 // add sensible action here. 300 } 301 } 302 } 303 }); 304 218 305 } 219 306
Note:
See TracChangeset
for help on using the changeset viewer.