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

Last change on this file since 11848 was 11553, checked in by Don-vip, 7 years ago

refactor handling of null values - use Java 8 Optional where possible

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