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

Last change on this file since 3083 was 3083, checked in by bastiK, 14 years ago

added svn:eol-style=native to source files

  • 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;
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 @SuppressWarnings("unused")
35 static private final Logger logger = Logger.getLogger(VersionInfoPanel.class.getName());
36
37 private PointInTimeType pointInTimeType;
38 private HistoryBrowserModel model;
39 private JMultilineLabel lblInfo;
40 private UrlLabel lblUser;
41 private UrlLabel lblChangeset;
42 private JPanel pnlUserAndChangeset;
43
44 protected void build() {
45 JPanel pnl1 = new JPanel();
46 pnl1.setLayout(new BorderLayout());
47 lblInfo = new JMultilineLabel("");
48 //lblInfo.setHorizontalAlignment(JLabel.LEFT);
49 pnl1.add(lblInfo, BorderLayout.CENTER);
50
51 pnlUserAndChangeset = new JPanel();
52 pnlUserAndChangeset.setLayout(new FlowLayout(FlowLayout.LEFT));
53 lblUser = new UrlLabel();
54 pnlUserAndChangeset.add(new JLabel(tr("User")));
55 pnlUserAndChangeset.add(lblUser);
56 pnlUserAndChangeset.add(new JLabel(tr("Changeset")));
57 lblChangeset = new UrlLabel();
58 pnlUserAndChangeset.add(lblChangeset);
59
60 setLayout(new GridBagLayout());
61 GridBagConstraints gc = new GridBagConstraints();
62 gc.anchor = GridBagConstraints.NORTHWEST;
63 gc.fill = GridBagConstraints.HORIZONTAL;
64 gc.weightx = 1.0;
65 gc.weighty = 1.0;
66 add(pnl1, gc);
67 gc.gridy = 1;
68 gc.weighty = 0.0;
69 add(pnlUserAndChangeset, gc);
70 }
71
72 protected HistoryOsmPrimitive getPrimitive() {
73 if (model == null || pointInTimeType == null)
74 return null;
75 return model.getPointInTime(pointInTimeType);
76 }
77
78 protected OsmDataLayer getEditLayer() {
79 try {
80 return Main.map.mapView.getEditLayer();
81 } catch(NullPointerException e) {
82 return null;
83 }
84 }
85
86 protected String getInfoText() {
87 HistoryOsmPrimitive primitive = getPrimitive();
88 if (primitive == null)
89 return "";
90 String text;
91 if (model.isLatest(primitive)) {
92 text = tr("<html>Version <strong>{0}</strong> currently edited in layer ''{1}''</html>",
93 Long.toString(primitive.getVersion()),
94 getEditLayer() == null ? tr("unknown") : getEditLayer().getName()
95 );
96 } else {
97 text = tr(
98 "<html>Version <strong>{0}</strong> created on <strong>{1}</strong></html>",
99 Long.toString(primitive.getVersion()),
100 new SimpleDateFormat().format(primitive.getTimestamp())
101 );
102 }
103 return text;
104 }
105
106 public VersionInfoPanel() {
107 pointInTimeType = null;
108 model = null;
109 build();
110 }
111
112 /**
113 * constructor
114 *
115 * @param model the model (must not be null)
116 * @param pointInTimeType the point in time this panel visualizes (must not be null)
117 * @exception IllegalArgumentException thrown, if model is null
118 * @exception IllegalArgumentException thrown, if pointInTimeType is null
119 *
120 */
121 public VersionInfoPanel(HistoryBrowserModel model, PointInTimeType pointInTimeType) throws IllegalArgumentException {
122 CheckParameterUtil.ensureParameterNotNull(pointInTimeType, "pointInTimeType");
123 CheckParameterUtil.ensureParameterNotNull(model, "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.