source: osm/applications/editors/josm/plugins/reltoolbox/src/relcontext/actions/CreateRelationAction.java@ 36102

Last change on this file since 36102 was 36102, checked in by taylor.smock, 12 months ago

reltoolbox: Clean up a bunch of lint warnings

File size: 4.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package relcontext.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Dialog.ModalityType;
7import java.awt.GridBagLayout;
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.util.Arrays;
11import java.util.Collection;
12import java.util.List;
13
14import javax.swing.Box;
15import javax.swing.JDialog;
16import javax.swing.JLabel;
17import javax.swing.JOptionPane;
18import javax.swing.JPanel;
19
20import org.openstreetmap.josm.actions.JosmAction;
21import org.openstreetmap.josm.command.AddCommand;
22import org.openstreetmap.josm.data.UndoRedoHandler;
23import org.openstreetmap.josm.data.osm.DataSet;
24import org.openstreetmap.josm.data.osm.OsmPrimitive;
25import org.openstreetmap.josm.data.osm.Relation;
26import org.openstreetmap.josm.data.osm.RelationMember;
27import org.openstreetmap.josm.gui.MainApplication;
28import org.openstreetmap.josm.gui.tagging.ac.AutoCompComboBox;
29import org.openstreetmap.josm.spi.preferences.Config;
30import org.openstreetmap.josm.tools.GBC;
31import org.openstreetmap.josm.tools.Shortcut;
32
33import 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 */
41public 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 if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE
130 || (answer instanceof Integer && (Integer) answer != JOptionPane.OK_OPTION))
131 return null;
132
133 String result = keys.getEditor().getItem().toString().trim();
134 Config.getPref().put(PREF_LASTTYPE, result);
135 return result;
136 }
137}
Note: See TracBrowser for help on using the repository browser.