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

Last change on this file since 12615 was 12043, checked in by michael2402, 7 years ago

Add nicer padding to version info panel

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