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