Changeset 9369 in josm for trunk/src/org
- Timestamp:
- 2016-01-09T22:21:00+01:00 (9 years ago)
- 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 7 7 import java.awt.Component; 8 8 import java.awt.Rectangle; 9 import java.awt.datatransfer.Transferable; 9 10 import java.awt.event.ActionEvent; 10 11 import java.awt.event.ActionListener; … … 24 25 import javax.swing.AbstractListModel; 25 26 import javax.swing.DefaultListSelectionModel; 27 import javax.swing.JComponent; 26 28 import javax.swing.JList; 27 29 import javax.swing.JMenuItem; 28 30 import javax.swing.JPopupMenu; 29 31 import javax.swing.ListSelectionModel; 32 import javax.swing.TransferHandler; 30 33 import javax.swing.event.ListDataEvent; 31 34 import javax.swing.event.ListDataListener; … … 66 69 import org.openstreetmap.josm.gui.PopupMenuHandler; 67 70 import org.openstreetmap.josm.gui.SideButton; 71 import org.openstreetmap.josm.gui.datatransfer.PrimitiveTransferable; 68 72 import org.openstreetmap.josm.gui.history.HistoryBrowserDialogManager; 69 73 import org.openstreetmap.josm.gui.layer.OsmDataLayer; … … 108 112 lstPrimitives.setSelectionModel(selectionModel); 109 113 lstPrimitives.setCellRenderer(new OsmPrimitivRenderer()); 110 // Fix #6290. Drag & Drop is not supported anyway and Copy/Paste is better propagated to main window111 lstPrimitives.set TransferHandler(null);114 lstPrimitives.setTransferHandler(new SelectionTransferHandler()); 115 lstPrimitives.setDragEnabled(true); 112 116 113 117 lstPrimitives.getSelectionModel().addListSelectionListener(actSelect); … … 855 859 } 856 860 } 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 } 857 877 } -
trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTableModel.java
r9368 r9369 423 423 424 424 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);427 425 if (primitives == null) 428 426 return; 429 427 int idx = index; 430 428 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); 441 430 members.add(idx++, member); 442 431 } … … 445 434 getSelectionModel().addSelectionInterval(index, index + primitives.size() - 1); 446 435 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); 447 451 } 448 452 -
trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTransferHandler.java
r9368 r9369 16 16 import org.openstreetmap.josm.Main; 17 17 import org.openstreetmap.josm.data.osm.OsmPrimitive; 18 import org.openstreetmap.josm.data.osm.PrimitiveData; 18 19 import org.openstreetmap.josm.data.osm.RelationMember; 19 20 import org.openstreetmap.josm.data.osm.RelationMemberData; 21 import org.openstreetmap.josm.gui.datatransfer.PrimitiveTransferable; 20 22 import org.openstreetmap.josm.gui.datatransfer.RelationMemberTransferable; 23 import org.openstreetmap.josm.tools.Utils.Function; 21 24 22 25 class MemberTransferHandler extends TransferHandler { … … 36 39 public boolean canImport(TransferSupport support) { 37 40 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); 39 43 } 40 44 41 45 @Override 42 46 public boolean importData(TransferSupport support) { 47 final MemberTable destination = (MemberTable) support.getComponent(); 43 48 final int insertRow = ((JTable.DropLocation) support.getDropLocation()).getRow(); 44 final Collection<RelationMemberData> memberData; 49 45 50 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) { 49 57 Main.warn(e); 50 58 return false; 51 59 } 52 final MemberTable destination = (MemberTable) support.getComponent();53 60 54 try {55 importRelationMemberData(memberData, destination, insertRow);56 } catch (Exception e) {57 Main.warn(e);58 throw e;59 }60 61 return true; 61 62 } 62 63 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) { 64 103 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); 71 108 } 72 109 }
Note:
See TracChangeset
for help on using the changeset viewer.