source: josm/trunk/src/org/openstreetmap/josm/gui/io/UploadParameterSummaryPanel.java@ 12678

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

add Changeset.getComment()

  • Property svn:eol-style set to native
File size: 8.6 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.BorderLayout;
8import java.beans.PropertyChangeEvent;
9import java.beans.PropertyChangeListener;
10
11import javax.swing.BorderFactory;
12import javax.swing.JLabel;
13import javax.swing.JPanel;
14import javax.swing.event.HyperlinkEvent;
15import javax.swing.event.HyperlinkListener;
16
17import org.openstreetmap.josm.data.osm.Changeset;
18import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
19import org.openstreetmap.josm.io.Capabilities;
20import org.openstreetmap.josm.io.OsmApi;
21import org.openstreetmap.josm.tools.ImageProvider;
22
23/**
24 * A panel that displays a summary of data the user is about to upload
25 * <p>
26 * FIXME this class should extend HtmlPanel instead (duplicated code in here)
27 */
28public class UploadParameterSummaryPanel extends JPanel implements HyperlinkListener, PropertyChangeListener {
29 private transient UploadStrategySpecification spec = new UploadStrategySpecification();
30 private int numObjects;
31 private JMultilineLabel jepMessage;
32 private JLabel lblWarning;
33
34 private transient Changeset selectedChangeset;
35 private boolean closeChangesetAfterNextUpload;
36 private transient ConfigurationParameterRequestHandler configHandler;
37
38 /**
39 * Constructs a new {@code UploadParameterSummaryPanel}.
40 */
41 public UploadParameterSummaryPanel() {
42 build();
43 updateSummary();
44 }
45
46 protected String buildChangesetSummary() {
47 StringBuilder msg = new StringBuilder(96);
48 if (selectedChangeset == null || selectedChangeset.isNew()) {
49 msg.append(tr("Objects are uploaded to a <strong>new changeset</strong>."));
50 } else {
51 msg.append(tr("Objects are uploaded to the <strong>open changeset</strong> {0} with upload comment ''{1}''.",
52 selectedChangeset.getId(),
53 selectedChangeset.getComment()
54 ));
55 }
56 msg.append(' ');
57 if (closeChangesetAfterNextUpload) {
58 msg.append(tr("The changeset is going to be <strong>closed</strong> after this upload"));
59 } else {
60 msg.append(tr("The changeset is <strong>left open</strong> after this upload"));
61 }
62 msg.append(" (<a href=\"urn:changeset-configuration\">").append(tr("configure changeset")).append("</a>)");
63 return msg.toString();
64 }
65
66 protected String buildStrategySummary() {
67 if (spec == null)
68 return "";
69 // check whether we can use one changeset only or whether we have to use multiple changesets
70 //
71 boolean useOneChangeset = true;
72 Capabilities capabilities = OsmApi.getOsmApi().getCapabilities();
73 int maxChunkSize = capabilities != null ? capabilities.getMaxChangesetSize() : -1;
74 if (maxChunkSize > 0 && numObjects > maxChunkSize) {
75 useOneChangeset = false;
76 }
77
78 int numRequests = spec.getNumRequests(numObjects);
79 String msg = null;
80 if (useOneChangeset) {
81 lblWarning.setVisible(false);
82 if (numRequests == 0) {
83 msg = trn(
84 "Uploading <strong>{0} object</strong> to <strong>1 changeset</strong>",
85 "Uploading <strong>{0} objects</strong> to <strong>1 changeset</strong>",
86 numObjects, numObjects
87 );
88 } else if (numRequests == 1) {
89 msg = trn(
90 "Uploading <strong>{0} object</strong> to <strong>1 changeset</strong> using <strong>1 request</strong>",
91 "Uploading <strong>{0} objects</strong> to <strong>1 changeset</strong> using <strong>1 request</strong>",
92 numObjects, numObjects
93 );
94 } else if (numRequests > 1) {
95 msg = tr("Uploading <strong>{0} objects</strong> to <strong>1 changeset</strong> using <strong>{1} requests</strong>",
96 numObjects, numRequests);
97 }
98 msg = msg + " (<a href=\"urn:advanced-configuration\">" + tr("advanced configuration") + "</a>)";
99 } else {
100 lblWarning.setVisible(true);
101 if (numRequests == 0) {
102 msg = tr("{0} objects exceed the max. allowed {1} objects in a changeset on the server ''{2}''. " +
103 "Please <a href=\"urn:advanced-configuration\">configure</a> how to proceed with <strong>multiple changesets</strong>",
104 numObjects, maxChunkSize, OsmApi.getOsmApi().getBaseUrl());
105 } else if (numRequests > 1) {
106 msg = tr("Uploading <strong>{0} objects</strong> to <strong>multiple changesets</strong> using <strong>{1} requests</strong>",
107 numObjects, numRequests);
108 msg = msg + " (<a href=\"urn:advanced-configuration\">" + tr("advanced configuration") + "</a>)";
109 }
110 }
111 return msg;
112 }
113
114 protected void build() {
115 jepMessage = new JMultilineLabel("");
116 jepMessage.addHyperlinkListener(this);
117
118 setLayout(new BorderLayout());
119 add(jepMessage, BorderLayout.CENTER);
120 lblWarning = new JLabel("");
121 lblWarning.setVisible(false);
122 lblWarning.setLabelFor(jepMessage);
123 lblWarning.setIcon(ImageProvider.get("warning-small"));
124 lblWarning.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
125 JPanel pnl = new JPanel(new BorderLayout());
126 pnl.add(lblWarning, BorderLayout.NORTH);
127 add(pnl, BorderLayout.WEST);
128 }
129
130 public void setConfigurationParameterRequestListener(ConfigurationParameterRequestHandler handler) {
131 this.configHandler = handler;
132 }
133
134 /**
135 * Sets the {@link UploadStrategySpecification} the user chose
136 * @param spec The specification to display
137 */
138 public void setUploadStrategySpecification(UploadStrategySpecification spec) {
139 this.spec = spec;
140 updateSummary();
141 }
142
143 /**
144 * Sets the number of objects that will be uploaded
145 * @param numObjects The number to display
146 */
147 public void setNumObjects(int numObjects) {
148 this.numObjects = numObjects;
149 updateSummary();
150 }
151
152 /**
153 * Display that the changeset will be closed after the upload
154 * @param value <code>true</code> if it will be closed
155 */
156 public void setCloseChangesetAfterNextUpload(boolean value) {
157 this.closeChangesetAfterNextUpload = value;
158 updateSummary();
159 }
160
161 protected void updateSummary() {
162 StringBuilder sb = new StringBuilder(32);
163 sb.append("<html>")
164 .append(buildStrategySummary())
165 .append("<br><br>")
166 .append(buildChangesetSummary())
167 .append("</html>");
168 jepMessage.setText(sb.toString());
169 }
170
171 /* --------------------------------------------------------------------- */
172 /* Interface HyperlinkListener
173 /* --------------------------------------------------------------------- */
174 @Override
175 public void hyperlinkUpdate(HyperlinkEvent e) {
176 if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
177 String desc = e.getDescription();
178 if (desc == null || configHandler == null)
179 return;
180 if ("urn:changeset-configuration".equals(desc)) {
181 configHandler.handleChangesetConfigurationRequest();
182 } else if ("urn:advanced-configuration".equals(desc)) {
183 configHandler.handleUploadStrategyConfigurationRequest();
184 }
185 }
186 }
187
188 /* --------------------------------------------------------------------- */
189 /* Interface PropertyChangeListener
190 /* --------------------------------------------------------------------- */
191 @Override
192 public void propertyChange(PropertyChangeEvent evt) {
193 if (evt.getPropertyName().equals(ChangesetManagementPanel.SELECTED_CHANGESET_PROP)) {
194 selectedChangeset = (Changeset) evt.getNewValue();
195 updateSummary();
196 } else if (evt.getPropertyName().equals(ChangesetManagementPanel.CLOSE_CHANGESET_AFTER_UPLOAD)) {
197 closeChangesetAfterNextUpload = (Boolean) evt.getNewValue();
198 updateSummary();
199 } else if (evt.getPropertyName().equals(UploadedObjectsSummaryPanel.NUM_OBJECTS_TO_UPLOAD_PROP)) {
200 numObjects = (Integer) evt.getNewValue();
201 updateSummary();
202 } else if (evt.getPropertyName().equals(UploadStrategySelectionPanel.UPLOAD_STRATEGY_SPECIFICATION_PROP)) {
203 this.spec = (UploadStrategySpecification) evt.getNewValue();
204 updateSummary();
205 }
206 }
207}
Note: See TracBrowser for help on using the repository browser.