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

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

fix #9659 - prefix changeset and source by "c:" or "s:" to distinguish them in history dialog

  • Property svn:eol-style set to native
File size: 9.0 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.Changeset;
23import org.openstreetmap.josm.data.osm.OsmPrimitive;
24import org.openstreetmap.josm.data.osm.User;
25import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
26import org.openstreetmap.josm.gui.JosmUserIdentityManager;
27import org.openstreetmap.josm.gui.layer.OsmDataLayer;
28import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
29import org.openstreetmap.josm.gui.widgets.UrlLabel;
30import org.openstreetmap.josm.tools.CheckParameterUtil;
31import org.openstreetmap.josm.tools.GBC;
32import org.openstreetmap.josm.tools.Utils;
33
34/**
35 * VersionInfoPanel is an UI component which displays the basic properties of a version
36 * of a {@link OsmPrimitive}.
37 *
38 */
39public class VersionInfoPanel extends JPanel implements Observer{
40 private PointInTimeType pointInTimeType;
41 private HistoryBrowserModel model;
42 private JMultilineLabel lblInfo;
43 private UrlLabel lblUser;
44 private UrlLabel lblChangeset;
45 private JPanel pnlChangesetComment;
46 private JPanel pnlChangesetSource;
47 private JLabel lblComment;
48 private JLabel lblSource;
49 private JTextArea lblChangesetComment;
50 private JTextArea lblChangesetSource;
51
52 protected static JTextArea buildTextArea(String tooltip) {
53 JTextArea lbl = new JTextArea();
54 lbl.setLineWrap(true);
55 lbl.setWrapStyleWord(true);
56 lbl.setEditable(false);
57 lbl.setOpaque(false);
58 lbl.setToolTipText(tooltip);
59 return lbl;
60 }
61
62 protected static JLabel buildLabel(String text, String tooltip, JTextArea textArea) {
63 // We need text field to be a JTextArea for line wrapping but cannot put HTML code in here,
64 // so create a separate JLabel with same characteristics (margin, font)
65 JLabel lbl = new JLabel("<html><p style='margin-top:"+textArea.getMargin().top+"'>"+text+"</html>");
66 lbl.setFont(textArea.getFont());
67 lbl.setToolTipText(tooltip);
68 return lbl;
69 }
70
71 protected static JPanel buildTextPanel(JLabel label, JTextArea textArea) {
72 JPanel pnl = new JPanel(new GridBagLayout());
73 pnl.add(label, GBC.std().anchor(GBC.NORTHWEST));
74 pnl.add(textArea, GBC.eol().fill());
75 return pnl;
76 }
77
78 protected void build() {
79 JPanel pnl1 = new JPanel(new BorderLayout());
80 lblInfo = new JMultilineLabel("");
81 pnl1.add(lblInfo, BorderLayout.CENTER);
82
83 JPanel pnlUserAndChangeset = new JPanel(new GridLayout(2,2));
84 lblUser = new UrlLabel("", 2);
85 pnlUserAndChangeset.add(new JLabel(tr("User:")));
86 pnlUserAndChangeset.add(lblUser);
87 pnlUserAndChangeset.add(new JLabel(tr("Changeset:")));
88 lblChangeset = new UrlLabel("", 2);
89 pnlUserAndChangeset.add(lblChangeset);
90
91 lblChangesetComment = buildTextArea(tr("Changeset comment"));
92 lblChangesetSource = buildTextArea(tr("Changeset source"));
93
94 lblComment = buildLabel(/*I18n: comment*/tr("<b>c</b>:"), tr("comment"), lblChangesetComment);
95 lblSource = buildLabel(/*I18n: source*/tr("<b>s</b>:"), tr("source"), lblChangesetSource);
96
97 pnlChangesetComment = buildTextPanel(lblComment, lblChangesetComment);
98 pnlChangesetSource = buildTextPanel(lblSource, lblChangesetSource);
99
100 setLayout(new GridBagLayout());
101 GridBagConstraints gc = new GridBagConstraints();
102 gc.anchor = GridBagConstraints.NORTHWEST;
103 gc.fill = GridBagConstraints.HORIZONTAL;
104 gc.weightx = 1.0;
105 gc.weighty = 1.0;
106 add(pnl1, gc);
107 gc.gridy = 1;
108 gc.weighty = 0.0;
109 add(pnlUserAndChangeset, gc);
110 gc.gridy = 2;
111 add(pnlChangesetComment, gc);
112 gc.gridy = 3;
113 add(pnlChangesetSource, gc);
114 }
115
116 protected HistoryOsmPrimitive getPrimitive() {
117 if (model == null || pointInTimeType == null)
118 return null;
119 return model.getPointInTime(pointInTimeType);
120 }
121
122 protected String getInfoText() {
123 HistoryOsmPrimitive primitive = getPrimitive();
124 if (primitive == null)
125 return "";
126 String text;
127 if (model.isLatest(primitive)) {
128 OsmDataLayer editLayer = Main.main.getEditLayer();
129 text = tr("<html>Version <strong>{0}</strong> currently edited in layer ''{1}''</html>",
130 Long.toString(primitive.getVersion()),
131 editLayer == null ? tr("unknown") : editLayer.getName()
132 );
133 } else {
134 String date = "?";
135 if (primitive.getTimestamp() != null) {
136 date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(primitive.getTimestamp());
137 }
138 text = tr(
139 "<html>Version <strong>{0}</strong> created on <strong>{1}</strong></html>",
140 Long.toString(primitive.getVersion()), date);
141 }
142 return text;
143 }
144
145 /**
146 * Constructs a new {@code VersionInfoPanel}.
147 */
148 public VersionInfoPanel() {
149 pointInTimeType = null;
150 model = null;
151 build();
152 }
153
154 /**
155 * constructor
156 *
157 * @param model the model (must not be null)
158 * @param pointInTimeType the point in time this panel visualizes (must not be null)
159 * @exception IllegalArgumentException thrown, if model is null
160 * @exception IllegalArgumentException thrown, if pointInTimeType is null
161 *
162 */
163 public VersionInfoPanel(HistoryBrowserModel model, PointInTimeType pointInTimeType) throws IllegalArgumentException {
164 CheckParameterUtil.ensureParameterNotNull(pointInTimeType, "pointInTimeType");
165 CheckParameterUtil.ensureParameterNotNull(model, "model");
166
167 this.model = model;
168 this.pointInTimeType = pointInTimeType;
169 model.addObserver(this);
170 build();
171 }
172
173 protected static String getUserUrl(String username) throws UnsupportedEncodingException {
174 return AbstractInfoAction.getBaseUserUrl() + "/" + URLEncoder.encode(username, "UTF-8").replaceAll("\\+", "%20");
175 }
176
177 @Override
178 public void update(Observable o, Object arg) {
179 lblInfo.setText(getInfoText());
180
181 HistoryOsmPrimitive primitive = getPrimitive();
182 Changeset cs = primitive.getChangeset();
183
184 if (!model.isLatest(primitive)) {
185 User user = primitive.getUser();
186 String url = AbstractInfoAction.getBaseBrowseUrl() + "/changeset/" + primitive.getChangesetId();
187 lblChangeset.setUrl(url);
188 lblChangeset.setDescription(Long.toString(primitive.getChangesetId()));
189
190 String username = "";
191 if (user != null) {
192 username = user.getName();
193 }
194 lblUser.setDescription(username);
195 try {
196 if (user != null && user != User.getAnonymous()) {
197 lblUser.setUrl(getUserUrl(username));
198 } else {
199 lblUser.setUrl(null);
200 }
201 } catch(UnsupportedEncodingException e) {
202 Main.error(e);
203 lblUser.setUrl(null);
204 }
205 } else {
206 String username = JosmUserIdentityManager.getInstance().getUserName();
207 if (username == null) {
208 lblUser.setDescription(tr("anonymous"));
209 lblUser.setUrl(null);
210 } else {
211 lblUser.setDescription(username);
212 try {
213 lblUser.setUrl(getUserUrl(username));
214 } catch(UnsupportedEncodingException e) {
215 Main.error(e);
216 lblUser.setUrl(null);
217 }
218 }
219 lblChangeset.setDescription(tr("none"));
220 lblChangeset.setUrl(null);
221 }
222
223 final Changeset oppCs = model.getPointInTime(pointInTimeType.opposite()).getChangeset();
224 updateText(cs, "comment", lblChangesetComment, lblComment, oppCs, pnlChangesetComment);
225 updateText(cs, "source", lblChangesetSource, lblSource, oppCs, pnlChangesetSource);
226 }
227
228 protected static void updateText(Changeset cs, String attr, JTextArea textArea, JLabel label, Changeset oppCs, JPanel panel) {
229 final String text = cs != null ? cs.get(attr) : null;
230 // Update text, hide prefixing label if empty
231 label.setVisible(text != null && !Utils.strip(text).isEmpty());
232 textArea.setText(text);
233 // Hide panel if values of both versions are empty
234 panel.setVisible(text != null || (oppCs != null && oppCs.get(attr) != null));
235 }
236}
Note: See TracBrowser for help on using the repository browser.