source: josm/trunk/src/org/openstreetmap/josm/gui/history/VersionInfoPanel.java@ 12636

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

see #15182 - deprecate Main.getLayerManager(). Replacement: gui.MainApplication.getLayerManager()

  • Property svn:eol-style set to native
File size: 13.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.history;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.awt.BorderLayout;
8import java.awt.Dimension;
9import java.awt.GridBagConstraints;
10import java.awt.GridBagLayout;
11import java.awt.Insets;
12import java.awt.event.ActionEvent;
13import java.text.DateFormat;
14import java.util.Collections;
15import java.util.Date;
16
17import javax.swing.AbstractAction;
18import javax.swing.JButton;
19import javax.swing.JComponent;
20import javax.swing.JLabel;
21import javax.swing.JPanel;
22import javax.swing.JTextArea;
23import javax.swing.event.ChangeEvent;
24import javax.swing.event.ChangeListener;
25
26import org.openstreetmap.josm.Main;
27import org.openstreetmap.josm.data.osm.Changeset;
28import org.openstreetmap.josm.data.osm.OsmPrimitive;
29import org.openstreetmap.josm.data.osm.User;
30import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
31import org.openstreetmap.josm.gui.JosmUserIdentityManager;
32import org.openstreetmap.josm.gui.MainApplication;
33import org.openstreetmap.josm.gui.dialogs.ChangesetDialog;
34import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager;
35import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetDiscussionPanel;
36import org.openstreetmap.josm.gui.layer.OsmDataLayer;
37import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
38import org.openstreetmap.josm.gui.widgets.UrlLabel;
39import org.openstreetmap.josm.tools.CheckParameterUtil;
40import org.openstreetmap.josm.tools.GBC;
41import org.openstreetmap.josm.tools.ImageProvider;
42import org.openstreetmap.josm.tools.Utils;
43import org.openstreetmap.josm.tools.date.DateUtils;
44
45/**
46 * VersionInfoPanel is an UI component which displays the basic properties of a version
47 * of a {@link OsmPrimitive}.
48 * @since 1709
49 */
50public class VersionInfoPanel extends JPanel implements ChangeListener {
51 private final PointInTimeType pointInTimeType;
52 private final transient HistoryBrowserModel model;
53 private JMultilineLabel lblInfo;
54 private UrlLabel lblUser;
55 private UrlLabel lblChangeset;
56 private final JButton lblChangesetComments = new JButton(ImageProvider.get("dialogs/notes/note_comment"));
57 private final OpenChangesetDialogAction changesetCommentsDialogAction = new OpenChangesetDialogAction(ChangesetDiscussionPanel.class);
58 private final OpenChangesetDialogAction changesetDialogAction = new OpenChangesetDialogAction(null);
59 private final JButton changesetButton = new JButton(changesetDialogAction);
60 private JPanel pnlChangesetSource;
61 private JPanel pnlChangesetImageryUsed;
62 private JLabel lblSource;
63 private JLabel lblImageryUsed;
64 private JTextArea texChangesetComment;
65 private JTextArea texChangesetSource;
66 private JTextArea texChangesetImageryUsed;
67
68 protected static JTextArea buildTextArea(String tooltip) {
69 JTextArea lbl = new JTextArea();
70 lbl.setLineWrap(true);
71 lbl.setWrapStyleWord(true);
72 lbl.setEditable(false);
73 lbl.setOpaque(false);
74 lbl.setToolTipText(tooltip);
75 return lbl;
76 }
77
78 protected static JLabel buildLabel(String text, String tooltip, JTextArea textArea) {
79 // We need text field to be a JTextArea for line wrapping but cannot put HTML code in here,
80 // so create a separate JLabel with same characteristics (margin, font)
81 JLabel lbl = new JLabel("<html><p style='margin-top:"+textArea.getMargin().top+"'>"+text+"</html>");
82 lbl.setFont(textArea.getFont());
83 lbl.setToolTipText(tooltip);
84 lbl.setLabelFor(textArea);
85 return lbl;
86 }
87
88 protected static JPanel buildTextPanel(JLabel label, JTextArea textArea) {
89 JPanel pnl = new JPanel(new GridBagLayout());
90 pnl.add(label, GBC.std().anchor(GBC.NORTHWEST));
91 pnl.add(textArea, GBC.eol().insets(2, 0, 0, 0).fill());
92 return pnl;
93 }
94
95 protected void build() {
96 JPanel pnl1 = new JPanel(new BorderLayout());
97 lblInfo = new JMultilineLabel("");
98 pnl1.add(lblInfo, BorderLayout.CENTER);
99
100 // +-----------------------+-------------------------------------+
101 // | User: | lblUser |
102 // +-----------------------+-------------------------------------+
103 // | changesetButton | lblChangeset | lblChangesetComments |
104 // +-----------------------+-------------------------------------+
105 JPanel pnlUserAndChangeset = new JPanel(new GridBagLayout());
106 pnlUserAndChangeset.add(new JLabel(tr("User:")), GBC.std());
107
108 lblUser = new UrlLabel("", 2);
109 pnlUserAndChangeset.add(lblUser, GBC.eol().insets(5, 0, 0, 0).weight(1, 0));
110
111 changesetButton.setMargin(new Insets(0, 0, 0, 2));
112 pnlUserAndChangeset.add(changesetButton, GBC.std().fill().weight(0, 0));
113
114 lblChangeset = new UrlLabel("", 2);
115 pnlUserAndChangeset.add(lblChangeset, GBC.std().insets(5, 0, 0, 0).weight(1, 0));
116
117 lblChangesetComments.setAction(changesetCommentsDialogAction);
118 lblChangesetComments.setMargin(new Insets(0, 0, 0, 0));
119 lblChangesetComments.setIcon(new ImageProvider("dialogs/notes/note_comment").setMaxSize(12).get());
120 pnlUserAndChangeset.add(lblChangesetComments, GBC.eol());
121
122 texChangesetComment = buildTextArea(tr("Changeset comment"));
123 texChangesetSource = buildTextArea(tr("Changeset source"));
124 texChangesetImageryUsed = buildTextArea(tr("Imagery used"));
125
126 lblSource = buildLabel(tr("<b>Source</b>:"), tr("Changeset source"), texChangesetSource);
127 lblImageryUsed = buildLabel(tr("<b>Imagery</b>:"), tr("Imagery used"), texChangesetImageryUsed);
128 pnlChangesetSource = buildTextPanel(lblSource, texChangesetSource);
129 pnlChangesetImageryUsed = buildTextPanel(lblImageryUsed, texChangesetImageryUsed);
130
131 setLayout(new GridBagLayout());
132 GridBagConstraints gc = new GridBagConstraints();
133 gc.anchor = GridBagConstraints.NORTHWEST;
134 gc.fill = GridBagConstraints.HORIZONTAL;
135 gc.weightx = 1.0;
136 gc.weighty = 1.0;
137 add(pnl1, gc);
138 gc.gridy = 1;
139 gc.weighty = 0.0;
140 add(pnlUserAndChangeset, gc);
141 gc.gridy = 2;
142 add(texChangesetComment, gc);
143 gc.gridy = 3;
144 add(pnlChangesetSource, gc);
145 gc.gridy = 4;
146 add(pnlChangesetImageryUsed, gc);
147 }
148
149 protected HistoryOsmPrimitive getPrimitive() {
150 if (model == null || pointInTimeType == null)
151 return null;
152 return model.getPointInTime(pointInTimeType);
153 }
154
155 protected String getInfoText(final Date timestamp, final long version, final boolean isLatest) {
156 String text;
157 if (isLatest) {
158 OsmDataLayer editLayer = MainApplication.getLayerManager().getEditLayer();
159 text = tr("<html>Version <strong>{0}</strong> currently edited in layer ''{1}''</html>",
160 Long.toString(version),
161 editLayer == null ? tr("unknown") : Utils.escapeReservedCharactersHTML(editLayer.getName())
162 );
163 } else {
164 String date = "?";
165 if (timestamp != null) {
166 date = DateUtils.formatDateTime(timestamp, DateFormat.SHORT, DateFormat.SHORT);
167 }
168 text = tr(
169 "<html>Version <strong>{0}</strong> created on <strong>{1}</strong></html>",
170 Long.toString(version), date);
171 }
172 return text;
173 }
174
175 /**
176 * Constructs a new {@code VersionInfoPanel}.
177 */
178 public VersionInfoPanel() {
179 pointInTimeType = null;
180 model = null;
181 build();
182 }
183
184 /**
185 * constructor
186 *
187 * @param model the model (must not be null)
188 * @param pointInTimeType the point in time this panel visualizes (must not be null)
189 * @throws IllegalArgumentException if model is null
190 * @throws IllegalArgumentException if pointInTimeType is null
191 */
192 public VersionInfoPanel(HistoryBrowserModel model, PointInTimeType pointInTimeType) {
193 CheckParameterUtil.ensureParameterNotNull(pointInTimeType, "pointInTimeType");
194 CheckParameterUtil.ensureParameterNotNull(model, "model");
195
196 this.model = model;
197 this.pointInTimeType = pointInTimeType;
198 model.addChangeListener(this);
199 build();
200 }
201
202 protected static String getUserUrl(String username) {
203 return Main.getBaseUserUrl() + '/' + Utils.encodeUrl(username).replaceAll("\\+", "%20");
204 }
205
206 @Override
207 public void stateChanged(ChangeEvent e) {
208 HistoryOsmPrimitive primitive = getPrimitive();
209 if (primitive != null) {
210 Changeset cs = primitive.getChangeset();
211 update(cs, model.isLatest(primitive), primitive.getTimestamp(), primitive.getVersion());
212 }
213 }
214
215 /**
216 * Updates the content of this panel based on the changeset information given by {@code primitive}.
217 * @param primitive the primitive to extract the changeset information from
218 * @param isLatest whether this relates to a not yet commited changeset
219 */
220 public void update(final OsmPrimitive primitive, final boolean isLatest) {
221 update(Changeset.fromPrimitive(primitive), isLatest, primitive.getTimestamp(), primitive.getVersion());
222 }
223
224 /**
225 * Updates the content of this panel based on the changeset information given by {@code cs}.
226 * @param cs the changeset information
227 * @param isLatest whether this relates to a not yet commited changeset
228 * @param timestamp the timestamp
229 * @param version the version of the primitive
230 */
231 public void update(final Changeset cs, final boolean isLatest, final Date timestamp, final long version) {
232 lblInfo.setText(getInfoText(timestamp, version, isLatest));
233
234 if (!isLatest && cs != null) {
235 User user = cs.getUser();
236 String url = Main.getBaseBrowseUrl() + "/changeset/" + cs.getId();
237 lblChangeset.setUrl(url);
238 lblChangeset.setDescription(Long.toString(cs.getId()));
239 changesetCommentsDialogAction.setId(cs.getId());
240 lblChangesetComments.setVisible(cs.getCommentsCount() > 0);
241 lblChangesetComments.setText(String.valueOf(cs.getCommentsCount()));
242 lblChangesetComments.setToolTipText(trn("This changeset has {0} comment", "This changeset has {0} comments",
243 cs.getCommentsCount(), cs.getCommentsCount()));
244 changesetDialogAction.setId(cs.getId());
245 changesetButton.setEnabled(true);
246
247 String username = "";
248 if (user != null) {
249 username = user.getName();
250 }
251 lblUser.setDescription(username);
252 if (user != null && user != User.getAnonymous()) {
253 lblUser.setUrl(getUserUrl(username));
254 } else {
255 lblUser.setUrl(null);
256 }
257 } else {
258 String username = JosmUserIdentityManager.getInstance().getUserName();
259 if (username == null) {
260 lblUser.setDescription(tr("anonymous"));
261 lblUser.setUrl(null);
262 } else {
263 lblUser.setDescription(username);
264 lblUser.setUrl(getUserUrl(username));
265 }
266 lblChangeset.setDescription(tr("none"));
267 lblChangeset.setUrl(null);
268 lblChangesetComments.setVisible(false);
269 changesetDialogAction.setId(null);
270 changesetButton.setEnabled(false);
271 }
272
273 final Changeset oppCs = model != null ? model.getPointInTime(pointInTimeType.opposite()).getChangeset() : null;
274 updateText(cs, "comment", texChangesetComment, null, oppCs, texChangesetComment);
275 updateText(cs, "source", texChangesetSource, lblSource, oppCs, pnlChangesetSource);
276 updateText(cs, "imagery_used", texChangesetImageryUsed, lblImageryUsed, oppCs, pnlChangesetImageryUsed);
277 }
278
279 protected static void updateText(Changeset cs, String attr, JTextArea textArea, JLabel label, Changeset oppCs, JComponent container) {
280 final String text = cs != null ? cs.get(attr) : null;
281 // Update text, hide prefixing label if empty
282 if (label != null) {
283 label.setVisible(text != null && !Utils.isStripEmpty(text));
284 }
285 textArea.setText(text);
286 // Hide container if values of both versions are empty
287 container.setVisible(text != null || (oppCs != null && oppCs.get(attr) != null));
288 }
289
290 static class OpenChangesetDialogAction extends AbstractAction {
291 private final Class<? extends JComponent> componentToSelect;
292 private Integer id;
293
294 OpenChangesetDialogAction(Class<? extends JComponent> componentToSelect) {
295 super(tr("Changeset"), new ImageProvider("dialogs/changeset", "changesetmanager").resetMaxSize(new Dimension(16, 16)).get());
296 putValue(SHORT_DESCRIPTION, tr("Opens the Changeset Manager window for the selected changesets"));
297 this.componentToSelect = componentToSelect;
298 }
299
300 void setId(Integer id) {
301 this.id = id;
302 }
303
304 @Override
305 public void actionPerformed(ActionEvent e) {
306 if (id != null) {
307 ChangesetDialog.LaunchChangesetManager.displayChangesets(Collections.singleton(id));
308 }
309 if (componentToSelect != null) {
310 ChangesetCacheManager.getInstance().setSelectedComponentInDetailPanel(componentToSelect);
311 }
312 }
313 }
314}
Note: See TracBrowser for help on using the repository browser.