Ignore:
Timestamp:
2013-02-21T15:05:09+01:00 (11 years ago)
Author:
akks
Message:

see #8384: Ctrl-Shift-V now can paste tags from text buffer like "a=2,c=3", "a 2 c 3", etc.

File:
1 edited

Legend:

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

    r5275 r5738  
    66import static org.openstreetmap.josm.tools.I18n.trn;
    77
     8import java.awt.GridBagLayout;
    89import java.awt.event.ActionEvent;
    910import java.awt.event.KeyEvent;
     
    1314import java.util.List;
    1415import java.util.Map;
     16import javax.swing.JLabel;
     17import javax.swing.JOptionPane;
     18import javax.swing.JPanel;
    1519
    1620import org.openstreetmap.josm.Main;
     
    2125import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    2226import org.openstreetmap.josm.data.osm.PrimitiveData;
    23 import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy;
    24 import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy.PasteBufferChangedListener;
    2527import org.openstreetmap.josm.data.osm.Tag;
    2628import org.openstreetmap.josm.data.osm.TagCollection;
     29import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
    2730import org.openstreetmap.josm.gui.conflict.tags.PasteTagsConflictResolverDialog;
     31import org.openstreetmap.josm.gui.help.HelpUtil;
     32import org.openstreetmap.josm.tools.GBC;
    2833import org.openstreetmap.josm.tools.Shortcut;
     34import org.openstreetmap.josm.tools.TextTagParser;
     35import org.openstreetmap.josm.tools.UrlLabel;
     36import org.openstreetmap.josm.tools.Utils;
    2937
    3038/**
     
    3644 * @author David Earl
    3745 */
    38 public final class PasteTagsAction extends JosmAction implements PasteBufferChangedListener {
     46public final class PasteTagsAction extends JosmAction {
    3947
    4048    public PasteTagsAction() {
     
    4351                Shortcut.registerShortcut("system:pastestyle", tr("Edit: {0}", tr("Paste Tags")),
    4452                KeyEvent.VK_V, Shortcut.CTRL_SHIFT), true);
    45         Main.pasteBuffer.addPasteBufferChangedListener(this);
    4653        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);
    4768    }
    4869
     
    246267        if (selection.isEmpty())
    247268            return;
    248 
    249         TagPaster tagPaster = new TagPaster(Main.pasteBuffer.getDirectlyAdded(), selection);
     269       
     270        String buf = Utils.getClipboardContent();
    250271
    251272        List<Command> commands = new ArrayList<Command>();
    252         for (Tag tag: tagPaster.execute()) {
    253             commands.add(new ChangePropertyCommand(selection, tag.getKey(), "".equals(tag.getValue())?null:tag.getValue()));
     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            }
    254294        }
    255295        if (!commands.isEmpty()) {
     
    262302                    ));
    263303        }
    264 
    265     }
    266 
    267     @Override public void pasteBufferChanged(PrimitiveDeepCopy newPasteBuffer) {
    268         updateEnabledState();
    269     }
     304       
     305    }
     306
    270307
    271308    @Override
    272309    protected void updateEnabledState() {
    273         if (getCurrentDataSet() == null || Main.pasteBuffer == null) {
     310        if (getCurrentDataSet() == null) {
    274311            setEnabled(false);
    275312            return;
    276313        }
    277         setEnabled(
    278                 !getCurrentDataSet().getSelected().isEmpty()
    279                 && !TagCollection.unionOfAllPrimitives(Main.pasteBuffer.getDirectlyAdded()).isEmpty()
    280         );
     314        // buffer listening slows down the program and is not very good for arbitrary text in buffer
     315        setEnabled(!getCurrentDataSet().getSelected().isEmpty());
    281316    }
    282317
    283318    @Override
    284319    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
    285         setEnabled(
    286                 selection!= null && !selection.isEmpty()
    287                 && !TagCollection.unionOfAllPrimitives(Main.pasteBuffer.getDirectlyAdded()).isEmpty()
    288         );
     320        setEnabled(selection!= null && !selection.isEmpty());
    289321    }
    290322}
Note: See TracChangeset for help on using the changeset viewer.