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

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

see #8465 - use diamond operator where applicable

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