[3431] | 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;
|
---|
[8984] | 8 | import java.awt.GraphicsEnvironment;
|
---|
[3431] | 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;
|
---|
[9233] | 24 | import javax.swing.JOptionPane;
|
---|
[3431] | 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;
|
---|
[10382] | 31 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[3431] | 32 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[9233] | 33 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
[12641] | 34 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[3431] | 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.tools.GBC;
|
---|
| 39 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
| 40 | import 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
|
---|
[11611] | 50 | * undo buffer, we must re-add the identical object (and not semantically equal ones).
|
---|
| 51 | *
|
---|
| 52 | * @since 3431
|
---|
[3431] | 53 | */
|
---|
| 54 | public class PurgeAction extends JosmAction {
|
---|
| 55 |
|
---|
[8308] | 56 | protected transient OsmDataLayer layer;
|
---|
[8285] | 57 | protected JCheckBox cbClearUndoRedo;
|
---|
[11252] | 58 | protected boolean modified;
|
---|
[3431] | 59 |
|
---|
| 60 | /**
|
---|
| 61 | * Subset of toPurgeChecked. Those that have not been in the selection.
|
---|
| 62 | */
|
---|
[8308] | 63 | protected transient List<OsmPrimitive> toPurgeAdditionally;
|
---|
[3479] | 64 |
|
---|
[11277] | 65 | /**
|
---|
| 66 | * Constructs a new {@code PurgeAction}.
|
---|
| 67 | */
|
---|
| 68 | public PurgeAction() {
|
---|
| 69 | /* translator note: other expressions for "purge" might be "forget", "clean", "obliterate", "prune" */
|
---|
[12688] | 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);
|
---|
[11277] | 73 | putValue("help", HelpUtil.ht("/Action/Purge"));
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[11343] | 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 |
|
---|
[3431] | 85 | @Override
|
---|
| 86 | public void actionPerformed(ActionEvent e) {
|
---|
| 87 | if (!isEnabled())
|
---|
| 88 | return;
|
---|
| 89 |
|
---|
[11252] | 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();
|
---|
[12841] | 101 | Main.pref.putBoolean("purge.clear_undo_redo", clearUndoRedo);
|
---|
[11252] | 102 | }
|
---|
| 103 |
|
---|
[12641] | 104 | MainApplication.undoRedo.add(cmd);
|
---|
[11252] | 105 | if (clearUndoRedo) {
|
---|
[12641] | 106 | MainApplication.undoRedo.clean();
|
---|
[11252] | 107 | getLayerManager().getEditDataSet().clearSelectionHistory();
|
---|
| 108 | }
|
---|
[11240] | 109 | }
|
---|
| 110 |
|
---|
| 111 | /**
|
---|
[11252] | 112 | * Creates command to purge selected OSM primitives.
|
---|
[11240] | 113 | * @param sel selected OSM primitives
|
---|
[11252] | 114 | * @return command to purge selected OSM primitives
|
---|
| 115 | * @since 11252
|
---|
[11240] | 116 | */
|
---|
[11252] | 117 | public PurgeCommand getPurgeCommand(Collection<OsmPrimitive> sel) {
|
---|
[12636] | 118 | layer = getLayerManager().getEditLayer();
|
---|
[7005] | 119 | toPurgeAdditionally = new ArrayList<>();
|
---|
[12718] | 120 | PurgeCommand cmd = PurgeCommand.build(sel, toPurgeAdditionally);
|
---|
[12688] | 121 | modified = cmd.getParticipatingPrimitives().stream().anyMatch(OsmPrimitive::isModified);
|
---|
| 122 | return cmd;
|
---|
[3431] | 123 | }
|
---|
| 124 |
|
---|
| 125 | private JPanel buildPanel(boolean modified) {
|
---|
| 126 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
| 127 |
|
---|
[8510] | 128 | pnl.add(Box.createRigidArea(new Dimension(400, 0)), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
[3431] | 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> " +
|
---|
[3479] | 133 | "They will be removed from the layer, but <i>not</i> deleted<br> " +
|
---|
[4868] | 134 | "on the server when uploading.")+"</html>",
|
---|
| 135 | ImageProvider.get("purge"), JLabel.LEFT), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
[3431] | 136 |
|
---|
| 137 | if (!toPurgeAdditionally.isEmpty()) {
|
---|
[8510] | 138 | pnl.add(new JSeparator(), GBC.eol().fill(GBC.HORIZONTAL).insets(0, 5, 0, 5));
|
---|
[3431] | 139 | pnl.add(new JLabel("<html>"+
|
---|
[3479] | 140 | tr("The following dependent objects will be purged<br> " +
|
---|
[4868] | 141 | "in addition to the selected objects:")+"</html>",
|
---|
| 142 | ImageProvider.get("warning-small"), JLabel.LEFT), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
[3431] | 143 |
|
---|
[10619] | 144 | toPurgeAdditionally.sort((o1, o2) -> {
|
---|
[10601] | 145 | int type = o2.getType().compareTo(o1.getType());
|
---|
| 146 | if (type != 0)
|
---|
| 147 | return type;
|
---|
| 148 | return Long.compare(o1.getUniqueId(), o2.getUniqueId());
|
---|
[3431] | 149 | });
|
---|
[7005] | 150 | JList<OsmPrimitive> list = new JList<>(toPurgeAdditionally.toArray(new OsmPrimitive[toPurgeAdditionally.size()]));
|
---|
[3431] | 151 | /* force selection to be active for all entries */
|
---|
[11343] | 152 | list.setCellRenderer(new SelectionForcedOsmPrimitivRenderer());
|
---|
[3431] | 153 | JScrollPane scroll = new JScrollPane(list);
|
---|
| 154 | scroll.setPreferredSize(new Dimension(250, 300));
|
---|
| 155 | scroll.setMinimumSize(new Dimension(250, 300));
|
---|
[7433] | 156 | pnl.add(scroll, GBC.std().fill(GBC.BOTH).weight(1.0, 1.0));
|
---|
[3431] | 157 |
|
---|
| 158 | JButton addToSelection = new JButton(new AbstractAction() {
|
---|
| 159 | {
|
---|
[10378] | 160 | putValue(SHORT_DESCRIPTION, tr("Add to selection"));
|
---|
[8510] | 161 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "select"));
|
---|
[3431] | 162 | }
|
---|
| 163 |
|
---|
[6084] | 164 | @Override
|
---|
[3431] | 165 | public void actionPerformed(ActionEvent e) {
|
---|
| 166 | layer.data.addSelected(toPurgeAdditionally);
|
---|
| 167 | }
|
---|
| 168 | });
|
---|
[8510] | 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));
|
---|
[3431] | 171 | }
|
---|
| 172 |
|
---|
| 173 | if (modified) {
|
---|
[8510] | 174 | pnl.add(new JSeparator(), GBC.eol().fill(GBC.HORIZONTAL).insets(0, 5, 0, 5));
|
---|
[3431] | 175 | pnl.add(new JLabel("<html>"+tr("Some of the objects are modified.<br> " +
|
---|
| 176 | "Proceed, if these changes should be discarded."+"</html>"),
|
---|
[3479] | 177 | ImageProvider.get("warning-small"), JLabel.LEFT),
|
---|
[3431] | 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 |
|
---|
[8510] | 184 | pnl.add(new JSeparator(), GBC.eol().fill(GBC.HORIZONTAL).insets(0, 5, 0, 5));
|
---|
[3431] | 185 | pnl.add(cbClearUndoRedo, GBC.eol());
|
---|
| 186 | return pnl;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | @Override
|
---|
| 190 | protected void updateEnabledState() {
|
---|
[10382] | 191 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
[11553] | 192 | setEnabled(ds != null && !ds.selectionEmpty());
|
---|
[3431] | 193 | }
|
---|
| 194 |
|
---|
| 195 | @Override
|
---|
| 196 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
| 197 | setEnabled(selection != null && !selection.isEmpty());
|
---|
| 198 | }
|
---|
| 199 | }
|
---|