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

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

see #15182 - move UploadStrategySpecification and required enums from gui.io to io

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