Changeset 8429 in josm


Ignore:
Timestamp:
2015-05-26T21:30:33+02:00 (10 years ago)
Author:
Don-vip
Message:

fix #11485 - robustness to JDK-6322854 when dealing with system selection

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelper.java

    r8426 r8429  
    468468            // select combobox with saving unix system selection (middle mouse paste)
    469469            Clipboard sysSel = Toolkit.getDefaultToolkit().getSystemSelection();
    470             if(sysSel != null) {
    471                 Transferable old = sysSel.getContents(null);
     470            if (sysSel != null) {
     471                Transferable old = Utils.getTransferableContent(sysSel);
    472472                cb.requestFocusInWindow();
    473473                cb.getEditor().selectAll();
  • trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletingComboBox.java

    r8268 r8429  
    2626import org.openstreetmap.josm.Main;
    2727import org.openstreetmap.josm.gui.widgets.JosmComboBox;
     28import org.openstreetmap.josm.tools.Utils;
    2829
    2930/**
     
    134135            // save unix system selection (middle mouse paste)
    135136            Clipboard sysSel = Toolkit.getDefaultToolkit().getSystemSelection();
    136             if(sysSel != null) {
    137                 Transferable old = sysSel.getContents(null);
     137            if (sysSel != null) {
     138                Transferable old = Utils.getTransferableContent(sysSel);
    138139                editorComponent.select(start, end);
    139140                sysSel.setContents(old, null);
     
    198199                        // save unix system selection (middle mouse paste)
    199200                        Clipboard sysSel = Toolkit.getDefaultToolkit().getSystemSelection();
    200                         if(sysSel != null) {
    201                             Transferable old = sysSel.getContents(null);
     201                        if (sysSel != null) {
     202                            Transferable old = Utils.getTransferableContent(sysSel);
    202203                            editorComponent.selectAll();
    203204                            sysSel.setContents(old, null);
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r8419 r8429  
    499499        try {
    500500            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(s), new ClipboardOwner() {
    501 
    502501                @Override
    503502                public void lostOwnership(Clipboard clpbrd, Transferable t) {
     503                    // Do nothing
    504504                }
    505505            });
     
    512512
    513513    /**
    514      * Extracts clipboard content as string.
    515      * @return string clipboard contents if available, {@code null} otherwise.
    516      */
    517     public static String getClipboardContent() {
    518         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
     514     * Extracts clipboard content as {@code Transferable} object.
     515     * @param clipboard clipboard from which contents are retrieved
     516     * @return clipboard contents if available, {@code null} otherwise.
     517     * @since 8429
     518     */
     519    public static Transferable getTransferableContent(Clipboard clipboard) {
    519520        Transferable t = null;
    520521        for (int tries = 0; t == null && tries < 10; tries++) {
     
    522523                t = clipboard.getContents(null);
    523524            } catch (IllegalStateException e) {
    524                 // Clipboard currently unavailable. On some platforms, the system clipboard is unavailable while it is accessed by another application.
     525                // Clipboard currently unavailable.
     526                // On some platforms, the system clipboard is unavailable while it is accessed by another application.
    525527                try {
    526528                    Thread.sleep(1);
     
    528530                    Main.warn("InterruptedException in "+Utils.class.getSimpleName()+" while getting clipboard content");
    529531                }
    530             }
    531         }
     532            } catch (NullPointerException e) {
     533                // JDK-6322854: On Linux/X11, NPE can happen for unknown reasons, on all versions of Java
     534                Main.error(e);
     535            }
     536        }
     537        return t;
     538    }
     539
     540    /**
     541     * Extracts clipboard content as string.
     542     * @return string clipboard contents if available, {@code null} otherwise.
     543     */
     544    public static String getClipboardContent() {
     545        Transferable t = getTransferableContent(Toolkit.getDefaultToolkit().getSystemClipboard());
    532546        try {
    533547            if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
     
    547561     */
    548562    public static String md5Hex(String data) {
    549         byte[] byteData = data.getBytes(StandardCharsets.UTF_8);
    550563        MessageDigest md = null;
    551564        try {
     
    554567            throw new RuntimeException(e);
    555568        }
     569        byte[] byteData = data.getBytes(StandardCharsets.UTF_8);
    556570        byte[] byteDigest = md.digest(byteData);
    557571        return toHexString(byteDigest);
Note: See TracChangeset for help on using the changeset viewer.