| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.dialogs;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
|---|
| 7 |
|
|---|
| 8 | import java.awt.BorderLayout;
|
|---|
| 9 | import java.awt.Dimension;
|
|---|
| 10 | import java.awt.FlowLayout;
|
|---|
| 11 | import java.awt.event.ActionEvent;
|
|---|
| 12 | import java.awt.event.WindowAdapter;
|
|---|
| 13 | import java.awt.event.WindowEvent;
|
|---|
| 14 | import java.util.ArrayList;
|
|---|
| 15 | import java.util.Collection;
|
|---|
| 16 | import java.util.Collections;
|
|---|
| 17 | import java.util.Comparator;
|
|---|
| 18 | import java.util.HashSet;
|
|---|
| 19 | import java.util.List;
|
|---|
| 20 | import java.util.Set;
|
|---|
| 21 |
|
|---|
| 22 | import javax.swing.AbstractAction;
|
|---|
| 23 | import javax.swing.JDialog;
|
|---|
| 24 | import javax.swing.JPanel;
|
|---|
| 25 | import javax.swing.JScrollPane;
|
|---|
| 26 | import javax.swing.JTable;
|
|---|
| 27 | import javax.swing.event.TableModelEvent;
|
|---|
| 28 | import javax.swing.event.TableModelListener;
|
|---|
| 29 | import javax.swing.table.DefaultTableColumnModel;
|
|---|
| 30 | import javax.swing.table.DefaultTableModel;
|
|---|
| 31 | import javax.swing.table.TableColumn;
|
|---|
| 32 |
|
|---|
| 33 | import org.openstreetmap.josm.Main;
|
|---|
| 34 | import org.openstreetmap.josm.data.osm.NameFormatter;
|
|---|
| 35 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 36 | import org.openstreetmap.josm.data.osm.RelationToChildReference;
|
|---|
| 37 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
|---|
| 38 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
|---|
| 39 | import org.openstreetmap.josm.gui.SideButton;
|
|---|
| 40 | import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
|
|---|
| 41 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
|---|
| 42 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
|---|
| 43 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
|---|
| 44 | import org.openstreetmap.josm.tools.I18n;
|
|---|
| 45 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 46 | import org.openstreetmap.josm.tools.WindowGeometry;
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * This dialog is used to get a user confirmation that a collection of primitives can be removed
|
|---|
| 50 | * from their parent relations.
|
|---|
| 51 | * @since 2308
|
|---|
| 52 | */
|
|---|
| 53 | public class DeleteFromRelationConfirmationDialog extends JDialog implements TableModelListener {
|
|---|
| 54 | /** the unique instance of this dialog */
|
|---|
| 55 | private static DeleteFromRelationConfirmationDialog instance;
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * Replies the unique instance of this dialog
|
|---|
| 59 | *
|
|---|
| 60 | * @return The unique instance of this dialog
|
|---|
| 61 | */
|
|---|
| 62 | public static synchronized DeleteFromRelationConfirmationDialog getInstance() {
|
|---|
| 63 | if (instance == null) {
|
|---|
| 64 | instance = new DeleteFromRelationConfirmationDialog();
|
|---|
| 65 | }
|
|---|
| 66 | return instance;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /** the data model */
|
|---|
| 70 | private RelationMemberTableModel model;
|
|---|
| 71 | private final HtmlPanel htmlPanel = new HtmlPanel();
|
|---|
| 72 | private boolean canceled;
|
|---|
| 73 | private final SideButton btnOK = new SideButton(new OKAction());
|
|---|
| 74 |
|
|---|
| 75 | protected JPanel buildRelationMemberTablePanel() {
|
|---|
| 76 | JTable table = new JTable(model, new RelationMemberTableColumnModel());
|
|---|
| 77 | JPanel pnl = new JPanel(new BorderLayout());
|
|---|
| 78 | pnl.add(new JScrollPane(table));
|
|---|
| 79 | return pnl;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | protected JPanel buildButtonPanel() {
|
|---|
| 83 | JPanel pnl = new JPanel(new FlowLayout());
|
|---|
| 84 | pnl.add(btnOK);
|
|---|
| 85 | btnOK.setFocusable(true);
|
|---|
| 86 | pnl.add(new SideButton(new CancelAction()));
|
|---|
| 87 | pnl.add(new SideButton(new ContextSensitiveHelpAction(ht("/Action/Delete#DeleteFromRelations"))));
|
|---|
| 88 | return pnl;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | protected final void build() {
|
|---|
| 92 | model = new RelationMemberTableModel();
|
|---|
| 93 | model.addTableModelListener(this);
|
|---|
| 94 | getContentPane().setLayout(new BorderLayout());
|
|---|
| 95 | getContentPane().add(htmlPanel, BorderLayout.NORTH);
|
|---|
| 96 | getContentPane().add(buildRelationMemberTablePanel(), BorderLayout.CENTER);
|
|---|
| 97 | getContentPane().add(buildButtonPanel(), BorderLayout.SOUTH);
|
|---|
| 98 |
|
|---|
| 99 | HelpUtil.setHelpContext(this.getRootPane(), ht("/Action/Delete#DeleteFromRelations"));
|
|---|
| 100 |
|
|---|
| 101 | addWindowListener(new WindowEventHandler());
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | protected void updateMessage() {
|
|---|
| 105 | int numObjectsToDelete = model.getNumObjectsToDelete();
|
|---|
| 106 | int numParentRelations = model.getNumParentRelations();
|
|---|
| 107 | final String msg1 = trn(
|
|---|
| 108 | "Please confirm to remove <strong>{0} object</strong>.",
|
|---|
| 109 | "Please confirm to remove <strong>{0} objects</strong>.",
|
|---|
| 110 | numObjectsToDelete, numObjectsToDelete);
|
|---|
| 111 | final String msg2 = trn(
|
|---|
| 112 | "{0} relation is affected.",
|
|---|
| 113 | "{0} relations are affected.",
|
|---|
| 114 | numParentRelations, numParentRelations);
|
|---|
| 115 | @I18n.QuirkyPluralString
|
|---|
| 116 | final String msg = "<html>" + msg1 + ' ' + msg2 + "</html>";
|
|---|
| 117 | htmlPanel.getEditorPane().setText(msg);
|
|---|
| 118 | invalidate();
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | protected void updateTitle() {
|
|---|
| 122 | int numObjectsToDelete = model.getNumObjectsToDelete();
|
|---|
| 123 | if (numObjectsToDelete > 0) {
|
|---|
| 124 | setTitle(trn("Deleting {0} object", "Deleting {0} objects", numObjectsToDelete, numObjectsToDelete));
|
|---|
| 125 | } else {
|
|---|
| 126 | setTitle(tr("Delete objects"));
|
|---|
| 127 | }
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /**
|
|---|
| 131 | * Constructs a new {@code DeleteFromRelationConfirmationDialog}.
|
|---|
| 132 | */
|
|---|
| 133 | public DeleteFromRelationConfirmationDialog() {
|
|---|
| 134 | super(GuiHelper.getFrameForComponent(Main.parent), "", ModalityType.DOCUMENT_MODAL);
|
|---|
| 135 | build();
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /**
|
|---|
| 139 | * Replies the data model used in this dialog
|
|---|
| 140 | *
|
|---|
| 141 | * @return the data model
|
|---|
| 142 | */
|
|---|
| 143 | public RelationMemberTableModel getModel() {
|
|---|
| 144 | return model;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /**
|
|---|
| 148 | * Replies true if the dialog was canceled
|
|---|
| 149 | *
|
|---|
| 150 | * @return true if the dialog was canceled
|
|---|
| 151 | */
|
|---|
| 152 | public boolean isCanceled() {
|
|---|
| 153 | return canceled;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | protected void setCanceled(boolean canceled) {
|
|---|
| 157 | this.canceled = canceled;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | @Override
|
|---|
| 161 | public void setVisible(boolean visible) {
|
|---|
| 162 | if (visible) {
|
|---|
| 163 | new WindowGeometry(
|
|---|
| 164 | getClass().getName() + ".geometry",
|
|---|
| 165 | WindowGeometry.centerInWindow(
|
|---|
| 166 | Main.parent,
|
|---|
| 167 | new Dimension(400, 200)
|
|---|
| 168 | )
|
|---|
| 169 | ).applySafe(this);
|
|---|
| 170 | setCanceled(false);
|
|---|
| 171 | } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775
|
|---|
| 172 | new WindowGeometry(this).remember(getClass().getName() + ".geometry");
|
|---|
| 173 | }
|
|---|
| 174 | super.setVisible(visible);
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | @Override
|
|---|
| 178 | public void tableChanged(TableModelEvent e) {
|
|---|
| 179 | updateMessage();
|
|---|
| 180 | updateTitle();
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | /**
|
|---|
| 184 | * The table model which manages the list of relation-to-child references
|
|---|
| 185 | *
|
|---|
| 186 | */
|
|---|
| 187 | public static class RelationMemberTableModel extends DefaultTableModel {
|
|---|
| 188 | private final transient List<RelationToChildReference> data;
|
|---|
| 189 |
|
|---|
| 190 | /**
|
|---|
| 191 | * Constructs a new {@code RelationMemberTableModel}.
|
|---|
| 192 | */
|
|---|
| 193 | public RelationMemberTableModel() {
|
|---|
| 194 | data = new ArrayList<>();
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | @Override
|
|---|
| 198 | public int getRowCount() {
|
|---|
| 199 | if (data == null) return 0;
|
|---|
| 200 | return data.size();
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | protected void sort() {
|
|---|
| 204 | Collections.sort(
|
|---|
| 205 | data,
|
|---|
| 206 | new Comparator<RelationToChildReference>() {
|
|---|
| 207 | private NameFormatter nf = DefaultNameFormatter.getInstance();
|
|---|
| 208 | @Override
|
|---|
| 209 | public int compare(RelationToChildReference o1, RelationToChildReference o2) {
|
|---|
| 210 | int cmp = o1.getChild().getDisplayName(nf).compareTo(o2.getChild().getDisplayName(nf));
|
|---|
| 211 | if (cmp != 0) return cmp;
|
|---|
| 212 | cmp = o1.getParent().getDisplayName(nf).compareTo(o2.getParent().getDisplayName(nf));
|
|---|
| 213 | if (cmp != 0) return cmp;
|
|---|
| 214 | return Integer.compare(o1.getPosition(), o2.getPosition());
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| 217 | );
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | public void populate(Collection<RelationToChildReference> references) {
|
|---|
| 221 | data.clear();
|
|---|
| 222 | if (references != null) {
|
|---|
| 223 | data.addAll(references);
|
|---|
| 224 | }
|
|---|
| 225 | sort();
|
|---|
| 226 | fireTableDataChanged();
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | public Set<OsmPrimitive> getObjectsToDelete() {
|
|---|
| 230 | Set<OsmPrimitive> ret = new HashSet<>();
|
|---|
| 231 | for (RelationToChildReference ref: data) {
|
|---|
| 232 | ret.add(ref.getChild());
|
|---|
| 233 | }
|
|---|
| 234 | return ret;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | public int getNumObjectsToDelete() {
|
|---|
| 238 | return getObjectsToDelete().size();
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | public Set<OsmPrimitive> getParentRelations() {
|
|---|
| 242 | Set<OsmPrimitive> ret = new HashSet<>();
|
|---|
| 243 | for (RelationToChildReference ref: data) {
|
|---|
| 244 | ret.add(ref.getParent());
|
|---|
| 245 | }
|
|---|
| 246 | return ret;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | public int getNumParentRelations() {
|
|---|
| 250 | return getParentRelations().size();
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | @Override
|
|---|
| 254 | public Object getValueAt(int rowIndex, int columnIndex) {
|
|---|
| 255 | if (data == null) return null;
|
|---|
| 256 | RelationToChildReference ref = data.get(rowIndex);
|
|---|
| 257 | switch(columnIndex) {
|
|---|
| 258 | case 0: return ref.getChild();
|
|---|
| 259 | case 1: return ref.getParent();
|
|---|
| 260 | case 2: return ref.getPosition()+1;
|
|---|
| 261 | case 3: return ref.getRole();
|
|---|
| 262 | default:
|
|---|
| 263 | assert false : "Illegal column index";
|
|---|
| 264 | }
|
|---|
| 265 | return null;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | @Override
|
|---|
| 269 | public boolean isCellEditable(int row, int column) {
|
|---|
| 270 | return false;
|
|---|
| 271 | }
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | private static class RelationMemberTableColumnModel extends DefaultTableColumnModel {
|
|---|
| 275 |
|
|---|
| 276 | protected final void createColumns() {
|
|---|
| 277 | TableColumn col = null;
|
|---|
| 278 |
|
|---|
| 279 | // column 0 - To Delete
|
|---|
| 280 | col = new TableColumn(0);
|
|---|
| 281 | col.setHeaderValue(tr("To delete"));
|
|---|
| 282 | col.setResizable(true);
|
|---|
| 283 | col.setWidth(100);
|
|---|
| 284 | col.setPreferredWidth(100);
|
|---|
| 285 | col.setCellRenderer(new OsmPrimitivRenderer());
|
|---|
| 286 | addColumn(col);
|
|---|
| 287 |
|
|---|
| 288 | // column 0 - From Relation
|
|---|
| 289 | col = new TableColumn(1);
|
|---|
| 290 | col.setHeaderValue(tr("From Relation"));
|
|---|
| 291 | col.setResizable(true);
|
|---|
| 292 | col.setWidth(100);
|
|---|
| 293 | col.setPreferredWidth(100);
|
|---|
| 294 | col.setCellRenderer(new OsmPrimitivRenderer());
|
|---|
| 295 | addColumn(col);
|
|---|
| 296 |
|
|---|
| 297 | // column 1 - Pos.
|
|---|
| 298 | col = new TableColumn(2);
|
|---|
| 299 | col.setHeaderValue(tr("Pos."));
|
|---|
| 300 | col.setResizable(true);
|
|---|
| 301 | col.setWidth(30);
|
|---|
| 302 | col.setPreferredWidth(30);
|
|---|
| 303 | addColumn(col);
|
|---|
| 304 |
|
|---|
| 305 | // column 2 - Role
|
|---|
| 306 | col = new TableColumn(3);
|
|---|
| 307 | col.setHeaderValue(tr("Role"));
|
|---|
| 308 | col.setResizable(true);
|
|---|
| 309 | col.setWidth(50);
|
|---|
| 310 | col.setPreferredWidth(50);
|
|---|
| 311 | addColumn(col);
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | RelationMemberTableColumnModel() {
|
|---|
| 315 | createColumns();
|
|---|
| 316 | }
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | class OKAction extends AbstractAction {
|
|---|
| 320 | OKAction() {
|
|---|
| 321 | putValue(NAME, tr("OK"));
|
|---|
| 322 | putValue(SMALL_ICON, ImageProvider.get("ok"));
|
|---|
| 323 | putValue(SHORT_DESCRIPTION, tr("Click to close the dialog and remove the object from the relations"));
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | @Override
|
|---|
| 327 | public void actionPerformed(ActionEvent e) {
|
|---|
| 328 | setCanceled(false);
|
|---|
| 329 | setVisible(false);
|
|---|
| 330 | }
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | class CancelAction extends AbstractAction {
|
|---|
| 334 | CancelAction() {
|
|---|
| 335 | putValue(NAME, tr("Cancel"));
|
|---|
| 336 | putValue(SMALL_ICON, ImageProvider.get("cancel"));
|
|---|
| 337 | putValue(SHORT_DESCRIPTION, tr("Click to close the dialog and to abort deleting the objects"));
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | @Override
|
|---|
| 341 | public void actionPerformed(ActionEvent e) {
|
|---|
| 342 | setCanceled(true);
|
|---|
| 343 | setVisible(false);
|
|---|
| 344 | }
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | class WindowEventHandler extends WindowAdapter {
|
|---|
| 348 |
|
|---|
| 349 | @Override
|
|---|
| 350 | public void windowClosing(WindowEvent e) {
|
|---|
| 351 | setCanceled(true);
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | @Override
|
|---|
| 355 | public void windowOpened(WindowEvent e) {
|
|---|
| 356 | btnOK.requestFocusInWindow();
|
|---|
| 357 | }
|
|---|
| 358 | }
|
|---|
| 359 | }
|
|---|