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