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