Changeset 9369 in josm for trunk/src/org


Ignore:
Timestamp:
2016-01-09T22:21:00+01:00 (9 years ago)
Author:
simon04
Message:

see #12300 - Allow to drag from select list to relation members

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

Legend:

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

    r9078 r9369  
    77import java.awt.Component;
    88import java.awt.Rectangle;
     9import java.awt.datatransfer.Transferable;
    910import java.awt.event.ActionEvent;
    1011import java.awt.event.ActionListener;
     
    2425import javax.swing.AbstractListModel;
    2526import javax.swing.DefaultListSelectionModel;
     27import javax.swing.JComponent;
    2628import javax.swing.JList;
    2729import javax.swing.JMenuItem;
    2830import javax.swing.JPopupMenu;
    2931import javax.swing.ListSelectionModel;
     32import javax.swing.TransferHandler;
    3033import javax.swing.event.ListDataEvent;
    3134import javax.swing.event.ListDataListener;
     
    6669import org.openstreetmap.josm.gui.PopupMenuHandler;
    6770import org.openstreetmap.josm.gui.SideButton;
     71import org.openstreetmap.josm.gui.datatransfer.PrimitiveTransferable;
    6872import org.openstreetmap.josm.gui.history.HistoryBrowserDialogManager;
    6973import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     
    108112        lstPrimitives.setSelectionModel(selectionModel);
    109113        lstPrimitives.setCellRenderer(new OsmPrimitivRenderer());
    110         // Fix #6290. Drag & Drop is not supported anyway and Copy/Paste is better propagated to main window
    111         lstPrimitives.setTransferHandler(null);
     114        lstPrimitives.setTransferHandler(new SelectionTransferHandler());
     115        lstPrimitives.setDragEnabled(true);
    112116
    113117        lstPrimitives.getSelectionModel().addListSelectionListener(actSelect);
     
    855859        }
    856860    }
     861
     862    /**
     863     * A transfer handler class for drag-and-drop support.
     864     */
     865    protected class SelectionTransferHandler extends TransferHandler {
     866
     867        @Override
     868        public int getSourceActions(JComponent c) {
     869            return COPY;
     870        }
     871
     872        @Override
     873        protected Transferable createTransferable(JComponent c) {
     874            return new PrimitiveTransferable(getSelectedPrimitives());
     875        }
     876    }
    857877}
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTableModel.java

    r9368 r9369  
    423423
    424424    private void addMembersAtIndex(List<? extends OsmPrimitive> primitives, int index) {
    425         final Collection<TaggingPreset> presets = TaggingPresets.getMatchingPresets(EnumSet.of(TaggingPresetType.RELATION),
    426                 presetHandler.getSelection().iterator().next().getKeys(), false);
    427425        if (primitives == null)
    428426            return;
    429427        int idx = index;
    430428        for (OsmPrimitive primitive : primitives) {
    431             Set<String> potentialRoles = new TreeSet<>();
    432             for (TaggingPreset tp : presets) {
    433                 String suggestedRole = tp.suggestRoleForOsmPrimitive(primitive);
    434                 if (suggestedRole != null) {
    435                     potentialRoles.add(suggestedRole);
    436                 }
    437             }
    438             // TODO: propose user to choose role among potential ones instead of picking first one
    439             final String role = potentialRoles.isEmpty() ? null : potentialRoles.iterator().next();
    440             RelationMember member = new RelationMember(role == null ? "" : role, primitive);
     429            final RelationMember member = getRelationMemberForPrimitive(primitive);
    441430            members.add(idx++, member);
    442431        }
     
    445434        getSelectionModel().addSelectionInterval(index, index + primitives.size() - 1);
    446435        fireMakeMemberVisible(index);
     436    }
     437
     438    RelationMember getRelationMemberForPrimitive(final OsmPrimitive primitive) {
     439        final Collection<TaggingPreset> presets = TaggingPresets.getMatchingPresets(EnumSet.of(TaggingPresetType.RELATION),
     440                presetHandler.getSelection().iterator().next().getKeys(), false);
     441        Collection<String> potentialRoles = new TreeSet<>();
     442        for (TaggingPreset tp : presets) {
     443            String suggestedRole = tp.suggestRoleForOsmPrimitive(primitive);
     444            if (suggestedRole != null) {
     445                potentialRoles.add(suggestedRole);
     446            }
     447        }
     448        // TODO: propose user to choose role among potential ones instead of picking first one
     449        final String role = potentialRoles.isEmpty() ? "" : potentialRoles.iterator().next();
     450        return new RelationMember(role == null ? "" : role, primitive);
    447451    }
    448452
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTransferHandler.java

    r9368 r9369  
    1616import org.openstreetmap.josm.Main;
    1717import org.openstreetmap.josm.data.osm.OsmPrimitive;
     18import org.openstreetmap.josm.data.osm.PrimitiveData;
    1819import org.openstreetmap.josm.data.osm.RelationMember;
    1920import org.openstreetmap.josm.data.osm.RelationMemberData;
     21import org.openstreetmap.josm.gui.datatransfer.PrimitiveTransferable;
    2022import org.openstreetmap.josm.gui.datatransfer.RelationMemberTransferable;
     23import org.openstreetmap.josm.tools.Utils.Function;
    2124
    2225class MemberTransferHandler extends TransferHandler {
     
    3639    public boolean canImport(TransferSupport support) {
    3740        support.setShowDropLocation(true);
    38         return support.isDataFlavorSupported(RelationMemberTransferable.RELATION_MEMBER_DATA);
     41        return support.isDataFlavorSupported(RelationMemberTransferable.RELATION_MEMBER_DATA)
     42                || support.isDataFlavorSupported(PrimitiveTransferable.PRIMITIVE_DATA);
    3943    }
    4044
    4145    @Override
    4246    public boolean importData(TransferSupport support) {
     47        final MemberTable destination = (MemberTable) support.getComponent();
    4348        final int insertRow = ((JTable.DropLocation) support.getDropLocation()).getRow();
    44         final Collection<RelationMemberData> memberData;
     49
    4550        try {
    46             //noinspection unchecked
    47             memberData = (Collection<RelationMemberData>) support.getTransferable().getTransferData(RelationMemberTransferable.RELATION_MEMBER_DATA);
    48         } catch (UnsupportedFlavorException | IOException e) {
     51            if (support.isDataFlavorSupported(RelationMemberTransferable.RELATION_MEMBER_DATA)) {
     52                importRelationMemberData(support, destination, insertRow);
     53            } else if (support.isDataFlavorSupported(PrimitiveTransferable.PRIMITIVE_DATA)) {
     54                importPrimitiveData(support, destination, insertRow);
     55            }
     56        } catch (Exception e) {
    4957            Main.warn(e);
    5058            return false;
    5159        }
    52         final MemberTable destination = (MemberTable) support.getComponent();
    5360
    54         try {
    55             importRelationMemberData(memberData, destination, insertRow);
    56         } catch (Exception e) {
    57             Main.warn(e);
    58             throw e;
    59         }
    6061        return true;
    6162    }
    6263
    63     protected void importRelationMemberData(Collection<RelationMemberData> memberData, MemberTable destination, int insertRow) {
     64    protected void importRelationMemberData(TransferSupport support, final MemberTable destination, int insertRow)
     65            throws UnsupportedFlavorException, IOException {
     66        //noinspection unchecked
     67        final Collection<RelationMemberData> memberData = (Collection<RelationMemberData>)
     68                support.getTransferable().getTransferData(RelationMemberTransferable.RELATION_MEMBER_DATA);
     69        importData(destination, insertRow, memberData, new Function<RelationMemberData, RelationMember>() {
     70            @Override
     71            public RelationMember apply(RelationMemberData member) {
     72                final OsmPrimitive p = destination.getLayer().data.getPrimitiveById(member.getUniqueId(), member.getType());
     73                if (p == null) {
     74                    Main.warn(tr("Cannot add {0} since it is not part of dataset", member));
     75                    return null;
     76                } else {
     77                    return new RelationMember(member.getRole(), p);
     78                }
     79            }
     80        });
     81    }
     82
     83    protected void importPrimitiveData(TransferSupport support, final MemberTable destination, int insertRow)
     84            throws UnsupportedFlavorException, IOException {
     85        //noinspection unchecked
     86        final Collection<PrimitiveData> data = (Collection<PrimitiveData>)
     87                support.getTransferable().getTransferData(PrimitiveTransferable.PRIMITIVE_DATA);
     88        importData(destination, insertRow, data, new Function<PrimitiveData, RelationMember>() {
     89            @Override
     90            public RelationMember apply(PrimitiveData data) {
     91                final OsmPrimitive p = destination.getLayer().data.getPrimitiveById(data);
     92                if (p == null) {
     93                    Main.warn(tr("Cannot add {0} since it is not part of dataset", data));
     94                    return null;
     95                } else {
     96                    return destination.getMemberTableModel().getRelationMemberForPrimitive(p);
     97                }
     98            }
     99        });
     100    }
     101
     102    protected <T> void importData(MemberTable destination, int insertRow, Collection<T> memberData, Function<T, RelationMember> toMemberFunction) {
    64103        final Collection<RelationMember> membersToAdd = new ArrayList<>(memberData.size());
    65         for (RelationMemberData member : memberData) {
    66             final OsmPrimitive p = destination.getLayer().data.getPrimitiveById(member.getUniqueId(), member.getType());
    67             if (p != null) {
    68                 membersToAdd.add(new RelationMember(member.getRole(), p));
    69             } else {
    70                 Main.warn(tr("Cannot add {0} since it is not part of dataset", member));
     104        for (T i : memberData) {
     105            final RelationMember member = toMemberFunction.apply(i);
     106            if (member != null) {
     107                membersToAdd.add(member);
    71108            }
    72109        }
Note: See TracChangeset for help on using the changeset viewer.