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

Last change on this file since 13202 was 13130, checked in by Don-vip, 6 years ago

fix #15572 - use ImageProvider attach API for all JOSM actions to ensure proper icon size everywhere

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