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

Last change on this file since 26811 was 25727, checked in by zverik, 14 years ago

replaced some shortcuts, added ones for create relation and multipolygon (reltoolbox plugin)

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