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

Last change on this file since 2626 was 2512, checked in by stoecker, 14 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

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