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

Last change on this file was 36217, checked in by GerdP, 9 months ago

fix #23521: fix some memory leaks

  • dispose dialogs
  • either avoid to create clones of ways or relations or use setNodes(null) / setMembers(null)
  • replaces most ChangeCommand instances by better specialized alternatives
  • add some comments
  • fix some checkstyle / sonar issues
File size: 4.9 KB
RevLine 
[32395]1// License: GPL. For details, see LICENSE file.
[25649]2package relcontext.actions;
3
[32395]4import static org.openstreetmap.josm.tools.I18n.tr;
5
[25672]6import java.awt.Dialog.ModalityType;
7import java.awt.GridBagLayout;
[25649]8import java.awt.event.ActionEvent;
[25727]9import java.awt.event.KeyEvent;
[25672]10import java.util.Arrays;
[32395]11import java.util.Collection;
[25672]12import java.util.List;
[32395]13
[25672]14import javax.swing.Box;
15import javax.swing.JDialog;
16import javax.swing.JLabel;
17import javax.swing.JOptionPane;
18import javax.swing.JPanel;
[32395]19
[25649]20import org.openstreetmap.josm.actions.JosmAction;
21import org.openstreetmap.josm.command.AddCommand;
[34551]22import org.openstreetmap.josm.data.UndoRedoHandler;
[33694]23import org.openstreetmap.josm.data.osm.DataSet;
[32395]24import org.openstreetmap.josm.data.osm.OsmPrimitive;
[25649]25import org.openstreetmap.josm.data.osm.Relation;
26import org.openstreetmap.josm.data.osm.RelationMember;
[33530]27import org.openstreetmap.josm.gui.MainApplication;
[35829]28import org.openstreetmap.josm.gui.tagging.ac.AutoCompComboBox;
[34551]29import org.openstreetmap.josm.spi.preferences.Config;
[25672]30import org.openstreetmap.josm.tools.GBC;
[25727]31import org.openstreetmap.josm.tools.Shortcut;
[32395]32
[25649]33import relcontext.ChosenRelation;
34
35/**
36 * Simple create relation with no tags and all selected objects in it with no roles.
37 * Choose relation afterwards.
38 *
39 * @author Zverik
40 */
41public class CreateRelationAction extends JosmAction {
[25692]42 private static final String PREF_LASTTYPE = "reltoolbox.createrelation.lasttype";
[25667]43 protected ChosenRelation chRel;
[25649]44
[32395]45 public CreateRelationAction(ChosenRelation chRel) {
[25727]46 super(tr("New"), "data/relation", tr("Create a relation from selected objects"),
47 Shortcut.registerShortcut("reltoolbox:create", tr("Relation Toolbox: {0}", tr("Create a new relation")),
[32395]48 KeyEvent.VK_N, Shortcut.ALT_CTRL), false);
[25667]49 this.chRel = chRel;
50 updateEnabledState();
51 }
[25649]52
[25667]53 public CreateRelationAction() {
54 this(null);
55 }
[25649]56
[32395]57 @Override
58 public void actionPerformed(ActionEvent e) {
[25672]59 String type = askForType();
[32398]60 if (type == null)
[25672]61 return;
[25649]62
[25667]63 Relation rel = new Relation();
[32398]64 if (type.length() > 0) {
[25667]65 rel.put("type", type);
[32395]66 }
[33694]67 DataSet ds = getLayerManager().getEditDataSet();
68 for (OsmPrimitive selected : ds.getSelected()) {
[25667]69 rel.addMember(new RelationMember("", selected));
[32395]70 }
[25649]71
[34551]72 UndoRedoHandler.getInstance().add(new AddCommand(ds, rel));
[25649]73
[32395]74 if (chRel != null) {
[25667]75 chRel.set(rel);
76 }
77 }
[25649]78
[25667]79 @Override
80 protected void updateEnabledState() {
[32398]81 if (getLayerManager().getEditDataSet() == null) {
[25667]82 setEnabled(false);
83 } else {
[32398]84 updateEnabledState(getLayerManager().getEditDataSet().getSelected());
[25667]85 }
86 }
[25649]87
[25667]88 @Override
[32395]89 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
[25667]90 setEnabled(selection != null && !selection.isEmpty());
91 }
[25672]92
[25679]93 // Thanks to TagInfo for the list
[36102]94 private static final List<String> RELATION_TYPES = Arrays.asList("multipolygon", "boundary", "route", "site",
95 "restriction", "associatedStreet", "public_transport", "street", "collection", "address", "enforcement",
96 "destination_sign", "route_master", "junction", "waterway", "bridge", "tunnel", "surveillance");
[25672]97
98 private String askForType() {
99 JPanel panel = new JPanel(new GridBagLayout());
[25695]100 panel.add(new JLabel(tr("Choose a type for the relation:")), GBC.eol().insets(0, 0, 0, 5));
[25672]101
[35829]102 final AutoCompComboBox<String> keys = new AutoCompComboBox<>();
103 keys.getModel().addAllElements(RELATION_TYPES);
[25672]104 keys.setEditable(true);
[34551]105 keys.getEditor().setItem(Config.getPref().get(PREF_LASTTYPE, "multipolygon"));
[25672]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 };
[34551]118 final JDialog dlg = optionPane.createDialog(MainApplication.getMainFrame(), tr("Create a new relation"));
[25672]119 dlg.setModalityType(ModalityType.DOCUMENT_MODAL);
120
[36102]121 keys.getEditor().addActionListener(e -> {
122 dlg.setVisible(false);
123 optionPane.setValue(JOptionPane.OK_OPTION);
[25672]124 });
125
126 dlg.setVisible(true);
127
128 Object answer = optionPane.getValue();
[36217]129 dlg.dispose();
[32395]130 if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE
[32398]131 || (answer instanceof Integer && (Integer) answer != JOptionPane.OK_OPTION))
[25672]132 return null;
133
[25692]134 String result = keys.getEditor().getItem().toString().trim();
[34551]135 Config.getPref().put(PREF_LASTTYPE, result);
[25692]136 return result;
[25672]137 }
[25649]138}
Note: See TracBrowser for help on using the repository browser.