Changeset 28050 in osm for applications/editors/josm/plugins/opendata/src
- Timestamp:
- 2012-03-11T21:59:38+01:00 (13 years ago)
- Location:
- applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata
- Files:
-
- 1 added
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/OdPlugin.java
r28044 r28050 19 19 import static org.openstreetmap.josm.tools.I18n.marktr; 20 20 21 import java.awt.Toolkit; 21 22 import java.awt.event.KeyEvent; 22 23 import java.io.File; 24 import java.net.URL; 23 25 import java.util.Arrays; 24 26 import java.util.HashMap; … … 27 29 28 30 import javax.swing.JMenu; 31 import javax.swing.JMenuItem; 29 32 30 33 import org.openstreetmap.josm.Main; … … 32 35 import org.openstreetmap.josm.gui.MainMenu; 33 36 import org.openstreetmap.josm.gui.MapFrame; 37 import org.openstreetmap.josm.gui.MenuScroller; 34 38 import org.openstreetmap.josm.gui.preferences.PreferenceSetting; 35 39 import org.openstreetmap.josm.plugins.Plugin; … … 55 59 import org.openstreetmap.josm.plugins.opendata.core.modules.ModuleHandler; 56 60 import org.openstreetmap.josm.plugins.opendata.core.modules.ModuleInformation; 61 import org.openstreetmap.josm.tools.Pair; 57 62 58 63 public final class OdPlugin extends Plugin implements OdConstants { … … 93 98 } 94 99 100 private JMenu getModuleMenu(Module module) { 101 String moduleName = module.getDisplayedName(); 102 if (moduleName == null || moduleName.isEmpty()) { 103 moduleName = module.getModuleInformation().getName(); 104 } 105 JMenu moduleMenu = new JMenu(moduleName); 106 moduleMenu.setIcon(module.getModuleInformation().getScaledIcon()); 107 return moduleMenu; 108 } 109 95 110 private void buildMenu() { 111 int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height; 96 112 for (Module module : ModuleHandler.moduleList) { 97 113 Map<DataSetCategory, JMenu> catMenus = new HashMap<DataSetCategory, JMenu>(); 98 114 JMenu moduleMenu = null; 99 115 for (AbstractDataSetHandler handler: module.getHandlers()) { 100 if (handler.getDataURL() != null) { 116 if (handler.getDataURL() != null || (handler.getDataURLs() != null && !handler.getDataURLs().isEmpty())) { 101 117 if (moduleMenu == null) { 102 String moduleName = module.getDisplayedName(); 103 if (moduleName == null || moduleName.isEmpty()) { 104 moduleName = module.getModuleInformation().getName(); 105 } 106 moduleMenu = new JMenu(moduleName); 107 moduleMenu.setIcon(module.getModuleInformation().getScaledIcon()); 118 moduleMenu = getModuleMenu(module); 108 119 } 109 120 DataSetCategory cat = handler.getCategory(); … … 118 129 endMenu = moduleMenu; 119 130 } 120 endMenu.add(new DownloadDataAction(handler)); 131 String handlerName = handler.getName(); 132 if (handlerName == null || handlerName.isEmpty()) { 133 handlerName = handler.getClass().getName(); 134 } 135 if (handler.getDataURL() != null) { 136 endMenu.add(new DownloadDataAction(handlerName, handler.getDataURL())); 137 } else if (handler.getDataURLs() != null) { 138 JMenu handlerMenu = new JMenu(handlerName); 139 JMenuItem item = null; 140 for (Pair<String, URL> pair : handler.getDataURLs()) { 141 if (pair != null && pair.a != null && pair.b != null) { 142 item = handlerMenu.add(new DownloadDataAction(pair.a, pair.b)); 143 } 144 } 145 if (item != null) { 146 MenuScroller.setScrollerFor(handlerMenu, (screenHeight / item.getPreferredSize().height)-3); 147 endMenu.add(handlerMenu); 148 } 149 } 121 150 } 122 151 } 123 152 if (moduleMenu != null) { 153 //MenuScroller.setScrollerFor(moduleMenu, screenHeight / moduleMenu.getItem(0).getPreferredSize().height); 124 154 menu.add(moduleMenu); 125 155 } 126 156 } 127 157 menu.addSeparator(); 128 MainMenu.add(menu, new OpenPreferencesActions()); 158 /*JMenuItem itemIcon =*/ MainMenu.add(menu, new OpenPreferencesActions()); 159 //MenuScroller.setScrollerFor(menu, screenHeight / itemIcon.getPreferredSize().height); 129 160 } 130 161 -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/OdConstants.java
r28033 r28050 126 126 public static final String Y_STRING = "Y|LAT|LATI|LATITUDE|NORTHING"; 127 127 128 // The list of all ProjectionPatterns (filled at each constr cutor call)128 // The list of all ProjectionPatterns (filled at each constructor call) 129 129 public static final Collection<ProjectionPatterns> PROJECTIONS = new ArrayList<ProjectionPatterns>(); 130 130 -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/actions/DownloadDataAction.java
r28044 r28050 17 17 18 18 import java.awt.event.ActionEvent; 19 import java.net.URL; 19 20 20 21 import javax.swing.Action; … … 22 23 import org.openstreetmap.josm.Main; 23 24 import org.openstreetmap.josm.actions.JosmAction; 24 import org.openstreetmap.josm.plugins.opendata.core.datasets.AbstractDataSetHandler;25 25 import org.openstreetmap.josm.tools.CheckParameterUtil; 26 26 27 27 public class DownloadDataAction extends JosmAction { 28 28 29 private final AbstractDataSetHandler handler;29 private final URL url; 30 30 31 public DownloadDataAction(AbstractDataSetHandler handler) { 32 CheckParameterUtil.ensureParameterNotNull(handler, "handler"); 33 this.handler = handler; 34 String name = handler.getName(); 35 if (name == null || name.isEmpty()) { 36 name = handler.getClass().getName(); 37 } 31 public DownloadDataAction(String name, URL url) { 32 super(false); 33 CheckParameterUtil.ensureParameterNotNull(name, "name"); 34 CheckParameterUtil.ensureParameterNotNull(url, "url"); 38 35 putValue(Action.NAME, name); 36 putValue("toolbar", "opendata_download_"+name.toLowerCase().replace(" ", "_")); 37 this.url = url; 39 38 } 40 39 41 40 @Override 42 41 public void actionPerformed(ActionEvent e) { 43 Main.main.menu.openLocation.openUrl(true, handler.getDataURL().toString());42 Main.main.menu.openLocation.openUrl(true, url.toString()); 44 43 } 45 44 } -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/actions/DownloadDataTask.java
r28044 r28050 56 56 for (Module module : ModuleHandler.moduleList) { 57 57 for (AbstractDataSetHandler handler : module.getHandlers()) { 58 if (handler != null && handler.getDataURL() != null && url.equals(handler.getDataURL().toString())) {58 if (handler.acceptsUrl(url)) { 59 59 this.handler = handler; 60 60 return true; … … 77 77 protected OsmDataLayer createNewLayer(String layerName) { 78 78 File associatedFile = ((NetworkReader)reader).getReadFile(); 79 String filename = ((NetworkReader)reader).getReadFileName(); 79 80 if (layerName == null || layerName.isEmpty()) { 80 layerName = associatedFile == null ? OsmDataLayer.createNewName() : associatedFile.getName(); 81 if (associatedFile != null) { 82 layerName = associatedFile.getName(); 83 } else if (filename != null && !filename.isEmpty()) { 84 layerName = filename; 85 } else { 86 layerName = OsmDataLayer.createNewName(); 87 } 81 88 } 82 89 DataSetUpdater.updateDataSet(dataSet, handler, associatedFile); -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/actions/OpenPreferencesActions.java
r28044 r28050 30 30 31 31 public OpenPreferencesActions() { 32 super(false); 32 33 putValue(NAME, tr("Preferences")); 33 34 putValue(SMALL_ICON, ImageProvider.get("dialogs", ICON_CORE_24)); 35 putValue("toolbar", "opendata_open_preferences"); 34 36 } 35 37 -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/datasets/AbstractDataSetHandler.java
r28044 r28050 17 17 18 18 import java.io.File; 19 import java.net.MalformedURLException; 19 20 import java.net.URL; 20 21 import java.nio.charset.Charset; … … 97 98 private String sourceDate; 98 99 private File associatedFile; 100 private URL dataURL; 99 101 100 102 public AbstractDataSetHandler() { … … 179 181 public URL getLicenseURL() {return null;} 180 182 181 public URL getDataURL() {return null;} 182 183 public URL getDataURL() {return dataURL;} 184 185 public final void setDataURL(String url) throws MalformedURLException { 186 this.dataURL = new URL(url); 187 } 188 189 public final void setDataURL(URL url) { 190 this.dataURL = url; 191 } 192 193 public List<Pair<String,URL>> getDataURLs() {return null;} 194 183 195 public AbstractReader getReaderForUrl(String url) {return null;} 184 196 … … 522 534 return false; 523 535 } 536 537 public boolean acceptsUrl(String url) { 538 if (getDataURL() != null && url.equals(getDataURL().toString())) { 539 return true; 540 } 541 if (getDataURLs() != null) { 542 for (Pair<String, URL> pair : getDataURLs()) { 543 if (pair.b != null && url.equals(pair.b.toString())) { 544 return true; 545 } 546 } 547 } 548 return false; 549 } 524 550 } -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/datasets/fr/FrenchConstants.java
r28018 r28050 23 23 * Portal 24 24 */ 25 public static final String FRENCH_PORTAL = "http://www.data.gouv.fr/ donnees/view/";25 public static final String FRENCH_PORTAL = "http://www.data.gouv.fr/"; 26 26 27 27 /** -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/datasets/fr/FrenchDataSetHandler.java
r28031 r28050 98 98 try { 99 99 if (nationalPortalPath != null && !nationalPortalPath.isEmpty()) { 100 return new URL(FRENCH_PORTAL + nationalPortalPath); 100 return new URL(FRENCH_PORTAL + "donnees/view/" + nationalPortalPath); 101 101 } 102 102 } catch (MalformedURLException e) { -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/NetworkReader.java
r28044 r28050 20 20 import java.io.File; 21 21 import java.io.InputStream; 22 import java.util.regex.Matcher; 23 import java.util.regex.Pattern; 22 24 23 25 import org.openstreetmap.josm.data.osm.DataSet; … … 26 28 import org.openstreetmap.josm.io.OsmServerReader; 27 29 import org.openstreetmap.josm.io.OsmTransferException; 30 import org.openstreetmap.josm.plugins.opendata.core.OdConstants; 28 31 import org.openstreetmap.josm.plugins.opendata.core.datasets.AbstractDataSetHandler; 29 32 import org.openstreetmap.josm.plugins.opendata.core.io.archive.ZipReader; … … 38 41 import org.openstreetmap.josm.tools.CheckParameterUtil; 39 42 40 public class NetworkReader extends OsmServerReader { 43 public class NetworkReader extends OsmServerReader implements OdConstants { 41 44 42 45 private final String url; … … 45 48 46 49 private File file; 50 private String filename; 47 51 48 52 public NetworkReader(String url, AbstractDataSetHandler handler, Class<? extends AbstractReader> readerClass) { … … 58 62 } 59 63 64 public final String getReadFileName() { 65 return filename; 66 } 67 68 private Class<? extends AbstractReader> findReaderByAttachment() { 69 String cdisp = this.activeConnection.getHeaderField("Content-disposition"); 70 if (cdisp != null) { 71 Matcher m = Pattern.compile("attachment; filename=(.*)").matcher(cdisp); 72 if (m.matches()) { 73 filename = m.group(1); 74 return findReaderByExtension(filename.toLowerCase()); 75 } 76 } 77 return null; 78 } 79 80 private Class<? extends AbstractReader> findReaderByContentType() { 81 String contentType = this.activeConnection.getContentType(); 82 if (contentType.startsWith("application/zip")) { 83 return ZipReader.class; 84 } else if (contentType.startsWith("application/vnd.ms-excel")) { 85 return XlsReader.class; 86 } else if (contentType.startsWith("application/octet-stream")) { 87 //return OdsReader.class;//FIXME, can be anything 88 } else if (contentType.startsWith("text/plain")) {//TODO: extract charset 89 return CsvReader.class; 90 } else if (contentType.startsWith("tdyn/html")) { 91 //return CsvReader.class;//FIXME, can also be .tar.gz 92 } else { 93 System.err.println("Unsupported content type: "+contentType); 94 } 95 return null; 96 } 97 98 private Class<? extends AbstractReader> findReaderByExtension(String filename) { 99 if (filename.endsWith("."+XLS_EXT)) { 100 return XlsReader.class; 101 } else if (filename.endsWith("."+CSV_EXT)) { 102 return CsvReader.class; 103 } else if (filename.endsWith("."+ODS_EXT)) { 104 return OdsReader.class; 105 } else if (filename.endsWith("."+KML_EXT)) { 106 return KmlReader.class; 107 } else if (filename.endsWith("."+KMZ_EXT)) { 108 return KmzReader.class; 109 } else if (filename.endsWith("."+MIF_EXT)) { 110 return MifReader.class; 111 } else if (filename.endsWith("."+SHP_EXT)) { 112 return ShpReader.class; 113 } else if (filename.endsWith("."+TAB_EXT)) { 114 return TabReader.class; 115 } else if (filename.endsWith("."+ZIP_EXT)) { 116 return ZipReader.class; 117 } else { 118 return null; 119 } 120 } 121 60 122 @Override 61 123 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException { … … 69 131 ProgressMonitor instance = progressMonitor.createSubTaskMonitor(1, false); 70 132 if (readerClass == null) { 71 String contentType = this.activeConnection.getContentType(); 72 if (contentType.startsWith("application/zip")) { 73 readerClass = ZipReader.class; 74 } else { 75 throw new IllegalArgumentException("Unsupported content type: "+contentType); 133 readerClass = findReaderByAttachment(); 134 } 135 if (readerClass == null) { 136 readerClass = findReaderByContentType(); 137 } 138 if (readerClass == null) { 139 readerClass = findReaderByExtension(url.toLowerCase()); 140 if (readerClass != null) { 141 filename = url.substring(url.lastIndexOf('/')); 76 142 } 143 } 144 if (readerClass == null) { 145 throw new OsmTransferException("Cannot find appropriate reader !");//TODO handler job ? 77 146 } 78 147 if (readerClass.equals(ZipReader.class)) { -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/ProjectionPatterns.java
r28000 r28050 63 63 64 64 public static final Pattern getCoordinatePattern(String coor, String proj) { 65 return Pattern.compile("(?:.*(?:"+coor+").*(?:"+proj+").*)|(?:.*("+proj+").*(?:"+coor+").*)", Pattern.CASE_INSENSITIVE); 65 if (proj != null && !proj.isEmpty()) { 66 return Pattern.compile("(?:.*(?:"+coor+").*(?:"+proj+").*)|(?:.*("+proj+").*(?:"+coor+").*)", Pattern.CASE_INSENSITIVE); 67 } else { 68 return Pattern.compile(coor, Pattern.CASE_INSENSITIVE); 69 } 66 70 } 67 71 } -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/archive/ZipReader.java
r28044 r28050 96 96 while ((entry = zis.getNextEntry()) != null) { 97 97 File file = new File(temp + File.separator + entry.getName()); 98 File parent = file.getParentFile(); 99 if (parent != null && !parent.exists()) { 100 parent.mkdirs(); 101 } 98 102 if (file.exists() && !file.delete()) { 99 103 throw new IOException("Could not delete temp file/dir: " + file.getAbsolutePath());
Note:
See TracChangeset
for help on using the changeset viewer.