| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package relcontext.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.Dialog.ModalityType;
|
|---|
| 7 | import java.awt.GridBagLayout;
|
|---|
| 8 | import java.awt.event.ActionEvent;
|
|---|
| 9 | import java.awt.event.ActionListener;
|
|---|
| 10 | import java.awt.event.KeyEvent;
|
|---|
| 11 | import java.util.Arrays;
|
|---|
| 12 | import java.util.Collection;
|
|---|
| 13 | import java.util.List;
|
|---|
| 14 |
|
|---|
| 15 | import javax.swing.Box;
|
|---|
| 16 | import javax.swing.JDialog;
|
|---|
| 17 | import javax.swing.JLabel;
|
|---|
| 18 | import javax.swing.JOptionPane;
|
|---|
| 19 | import javax.swing.JPanel;
|
|---|
| 20 |
|
|---|
| 21 | import org.openstreetmap.josm.Main;
|
|---|
| 22 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 23 | import org.openstreetmap.josm.command.AddCommand;
|
|---|
| 24 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 25 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 26 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 27 | import org.openstreetmap.josm.data.osm.RelationMember;
|
|---|
| 28 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 29 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingComboBox;
|
|---|
| 30 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 31 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 32 |
|
|---|
| 33 | import relcontext.ChosenRelation;
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * Simple create relation with no tags and all selected objects in it with no roles.
|
|---|
| 37 | * Choose relation afterwards.
|
|---|
| 38 | *
|
|---|
| 39 | * @author Zverik
|
|---|
| 40 | */
|
|---|
| 41 | public class CreateRelationAction extends JosmAction {
|
|---|
| 42 | private static final String PREF_LASTTYPE = "reltoolbox.createrelation.lasttype";
|
|---|
| 43 | protected ChosenRelation chRel;
|
|---|
| 44 |
|
|---|
| 45 | public CreateRelationAction(ChosenRelation chRel) {
|
|---|
| 46 | super(tr("New"), "data/relation", tr("Create a relation from selected objects"),
|
|---|
| 47 | Shortcut.registerShortcut("reltoolbox:create", tr("Relation Toolbox: {0}", tr("Create a new relation")),
|
|---|
| 48 | KeyEvent.VK_N, Shortcut.ALT_CTRL), false);
|
|---|
| 49 | this.chRel = chRel;
|
|---|
| 50 | updateEnabledState();
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | public CreateRelationAction() {
|
|---|
| 54 | this(null);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | @Override
|
|---|
| 58 | public void actionPerformed(ActionEvent e) {
|
|---|
| 59 | String type = askForType();
|
|---|
| 60 | if (type == null)
|
|---|
| 61 | return;
|
|---|
| 62 |
|
|---|
| 63 | Relation rel = new Relation();
|
|---|
| 64 | if (type.length() > 0) {
|
|---|
| 65 | rel.put("type", type);
|
|---|
| 66 | }
|
|---|
| 67 | DataSet ds = getLayerManager().getEditDataSet();
|
|---|
| 68 | for (OsmPrimitive selected : ds.getSelected()) {
|
|---|
| 69 | rel.addMember(new RelationMember("", selected));
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | MainApplication.undoRedo.add(new AddCommand(ds, rel));
|
|---|
| 73 |
|
|---|
| 74 | if (chRel != null) {
|
|---|
| 75 | chRel.set(rel);
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | @Override
|
|---|
| 80 | protected void updateEnabledState() {
|
|---|
| 81 | if (getLayerManager().getEditDataSet() == null) {
|
|---|
| 82 | setEnabled(false);
|
|---|
| 83 | } else {
|
|---|
| 84 | updateEnabledState(getLayerManager().getEditDataSet().getSelected());
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | @Override
|
|---|
| 89 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
|---|
| 90 | setEnabled(selection != null && !selection.isEmpty());
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | // Thanks to TagInfo for the list
|
|---|
| 94 | private static final List<String> RELATION_TYPES = Arrays.asList(new String[] {
|
|---|
| 95 | "multipolygon", "boundary", "route", "site", "restriction", "associatedStreet", "public_transport",
|
|---|
| 96 | "street", "collection", "address", "enforcement", "destination_sign", "route_master", "junction",
|
|---|
| 97 | "waterway", "bridge", "tunnel", "surveillance"
|
|---|
| 98 | });
|
|---|
| 99 |
|
|---|
| 100 | private String askForType() {
|
|---|
| 101 | JPanel panel = new JPanel(new GridBagLayout());
|
|---|
| 102 | panel.add(new JLabel(tr("Choose a type for the relation:")), GBC.eol().insets(0, 0, 0, 5));
|
|---|
| 103 |
|
|---|
| 104 | final AutoCompletingComboBox keys = new AutoCompletingComboBox();
|
|---|
| 105 | keys.setPossibleItems(RELATION_TYPES);
|
|---|
| 106 | keys.setEditable(true);
|
|---|
| 107 | keys.getEditor().setItem(Main.pref.get(PREF_LASTTYPE, "multipolygon"));
|
|---|
| 108 |
|
|---|
| 109 | panel.add(new JLabel(tr("Type")), GBC.std());
|
|---|
| 110 | panel.add(Box.createHorizontalStrut(10), GBC.std());
|
|---|
| 111 | panel.add(keys, GBC.eol().fill(GBC.HORIZONTAL));
|
|---|
| 112 |
|
|---|
| 113 | final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) {
|
|---|
| 114 | @Override
|
|---|
| 115 | public void selectInitialValue() {
|
|---|
| 116 | keys.requestFocusInWindow();
|
|---|
| 117 | keys.getEditor().selectAll();
|
|---|
| 118 | }
|
|---|
| 119 | };
|
|---|
| 120 | final JDialog dlg = optionPane.createDialog(Main.parent, tr("Create a new relation"));
|
|---|
| 121 | dlg.setModalityType(ModalityType.DOCUMENT_MODAL);
|
|---|
| 122 |
|
|---|
| 123 | keys.getEditor().addActionListener(new ActionListener() {
|
|---|
| 124 | @Override
|
|---|
| 125 | public void actionPerformed(ActionEvent e) {
|
|---|
| 126 | dlg.setVisible(false);
|
|---|
| 127 | optionPane.setValue(JOptionPane.OK_OPTION);
|
|---|
| 128 | }
|
|---|
| 129 | });
|
|---|
| 130 |
|
|---|
| 131 | dlg.setVisible(true);
|
|---|
| 132 |
|
|---|
| 133 | Object answer = optionPane.getValue();
|
|---|
| 134 | if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE
|
|---|
| 135 | || (answer instanceof Integer && (Integer) answer != JOptionPane.OK_OPTION))
|
|---|
| 136 | return null;
|
|---|
| 137 |
|
|---|
| 138 | String result = keys.getEditor().getItem().toString().trim();
|
|---|
| 139 | Main.pref.put(PREF_LASTTYPE, result);
|
|---|
| 140 | return result;
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|