Changeset 11279 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2016-11-18T11:57:20+01:00 (8 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/OpenLocationAction.java
r10630 r11279 5 5 import static org.openstreetmap.josm.tools.I18n.tr; 6 6 7 import java.awt.GridBagConstraints;8 7 import java.awt.GridBagLayout; 9 8 import java.awt.GridLayout; … … 15 14 import java.util.LinkedList; 16 15 import java.util.List; 16 import java.util.Objects; 17 17 import java.util.concurrent.Future; 18 import java.util.stream.Collectors; 18 19 19 20 import javax.swing.JCheckBox; … … 38 39 import org.openstreetmap.josm.actions.downloadtasks.DownloadTask; 39 40 import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler; 41 import org.openstreetmap.josm.data.preferences.BooleanProperty; 40 42 import org.openstreetmap.josm.gui.ExtendedDialog; 41 43 import org.openstreetmap.josm.gui.HelpAwareOptionPane; 42 44 import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor; 43 45 import org.openstreetmap.josm.gui.widgets.HistoryComboBox; 46 import org.openstreetmap.josm.tools.GBC; 44 47 import org.openstreetmap.josm.tools.Shortcut; 45 48 import org.openstreetmap.josm.tools.Utils; … … 51 54 */ 52 55 public class OpenLocationAction extends JosmAction { 53 56 /** 57 * true if the URL needs to be opened in a new layer, false otherwise 58 */ 59 private static final BooleanProperty USE_NEW_LAYER = new BooleanProperty("download.newlayer", true); 54 60 protected final transient List<Class<? extends DownloadTask>> downloadTasks; 55 61 … … 103 109 @Override 104 110 public void actionPerformed(ActionEvent e) { 105 106 JCheckBox layer = new JCheckBox(tr("Separate Layer"));107 layer.setToolTipText(tr("Select if the data should be downloaded into a new layer"));108 layer.setSelected(Main.pref.getBoolean("download.newlayer"));109 111 JPanel all = new JPanel(new GridBagLayout()); 110 GridBagConstraints gc = new GridBagConstraints(); 111 gc.fill = GridBagConstraints.HORIZONTAL; 112 gc.weightx = 1.0; 113 gc.anchor = GridBagConstraints.FIRST_LINE_START; 114 all.add(new JLabel(tr("Enter URL to download:")), gc); 112 113 // download URL selection 114 all.add(new JLabel(tr("Enter URL to download:")), GBC.eol()); 115 115 HistoryComboBox uploadAddresses = new HistoryComboBox(); 116 116 uploadAddresses.setToolTipText(tr("Enter an URL from where data should be downloaded")); 117 117 restoreUploadAddressHistory(uploadAddresses); 118 gc.gridy = 1; 119 all.add(uploadAddresses, gc); 120 gc.gridy = 2; 121 gc.fill = GridBagConstraints.BOTH; 122 gc.weighty = 1.0; 123 all.add(layer, gc); 118 all.add(uploadAddresses, GBC.eop().fill(GBC.BOTH)); 119 120 // use separate layer 121 JCheckBox layer = new JCheckBox(tr("Separate Layer")); 122 layer.setToolTipText(tr("Select if the data should be downloaded into a new layer")); 123 layer.setSelected(USE_NEW_LAYER.get()); 124 all.add(layer, GBC.eop().fill(GBC.BOTH)); 125 124 126 ExtendedDialog dialog = new ExtendedDialog(Main.parent, 125 127 tr("Download Location"), … … 134 136 dialog.configureContextsensitiveHelp("/Action/OpenLocation", true /* show help button */); 135 137 dialog.showDialog(); 136 if (dialog.getValue() != 1) return; 137 remindUploadAddressHistory(uploadAddresses); 138 openUrl(layer.isSelected(), Utils.strip(uploadAddresses.getText())); 138 if (dialog.getValue() == 1) { 139 USE_NEW_LAYER.put(layer.isSelected()); 140 remindUploadAddressHistory(uploadAddresses); 141 openUrl(Utils.strip(uploadAddresses.getText())); 142 } 139 143 } 140 144 … … 147 151 */ 148 152 public Collection<DownloadTask> findDownloadTasks(final String url, boolean isRemotecontrol) { 149 List<DownloadTask> result = new ArrayList<>(); 150 for (Class<? extends DownloadTask> taskClass : downloadTasks) { 151 if (taskClass != null) { 152 try { 153 DownloadTask task = taskClass.getConstructor().newInstance(); 154 if (task.acceptsUrl(url, isRemotecontrol)) { 155 result.add(task); 153 return downloadTasks.stream() 154 .filter(Objects::nonNull) 155 .map(taskClass -> { 156 try { 157 return taskClass.getConstructor().newInstance(); 158 } catch (ReflectiveOperationException e) { 159 Main.error(e); 160 return null; 156 161 } 157 } catch (ReflectiveOperationException e) { 158 Main.error(e); 159 } 160 } 161 } 162 return result; 162 }) 163 .filter(Objects::nonNull) 164 .filter(task -> task.acceptsUrl(url, isRemotecontrol)) 165 .collect(Collectors.toList()); 163 166 } 164 167 … … 189 192 * @param url The URL to open 190 193 */ 191 public void openUrl(boolean newLayer, final String url) { 194 public void openUrl(boolean newLayer, String url) { 195 realOpenUrl(newLayer, url); 196 } 197 198 /** 199 * Open the given URL. This class checks the {@link #USE_NEW_LAYER} preference to check if a new layer should be used. 200 * @param url The URL to open 201 * @return <code>true</code> if loading the task was started successfully. 202 */ 203 public boolean openUrl(String url) { 204 return realOpenUrl(USE_NEW_LAYER.get(), url); 205 } 206 207 private boolean realOpenUrl(boolean newLayer, String url) { 192 208 Collection<DownloadTask> tasks = findDownloadTasks(url, false); 193 209 … … 196 212 } else if (tasks.isEmpty()) { 197 213 warnNoSuitableTasks(url); 198 return ;214 return false; 199 215 } 200 216 … … 205 221 Future<?> future = task.loadUrl(newLayer, url, monitor); 206 222 Main.worker.submit(new PostDownloadHandler(task, future)); 223 return true; 207 224 } catch (IllegalArgumentException e) { 208 225 Main.error(e); 209 226 } 210 227 } 228 return false; 211 229 } 212 230 … … 235 253 * @param url the given url 236 254 */ 237 void warnNoSuitableTasks(final String url) {255 protected void warnNoSuitableTasks(final String url) { 238 256 final String details = findSummaryDocumentation(); // Explain what patterns are supported 239 257 HelpAwareOptionPane.showMessageDialogInEDT(Main.parent, "<html><p>" + tr( -
trunk/src/org/openstreetmap/josm/gui/datatransfer/importers/OsmLinkPaster.java
r10881 r11279 6 6 import java.awt.datatransfer.UnsupportedFlavorException; 7 7 import java.io.IOException; 8 import java.util.Collections;9 import java.util.List;10 8 import java.util.regex.Matcher; 11 9 import java.util.regex.Pattern; … … 13 11 import javax.swing.TransferHandler.TransferSupport; 14 12 15 import org.openstreetmap.josm. Main;13 import org.openstreetmap.josm.actions.OpenLocationAction; 16 14 import org.openstreetmap.josm.data.coor.EastNorth; 17 15 import org.openstreetmap.josm.data.coor.LatLon; 18 import org.openstreetmap.josm.data.osm.OsmPrimitiveType;19 import org.openstreetmap.josm.data.osm.PrimitiveId;20 import org.openstreetmap.josm.data.osm.SimplePrimitiveId;21 16 import org.openstreetmap.josm.data.preferences.BooleanProperty; 22 17 import org.openstreetmap.josm.gui.MapView; 23 import org.openstreetmap.josm.gui.io.DownloadPrimitivesWithReferrersTask;24 18 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 25 19 … … 52 46 53 47 String transferData = (String) support.getTransferable().getTransferData(df); 54 List<PrimitiveId> ids = parseIds(transferData); 55 56 if (!ids.isEmpty()) { 57 Main.worker.submit(new DownloadPrimitivesWithReferrersTask(layer == null, 58 ids, PASTE_REFERRERS.get(), PASTE_REFERRERS.get(), null, null)); 48 OpenLocationAction action = new OpenLocationAction() { 49 @Override 50 protected void warnNoSuitableTasks(String url) { 51 // ignore this. 52 } 53 }; 54 if (action.openUrl(transferData)) { 59 55 return true; 60 56 } … … 82 78 } 83 79 } 84 85 static List<PrimitiveId> parseIds(String transferData) {86 Matcher matcher = Pattern87 .compile(OSM_SERVER + "(?<type>node|way|relation)/(?<id>\\d+)(\\/.*)?$")88 .matcher(transferData);89 90 if (!matcher.matches()) {91 return Collections.emptyList();92 }93 94 OsmPrimitiveType type;95 switch (matcher.group("type")) {96 case "way":97 type = OsmPrimitiveType.WAY;98 break;99 100 case "node":101 type = OsmPrimitiveType.NODE;102 break;103 104 case "relation":105 type = OsmPrimitiveType.RELATION;106 break;107 108 default:109 throw new AssertionError();110 }111 112 return Collections.singletonList(new SimplePrimitiveId(Long.parseLong(matcher.group("id")), type));113 }114 80 }
Note:
See TracChangeset
for help on using the changeset viewer.