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