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
RevLine 
[2512]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.history;
3
[5926]4import static org.openstreetmap.josm.tools.I18n.tr;
[9526]5import static org.openstreetmap.josm.tools.I18n.trn;
[5926]6
[5053]7import java.awt.BorderLayout;
[8254]8import java.awt.Dimension;
[5053]9import java.awt.GridBagConstraints;
10import java.awt.GridBagLayout;
[8254]11import java.awt.Insets;
12import java.awt.event.ActionEvent;
[5319]13import java.text.DateFormat;
[8254]14import java.util.Collections;
[9468]15import java.util.Date;
[2512]16
[8254]17import javax.swing.AbstractAction;
18import javax.swing.JButton;
[6865]19import javax.swing.JComponent;
[2512]20import javax.swing.JLabel;
21import javax.swing.JPanel;
[6440]22import javax.swing.JTextArea;
[10210]23import javax.swing.event.ChangeEvent;
24import javax.swing.event.ChangeListener;
[2512]25
26import org.openstreetmap.josm.Main;
[6827]27import org.openstreetmap.josm.data.osm.Changeset;
[5440]28import org.openstreetmap.josm.data.osm.OsmPrimitive;
[4602]29import org.openstreetmap.josm.data.osm.User;
[2512]30import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
[5440]31import org.openstreetmap.josm.gui.JosmUserIdentityManager;
[12636]32import org.openstreetmap.josm.gui.MainApplication;
[8254]33import org.openstreetmap.josm.gui.dialogs.ChangesetDialog;
[9526]34import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager;
35import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetDiscussionPanel;
[2512]36import org.openstreetmap.josm.gui.layer.OsmDataLayer;
[6340]37import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
38import org.openstreetmap.josm.gui.widgets.UrlLabel;
[2850]39import org.openstreetmap.josm.tools.CheckParameterUtil;
[6836]40import org.openstreetmap.josm.tools.GBC;
[8254]41import org.openstreetmap.josm.tools.ImageProvider;
[6836]42import org.openstreetmap.josm.tools.Utils;
[7299]43import org.openstreetmap.josm.tools.date.DateUtils;
[2512]44
45/**
46 * VersionInfoPanel is an UI component which displays the basic properties of a version
[5266]47 * of a {@link OsmPrimitive}.
[7299]48 * @since 1709
[2512]49 */
[10210]50public class VersionInfoPanel extends JPanel implements ChangeListener {
[9078]51 private final PointInTimeType pointInTimeType;
52 private final transient HistoryBrowserModel model;
[2512]53 private JMultilineLabel lblInfo;
54 private UrlLabel lblUser;
55 private UrlLabel lblChangeset;
[9526]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);
[8254]59 private final JButton changesetButton = new JButton(changesetDialogAction);
[6836]60 private JPanel pnlChangesetSource;
[8226]61 private JPanel pnlChangesetImageryUsed;
[6836]62 private JLabel lblSource;
[8226]63 private JLabel lblImageryUsed;
[8426]64 private JTextArea texChangesetComment;
65 private JTextArea texChangesetSource;
66 private JTextArea texChangesetImageryUsed;
[2512]67
[6827]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
[6836]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);
[8426]84 lbl.setLabelFor(textArea);
[6836]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));
[8226]91 pnl.add(textArea, GBC.eol().insets(2, 0, 0, 0).fill());
[6836]92 return pnl;
93 }
94
[2512]95 protected void build() {
[6836]96 JPanel pnl1 = new JPanel(new BorderLayout());
[2512]97 lblInfo = new JMultilineLabel("");
98 pnl1.add(lblInfo, BorderLayout.CENTER);
99
[12043]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
[5050]108 lblUser = new UrlLabel("", 2);
[12043]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
[5050]114 lblChangeset = new UrlLabel("", 2);
[12043]115 pnlUserAndChangeset.add(lblChangeset, GBC.std().insets(5, 0, 0, 0).weight(1, 0));
116
[9526]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());
[12043]120 pnlUserAndChangeset.add(lblChangesetComments, GBC.eol());
[2512]121
[8426]122 texChangesetComment = buildTextArea(tr("Changeset comment"));
123 texChangesetSource = buildTextArea(tr("Changeset source"));
124 texChangesetImageryUsed = buildTextArea(tr("Imagery used"));
[6864]125
[8426]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);
[6440]130
[2512]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);
[6440]141 gc.gridy = 2;
[8426]142 add(texChangesetComment, gc);
[6827]143 gc.gridy = 3;
[6836]144 add(pnlChangesetSource, gc);
[8226]145 gc.gridy = 4;
146 add(pnlChangesetImageryUsed, gc);
[2512]147 }
148
149 protected HistoryOsmPrimitive getPrimitive() {
150 if (model == null || pointInTimeType == null)
151 return null;
152 return model.getPointInTime(pointInTimeType);
153 }
154
[9468]155 protected String getInfoText(final Date timestamp, final long version, final boolean isLatest) {
[2512]156 String text;
[9468]157 if (isLatest) {
[12636]158 OsmDataLayer editLayer = MainApplication.getLayerManager().getEditLayer();
[2512]159 text = tr("<html>Version <strong>{0}</strong> currently edited in layer ''{1}''</html>",
[9468]160 Long.toString(version),
[11848]161 editLayer == null ? tr("unknown") : Utils.escapeReservedCharactersHTML(editLayer.getName())
[4299]162 );
[2512]163 } else {
[5929]164 String date = "?";
[9468]165 if (timestamp != null) {
166 date = DateUtils.formatDateTime(timestamp, DateFormat.SHORT, DateFormat.SHORT);
[5929]167 }
[2512]168 text = tr(
169 "<html>Version <strong>{0}</strong> created on <strong>{1}</strong></html>",
[9468]170 Long.toString(version), date);
[2512]171 }
172 return text;
173 }
174
[5929]175 /**
176 * Constructs a new {@code VersionInfoPanel}.
177 */
[2512]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)
[8291]189 * @throws IllegalArgumentException if model is null
190 * @throws IllegalArgumentException if pointInTimeType is null
[2512]191 */
[8291]192 public VersionInfoPanel(HistoryBrowserModel model, PointInTimeType pointInTimeType) {
[2850]193 CheckParameterUtil.ensureParameterNotNull(pointInTimeType, "pointInTimeType");
194 CheckParameterUtil.ensureParameterNotNull(model, "model");
[2512]195
196 this.model = model;
197 this.pointInTimeType = pointInTimeType;
[10210]198 model.addChangeListener(this);
[2512]199 build();
200 }
201
[8304]202 protected static String getUserUrl(String username) {
[10378]203 return Main.getBaseUserUrl() + '/' + Utils.encodeUrl(username).replaceAll("\\+", "%20");
[6827]204 }
205
[6084]206 @Override
[10210]207 public void stateChanged(ChangeEvent e) {
[6827]208 HistoryOsmPrimitive primitive = getPrimitive();
[9680]209 if (primitive != null) {
210 Changeset cs = primitive.getChangeset();
[11416]211 update(cs, model.isLatest(primitive), primitive.getTimestamp(), primitive.getVersion());
[9680]212 }
[9468]213 }
[6827]214
[9526]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 */
[9468]220 public void update(final OsmPrimitive primitive, final boolean isLatest) {
221 update(Changeset.fromPrimitive(primitive), isLatest, primitive.getTimestamp(), primitive.getVersion());
222 }
223
[9526]224 /**
225 * Updates the content of this panel based on the changeset information given by {@code cs}.
[11416]226 * @param cs the changeset information
[9526]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 */
[9468]231 public void update(final Changeset cs, final boolean isLatest, final Date timestamp, final long version) {
232 lblInfo.setText(getInfoText(timestamp, version, isLatest));
233
[11416]234 if (!isLatest && cs != null) {
[9468]235 User user = cs.getUser();
236 String url = Main.getBaseBrowseUrl() + "/changeset/" + cs.getId();
[2512]237 lblChangeset.setUrl(url);
[9468]238 lblChangeset.setDescription(Long.toString(cs.getId()));
[9526]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()));
[9468]244 changesetDialogAction.setId(cs.getId());
[8254]245 changesetButton.setEnabled(true);
[2512]246
[6827]247 String username = "";
248 if (user != null) {
249 username = user.getName();
250 }
251 lblUser.setDescription(username);
[8304]252 if (user != null && user != User.getAnonymous()) {
253 lblUser.setUrl(getUserUrl(username));
254 } else {
[2512]255 lblUser.setUrl(null);
256 }
257 } else {
[6827]258 String username = JosmUserIdentityManager.getInstance().getUserName();
259 if (username == null) {
[2512]260 lblUser.setDescription(tr("anonymous"));
[5440]261 lblUser.setUrl(null);
[2512]262 } else {
[6827]263 lblUser.setDescription(username);
[8304]264 lblUser.setUrl(getUserUrl(username));
[2512]265 }
266 lblChangeset.setDescription(tr("none"));
267 lblChangeset.setUrl(null);
[9526]268 lblChangesetComments.setVisible(false);
[8254]269 changesetDialogAction.setId(null);
270 changesetButton.setEnabled(false);
[2512]271 }
[6827]272
[9468]273 final Changeset oppCs = model != null ? model.getPointInTime(pointInTimeType.opposite()).getChangeset() : null;
[8426]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);
[2512]277 }
[7299]278
[6865]279 protected static void updateText(Changeset cs, String attr, JTextArea textArea, JLabel label, Changeset oppCs, JComponent container) {
[6836]280 final String text = cs != null ? cs.get(attr) : null;
281 // Update text, hide prefixing label if empty
[6865]282 if (label != null) {
[11435]283 label.setVisible(text != null && !Utils.isStripEmpty(text));
[6865]284 }
[6836]285 textArea.setText(text);
[6865]286 // Hide container if values of both versions are empty
287 container.setVisible(text != null || (oppCs != null && oppCs.get(attr) != null));
[6836]288 }
[8254]289
290 static class OpenChangesetDialogAction extends AbstractAction {
[9526]291 private final Class<? extends JComponent> componentToSelect;
[8254]292 private Integer id;
293
[9526]294 OpenChangesetDialogAction(Class<? extends JComponent> componentToSelect) {
[8254]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"));
[9526]297 this.componentToSelect = componentToSelect;
[8254]298 }
299
[9526]300 void setId(Integer id) {
[8254]301 this.id = id;
302 }
303
304 @Override
305 public void actionPerformed(ActionEvent e) {
[11416]306 if (id != null) {
307 ChangesetDialog.LaunchChangesetManager.displayChangesets(Collections.singleton(id));
308 }
[9526]309 if (componentToSelect != null) {
310 ChangesetCacheManager.getInstance().setSelectedComponentInDetailPanel(componentToSelect);
311 }
[8254]312 }
313 }
[2512]314}
Note: See TracBrowser for help on using the repository browser.