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