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