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

Last change on this file since 2959 was 2850, checked in by mjulius, 14 years ago

fix messages for gui

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;
15import java.util.logging.Logger;
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.history.HistoryOsmPrimitive;
23import org.openstreetmap.josm.gui.JMultilineLabel;
24import org.openstreetmap.josm.gui.layer.OsmDataLayer;
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 static private final Logger logger = Logger.getLogger(VersionInfoPanel.class.getName());
35
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 FlowLayout(FlowLayout.LEFT));
52 lblUser = new UrlLabel();
53 pnlUserAndChangeset.add(new JLabel(tr("User")));
54 pnlUserAndChangeset.add(lblUser);
55 pnlUserAndChangeset.add(new JLabel(tr("Changeset")));
56 lblChangeset = new UrlLabel();
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 new SimpleDateFormat().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().getUid() != -1) {
140 url = AbstractInfoAction.getBaseUserUrl() + "/" + URLEncoder.encode(getPrimitive().getUser(), "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 = getPrimitive().getUser();
150 lblUser.setDescription(username);
151 } else {
152 String user = Main.pref.get("osm-server.username");
153 if (user == null) {
154 lblUser.setDescription(tr("anonymous"));
155 } else {
156 try {
157 String url = AbstractInfoAction.getBaseUserUrl() + "/" + URLEncoder.encode(user, "UTF-8").replaceAll("\\+", "%20");
158 lblUser.setUrl(url);
159 } catch(UnsupportedEncodingException e) {
160 e.printStackTrace();
161 lblUser.setUrl(null);
162 }
163 lblUser.setDescription(user);
164 }
165 lblChangeset.setDescription(tr("none"));
166 lblChangeset.setUrl(null);
167 }
168 }
169}
Note: See TracBrowser for help on using the repository browser.