Ticket #8500: pastingFallback.patch

File pastingFallback.patch, 5.8 KB (added by akks, 13 years ago)
  • src/org/openstreetmap/josm/actions/PasteTagsAction.java

     
    4545 */
    4646public final class PasteTagsAction extends JosmAction {
    4747
     48    private static final String help = ht("/Action/PasteTags");
     49   
    4850    public PasteTagsAction() {
    4951        super(tr("Paste Tags"), "pastetags",
    5052                tr("Apply tags of contents of paste buffer to all selected items."),
    5153                Shortcut.registerShortcut("system:pastestyle", tr("Edit: {0}", tr("Paste Tags")),
    5254                KeyEvent.VK_V, Shortcut.CTRL_SHIFT), true);
    53         putValue("help", ht("/Action/PasteTags"));
     55        putValue("help", help);
    5456    }
    5557
    56     private void showBadBufferMessage() {
    57         String msg = tr("<html><p> Sorry, it is impossible to paste tags from buffer. It does not contain any JOSM object"
    58             + " or suitable text. </p></html>");
    59         JPanel p = new JPanel(new GridBagLayout());
    60         p.add(new JLabel(msg),GBC.eop());
    61         p.add(new UrlLabel(
    62                 HelpUtil.getHelpTopicUrl(HelpUtil.buildAbsoluteHelpTopic((String)getValue("help")))),
    63                 GBC.eop());
    64 
    65         ConditionalOptionPaneUtil.showMessageDialog(
    66             "paste_badbuffer", Main.parent,
    67             p, tr("Warning"), JOptionPane.WARNING_MESSAGE);
    68     }
    69 
    7058    public static class TagPaster {
    7159
    7260        private final Collection<PrimitiveData> source;
     
    268256            return;
    269257       
    270258        String buf = Utils.getClipboardContent();
     259        boolean ok = false;
    271260
    272         List<Command> commands = new ArrayList<Command>();
    273         if (buf==null) {
     261        if (buf == null || buf.matches("(\\d+,)*\\d+")) {
     262            ok = pasteTagsFromJOSMBuffer(selection);
     263        } else {
     264            // Paste tags from arbitrary text
     265            ok = pasteTagsFromText(selection, buf);
     266            if (!ok) {
     267                ok = pasteTagsFromJOSMBuffer(selection);
     268            }
     269        }
     270        if (!ok) {
    274271            showBadBufferMessage();
    275             return;
    276272        }
    277         if (buf.matches("(\\d+,)*\\d+")) { // Paste tags from JOSM buffer
    278             PasteTagsAction.TagPaster tagPaster = new PasteTagsAction.TagPaster(Main.pasteBuffer.getDirectlyAdded(), selection);
    279             for (Tag tag: tagPaster.execute()) {
    280                 commands.add(new ChangePropertyCommand(selection, tag.getKey(), "".equals(tag.getValue())?null:tag.getValue()));
    281             }
    282         } else { // Paste tags from arbitrary text
    283             Map<String, String> tags = TextTagParser.readTagsFromText(buf);
    284             if (tags==null || tags.isEmpty()) {
    285                 showBadBufferMessage();
    286                 return;
    287             }
    288             if (!TextTagParser.validateTags(tags)) return;
     273    }
     274
     275    /** Paste tags from arbitrary text
     276     * @return true if action was successful
     277     */
     278    public static boolean pasteTagsFromText(Collection<OsmPrimitive> selection, String text) {
     279        Map<String, String> tags = TextTagParser.readTagsFromText(text);
     280        List<Command> commands = new ArrayList<Command>();
     281        if (tags==null || tags.isEmpty()) {
     282            return false;
     283        } else {
     284            if (!TextTagParser.validateTags(tags)) return false;
    289285            String v;
    290286            for (String key: tags.keySet()) {
    291287                v = tags.get(key);
    292288                commands.add(new ChangePropertyCommand(selection, key, "".equals(v)?null:v));
    293289            }
     290        }       
     291        commitCommands(selection, commands);
     292        return !commands.isEmpty();
     293    }
     294   
     295   
     296    /** Paste tags from JOSM buffer
     297     * @param selection objects
     298     * @param commands
     299     * @return
     300     */
     301    public static boolean pasteTagsFromJOSMBuffer(Collection<OsmPrimitive> selection) {
     302        List<PrimitiveData> directlyAdded = Main.pasteBuffer.getDirectlyAdded();
     303        if (directlyAdded==null || directlyAdded.isEmpty()) return false;
     304
     305        PasteTagsAction.TagPaster tagPaster = new PasteTagsAction.TagPaster(directlyAdded, selection);
     306        List<Command> commands = new ArrayList<Command>();
     307        for (Tag tag : tagPaster.execute()) {
     308            commands.add(new ChangePropertyCommand(selection, tag.getKey(), "".equals(tag.getValue()) ? null : tag.getValue()));
    294309        }
     310        commitCommands(selection, commands);
     311        return true;
     312    }
     313
     314    /**
     315     * Create and execute SequenceCommand with descriptive title
     316     * @param commands
     317     */
     318    private static void commitCommands(Collection<OsmPrimitive> selection, List<Command> commands) {
    295319        if (!commands.isEmpty()) {
    296320            String title1 = trn("Pasting {0} tag", "Pasting {0} tags", commands.size(), commands.size());
    297321            String title2 = trn("to {0} object", "to {0} objects", selection.size(), selection.size());
     
    301325                            commands
    302326                    ));
    303327        }
    304        
    305328    }
     329   
     330    private static void showBadBufferMessage() {
     331        String msg = tr("<html><p> Sorry, it is impossible to paste tags from buffer. It does not contain any JOSM object"
     332            + " or suitable text. </p></html>");
     333        JPanel p = new JPanel(new GridBagLayout());
     334        p.add(new JLabel(msg),GBC.eop());
     335        p.add(new UrlLabel(
     336                HelpUtil.getHelpTopicUrl(HelpUtil.buildAbsoluteHelpTopic(help))),
     337                GBC.eop());
    306338
    307 
     339        ConditionalOptionPaneUtil.showMessageDialog(
     340            "paste_badbuffer", Main.parent,
     341            p, tr("Warning"), JOptionPane.WARNING_MESSAGE);
     342    }
     343   
    308344    @Override
    309345    protected void updateEnabledState() {
    310346        if (getCurrentDataSet() == null) {