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

Last change on this file since 5340 was 5319, checked in by simon04, 12 years ago

fix #7835 - localize dates in UI

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