|
| 1 | +package org.openstreetmap.josm.plugins.pt_assistant.actions; |
| 2 | + |
| 3 | +import java.awt.event.ActionEvent; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Optional; |
| 8 | + |
| 9 | +import javax.swing.JOptionPane; |
| 10 | + |
| 11 | +import org.openstreetmap.josm.data.osm.Relation; |
| 12 | +import org.openstreetmap.josm.data.osm.RelationMember; |
| 13 | +import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor; |
| 14 | +import org.openstreetmap.josm.gui.dialogs.relation.actions.AbstractRelationEditorAction; |
| 15 | +import org.openstreetmap.josm.gui.dialogs.relation.actions.IRelationEditorActionAccess; |
| 16 | +import org.openstreetmap.josm.gui.dialogs.relation.actions.IRelationEditorUpdateOn; |
| 17 | +import org.openstreetmap.josm.tools.I18n; |
| 18 | +import org.openstreetmap.josm.tools.ImageProvider; |
| 19 | + |
| 20 | +public class ExtractRelationMembersToNewRelationAction extends AbstractRelationEditorAction { |
| 21 | + public ExtractRelationMembersToNewRelationAction(IRelationEditorActionAccess editorAccess) { |
| 22 | + super(editorAccess, IRelationEditorUpdateOn.MEMBER_TABLE_SELECTION, IRelationEditorUpdateOn.MEMBER_TABLE_CHANGE, IRelationEditorUpdateOn.TAG_CHANGE); |
| 23 | + new ImageProvider("bus").getResource().attachImageIcon(this); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public void actionPerformed(ActionEvent actionEvent) { |
| 28 | + final Collection<RelationMember> selectedMembers = editorAccess.getMemberTableModel().getSelectedMembers(); |
| 29 | + final Map<String, String> tags = editorAccess.getTagModel().getTags(); |
| 30 | + |
| 31 | + final Relation extractedRelation = new Relation(); |
| 32 | + extractedRelation.setKeys(tags); |
| 33 | + for (final RelationMember member : selectedMembers) { |
| 34 | + extractedRelation.addMember(member); |
| 35 | + } |
| 36 | + |
| 37 | + if ( |
| 38 | + JOptionPane.showConfirmDialog( |
| 39 | + getMemberTable(), |
| 40 | + I18n.tr( |
| 41 | + "Do you really want to create a new relation from the {0} selected members of relation {1}, copying the {2} tags to the new relation?", |
| 42 | + selectedMembers.size(), |
| 43 | + getEditor().getRelation().getId(), |
| 44 | + tags.size() |
| 45 | + ), |
| 46 | + I18n.tr("Extract part from relation?"), |
| 47 | + JOptionPane.OK_CANCEL_OPTION, |
| 48 | + JOptionPane.QUESTION_MESSAGE |
| 49 | + ) == JOptionPane.OK_OPTION |
| 50 | + ) { |
| 51 | + getEditor().getLayer().data.addPrimitive(extractedRelation); |
| 52 | + RelationEditor.getEditor(getEditor().getLayer(), extractedRelation, Collections.emptyList()).setVisible(true); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public boolean isExpertOnly() { |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + protected void updateEnabledState() { |
| 63 | + final boolean newEnabledState = !editorAccess.getSelectionTableModel().getSelection().isEmpty() |
| 64 | + && Optional.ofNullable(editorAccess.getTagModel().get("type")).filter(it -> "route".equals(it.getValue())).isPresent(); |
| 65 | + |
| 66 | + putValue(SHORT_DESCRIPTION, ( |
| 67 | + newEnabledState |
| 68 | + ? I18n.tr("Extract part of the route into new relation") |
| 69 | + : I18n.tr("Extract into new relation (needs type=route tag and at least one selected relation member)") |
| 70 | + )); |
| 71 | + setEnabled(newEnabledState); |
| 72 | + } |
| 73 | +} |
0 commit comments