Ignore:
Timestamp:
2013-03-29T12:16:54+01:00 (11 years ago)
Author:
akks
Message:

fix #8500, see #8384: added "Clear buffer" button to the messages shown for incorrect buffer (clipboard)
(next Ctrl-Shift-V will paste tags from JOSM-copied objects, if available)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/PasteTagsAction.java

    r5738 r5809  
    66import static org.openstreetmap.josm.tools.I18n.trn;
    77
    8 import java.awt.GridBagLayout;
    98import java.awt.event.ActionEvent;
    109import java.awt.event.KeyEvent;
     
    1413import java.util.List;
    1514import java.util.Map;
    16 import javax.swing.JLabel;
    17 import javax.swing.JOptionPane;
    18 import javax.swing.JPanel;
    1915
    2016import org.openstreetmap.josm.Main;
     
    2723import org.openstreetmap.josm.data.osm.Tag;
    2824import org.openstreetmap.josm.data.osm.TagCollection;
    29 import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
    3025import org.openstreetmap.josm.gui.conflict.tags.PasteTagsConflictResolverDialog;
    3126import org.openstreetmap.josm.gui.help.HelpUtil;
    32 import org.openstreetmap.josm.tools.GBC;
    3327import org.openstreetmap.josm.tools.Shortcut;
    3428import org.openstreetmap.josm.tools.TextTagParser;
    35 import org.openstreetmap.josm.tools.UrlLabel;
    3629import org.openstreetmap.josm.tools.Utils;
    3730
     
    4639public final class PasteTagsAction extends JosmAction {
    4740
     41    private static final String help = ht("/Action/PasteTags");
     42    private static final String helpUrl = HelpUtil.getHelpTopicUrl(HelpUtil.buildAbsoluteHelpTopic(help));
     43   
    4844    public PasteTagsAction() {
    4945        super(tr("Paste Tags"), "pastetags",
     
    5147                Shortcut.registerShortcut("system:pastestyle", tr("Edit: {0}", tr("Paste Tags")),
    5248                KeyEvent.VK_V, Shortcut.CTRL_SHIFT), true);
    53         putValue("help", ht("/Action/PasteTags"));
    54     }
    55 
    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);
     49        putValue("help", help);
    6850    }
    6951
     
    269251       
    270252        String buf = Utils.getClipboardContent();
    271 
     253        if (buf == null || buf.isEmpty() || buf.matches("(\\d+,)*\\d+")) {
     254            pasteTagsFromJOSMBuffer(selection);
     255        } else {
     256            // Paste tags from arbitrary text
     257            pasteTagsFromText(selection, buf);
     258        }
     259    }
     260
     261    /** Paste tags from arbitrary text
     262     * @return false if action was successful
     263     */
     264    public static boolean pasteTagsFromText(Collection<OsmPrimitive> selection, String text) {
     265        Map<String, String> tags = TextTagParser.readTagsFromText(text);
    272266        List<Command> commands = new ArrayList<Command>();
    273         if (buf==null) {
    274             showBadBufferMessage();
    275             return;
    276         }
    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;
    289             String v;
    290             for (String key: tags.keySet()) {
    291                 v = tags.get(key);
    292                 commands.add(new ChangePropertyCommand(selection, key, "".equals(v)?null:v));
    293             }
    294         }
     267        if (tags==null || tags.isEmpty()) {
     268            TextTagParser.showBadBufferMessage(helpUrl);
     269            return false;
     270        };
     271        if (!TextTagParser.validateTags(tags)) return false;
     272        String v;
     273        for (String key: tags.keySet()) {
     274            v = tags.get(key);
     275            commands.add(new ChangePropertyCommand(selection, key, "".equals(v)?null:v));
     276        }
     277        commitCommands(selection, commands);
     278        return !commands.isEmpty();
     279    }
     280       
     281    /** Paste tags from JOSM buffer
     282     * @param selection objects
     283     * @param commands
     284     * @return
     285     */
     286    public static boolean pasteTagsFromJOSMBuffer(Collection<OsmPrimitive> selection) {
     287        List<PrimitiveData> directlyAdded = Main.pasteBuffer.getDirectlyAdded();
     288        if (directlyAdded==null || directlyAdded.isEmpty()) return false;
     289
     290        PasteTagsAction.TagPaster tagPaster = new PasteTagsAction.TagPaster(directlyAdded, selection);
     291        List<Command> commands = new ArrayList<Command>();
     292        for (Tag tag : tagPaster.execute()) {
     293            commands.add(new ChangePropertyCommand(selection, tag.getKey(), "".equals(tag.getValue()) ? null : tag.getValue()));
     294        }
     295        commitCommands(selection, commands);
     296        return true;
     297    }
     298
     299    /**
     300     * Create and execute SequenceCommand with descriptive title
     301     * @param commands
     302     */
     303    private static void commitCommands(Collection<OsmPrimitive> selection, List<Command> commands) {
    295304        if (!commands.isEmpty()) {
    296305            String title1 = trn("Pasting {0} tag", "Pasting {0} tags", commands.size(), commands.size());
     
    302311                    ));
    303312        }
    304        
    305     }
    306 
    307 
     313    }
     314   
    308315    @Override
    309316    protected void updateEnabledState() {
Note: See TracChangeset for help on using the changeset viewer.