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

Last change on this file since 6623 was 6519, checked in by simon04, 10 years ago

Word wrap changeset comment in history dialog

  • Property svn:eol-style set to native
File size: 6.9 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;
5
6import java.awt.BorderLayout;
7import java.awt.GridBagConstraints;
8import java.awt.GridBagLayout;
9import java.awt.GridLayout;
10import java.io.UnsupportedEncodingException;
11import java.net.URLEncoder;
12import java.text.DateFormat;
13import java.util.Observable;
14import java.util.Observer;
15
16import javax.swing.JLabel;
17import javax.swing.JPanel;
18import javax.swing.JTextArea;
19
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.actions.AbstractInfoAction;
22import org.openstreetmap.josm.data.osm.OsmPrimitive;
23import org.openstreetmap.josm.data.osm.User;
24import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
25import org.openstreetmap.josm.gui.JosmUserIdentityManager;
26import org.openstreetmap.josm.gui.layer.OsmDataLayer;
27import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
28import org.openstreetmap.josm.gui.widgets.UrlLabel;
29import org.openstreetmap.josm.tools.CheckParameterUtil;
30
31/**
32 * VersionInfoPanel is an UI component which displays the basic properties of a version
33 * of a {@link OsmPrimitive}.
34 *
35 */
36public class VersionInfoPanel extends JPanel implements Observer{
37 private PointInTimeType pointInTimeType;
38 private HistoryBrowserModel model;
39 private JMultilineLabel lblInfo;
40 private UrlLabel lblUser;
41 private UrlLabel lblChangeset;
42 private JTextArea lblChangesetComment;
43
44 protected void build() {
45 JPanel pnl1 = new JPanel();
46 pnl1.setLayout(new BorderLayout());
47 lblInfo = new JMultilineLabel("");
48 pnl1.add(lblInfo, BorderLayout.CENTER);
49
50 JPanel pnlUserAndChangeset = new JPanel();
51 pnlUserAndChangeset.setLayout(new GridLayout(2,2));
52 lblUser = new UrlLabel("", 2);
53 pnlUserAndChangeset.add(new JLabel(tr("User:")));
54 pnlUserAndChangeset.add(lblUser);
55 pnlUserAndChangeset.add(new JLabel(tr("Changeset:")));
56 lblChangeset = new UrlLabel("", 2);
57 pnlUserAndChangeset.add(lblChangeset);
58
59 lblChangesetComment = new JTextArea();
60 lblChangesetComment.setLineWrap(true);
61 lblChangesetComment.setWrapStyleWord(true);
62 lblChangesetComment.setEditable(false);
63 lblChangesetComment.setOpaque(false);
64
65 setLayout(new GridBagLayout());
66 GridBagConstraints gc = new GridBagConstraints();
67 gc.anchor = GridBagConstraints.NORTHWEST;
68 gc.fill = GridBagConstraints.HORIZONTAL;
69 gc.weightx = 1.0;
70 gc.weighty = 1.0;
71 add(pnl1, gc);
72 gc.gridy = 1;
73 gc.weighty = 0.0;
74 add(pnlUserAndChangeset, gc);
75 gc.gridy = 2;
76 add(lblChangesetComment, gc);
77 }
78
79 protected HistoryOsmPrimitive getPrimitive() {
80 if (model == null || pointInTimeType == null)
81 return null;
82 return model.getPointInTime(pointInTimeType);
83 }
84
85 protected String getInfoText() {
86 HistoryOsmPrimitive primitive = getPrimitive();
87 if (primitive == null)
88 return "";
89 String text;
90 if (model.isLatest(primitive)) {
91 OsmDataLayer editLayer = Main.main.getEditLayer();
92 text = tr("<html>Version <strong>{0}</strong> currently edited in layer ''{1}''</html>",
93 Long.toString(primitive.getVersion()),
94 editLayer == null ? tr("unknown") : editLayer.getName()
95 );
96 } else {
97 String date = "?";
98 if (primitive.getTimestamp() != null) {
99 date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(primitive.getTimestamp());
100 }
101 text = tr(
102 "<html>Version <strong>{0}</strong> created on <strong>{1}</strong></html>",
103 Long.toString(primitive.getVersion()), date);
104 }
105 return text;
106 }
107
108 /**
109 * Constructs a new {@code VersionInfoPanel}.
110 */
111 public VersionInfoPanel() {
112 pointInTimeType = null;
113 model = null;
114 build();
115 }
116
117 /**
118 * constructor
119 *
120 * @param model the model (must not be null)
121 * @param pointInTimeType the point in time this panel visualizes (must not be null)
122 * @exception IllegalArgumentException thrown, if model is null
123 * @exception IllegalArgumentException thrown, if pointInTimeType is null
124 *
125 */
126 public VersionInfoPanel(HistoryBrowserModel model, PointInTimeType pointInTimeType) throws IllegalArgumentException {
127 CheckParameterUtil.ensureParameterNotNull(pointInTimeType, "pointInTimeType");
128 CheckParameterUtil.ensureParameterNotNull(model, "model");
129
130 this.model = model;
131 this.pointInTimeType = pointInTimeType;
132 model.addObserver(this);
133 build();
134 }
135
136 @Override
137 public void update(Observable o, Object arg) {
138 lblInfo.setText(getInfoText());
139
140 if (!model.isLatest(getPrimitive())) {
141 String url = AbstractInfoAction.getBaseBrowseUrl() + "/changeset/" + getPrimitive().getChangesetId();
142 lblChangeset.setUrl(url);
143 lblChangeset.setDescription(Long.toString(getPrimitive().getChangesetId()));
144 final String comment = getPrimitive().getChangeset() != null ? getPrimitive().getChangeset().get("comment") : null;
145 lblChangesetComment.setText(comment);
146 lblChangesetComment.setToolTipText(tr("Changeset comment"));
147
148 try {
149 if (getPrimitive().getUser() != null && getPrimitive().getUser() != User.getAnonymous()) {
150 url = AbstractInfoAction.getBaseUserUrl() + "/" + URLEncoder.encode(getPrimitive().getUser().getName(), "UTF-8").replaceAll("\\+", "%20");
151 lblUser.setUrl(url);
152 } else {
153 lblUser.setUrl(null);
154 }
155 } catch(UnsupportedEncodingException e) {
156 e.printStackTrace();
157 lblUser.setUrl(null);
158 }
159 String username = "";
160 if (getPrimitive().getUser() != null) {
161 username = getPrimitive().getUser().getName();
162 }
163 lblUser.setDescription(username);
164 } else {
165 String user = JosmUserIdentityManager.getInstance().getUserName();
166 if (user == null) {
167 lblUser.setDescription(tr("anonymous"));
168 lblUser.setUrl(null);
169 } else {
170 try {
171 String url = AbstractInfoAction.getBaseUserUrl() + "/" + URLEncoder.encode(user, "UTF-8").replaceAll("\\+", "%20");
172 lblUser.setUrl(url);
173 } catch(UnsupportedEncodingException e) {
174 e.printStackTrace();
175 lblUser.setUrl(null);
176 }
177 lblUser.setDescription(user);
178 }
179 lblChangeset.setDescription(tr("none"));
180 lblChangeset.setUrl(null);
181 }
182 }
183}
Note: See TracBrowser for help on using the repository browser.