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

Last change on this file since 35602 was 34551, checked in by donvip, 8 years ago

update to JOSM 14153

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