1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.GraphicsEnvironment;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.Insets;
|
---|
11 | import java.awt.event.ActionEvent;
|
---|
12 | import java.awt.event.KeyEvent;
|
---|
13 | import java.util.ArrayList;
|
---|
14 | import java.util.Collection;
|
---|
15 | import java.util.List;
|
---|
16 |
|
---|
17 | import javax.swing.AbstractAction;
|
---|
18 | import javax.swing.BorderFactory;
|
---|
19 | import javax.swing.Box;
|
---|
20 | import javax.swing.JButton;
|
---|
21 | import javax.swing.JCheckBox;
|
---|
22 | import javax.swing.JLabel;
|
---|
23 | import javax.swing.JList;
|
---|
24 | import javax.swing.JOptionPane;
|
---|
25 | import javax.swing.JPanel;
|
---|
26 | import javax.swing.JScrollPane;
|
---|
27 | import javax.swing.JSeparator;
|
---|
28 |
|
---|
29 | import org.openstreetmap.josm.Main;
|
---|
30 | import org.openstreetmap.josm.command.PurgeCommand;
|
---|
31 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
32 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
33 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
34 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
35 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
36 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
37 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
38 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
39 | import org.openstreetmap.josm.tools.GBC;
|
---|
40 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
41 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * The action to purge the selected primitives, i.e. remove them from the
|
---|
45 | * data layer, or remove their content and make them incomplete.
|
---|
46 | *
|
---|
47 | * This means, the deleted flag is not affected and JOSM simply forgets
|
---|
48 | * about these primitives.
|
---|
49 | *
|
---|
50 | * This action is undo-able. In order not to break previous commands in the
|
---|
51 | * undo buffer, we must re-add the identical object (and not semantically equal ones).
|
---|
52 | *
|
---|
53 | * @since 3431
|
---|
54 | */
|
---|
55 | public class PurgeAction extends JosmAction {
|
---|
56 |
|
---|
57 | protected transient OsmDataLayer layer;
|
---|
58 | protected JCheckBox cbClearUndoRedo;
|
---|
59 | protected boolean modified;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Subset of toPurgeChecked. Those that have not been in the selection.
|
---|
63 | */
|
---|
64 | protected transient List<OsmPrimitive> toPurgeAdditionally;
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Constructs a new {@code PurgeAction}.
|
---|
68 | */
|
---|
69 | public PurgeAction() {
|
---|
70 | /* translator note: other expressions for "purge" might be "forget", "clean", "obliterate", "prune" */
|
---|
71 | super(tr("Purge..."), "purge", tr("Forget objects but do not delete them on server when uploading."),
|
---|
72 | Shortcut.registerShortcut("system:purge", tr("Edit: {0}", tr("Purge")), KeyEvent.VK_P, Shortcut.CTRL_SHIFT),
|
---|
73 | true);
|
---|
74 | putValue("help", HelpUtil.ht("/Action/Purge"));
|
---|
75 | }
|
---|
76 |
|
---|
77 | /** force selection to be active for all entries */
|
---|
78 | static class SelectionForcedOsmPrimitivRenderer extends OsmPrimitivRenderer {
|
---|
79 | @Override
|
---|
80 | public Component getListCellRendererComponent(JList<? extends OsmPrimitive> list,
|
---|
81 | OsmPrimitive value, int index, boolean isSelected, boolean cellHasFocus) {
|
---|
82 | return super.getListCellRendererComponent(list, value, index, true, false);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | @Override
|
---|
87 | public void actionPerformed(ActionEvent e) {
|
---|
88 | if (!isEnabled())
|
---|
89 | return;
|
---|
90 |
|
---|
91 | PurgeCommand cmd = getPurgeCommand(getLayerManager().getEditDataSet().getAllSelected());
|
---|
92 | boolean clearUndoRedo = false;
|
---|
93 |
|
---|
94 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
95 | final boolean answer = ConditionalOptionPaneUtil.showConfirmationDialog(
|
---|
96 | "purge", Main.parent, buildPanel(modified), tr("Confirm Purging"),
|
---|
97 | JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_OPTION);
|
---|
98 | if (!answer)
|
---|
99 | return;
|
---|
100 |
|
---|
101 | clearUndoRedo = cbClearUndoRedo.isSelected();
|
---|
102 | Config.getPref().putBoolean("purge.clear_undo_redo", clearUndoRedo);
|
---|
103 | }
|
---|
104 |
|
---|
105 | MainApplication.undoRedo.add(cmd);
|
---|
106 | if (clearUndoRedo) {
|
---|
107 | MainApplication.undoRedo.clean();
|
---|
108 | getLayerManager().getEditDataSet().clearSelectionHistory();
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Creates command to purge selected OSM primitives.
|
---|
114 | * @param sel selected OSM primitives
|
---|
115 | * @return command to purge selected OSM primitives
|
---|
116 | * @since 11252
|
---|
117 | */
|
---|
118 | public PurgeCommand getPurgeCommand(Collection<OsmPrimitive> sel) {
|
---|
119 | layer = getLayerManager().getEditLayer();
|
---|
120 | toPurgeAdditionally = new ArrayList<>();
|
---|
121 | PurgeCommand cmd = PurgeCommand.build(sel, toPurgeAdditionally);
|
---|
122 | modified = cmd.getParticipatingPrimitives().stream().anyMatch(OsmPrimitive::isModified);
|
---|
123 | return cmd;
|
---|
124 | }
|
---|
125 |
|
---|
126 | private JPanel buildPanel(boolean modified) {
|
---|
127 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
128 |
|
---|
129 | pnl.add(Box.createRigidArea(new Dimension(400, 0)), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
130 |
|
---|
131 | pnl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
---|
132 | pnl.add(new JLabel("<html>"+
|
---|
133 | tr("This operation makes JOSM forget the selected objects.<br> " +
|
---|
134 | "They will be removed from the layer, but <i>not</i> deleted<br> " +
|
---|
135 | "on the server when uploading.")+"</html>",
|
---|
136 | ImageProvider.get("purge"), JLabel.LEFT), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
137 |
|
---|
138 | if (!toPurgeAdditionally.isEmpty()) {
|
---|
139 | pnl.add(new JSeparator(), GBC.eol().fill(GBC.HORIZONTAL).insets(0, 5, 0, 5));
|
---|
140 | pnl.add(new JLabel("<html>"+
|
---|
141 | tr("The following dependent objects will be purged<br> " +
|
---|
142 | "in addition to the selected objects:")+"</html>",
|
---|
143 | ImageProvider.get("warning-small"), JLabel.LEFT), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
144 |
|
---|
145 | toPurgeAdditionally.sort((o1, o2) -> {
|
---|
146 | int type = o2.getType().compareTo(o1.getType());
|
---|
147 | if (type != 0)
|
---|
148 | return type;
|
---|
149 | return Long.compare(o1.getUniqueId(), o2.getUniqueId());
|
---|
150 | });
|
---|
151 | JList<OsmPrimitive> list = new JList<>(toPurgeAdditionally.toArray(new OsmPrimitive[0]));
|
---|
152 | /* force selection to be active for all entries */
|
---|
153 | list.setCellRenderer(new SelectionForcedOsmPrimitivRenderer());
|
---|
154 | JScrollPane scroll = new JScrollPane(list);
|
---|
155 | scroll.setPreferredSize(new Dimension(250, 300));
|
---|
156 | scroll.setMinimumSize(new Dimension(250, 300));
|
---|
157 | pnl.add(scroll, GBC.std().fill(GBC.BOTH).weight(1.0, 1.0));
|
---|
158 |
|
---|
159 | JButton addToSelection = new JButton(new AbstractAction() {
|
---|
160 | {
|
---|
161 | putValue(SHORT_DESCRIPTION, tr("Add to selection"));
|
---|
162 | new ImageProvider("dialogs", "select").getResource().attachImageIcon(this, true);
|
---|
163 | }
|
---|
164 |
|
---|
165 | @Override
|
---|
166 | public void actionPerformed(ActionEvent e) {
|
---|
167 | layer.data.addSelected(toPurgeAdditionally);
|
---|
168 | }
|
---|
169 | });
|
---|
170 | addToSelection.setMargin(new Insets(0, 0, 0, 0));
|
---|
171 | pnl.add(addToSelection, GBC.eol().anchor(GBC.SOUTHWEST).weight(0.0, 1.0).insets(2, 0, 0, 3));
|
---|
172 | }
|
---|
173 |
|
---|
174 | if (modified) {
|
---|
175 | pnl.add(new JSeparator(), GBC.eol().fill(GBC.HORIZONTAL).insets(0, 5, 0, 5));
|
---|
176 | pnl.add(new JLabel("<html>"+tr("Some of the objects are modified.<br> " +
|
---|
177 | "Proceed, if these changes should be discarded."+"</html>"),
|
---|
178 | ImageProvider.get("warning-small"), JLabel.LEFT),
|
---|
179 | GBC.eol().fill(GBC.HORIZONTAL));
|
---|
180 | }
|
---|
181 |
|
---|
182 | cbClearUndoRedo = new JCheckBox(tr("Clear Undo/Redo buffer"));
|
---|
183 | cbClearUndoRedo.setSelected(Config.getPref().getBoolean("purge.clear_undo_redo", false));
|
---|
184 |
|
---|
185 | pnl.add(new JSeparator(), GBC.eol().fill(GBC.HORIZONTAL).insets(0, 5, 0, 5));
|
---|
186 | pnl.add(cbClearUndoRedo, GBC.eol());
|
---|
187 | return pnl;
|
---|
188 | }
|
---|
189 |
|
---|
190 | @Override
|
---|
191 | protected void updateEnabledState() {
|
---|
192 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
193 | setEnabled(ds != null && !ds.selectionEmpty());
|
---|
194 | }
|
---|
195 |
|
---|
196 | @Override
|
---|
197 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
198 | updateEnabledStateOnModifiableSelection(selection);
|
---|
199 | }
|
---|
200 | }
|
---|