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.awt.Font;
|
---|
9 | import java.beans.PropertyChangeEvent;
|
---|
10 | import java.beans.PropertyChangeListener;
|
---|
11 | import java.text.MessageFormat;
|
---|
12 | import java.util.logging.Logger;
|
---|
13 |
|
---|
14 | import javax.swing.BorderFactory;
|
---|
15 | import javax.swing.JEditorPane;
|
---|
16 | import javax.swing.JLabel;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 | import javax.swing.UIManager;
|
---|
19 | import javax.swing.event.HyperlinkEvent;
|
---|
20 | import javax.swing.event.HyperlinkListener;
|
---|
21 | import javax.swing.text.html.HTMLEditorKit;
|
---|
22 | import javax.swing.text.html.StyleSheet;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
25 | import org.openstreetmap.josm.io.OsmApi;
|
---|
26 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
27 |
|
---|
28 | public class UploadParameterSummaryPanel extends JPanel implements HyperlinkListener, PropertyChangeListener{
|
---|
29 | @SuppressWarnings("unused")
|
---|
30 | static private final Logger logger = Logger.getLogger(UploadParameterSummaryPanel.class.getName());
|
---|
31 |
|
---|
32 | private UploadStrategySpecification spec = new UploadStrategySpecification();
|
---|
33 | private int numObjects;
|
---|
34 | private JEditorPane jepMessage;
|
---|
35 | private JLabel lblWarning;
|
---|
36 |
|
---|
37 | private Changeset selectedChangeset;
|
---|
38 | private boolean closeChangesetAfterNextUpload;
|
---|
39 | private ConfigurationParameterRequestHandler configHandler;
|
---|
40 |
|
---|
41 | protected String buildChangesetSummary() {
|
---|
42 | StringBuffer msg = new StringBuffer();
|
---|
43 | if (selectedChangeset == null || selectedChangeset.isNew()) {
|
---|
44 | msg.append(tr("Objects are uploaded to a <strong>new changeset</strong>."));
|
---|
45 | } else {
|
---|
46 | String uploadComment = selectedChangeset.get("comment") == null ?
|
---|
47 | "" : selectedChangeset.get("comment");
|
---|
48 | msg.append(tr("Objects are uploaded to the <strong>open changeset</strong> {0} with upload comment ''{1}''.",
|
---|
49 | selectedChangeset.getId(),
|
---|
50 | uploadComment
|
---|
51 | ));
|
---|
52 | }
|
---|
53 | msg.append(" ");
|
---|
54 | if (closeChangesetAfterNextUpload) {
|
---|
55 | msg.append(tr("The changeset is going to be <strong>closed</strong> after this upload"));
|
---|
56 | } else {
|
---|
57 | msg.append(tr("The changeset is <strong>left open</strong> after this upload"));
|
---|
58 | }
|
---|
59 | msg.append(" (<a href=\"urn:changeset-configuration\">" + tr("configure changeset") + "</a>)");
|
---|
60 | return msg.toString();
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected String buildStrategySummary() {
|
---|
64 | if (spec == null)
|
---|
65 | return "";
|
---|
66 | // check whether we can use one changeset only or whether we have to use
|
---|
67 | // multiple changesets
|
---|
68 | //
|
---|
69 | boolean useOneChangeset = true;
|
---|
70 | int maxChunkSize = OsmApi.getOsmApi().getCapabilities().getMaxChangesetSize();
|
---|
71 | if (maxChunkSize > 0 && numObjects > maxChunkSize) {
|
---|
72 | useOneChangeset = false;
|
---|
73 | }
|
---|
74 |
|
---|
75 | int numRequests = spec.getNumRequests(numObjects);
|
---|
76 | String msg = null;
|
---|
77 | if (useOneChangeset) {
|
---|
78 | lblWarning.setVisible(false);
|
---|
79 | if (numRequests == 0) {
|
---|
80 | msg = trn(
|
---|
81 | "Uploading <strong>{0} object</strong> to <strong>1 changeset</strong>",
|
---|
82 | "Uploading <strong>{0} objects</strong> to <strong>1 changeset</strong>",
|
---|
83 | numObjects, numObjects
|
---|
84 | );
|
---|
85 | } else if (numRequests == 1) {
|
---|
86 | msg = trn(
|
---|
87 | "Uploading <strong>{0} object</strong> to <strong>1 changeset</strong> using <strong>1 request</strong>",
|
---|
88 | "Uploading <strong>{0} objects</strong> to <strong>1 changeset</strong> using <strong>1 request</strong>",
|
---|
89 | numObjects, numObjects
|
---|
90 | );
|
---|
91 | } else if (numRequests > 1){
|
---|
92 | msg = tr("Uploading <strong>{0} objects</strong> to <strong>1 changeset</strong> using <strong>{1} requests</strong>", numObjects, numRequests);
|
---|
93 | }
|
---|
94 | msg = msg + " (<a href=\"urn:advanced-configuration\">" + tr("advanced configuration") + "</a>)";
|
---|
95 | } else {
|
---|
96 | lblWarning.setVisible(true);
|
---|
97 | if (numRequests == 0) {
|
---|
98 | 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>",
|
---|
99 | numObjects, maxChunkSize, OsmApi.getOsmApi().getBaseUrl());
|
---|
100 | } else if (numRequests > 1){
|
---|
101 | msg = tr("Uploading <strong>{0} objects</strong> to <strong>multiple changesets</strong> using <strong>{1} requests</strong>", numObjects, numRequests);
|
---|
102 | msg = msg + " (<a href=\"urn:advanced-configuration\">" + tr("advanced configuration") + "</a>)";
|
---|
103 | }
|
---|
104 | }
|
---|
105 | return msg;
|
---|
106 | }
|
---|
107 |
|
---|
108 | protected void build() {
|
---|
109 | jepMessage = new JEditorPane("text/html", "");
|
---|
110 | jepMessage.setOpaque(false);
|
---|
111 | jepMessage.setEditable(false);
|
---|
112 | jepMessage.addHyperlinkListener(this);
|
---|
113 | Font f = UIManager.getFont("Label.font");
|
---|
114 | StyleSheet ss = new StyleSheet();
|
---|
115 | String rule = MessageFormat.format(
|
---|
116 | "font-family: ''{0}'';font-size: {1,number}pt; font-weight: {2}; font-style: {3}",
|
---|
117 | f.getName(),
|
---|
118 | f.getSize(),
|
---|
119 | f.isBold() ? "bold" : "normal",
|
---|
120 | f.isItalic() ? "italic" : "normal"
|
---|
121 | );
|
---|
122 | rule = "body {" + rule + "}";
|
---|
123 | rule = MessageFormat.format(
|
---|
124 | "font-family: ''{0}'';font-size: {1,number}pt; font-weight: {2}; font-style: {3}",
|
---|
125 | f.getName(),
|
---|
126 | f.getSize(),
|
---|
127 | "bold",
|
---|
128 | f.isItalic() ? "italic" : "normal"
|
---|
129 | );
|
---|
130 | rule = "strong {" + rule + "}";
|
---|
131 | ss.addRule(rule);
|
---|
132 | ss.addRule("a {text-decoration: underline; color: blue}");
|
---|
133 | HTMLEditorKit kit = new HTMLEditorKit();
|
---|
134 | kit.setStyleSheet(ss);
|
---|
135 | jepMessage.setEditorKit(kit);
|
---|
136 |
|
---|
137 | setLayout(new BorderLayout());
|
---|
138 | add(jepMessage, BorderLayout.CENTER);
|
---|
139 | lblWarning = new JLabel("");
|
---|
140 | lblWarning.setVisible(false);
|
---|
141 | lblWarning.setIcon(ImageProvider.get("warning-small.png"));
|
---|
142 | lblWarning.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
143 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
144 | pnl.add(lblWarning, BorderLayout.NORTH);
|
---|
145 | add(pnl, BorderLayout.WEST);
|
---|
146 | }
|
---|
147 |
|
---|
148 | public UploadParameterSummaryPanel() {
|
---|
149 | build();
|
---|
150 | updateSummary();
|
---|
151 | }
|
---|
152 |
|
---|
153 | public void setConfigurationParameterRequestListener(ConfigurationParameterRequestHandler handler) {
|
---|
154 | this.configHandler = handler;
|
---|
155 | }
|
---|
156 |
|
---|
157 | public void setUploadStrategySpecification(UploadStrategySpecification spec) {
|
---|
158 | this.spec = spec;
|
---|
159 | updateSummary();
|
---|
160 | }
|
---|
161 |
|
---|
162 | public void setNumObjects(int numObjects) {
|
---|
163 | this.numObjects = numObjects;
|
---|
164 | updateSummary();
|
---|
165 | }
|
---|
166 |
|
---|
167 | public void setCloseChangesetAfterNextUpload(boolean value) {
|
---|
168 | this.closeChangesetAfterNextUpload = value;
|
---|
169 | updateSummary();
|
---|
170 | }
|
---|
171 |
|
---|
172 | protected void updateSummary() {
|
---|
173 | StringBuffer sb = new StringBuffer();
|
---|
174 | sb.append("<html>");
|
---|
175 | sb.append(buildStrategySummary());
|
---|
176 | sb.append("<br><br>");
|
---|
177 | sb.append(buildChangesetSummary());
|
---|
178 | sb.append("</html>");
|
---|
179 | jepMessage.setText(sb.toString());
|
---|
180 | }
|
---|
181 |
|
---|
182 | /* --------------------------------------------------------------------- */
|
---|
183 | /* Interface HyperlinkListener
|
---|
184 | /* --------------------------------------------------------------------- */
|
---|
185 | public void hyperlinkUpdate(HyperlinkEvent e) {
|
---|
186 | if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
|
---|
187 | if (e.getDescription() == null || configHandler == null)
|
---|
188 | return;
|
---|
189 | if (e.getDescription().equals("urn:changeset-configuration")) {
|
---|
190 | configHandler.handleChangesetConfigurationRequest();
|
---|
191 | } else if (e.getDescription().equals("urn:advanced-configuration")) {
|
---|
192 | configHandler.handleUploadStrategyConfigurationRequest();
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | /* --------------------------------------------------------------------- */
|
---|
198 | /* Interface PropertyChangeListener
|
---|
199 | /* --------------------------------------------------------------------- */
|
---|
200 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
201 | if (evt.getPropertyName().equals(ChangesetManagementPanel.SELECTED_CHANGESET_PROP)) {
|
---|
202 | selectedChangeset = (Changeset)evt.getNewValue();
|
---|
203 | updateSummary();
|
---|
204 | } else if (evt.getPropertyName().equals(ChangesetManagementPanel.CLOSE_CHANGESET_AFTER_UPLOAD)) {
|
---|
205 | closeChangesetAfterNextUpload = (Boolean)evt.getNewValue();
|
---|
206 | updateSummary();
|
---|
207 | } else if (evt.getPropertyName().equals(UploadedObjectsSummaryPanel.NUM_OBJECTS_TO_UPLOAD_PROP)) {
|
---|
208 | numObjects = (Integer)evt.getNewValue();
|
---|
209 | updateSummary();
|
---|
210 | } else if (evt.getPropertyName().equals(UploadStrategySelectionPanel.UPLOAD_STRATEGY_SPECIFICATION_PROP)) {
|
---|
211 | this.spec = (UploadStrategySpecification)evt.getNewValue();
|
---|
212 | updateSummary();
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|