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

Last change on this file since 13261 was 13206, checked in by Don-vip, 6 years ago

enable PMD rule OptimizableToArrayCall

  • Property svn:eol-style set to native
File size: 8.1 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.spi.preferences.Config;
39import org.openstreetmap.josm.tools.GBC;
40import org.openstreetmap.josm.tools.ImageProvider;
41import 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 */
55public 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 setEnabled(selection != null && !selection.isEmpty());
199 }
200}
Note: See TracBrowser for help on using the repository browser.