source: josm/trunk/src/org/openstreetmap/josm/gui/io/UploadedObjectsSummaryPanel.java@ 13660

Last change on this file since 13660 was 13660, checked in by Don-vip, 6 years ago

fix #15883 - Zoom on element when double-clicking on elements of upload window (patch by bagage)

  • Property svn:eol-style set to native
File size: 7.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.awt.GridBagConstraints;
8import java.awt.GridBagLayout;
9import java.awt.event.MouseAdapter;
10import java.awt.event.MouseEvent;
11import java.util.ArrayList;
12import java.util.Collections;
13import java.util.List;
14import java.util.Optional;
15
16import javax.swing.AbstractListModel;
17import javax.swing.JLabel;
18import javax.swing.JList;
19import javax.swing.JPanel;
20import javax.swing.JScrollPane;
21
22import org.openstreetmap.josm.actions.AutoScaleAction;
23import org.openstreetmap.josm.data.osm.OsmPrimitive;
24import org.openstreetmap.josm.gui.PrimitiveRenderer;
25
26/**
27 * This panel displays a summary of the objects to upload. It is displayed in the upper part of the {@link UploadDialog}.
28 * @since 2599
29 */
30public class UploadedObjectsSummaryPanel extends JPanel {
31 /**
32 * The swing property name for the number of objects to upload
33 */
34 public static final String NUM_OBJECTS_TO_UPLOAD_PROP = UploadedObjectsSummaryPanel.class.getName() + ".numObjectsToUpload";
35
36 /** the list with the added primitives */
37 private PrimitiveList lstAdd;
38 private JLabel lblAdd;
39 private JScrollPane spAdd;
40 /** the list with the updated primitives */
41 private PrimitiveList lstUpdate;
42 private JLabel lblUpdate;
43 private JScrollPane spUpdate;
44 /** the list with the deleted primitives */
45 private PrimitiveList lstDelete;
46 private JLabel lblDelete;
47 private JScrollPane spDelete;
48
49 /**
50 * Constructs a new {@code UploadedObjectsSummaryPanel}.
51 */
52 public UploadedObjectsSummaryPanel() {
53 build();
54 }
55
56 protected void build() {
57 setLayout(new GridBagLayout());
58 PrimitiveRenderer renderer = new PrimitiveRenderer();
59 MouseAdapter mouseListener = new MouseAdapter() {
60 @Override
61 public void mouseClicked(MouseEvent evt) {
62 if (evt.getButton() == MouseEvent.BUTTON1 && evt.getClickCount() == 2) {
63 PrimitiveList list = (PrimitiveList) evt.getSource();
64 int index = list.locationToIndex(evt.getPoint());
65 AutoScaleAction.zoomTo(Collections.singleton(list.getModel().getElementAt(index)));
66 }
67 }
68 };
69 // initialize the three lists for uploaded primitives, but don't add them to the dialog yet, see setUploadedPrimitives()
70 //
71 lstAdd = new PrimitiveList();
72 lstAdd.setCellRenderer(renderer);
73 lstAdd.addMouseListener(mouseListener);
74 lstAdd.setVisibleRowCount(Math.min(lstAdd.getModel().getSize(), 10));
75 spAdd = new JScrollPane(lstAdd);
76 lblAdd = new JLabel(tr("Objects to add:"));
77 lblAdd.setLabelFor(lstAdd);
78
79 lstUpdate = new PrimitiveList();
80 lstUpdate.setCellRenderer(renderer);
81 lstUpdate.addMouseListener(mouseListener);
82 lstUpdate.setVisibleRowCount(Math.min(lstUpdate.getModel().getSize(), 10));
83 spUpdate = new JScrollPane(lstUpdate);
84 lblUpdate = new JLabel(tr("Objects to modify:"));
85 lblUpdate.setLabelFor(lstUpdate);
86
87 lstDelete = new PrimitiveList();
88 lstDelete.setCellRenderer(renderer);
89 lstDelete.addMouseListener(mouseListener);
90 lstDelete.setVisibleRowCount(Math.min(lstDelete.getModel().getSize(), 10));
91 spDelete = new JScrollPane(lstDelete);
92 lblDelete = new JLabel(tr("Objects to delete:"));
93 lblDelete.setLabelFor(lstDelete);
94 }
95
96 /**
97 * Sets the collections of primitives which will be uploaded
98 *
99 * @param add the collection of primitives to add
100 * @param update the collection of primitives to update
101 * @param delete the collection of primitives to delete
102 */
103 public void setUploadedPrimitives(List<OsmPrimitive> add, List<OsmPrimitive> update, List<OsmPrimitive> delete) {
104 lstAdd.getPrimitiveListModel().setPrimitives(add);
105 lstUpdate.getPrimitiveListModel().setPrimitives(update);
106 lstDelete.getPrimitiveListModel().setPrimitives(delete);
107
108 GridBagConstraints gcLabel = new GridBagConstraints();
109 gcLabel.fill = GridBagConstraints.HORIZONTAL;
110 gcLabel.weightx = 1.0;
111 gcLabel.weighty = 0.0;
112 gcLabel.anchor = GridBagConstraints.FIRST_LINE_START;
113
114 GridBagConstraints gcList = new GridBagConstraints();
115 gcList.fill = GridBagConstraints.BOTH;
116 gcList.weightx = 1.0;
117 gcList.weighty = 1.0;
118 gcList.anchor = GridBagConstraints.CENTER;
119 removeAll();
120 int y = -1;
121 if (!add.isEmpty()) {
122 y++;
123 gcLabel.gridy = y;
124 lblAdd.setText(trn("{0} object to add:", "{0} objects to add:", add.size(), add.size()));
125 add(lblAdd, gcLabel);
126 y++;
127 gcList.gridy = y;
128 add(spAdd, gcList);
129 }
130 if (!update.isEmpty()) {
131 y++;
132 gcLabel.gridy = y;
133 lblUpdate.setText(trn("{0} object to modify:", "{0} objects to modify:", update.size(), update.size()));
134 add(lblUpdate, gcLabel);
135 y++;
136 gcList.gridy = y;
137 add(spUpdate, gcList);
138 }
139 if (!delete.isEmpty()) {
140 y++;
141 gcLabel.gridy = y;
142 lblDelete.setText(trn("{0} object to delete:", "{0} objects to delete:", delete.size(), delete.size()));
143 add(lblDelete, gcLabel);
144 y++;
145 gcList.gridy = y;
146 add(spDelete, gcList);
147 }
148
149 firePropertyChange(NUM_OBJECTS_TO_UPLOAD_PROP, 0, getNumObjectsToUpload());
150 }
151
152 /**
153 * Replies the number of objects to upload
154 *
155 * @return the number of objects to upload
156 */
157 public int getNumObjectsToUpload() {
158 return lstAdd.getModel().getSize()
159 + lstUpdate.getModel().getSize()
160 + lstDelete.getModel().getSize();
161 }
162
163 /**
164 * A simple list of OSM primitives.
165 */
166 static class PrimitiveList extends JList<OsmPrimitive> {
167 /**
168 * Constructs a new {@code PrimitiveList}.
169 */
170 PrimitiveList() {
171 super(new PrimitiveListModel());
172 }
173
174 public PrimitiveListModel getPrimitiveListModel() {
175 return (PrimitiveListModel) getModel();
176 }
177 }
178
179 /**
180 * A list model for a list of OSM primitives.
181 */
182 static class PrimitiveListModel extends AbstractListModel<OsmPrimitive> {
183 private transient List<OsmPrimitive> primitives;
184
185 /**
186 * Constructs a new {@code PrimitiveListModel}.
187 */
188 PrimitiveListModel() {
189 primitives = new ArrayList<>();
190 }
191
192 PrimitiveListModel(List<OsmPrimitive> primitives) {
193 setPrimitives(primitives);
194 }
195
196 public void setPrimitives(List<OsmPrimitive> primitives) {
197 this.primitives = Optional.ofNullable(primitives).orElseGet(ArrayList::new);
198 fireContentsChanged(this, 0, getSize());
199 }
200
201 @Override
202 public OsmPrimitive getElementAt(int index) {
203 if (primitives == null) return null;
204 return primitives.get(index);
205 }
206
207 @Override
208 public int getSize() {
209 if (primitives == null) return 0;
210 return primitives.size();
211 }
212 }
213}
Note: See TracBrowser for help on using the repository browser.