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

Last change on this file since 5050 was 5050, checked in by akks, 12 years ago

UrlLabel class simplification by Zverik, better label layout in VersionInfoPanel (see #7450, #7326)

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