- Timestamp:
- 2012-03-14T21:39:27+01:00 (13 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui/dialogs
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/dialogs/RelationListDialog.java
r5028 r5082 4 4 import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 5 5 import static org.openstreetmap.josm.tools.I18n.tr; 6 import static org.openstreetmap.josm.tools.I18n.trn; 6 7 7 8 import java.awt.Point; … … 26 27 import javax.swing.JList; 27 28 import javax.swing.JMenuItem; 28 import javax.swing.KeyStroke;29 29 import javax.swing.ListSelectionModel; 30 30 import javax.swing.SwingUtilities; … … 34 34 35 35 import org.openstreetmap.josm.Main; 36 import org.openstreetmap.josm.command.Command; 37 import org.openstreetmap.josm.command.SequenceCommand; 38 import org.openstreetmap.josm.data.SelectionChangedListener; 39 import org.openstreetmap.josm.data.osm.DataSet; 36 40 import org.openstreetmap.josm.data.osm.OsmPrimitive; 37 41 import org.openstreetmap.josm.data.osm.Relation; … … 55 59 import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask; 56 60 import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationTask; 61 import org.openstreetmap.josm.gui.dialogs.relation.GenericRelationEditor; 57 62 import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor; 58 63 import org.openstreetmap.josm.gui.layer.Layer; … … 80 85 private DeleteAction deleteAction; 81 86 private NewAction newAction; 87 private AddToRelation addToRelation; 82 88 /** the popup menu */ 83 89 private RelationDialogPopupMenu popupMenu; … … 145 151 //displaylist.getActionMap().put("deleteRelation", deleteAction); 146 152 153 addToRelation = new AddToRelation(); 147 154 popupMenu = new RelationDialogPopupMenu(displaylist); 148 155 } … … 152 159 newAction.updateEnabledState(); 153 160 DatasetEventManager.getInstance().addDatasetListener(this, FireMode.IN_EDT); 161 DataSet.addSelectionListener(addToRelation); 154 162 dataChanged(null); 155 163 } … … 158 166 MapView.removeLayerChangeListener(newAction); 159 167 DatasetEventManager.getInstance().removeDatasetListener(this); 168 DataSet.removeSelectionListener(addToRelation); 160 169 } 161 170 … … 578 587 public void valueChanged(ListSelectionEvent e) { 579 588 updateEnabledState(); 589 } 590 } 591 592 class AddToRelation extends AbstractAction implements ListSelectionListener, SelectionChangedListener { 593 594 public AddToRelation() { 595 super("", ImageProvider.get("dialogs/conflict", "copyendright")); 596 putValue(SHORT_DESCRIPTION, tr("Add all objects selected in the current dataset after the last member")); 597 setEnabled(false); 598 } 599 600 @Override 601 public void actionPerformed(ActionEvent e) { 602 Collection<Command> cmds = new LinkedList<Command>(); 603 for (Relation orig : getSelectedRelations()) { 604 Command c = GenericRelationEditor.addPrimitivesToRelation(orig, Main.main.getCurrentDataSet().getSelected()); 605 if (c != null) { 606 cmds.add(c); 607 } 608 } 609 if (!cmds.isEmpty()) { 610 Main.main.undoRedo.add(new SequenceCommand(tr("Add selection to relation"), cmds)); 611 } 612 } 613 614 @Override 615 public void valueChanged(ListSelectionEvent e) { 616 putValue(NAME, trn("Add selection to {0} relation", "Add selection to {0} relations", 617 getSelectedRelations().size(), getSelectedRelations().size())); 618 } 619 620 @Override 621 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) { 622 setEnabled(newSelection != null && !newSelection.isEmpty()); 580 623 } 581 624 } … … 795 838 796 839 // -- download members action 797 //798 840 add(new DownloadMembersAction()); 799 841 800 842 // -- download incomplete members action 801 //802 843 add(new DownloadSelectedIncompleteMembersAction()); 803 844 … … 805 846 806 847 // -- select members action 807 //808 848 add(new SelectMembersAction(false)); 809 849 add(new SelectMembersAction(true)); 810 850 811 851 // -- select action 812 //813 852 add(new SelectAction(false)); 814 853 add(new SelectAction(true)); 854 855 addSeparator(); 856 857 add(addToRelation); 815 858 } 816 859 } -
trunk/src/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditor.java
r4982 r5082 47 47 import org.openstreetmap.josm.command.AddCommand; 48 48 import org.openstreetmap.josm.command.ChangeCommand; 49 import org.openstreetmap.josm.command.Command; 49 50 import org.openstreetmap.josm.command.ConflictAddCommand; 50 51 import org.openstreetmap.josm.data.conflict.Conflict; … … 688 689 } 689 690 690 static class AddAbortException extends Exception { 691 } 692 693 abstract class AddFromSelectionAction extends AbstractAction { 691 static class AddAbortException extends Exception { 692 } 693 694 static boolean confirmAddingPrimtive(OsmPrimitive primitive) throws AddAbortException { 695 String msg = tr("<html>This relation already has one or more members referring to<br>" 696 + "the object ''{0}''<br>" 697 + "<br>" 698 + "Do you really want to add another relation member?</html>", 699 primitive.getDisplayName(DefaultNameFormatter.getInstance()) 700 ); 701 int ret = ConditionalOptionPaneUtil.showOptionDialog( 702 "add_primitive_to_relation", 703 Main.parent, 704 msg, 705 tr("Multiple members referring to same object."), 706 JOptionPane.YES_NO_CANCEL_OPTION, 707 JOptionPane.WARNING_MESSAGE, 708 null, 709 null 710 ); 711 switch(ret) { 712 case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION : return true; 713 case JOptionPane.YES_OPTION: return true; 714 case JOptionPane.NO_OPTION: return false; 715 case JOptionPane.CLOSED_OPTION: return false; 716 case JOptionPane.CANCEL_OPTION: throw new AddAbortException(); 717 } 718 // should not happen 719 return false; 720 } 721 722 static void warnOfCircularReferences(OsmPrimitive primitive) { 723 String msg = tr("<html>You are trying to add a relation to itself.<br>" 724 + "<br>" 725 + "This creates circular references and is therefore discouraged.<br>" 726 + "Skipping relation ''{0}''.</html>", 727 primitive.getDisplayName(DefaultNameFormatter.getInstance())); 728 JOptionPane.showMessageDialog( 729 Main.parent, 730 msg, 731 tr("Warning"), 732 JOptionPane.WARNING_MESSAGE); 733 } 734 735 public static Command addPrimitivesToRelation(final Relation orig, Collection<? extends OsmPrimitive> primitivesToAdd) { 736 try { 737 Relation relation = new Relation(orig); 738 boolean modified = false; 739 for (OsmPrimitive p : primitivesToAdd) { 740 if (p instanceof Relation && orig != null && orig.equals(p)) { 741 warnOfCircularReferences(p); 742 continue; 743 } else if (MemberTableModel.hasMembersReferringTo(relation.getMembers(), Collections.singleton(p)) 744 && !confirmAddingPrimtive(p)) { 745 continue; 746 } 747 relation.addMember(new RelationMember("", p)); 748 modified = true; 749 } 750 return modified ? new ChangeCommand(orig, relation) : null; 751 } catch (AddAbortException ign) { 752 return null; 753 } 754 } 755 756 abstract class AddFromSelectionAction extends AbstractAction { 694 757 protected boolean isPotentialDuplicate(OsmPrimitive primitive) { 695 758 return memberTableModel.hasMembersReferringTo(Collections.singleton(primitive)); 696 }697 698 protected boolean confirmAddingPrimtive(OsmPrimitive primitive) throws AddAbortException {699 String msg = tr("<html>This relation already has one or more members referring to<br>"700 + "the object ''{0}''<br>"701 + "<br>"702 + "Do you really want to add another relation member?</html>",703 primitive.getDisplayName(DefaultNameFormatter.getInstance())704 );705 int ret = ConditionalOptionPaneUtil.showOptionDialog(706 "add_primitive_to_relation",707 Main.parent,708 msg,709 tr("Multiple members referring to same object."),710 JOptionPane.YES_NO_CANCEL_OPTION,711 JOptionPane.WARNING_MESSAGE,712 null,713 null714 );715 switch(ret) {716 case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION : return true;717 case JOptionPane.YES_OPTION: return true;718 case JOptionPane.NO_OPTION: return false;719 case JOptionPane.CLOSED_OPTION: return false;720 case JOptionPane.CANCEL_OPTION: throw new AddAbortException();721 }722 // should not happen723 return false;724 }725 726 protected void warnOfCircularReferences(OsmPrimitive primitive) {727 String msg = tr("<html>You are trying to add a relation to itself.<br>"728 + "<br>"729 + "This creates circular references and is therefore discouraged.<br>"730 + "Skipping relation ''{0}''.</html>",731 primitive.getDisplayName(DefaultNameFormatter.getInstance())732 );733 JOptionPane.showMessageDialog(734 Main.parent,735 msg,736 tr("Warning"),737 JOptionPane.WARNING_MESSAGE738 );739 759 } 740 760 -
trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTableModel.java
r4792 r5082 560 560 561 561 /** 562 * Replies true if there is at least one relation member in this model 563 * which refers to at least on the primitives in <code>primitives</code>. 564 * 562 * Replies true if there is at least one relation member given as {@code members} 563 * which refers to at least on the primitives in {@code primitives}. 564 * 565 * @param members the members 565 566 * @param primitives the collection of primitives 566 567 * @return true if there is at least one relation member in this model … … 568 569 * otherwise 569 570 */ 571 public static boolean hasMembersReferringTo(Collection<RelationMember> members, Collection<OsmPrimitive> primitives) { 572 if (primitives == null || primitives.isEmpty()) { 573 return false; 574 } 575 HashSet<OsmPrimitive> referrers = new HashSet<OsmPrimitive>(); 576 for (RelationMember member : members) { 577 referrers.add(member.getMember()); 578 } 579 for (OsmPrimitive referred : primitives) { 580 if (referrers.contains(referred)) { 581 return true; 582 } 583 } 584 return false; 585 } 586 587 /** 588 * Replies true if there is at least one relation member in this model 589 * which refers to at least on the primitives in <code>primitives</code>. 590 * 591 * @param primitives the collection of primitives 592 * @return true if there is at least one relation member in this model 593 * which refers to at least on the primitives in <code>primitives</code>; false 594 * otherwise 595 */ 570 596 public boolean hasMembersReferringTo(Collection<OsmPrimitive> primitives) { 571 if (primitives == null || primitives.isEmpty()) 572 return false; 573 HashSet<OsmPrimitive> referrers = new HashSet<OsmPrimitive>(); 574 for(RelationMember member : members) { 575 referrers.add(member.getMember()); 576 } 577 Iterator<OsmPrimitive> it = primitives.iterator(); 578 while(it.hasNext()) { 579 OsmPrimitive referred = it.next(); 580 if (referrers.contains(referred)) 581 return true; 582 } 583 return false; 597 return hasMembersReferringTo(members, primitives); 584 598 } 585 599
Note:
See TracChangeset
for help on using the changeset viewer.