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

Last change on this file since 32398 was 32398, checked in by donvip, 10 years ago

checkstyle

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