Ticket #4609: 4609.v2.patch
| File 4609.v2.patch, 20.9 KB (added by , 15 years ago) |
|---|
-
src/org/openstreetmap/josm/actions/CopyAction.java
diff --git a/src/org/openstreetmap/josm/actions/CopyAction.java b/src/org/openstreetmap/josm/actions/CopyAction.java index 9cb0960..90190f5 100644
a b package org.openstreetmap.josm.actions; 5 5 import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 6 6 import static org.openstreetmap.josm.tools.I18n.tr; 7 7 8 import java.awt.Toolkit;9 import java.awt.datatransfer.Clipboard;10 import java.awt.datatransfer.ClipboardOwner;11 import java.awt.datatransfer.StringSelection;12 import java.awt.datatransfer.Transferable;13 8 import java.awt.event.ActionEvent; 14 9 import java.awt.event.KeyEvent; 15 10 import java.util.Collection; … … import org.openstreetmap.josm.Main; 20 15 import org.openstreetmap.josm.data.osm.OsmPrimitive; 21 16 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 22 17 import org.openstreetmap.josm.tools.Shortcut; 18 import org.openstreetmap.josm.tools.Utils; 23 19 24 20 public final class CopyAction extends JosmAction { 25 21 … … public final class CopyAction extends JosmAction { 30 26 putValue("help", ht("/Action/Copy")); 31 27 } 32 28 29 @Override 33 30 public void actionPerformed(ActionEvent e) { 34 31 if(isEmptySelection()) return; 35 32 Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected(); … … public final class CopyAction extends JosmAction { 41 38 /* copy ids to the clipboard */ 42 39 StringBuilder idsBuilder = new StringBuilder(); 43 40 for (OsmPrimitive p : primitives) { 44 idsBuilder.append(p.getId() +",");41 idsBuilder.append(p.getId()).append(","); 45 42 } 46 43 String ids = idsBuilder.substring(0, idsBuilder.length() - 1); 47 try { 48 Toolkit.getDefaultToolkit().getSystemClipboard().setContents( 49 new StringSelection(ids.toString()), new ClipboardOwner() { 50 public void lostOwnership(Clipboard clipboard, Transferable contents) {} 51 } 52 ); 53 } 54 catch (RuntimeException x) {} 44 Utils.copyToClipboard(ids); 55 45 56 46 Main.pasteBuffer.makeCopy(primitives); 57 47 Main.pasteSource = source; -
new file src/org/openstreetmap/josm/actions/CopyCoordinatesAction.java
diff --git a/src/org/openstreetmap/josm/actions/CopyCoordinatesAction.java b/src/org/openstreetmap/josm/actions/CopyCoordinatesAction.java new file mode 100644 index 0000000..6fa9447
- + 1 package org.openstreetmap.josm.actions; 2 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 5 import java.awt.event.ActionEvent; 6 import java.util.Collection; 7 import java.util.Collections; 8 import org.openstreetmap.josm.data.osm.Node; 9 import org.openstreetmap.josm.data.osm.OsmPrimitive; 10 import org.openstreetmap.josm.tools.Utils; 11 12 public class CopyCoordinatesAction extends JosmAction { 13 14 public CopyCoordinatesAction() { 15 super(tr("Copy Coordinates"), null, 16 tr("Copy coordinates of selected nodes to clipboard."), null, false); 17 putValue("toolbar", "copy/coordinates"); 18 } 19 20 @Override 21 public void actionPerformed(ActionEvent ae) { 22 StringBuilder s = new StringBuilder(); 23 for (Node n : getSelectedNodes()) { 24 s.append(n.getCoor().lat()); 25 s.append(", "); 26 s.append(n.getCoor().lon()); 27 s.append("\n"); 28 } 29 Utils.copyToClipboard(s.toString().trim()); 30 } 31 32 @Override 33 protected void updateEnabledState() { 34 setEnabled(!getSelectedNodes().isEmpty()); 35 } 36 37 @Override 38 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { 39 updateEnabledState(); 40 } 41 42 private Collection<Node> getSelectedNodes() { 43 if (getCurrentDataSet() == null || getCurrentDataSet().getSelected() == null) { 44 return Collections.emptyList(); 45 } else { 46 return Utils.filteredCollection(getCurrentDataSet().getSelected(), Node.class); 47 } 48 } 49 } -
src/org/openstreetmap/josm/actions/Map_Rectifier_WMSmenuAction.java
diff --git a/src/org/openstreetmap/josm/actions/Map_Rectifier_WMSmenuAction.java b/src/org/openstreetmap/josm/actions/Map_Rectifier_WMSmenuAction.java index d4a308b..c964d2d 100644
a b import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 6 import java.awt.GridBagConstraints; 7 7 import java.awt.GridBagLayout; 8 import java.awt.Toolkit;9 import java.awt.datatransfer.DataFlavor;10 import java.awt.datatransfer.Transferable;11 8 import java.awt.event.ActionEvent; 12 9 import java.awt.event.KeyEvent; 13 10 import java.util.ArrayList; … … import org.openstreetmap.josm.gui.layer.WMSLayer; 28 25 import org.openstreetmap.josm.tools.GBC; 29 26 import org.openstreetmap.josm.tools.Shortcut; 30 27 import org.openstreetmap.josm.tools.UrlLabel; 28 import org.openstreetmap.josm.tools.Utils; 31 29 32 30 public class Map_Rectifier_WMSmenuAction extends JosmAction { 33 31 /** … … public class Map_Rectifier_WMSmenuAction extends JosmAction { 117 115 118 116 JTextField tfWmsUrl = new JTextField(30); 119 117 120 String clip = getClipboardContents(); 118 String clip = Utils.getClipboardContent(); 119 clip = clip == null ? "" : clip.trim(); 121 120 ButtonGroup group = new ButtonGroup(); 122 121 123 122 JRadioButton firstBtn = null; … … public class Map_Rectifier_WMSmenuAction extends JosmAction { 223 222 Main.main.addLayer(new WMSLayer(new ImageryInfo(title, url))); 224 223 } 225 224 226 /**227 * Helper function that extracts a String from the Clipboard if available.228 * Returns an empty String otherwise229 * @return String Clipboard contents if available230 */231 private String getClipboardContents() {232 String result = "";233 Transferable contents = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);234 235 if(contents == null || !contents.isDataFlavorSupported(DataFlavor.stringFlavor))236 return "";237 238 try {239 result = (String)contents.getTransferData(DataFlavor.stringFlavor);240 } catch(Exception ex) {241 return "";242 }243 return result.trim();244 }245 246 225 @Override 247 226 protected void updateEnabledState() { 248 227 setEnabled(Main.map != null && Main.map.mapView != null && !Main.map.mapView.getAllLayers().isEmpty()); -
src/org/openstreetmap/josm/actions/ShowStatusReportAction.java
diff --git a/src/org/openstreetmap/josm/actions/ShowStatusReportAction.java b/src/org/openstreetmap/josm/actions/ShowStatusReportAction.java index cf80ac6..50b3b60 100644
a b import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 5 5 import static org.openstreetmap.josm.tools.I18n.tr; 6 6 7 7 import java.awt.Dimension; 8 import java.awt.Toolkit;9 import java.awt.datatransfer.Clipboard;10 import java.awt.datatransfer.ClipboardOwner;11 import java.awt.datatransfer.StringSelection;12 import java.awt.datatransfer.Transferable;13 8 import java.awt.event.ActionEvent; 14 9 import java.awt.event.KeyEvent; 15 10 import java.io.BufferedReader; … … import org.openstreetmap.josm.data.osm.DatasetConsistencyTest; 26 21 import org.openstreetmap.josm.gui.ExtendedDialog; 27 22 import org.openstreetmap.josm.plugins.PluginHandler; 28 23 import org.openstreetmap.josm.tools.Shortcut; 24 import org.openstreetmap.josm.tools.Utils; 29 25 30 26 /** 31 27 * @author xeen … … public final class ShowStatusReportAction extends JosmAction { 126 122 ed.showDialog(); 127 123 128 124 if(ed.getValue() != 1) return; 129 try { 130 Toolkit.getDefaultToolkit().getSystemClipboard().setContents( 131 new StringSelection(text.toString()), new ClipboardOwner() { 132 public void lostOwnership(Clipboard clipboard, Transferable contents) {} 133 } 134 ); 135 } 136 catch (RuntimeException x) {} 125 Utils.copyToClipboard(text.toString()); 137 126 } 138 127 } -
src/org/openstreetmap/josm/gui/MainMenu.java
diff --git a/src/org/openstreetmap/josm/gui/MainMenu.java b/src/org/openstreetmap/josm/gui/MainMenu.java index b8b5cf5..311f8d3 100644
a b import org.openstreetmap.josm.actions.ChangesetManagerToggleAction; 23 23 import org.openstreetmap.josm.actions.CloseChangesetAction; 24 24 import org.openstreetmap.josm.actions.CombineWayAction; 25 25 import org.openstreetmap.josm.actions.CopyAction; 26 import org.openstreetmap.josm.actions.CopyCoordinatesAction; 26 27 import org.openstreetmap.josm.actions.CreateCircleAction; 27 28 import org.openstreetmap.josm.actions.CreateMultipolygonAction; 28 29 import org.openstreetmap.josm.actions.DeleteAction; … … public class MainMenu extends JMenuBar { 124 125 public final UndoAction undo = new UndoAction(); 125 126 public final RedoAction redo = new RedoAction(); 126 127 public final JosmAction copy = new CopyAction(); 128 public final JosmAction copyCoordinates = new CopyCoordinatesAction(); 127 129 public final PasteAction paste = new PasteAction(); 128 130 public final JosmAction pasteTags = new PasteTagsAction(); 129 131 public final JosmAction duplicate = new DuplicateAction(); … … public class MainMenu extends JMenuBar { 256 258 add(editMenu, redo); 257 259 editMenu.addSeparator(); 258 260 add(editMenu, copy); 261 add(editMenu, copyCoordinates); 259 262 add(editMenu, paste); 260 263 add(editMenu, pasteTags); 261 264 add(editMenu, duplicate); -
src/org/openstreetmap/josm/gui/download/BoundingBoxSelection.java
diff --git a/src/org/openstreetmap/josm/gui/download/BoundingBoxSelection.java b/src/org/openstreetmap/josm/gui/download/BoundingBoxSelection.java index bc3d7a0..e3c14be 100644
a b import java.awt.Color; 7 7 import java.awt.Dimension; 8 8 import java.awt.GridBagLayout; 9 9 import java.awt.Toolkit; 10 import java.awt.datatransfer.DataFlavor;11 10 import java.awt.datatransfer.FlavorEvent; 12 11 import java.awt.datatransfer.FlavorListener; 13 import java.awt.datatransfer.Transferable;14 import java.awt.datatransfer.UnsupportedFlavorException;15 12 import java.awt.event.ActionEvent; 16 13 import java.awt.event.ActionListener; 17 14 import java.awt.event.FocusAdapter; 18 15 import java.awt.event.FocusEvent; 19 16 import java.awt.event.MouseAdapter; 20 17 import java.awt.event.MouseEvent; 21 import java.io.IOException;22 18 23 19 import javax.swing.AbstractAction; 24 20 import javax.swing.BorderFactory; … … import org.openstreetmap.josm.data.coor.LatLon; 39 35 import org.openstreetmap.josm.tools.GBC; 40 36 import org.openstreetmap.josm.tools.ImageProvider; 41 37 import org.openstreetmap.josm.tools.OsmUrlToBounds; 38 import org.openstreetmap.josm.tools.Utils; 42 39 43 40 /** 44 41 * Bounding box selector. … … public class BoundingBoxSelection implements DownloadSelection { 305 302 Toolkit.getDefaultToolkit().getSystemClipboard().addFlavorListener(this); 306 303 } 307 304 308 protected String getClipboardContent() {309 Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);310 try {311 if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {312 String text = (String)t.getTransferData(DataFlavor.stringFlavor);313 return text;314 }315 } catch (UnsupportedFlavorException ex) {316 ex.printStackTrace();317 return null;318 } catch (IOException ex) {319 ex.printStackTrace();320 return null;321 }322 return null;323 }324 325 305 public void actionPerformed(ActionEvent e) { 326 String content = getClipboardContent();306 String content = Utils.getClipboardContent(); 327 307 if (content != null) { 328 308 tfOsmUrl.setText(content); 329 309 } 330 310 } 331 311 332 312 protected void updateEnabledState() { 333 setEnabled( getClipboardContent() != null);313 setEnabled(Utils.getClipboardContent() != null); 334 314 } 335 315 336 316 public void flavorsChanged(FlavorEvent e) { -
src/org/openstreetmap/josm/gui/download/DownloadDialog.java
diff --git a/src/org/openstreetmap/josm/gui/download/DownloadDialog.java b/src/org/openstreetmap/josm/gui/download/DownloadDialog.java index a7b4264..3b29fbb 100644
a b import java.awt.FlowLayout; 12 12 import java.awt.Font; 13 13 import java.awt.GridBagConstraints; 14 14 import java.awt.GridBagLayout; 15 import java.awt.Toolkit;16 import java.awt.datatransfer.DataFlavor;17 import java.awt.datatransfer.Transferable;18 15 import java.awt.event.ActionEvent; 19 16 import java.awt.event.InputEvent; 20 17 import java.awt.event.KeyEvent; … … import org.openstreetmap.josm.plugins.PluginHandler; 43 40 import org.openstreetmap.josm.tools.GBC; 44 41 import org.openstreetmap.josm.tools.ImageProvider; 45 42 import org.openstreetmap.josm.tools.OsmUrlToBounds; 43 import org.openstreetmap.josm.tools.Utils; 46 44 import org.openstreetmap.josm.tools.WindowGeometry; 47 45 48 46 /** … … public class DownloadDialog extends JDialog { 182 180 183 181 getRootPane().getActionMap().put("checkClipboardContents", new AbstractAction() { 184 182 public void actionPerformed(ActionEvent e) { 185 checkClipboardContents(); 183 String clip = Utils.getClipboardContent(); 184 if (clip == null) { 185 return; 186 } 187 Bounds b = OsmUrlToBounds.parse(clip); 188 if (b != null) { 189 boundingBoxChanged(new Bounds(b), null); 190 } 186 191 } 187 192 }); 188 193 HelpUtil.setHelpContext(getRootPane(), ht("/Dialog/Download")); … … public class DownloadDialog extends JDialog { 190 195 restoreSettings(); 191 196 } 192 197 193 private void checkClipboardContents() {194 String result = "";195 Transferable contents = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);196 197 if(contents == null || !contents.isDataFlavorSupported(DataFlavor.stringFlavor))198 return;199 200 try {201 result = (String)contents.getTransferData(DataFlavor.stringFlavor);202 }203 catch(Exception ex) {204 return;205 }206 207 Bounds b = OsmUrlToBounds.parse(result);208 if (b != null) {209 boundingBoxChanged(new Bounds(b),null);210 }211 }212 213 198 private void updateSizeCheck() { 214 199 if (currentBounds == null) { 215 200 sizeCheck.setText(tr("No area selected yet")); -
src/org/openstreetmap/josm/gui/widgets/BoundingBoxSelectionPanel.java
diff --git a/src/org/openstreetmap/josm/gui/widgets/BoundingBoxSelectionPanel.java b/src/org/openstreetmap/josm/gui/widgets/BoundingBoxSelectionPanel.java index 375b8e0..7ff4706 100644
a b import java.awt.GridBagConstraints; 8 8 import java.awt.GridBagLayout; 9 9 import java.awt.Insets; 10 10 import java.awt.Toolkit; 11 import java.awt.datatransfer.DataFlavor;12 11 import java.awt.datatransfer.FlavorEvent; 13 12 import java.awt.datatransfer.FlavorListener; 14 import java.awt.datatransfer.Transferable;15 import java.awt.datatransfer.UnsupportedFlavorException;16 13 import java.awt.event.ActionEvent; 17 14 import java.awt.event.MouseEvent; 18 import java.io.IOException;19 15 20 16 import javax.swing.AbstractAction; 21 17 import javax.swing.BorderFactory; … … import org.openstreetmap.josm.gui.JMultilineLabel; 34 30 import org.openstreetmap.josm.tools.GBC; 35 31 import org.openstreetmap.josm.tools.ImageProvider; 36 32 import org.openstreetmap.josm.tools.OsmUrlToBounds; 33 import org.openstreetmap.josm.tools.Utils; 37 34 38 35 /** 39 36 * … … public class BoundingBoxSelectionPanel extends JPanel { 236 233 Toolkit.getDefaultToolkit().getSystemClipboard().addFlavorListener(this); 237 234 } 238 235 239 protected String getClipboardContent() {240 Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);241 try {242 if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {243 String text = (String)t.getTransferData(DataFlavor.stringFlavor);244 return text;245 }246 } catch (UnsupportedFlavorException ex) {247 ex.printStackTrace();248 return null;249 } catch (IOException ex) {250 ex.printStackTrace();251 return null;252 }253 return null;254 }255 256 236 public void actionPerformed(ActionEvent e) { 257 String content = getClipboardContent();237 String content = Utils.getClipboardContent(); 258 238 if (content != null) { 259 239 tfOsmUrl.setText(content); 260 240 } 261 241 } 262 242 263 243 protected void updateEnabledState() { 264 setEnabled( getClipboardContent() != null);244 setEnabled(Utils.getClipboardContent() != null); 265 245 } 266 246 267 247 public void flavorsChanged(FlavorEvent e) { -
src/org/openstreetmap/josm/tools/BugReportExceptionHandler.java
diff --git a/src/org/openstreetmap/josm/tools/BugReportExceptionHandler.java b/src/org/openstreetmap/josm/tools/BugReportExceptionHandler.java index a088e96..d6b3338 100644
a b import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 6 import java.awt.Component; 7 7 import java.awt.GridBagLayout; 8 import java.awt.Toolkit;9 import java.awt.datatransfer.Clipboard;10 import java.awt.datatransfer.ClipboardOwner;11 import java.awt.datatransfer.StringSelection;12 import java.awt.datatransfer.Transferable;13 8 import java.io.PrintWriter; 14 9 import java.io.StringWriter; 15 10 import java.net.URL; … … public final class BugReportExceptionHandler implements Thread.UncaughtException 126 121 tr("Alternatively, if that does not work you can manually fill in the information " + 127 122 "below at this URL:")), GBC.eol()); 128 123 p.add(new UrlLabel("http://josm.openstreetmap.de/newticket"), GBC.eop().insets(8,0,0,0)); 129 try { 130 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), new ClipboardOwner(){ 131 public void lostOwnership(Clipboard clipboard, Transferable contents) {} 132 }); 124 if (Utils.copyToClipboard(text)) { 133 125 p.add(new JLabel(tr("(The text has already been copied to your clipboard.)")), GBC.eop()); 134 126 } 135 catch (RuntimeException x) {}136 127 137 128 JTextArea info = new JTextArea(text, 20, 60); 138 129 info.setCaretPosition(0); -
src/org/openstreetmap/josm/tools/Utils.java
diff --git a/src/org/openstreetmap/josm/tools/Utils.java b/src/org/openstreetmap/josm/tools/Utils.java index ffabb45..20f07ce 100644
a b 2 2 package org.openstreetmap.josm.tools; 3 3 4 4 import java.awt.Color; 5 import java.awt.Toolkit; 6 import java.awt.datatransfer.Clipboard; 7 import java.awt.datatransfer.ClipboardOwner; 8 import java.awt.datatransfer.DataFlavor; 9 import java.awt.datatransfer.StringSelection; 10 import java.awt.datatransfer.Transferable; 11 import java.awt.datatransfer.UnsupportedFlavorException; 5 12 import java.io.File; 6 13 import java.io.IOException; 7 14 import java.io.InputStream; … … public class Utils { 263 270 public static boolean equalsEpsilon(double a, double b) { 264 271 return Math.abs(a - b) <= EPSILION; 265 272 } 273 274 /** 275 * Copies the string {@code s} to system clipboard. 276 * @param s string to be copied to clipboard. 277 * @return true if succeeded, false otherwise. 278 */ 279 public static boolean copyToClipboard(String s) { 280 try { 281 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(s), new ClipboardOwner() { 282 283 @Override 284 public void lostOwnership(Clipboard clpbrd, Transferable t) { 285 } 286 }); 287 return true; 288 } catch (IllegalStateException ex) { 289 ex.printStackTrace(); 290 return false; 291 } 292 } 293 294 /** 295 * Extracts clipboard content as string. 296 * @return string clipboard contents if available, {@code null} otherwise. 297 */ 298 public static String getClipboardContent() { 299 Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); 300 try { 301 if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) { 302 String text = (String) t.getTransferData(DataFlavor.stringFlavor); 303 return text; 304 } 305 } catch (UnsupportedFlavorException ex) { 306 ex.printStackTrace(); 307 return null; 308 } catch (IOException ex) { 309 ex.printStackTrace(); 310 return null; 311 } 312 return null; 313 } 266 314 }
