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