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

Last change on this file since 33530 was 33530, checked in by donvip, 7 years ago

update to JOSM 12663

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.ActionListener;
10import java.awt.event.KeyEvent;
11import java.util.Arrays;
12import java.util.Collection;
13import java.util.List;
14
15import javax.swing.Box;
16import javax.swing.JDialog;
17import javax.swing.JLabel;
18import javax.swing.JOptionPane;
19import javax.swing.JPanel;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.actions.JosmAction;
23import org.openstreetmap.josm.command.AddCommand;
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.AutoCompletingComboBox;
29import org.openstreetmap.josm.tools.GBC;
30import org.openstreetmap.josm.tools.Shortcut;
31
32import relcontext.ChosenRelation;
33
34/**
35 * Simple create relation with no tags and all selected objects in it with no roles.
36 * Choose relation afterwards.
37 *
38 * @author Zverik
39 */
40public class CreateRelationAction extends JosmAction {
41 private static final String PREF_LASTTYPE = "reltoolbox.createrelation.lasttype";
42 protected ChosenRelation chRel;
43
44 public CreateRelationAction(ChosenRelation chRel) {
45 super(tr("New"), "data/relation", tr("Create a relation from selected objects"),
46 Shortcut.registerShortcut("reltoolbox:create", tr("Relation Toolbox: {0}", tr("Create a new relation")),
47 KeyEvent.VK_N, Shortcut.ALT_CTRL), false);
48 this.chRel = chRel;
49 updateEnabledState();
50 }
51
52 public CreateRelationAction() {
53 this(null);
54 }
55
56 @Override
57 public void actionPerformed(ActionEvent e) {
58 String type = askForType();
59 if (type == null)
60 return;
61
62 Relation rel = new Relation();
63 if (type.length() > 0) {
64 rel.put("type", type);
65 }
66 for (OsmPrimitive selected : getLayerManager().getEditDataSet().getSelected()) {
67 rel.addMember(new RelationMember("", selected));
68 }
69
70 MainApplication.undoRedo.add(new AddCommand(rel));
71
72 if (chRel != null) {
73 chRel.set(rel);
74 }
75 }
76
77 @Override
78 protected void updateEnabledState() {
79 if (getLayerManager().getEditDataSet() == null) {
80 setEnabled(false);
81 } else {
82 updateEnabledState(getLayerManager().getEditDataSet().getSelected());
83 }
84 }
85
86 @Override
87 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
88 setEnabled(selection != null && !selection.isEmpty());
89 }
90
91 // Thanks to TagInfo for the list
92 private static final List<String> RELATION_TYPES = Arrays.asList(new String[] {
93 "multipolygon", "boundary", "route", "site", "restriction", "associatedStreet", "public_transport",
94 "street", "collection", "address", "enforcement", "destination_sign", "route_master", "junction",
95 "waterway", "bridge", "tunnel", "surveillance"
96 });
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 AutoCompletingComboBox keys = new AutoCompletingComboBox();
103 keys.setPossibleItems(RELATION_TYPES);
104 keys.setEditable(true);
105 keys.getEditor().setItem(Main.pref.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(Main.parent, tr("Create a new relation"));
119 dlg.setModalityType(ModalityType.DOCUMENT_MODAL);
120
121 keys.getEditor().addActionListener(new ActionListener() {
122 @Override
123 public void actionPerformed(ActionEvent e) {
124 dlg.setVisible(false);
125 optionPane.setValue(JOptionPane.OK_OPTION);
126 }
127 });
128
129 dlg.setVisible(true);
130
131 Object answer = optionPane.getValue();
132 if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE
133 || (answer instanceof Integer && (Integer) answer != JOptionPane.OK_OPTION))
134 return null;
135
136 String result = keys.getEditor().getItem().toString().trim();
137 Main.pref.put(PREF_LASTTYPE, result);
138 return result;
139 }
140}
Note: See TracBrowser for help on using the repository browser.