source: josm/trunk/src/org/openstreetmap/josm/actions/PurgeAction.java@ 12809

Last change on this file since 12809 was 12718, checked in by Don-vip, 7 years ago

see #13036 - see #15229 - see #15182 - make Commands depends only on a DataSet, not a Layer. This removes a lot of GUI dependencies

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