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.KeyEvent;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import javax.swing.Box;
|
---|
15 | import javax.swing.JDialog;
|
---|
16 | import javax.swing.JLabel;
|
---|
17 | import javax.swing.JOptionPane;
|
---|
18 | import javax.swing.JPanel;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
21 | import org.openstreetmap.josm.command.AddCommand;
|
---|
22 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
23 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
24 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
25 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
26 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
27 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
28 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompComboBox;
|
---|
29 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
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 | UndoRedoHandler.getInstance().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("multipolygon", "boundary", "route", "site",
|
---|
95 | "restriction", "associatedStreet", "public_transport", "street", "collection", "address", "enforcement",
|
---|
96 | "destination_sign", "route_master", "junction", "waterway", "bridge", "tunnel", "surveillance");
|
---|
97 |
|
---|
98 | private String askForType() {
|
---|
99 | JPanel panel = new JPanel(new GridBagLayout());
|
---|
100 | panel.add(new JLabel(tr("Choose a type for the relation:")), GBC.eol().insets(0, 0, 0, 5));
|
---|
101 |
|
---|
102 | final AutoCompComboBox<String> keys = new AutoCompComboBox<>();
|
---|
103 | keys.getModel().addAllElements(RELATION_TYPES);
|
---|
104 | keys.setEditable(true);
|
---|
105 | keys.getEditor().setItem(Config.getPref().get(PREF_LASTTYPE, "multipolygon"));
|
---|
106 |
|
---|
107 | panel.add(new JLabel(tr("Type")), GBC.std());
|
---|
108 | panel.add(Box.createHorizontalStrut(10), GBC.std());
|
---|
109 | panel.add(keys, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
110 |
|
---|
111 | final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) {
|
---|
112 | @Override
|
---|
113 | public void selectInitialValue() {
|
---|
114 | keys.requestFocusInWindow();
|
---|
115 | keys.getEditor().selectAll();
|
---|
116 | }
|
---|
117 | };
|
---|
118 | final JDialog dlg = optionPane.createDialog(MainApplication.getMainFrame(), tr("Create a new relation"));
|
---|
119 | dlg.setModalityType(ModalityType.DOCUMENT_MODAL);
|
---|
120 |
|
---|
121 | keys.getEditor().addActionListener(e -> {
|
---|
122 | dlg.setVisible(false);
|
---|
123 | optionPane.setValue(JOptionPane.OK_OPTION);
|
---|
124 | });
|
---|
125 |
|
---|
126 | dlg.setVisible(true);
|
---|
127 |
|
---|
128 | Object answer = optionPane.getValue();
|
---|
129 | dlg.dispose();
|
---|
130 | if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE
|
---|
131 | || (answer instanceof Integer && (Integer) answer != JOptionPane.OK_OPTION))
|
---|
132 | return null;
|
---|
133 |
|
---|
134 | String result = keys.getEditor().getItem().toString().trim();
|
---|
135 | Config.getPref().put(PREF_LASTTYPE, result);
|
---|
136 | return result;
|
---|
137 | }
|
---|
138 | }
|
---|