Ignore:
Timestamp:
2016-07-23T14:54:19+02:00 (8 years ago)
Author:
Don-vip
Message:

fix #12478, fix #12565, fix #11114 - Use ​Swing Copy/Paste instead of CopyAction/PasteAction with custom buffer (patch by michael2402, modified) - gsoc-core

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/tagging/TagEditorModel.java

    r10217 r10604  
    88import java.util.ArrayList;
    99import java.util.Collection;
     10import java.util.Collections;
    1011import java.util.Comparator;
    1112import java.util.EnumSet;
     
    2526import org.openstreetmap.josm.data.osm.Tag;
    2627import org.openstreetmap.josm.data.osm.TagCollection;
     28import org.openstreetmap.josm.data.osm.TagMap;
    2729import org.openstreetmap.josm.data.osm.Tagged;
    2830import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetType;
     
    4749
    4850    private transient OsmPrimitive primitive;
     51
     52    private EndEditListener endEditListener;
    4953
    5054    /**
     
    167171     */
    168172    public void clear() {
     173        commitPendingEdit();
    169174        boolean wasEmpty = tags.isEmpty();
    170175        tags.clear();
     
    183188     */
    184189    public void add(TagModel tag) {
     190        commitPendingEdit();
    185191        CheckParameterUtil.ensureParameterNotNull(tag, "tag");
    186192        tags.add(tag);
     
    189195    }
    190196
     197    /**
     198     * Add a tag at the beginning of the table.
     199     *
     200     * @param tag The tag to add
     201     *
     202     * @throws IllegalArgumentException if tag is null
     203     *
     204     * @see #add(TagModel)
     205     */
    191206    public void prepend(TagModel tag) {
     207        commitPendingEdit();
    192208        CheckParameterUtil.ensureParameterNotNull(tag, "tag");
    193209        tags.add(0, tag);
     
    209225     */
    210226    public void add(String name, String value) {
     227        commitPendingEdit();
    211228        String key = (name == null) ? "" : name;
    212229        String val = (value == null) ? "" : value;
     
    259276        if (tags == null)
    260277            return;
     278        commitPendingEdit();
    261279        for (int tagIdx : tagIndices) {
    262280            TagModel tag = tags.get(tagIdx);
     
    277295        if (tags == null)
    278296            return;
     297        commitPendingEdit();
    279298        for (int tagIdx : tagIndices) {
    280299            TagModel tag = tags.get(tagIdx);
     
    293312     */
    294313    public void delete(String name) {
     314        commitPendingEdit();
    295315        if (name == null)
    296316            return;
     
    318338        if (tags == null)
    319339            return;
     340        commitPendingEdit();
    320341        List<TagModel> toDelete = new ArrayList<>();
    321342        for (int tagIdx : tagIndices) {
     
    356377     */
    357378    public void initFromPrimitive(Tagged primitive) {
     379        commitPendingEdit();
    358380        this.tags.clear();
    359381        for (String key : primitive.keySet()) {
     
    361383            this.tags.add(new TagModel(key, value));
    362384        }
     385        sort();
    363386        TagModel tag = new TagModel();
    364         sort();
    365387        tags.add(tag);
    366388        setDirty(false);
     
    374396     */
    375397    public void initFromTags(Map<String, String> tags) {
     398        commitPendingEdit();
    376399        this.tags.clear();
    377400        for (Entry<String, String> entry : tags.entrySet()) {
     
    391414     */
    392415    public void initFromTags(TagCollection tags) {
     416        commitPendingEdit();
    393417        this.tags.clear();
    394418        if (tags == null) {
     
    424448     */
    425449    private Map<String, String> applyToTags(boolean keepEmpty) {
    426         Map<String, String> result = new HashMap<>();
     450        // TagMap preserves the order of tags.
     451        TagMap result = new TagMap();
    427452        for (TagModel tag: this.tags) {
    428453            // tag still holds an unchanged list of different values for the same key.
     
    433458
    434459            // tag name holds an empty key. Don't apply it to the selection.
    435             //
    436460            if (!keepEmpty && (tag.getName().trim().isEmpty() || tag.getValue().trim().isEmpty())) {
    437461                continue;
     
    539563     */
    540564    protected void sort() {
    541         java.util.Collections.sort(
     565        Collections.sort(
    542566                tags,
    543567                new Comparator<TagModel>() {
     
    591615     */
    592616    public void updateTags(List<Tag> tags) {
    593          if (tags.isEmpty())
     617        if (tags.isEmpty())
    594618            return;
    595619
     620        commitPendingEdit();
    596621        Map<String, TagModel> modelTags = new HashMap<>();
    597622        for (int i = 0; i < getRowCount(); i++) {
     
    648673    }
    649674
     675    /**
     676     * Sets the listener that is notified when an edit should be aborted.
     677     * @param endEditListener The listener to be notified when editing should be aborted.
     678     */
     679    public void setEndEditListener(EndEditListener endEditListener) {
     680        this.endEditListener = endEditListener;
     681    }
     682
     683    private void commitPendingEdit() {
     684        if (endEditListener != null) {
     685            endEditListener.endCellEditing();
     686        }
     687    }
     688
    650689    class SelectionStateMemento {
    651690        private final int rowMin;
     
    674713        }
    675714    }
     715
     716    /**
     717     * A listener that is called whenever the cells may be updated from outside the editor and the editor should thus be commited.
     718     * @since 10604
     719     */
     720    @FunctionalInterface
     721    public interface EndEditListener {
     722        /**
     723         * Requests to end the editing of any cells on this model
     724         */
     725        void endCellEditing();
     726    }
    676727}
  • trunk/src/org/openstreetmap/josm/gui/tagging/TagTable.java

    r10378 r10604  
    22package org.openstreetmap.josm.gui.tagging;
    33
    4 import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
    54import static org.openstreetmap.josm.tools.I18n.tr;
    65
     
    1312import java.beans.PropertyChangeEvent;
    1413import java.beans.PropertyChangeListener;
    15 import java.util.ArrayList;
    1614import java.util.Collections;
    1715import java.util.EventObject;
    18 import java.util.List;
    19 import java.util.Map;
    2016import java.util.concurrent.CopyOnWriteArrayList;
    2117
     
    3228
    3329import org.openstreetmap.josm.Main;
    34 import org.openstreetmap.josm.actions.CopyAction;
    35 import org.openstreetmap.josm.actions.PasteTagsAction;
    36 import org.openstreetmap.josm.data.osm.OsmPrimitive;
    37 import org.openstreetmap.josm.data.osm.PrimitiveData;
    3830import org.openstreetmap.josm.data.osm.Relation;
    39 import org.openstreetmap.josm.data.osm.Tag;
     31import org.openstreetmap.josm.data.osm.TagMap;
     32import org.openstreetmap.josm.gui.datatransfer.OsmTransferHandler;
     33import org.openstreetmap.josm.gui.tagging.TagEditorModel.EndEditListener;
    4034import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
    4135import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
    4236import org.openstreetmap.josm.gui.widgets.JosmTable;
    4337import org.openstreetmap.josm.tools.ImageProvider;
    44 import org.openstreetmap.josm.tools.TextTagParser;
    45 import org.openstreetmap.josm.tools.Utils;
    4638
    4739/**
     
    4941 * @since 1762
    5042 */
    51 public class TagTable extends JosmTable {
     43public class TagTable extends JosmTable implements EndEditListener {
    5244    /** the table cell editor used by this table */
    5345    private TagCellEditor editor;
     
    211203            }
    212204
    213             if (isEditing()) {
    214                 CellEditor cEditor = getCellEditor();
    215                 if (cEditor != null) {
    216                     cEditor.cancelCellEditing();
    217                 }
    218             }
     205            endCellEditing();
    219206
    220207            if (model.getRowCount() == 0) {
     
    233220
    234221        protected final void updateEnabledState() {
    235             if (isEditing() && getSelectedColumnCount() == 1 && getSelectedRowCount() == 1) {
    236                 setEnabled(true);
    237             } else if (!isEditing() && getSelectedColumnCount() == 1 && getSelectedRowCount() == 1) {
    238                 setEnabled(true);
    239             } else if (getSelectedColumnCount() > 1 || getSelectedRowCount() > 1) {
     222            if (getSelectedColumnCount() >= 1 && getSelectedRowCount() >= 1) {
    240223                setEnabled(true);
    241224            } else {
     
    295278            Relation relation = new Relation();
    296279            model.applyToPrimitive(relation);
    297 
    298             String buf = Utils.getClipboardContent();
    299             if (buf == null || buf.isEmpty() || buf.matches(CopyAction.CLIPBOARD_REGEXP)) {
    300                 List<PrimitiveData> directlyAdded = Main.pasteBuffer.getDirectlyAdded();
    301                 if (directlyAdded == null || directlyAdded.isEmpty()) return;
    302                 PasteTagsAction.TagPaster tagPaster = new PasteTagsAction.TagPaster(directlyAdded,
    303                         Collections.<OsmPrimitive>singletonList(relation));
    304                 model.updateTags(tagPaster.execute());
    305             } else {
    306                  // Paste tags from arbitrary text
    307                  Map<String, String> tags = TextTagParser.readTagsFromText(buf);
    308                  if (tags == null || tags.isEmpty()) {
    309                     TextTagParser.showBadBufferMessage(ht("/Action/PasteTags"));
    310                  } else if (TextTagParser.validateTags(tags)) {
    311                      List<Tag> newTags = new ArrayList<>();
    312                      for (Map.Entry<String, String> entry: tags.entrySet()) {
    313                         String k = entry.getKey();
    314                         String v = entry.getValue();
    315                         newTags.add(new Tag(k, v));
    316                      }
    317                      model.updateTags(newTags);
    318                  }
    319             }
     280            new OsmTransferHandler().pasteTags(Collections.singleton(relation));
     281            model.updateTags(new TagMap(relation.getKeys()).getTags());
    320282        }
    321283
     
    415377              model.getRowSelectionModel());
    416378        this.model = model;
     379        model.setEndEditListener(this);
    417380        init(maxCharacters);
    418381    }
     
    488451     */
    489452    public void setTagCellEditor(TagCellEditor editor) {
    490         if (isEditing()) {
    491             this.editor.cancelCellEditing();
    492         }
     453        endCellEditing();
    493454        this.editor = editor;
    494455        getColumnModel().getColumn(0).setCellEditor(editor);
     
    546507        // delegate to the default implementation
    547508        return super.editCellAt(row, column, e);
     509    }
     510
     511    @Override
     512    public void endCellEditing() {
     513        if (isEditing()) {
     514            CellEditor cEditor = getCellEditor();
     515            if (cEditor != null) {
     516                // First attempt to commit. If this does not work, cancel.
     517                cEditor.stopCellEditing();
     518                cEditor.cancelCellEditing();
     519            }
     520        }
    548521    }
    549522
  • trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletingComboBox.java

    r10308 r10604  
    2424
    2525import org.openstreetmap.josm.Main;
    26 import org.openstreetmap.josm.gui.util.GuiHelper;
     26import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
    2727import org.openstreetmap.josm.gui.widgets.JosmComboBox;
    28 import org.openstreetmap.josm.tools.Utils;
    2928
    3029/**
     
    134133            final JTextComponent editorComponent = comboBox.getEditorComponent();
    135134            // save unix system selection (middle mouse paste)
    136             Clipboard sysSel = GuiHelper.getSystemSelection();
     135            Clipboard sysSel = ClipboardUtils.getSystemSelection();
    137136            if (sysSel != null) {
    138                 Transferable old = Utils.getTransferableContent(sysSel);
     137                Transferable old = ClipboardUtils.getClipboardContent(sysSel);
    139138                editorComponent.select(start, end);
    140139                if (old != null) {
     
    201200                        }
    202201                        // save unix system selection (middle mouse paste)
    203                         Clipboard sysSel = GuiHelper.getSystemSelection();
     202                        Clipboard sysSel = ClipboardUtils.getSystemSelection();
    204203                        if (sysSel != null) {
    205                             Transferable old = Utils.getTransferableContent(sysSel);
     204                            Transferable old = ClipboardUtils.getClipboardContent(sysSel);
    206205                            editorComponent.selectAll();
    207206                            if (old != null) {
Note: See TracChangeset for help on using the changeset viewer.