1 | package relcontext.actions;
|
---|
2 |
|
---|
3 | import java.awt.Dialog.ModalityType;
|
---|
4 | import java.awt.GridBagLayout;
|
---|
5 | import java.util.Collection;
|
---|
6 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
7 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.ActionListener;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.util.Arrays;
|
---|
12 | import java.util.List;
|
---|
13 | import javax.swing.Box;
|
---|
14 | import javax.swing.JDialog;
|
---|
15 | import javax.swing.JLabel;
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
20 | import org.openstreetmap.josm.command.AddCommand;
|
---|
21 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
22 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
23 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingComboBox;
|
---|
24 | import org.openstreetmap.josm.tools.GBC;
|
---|
25 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
26 | import 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 | */
|
---|
34 | public 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 | }
|
---|