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

Last change on this file since 4310 was 4299, checked in by stoecker, 13 years ago

fix #6620 - patch by Casiope - Minor improvement in VersionPanel

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