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