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

Last change on this file since 6336 was 6336, checked in by Don-vip, 10 years ago

code cleanup / robustness in edit layer handling

  • Property svn:eol-style set to native
File size: 6.2 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.GridBagConstraints;
8import java.awt.GridBagLayout;
9import java.awt.GridLayout;
10import java.io.UnsupportedEncodingException;
11import java.net.URLEncoder;
12import java.text.DateFormat;
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.OsmPrimitive;
22import org.openstreetmap.josm.data.osm.User;
23import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
24import org.openstreetmap.josm.gui.JMultilineLabel;
25import org.openstreetmap.josm.gui.JosmUserIdentityManager;
26import org.openstreetmap.josm.gui.layer.OsmDataLayer;
27import org.openstreetmap.josm.tools.CheckParameterUtil;
28import org.openstreetmap.josm.tools.UrlLabel;
29
30/**
31 * VersionInfoPanel is an UI component which displays the basic properties of a version
32 * of a {@link OsmPrimitive}.
33 *
34 */
35public class VersionInfoPanel extends JPanel implements Observer{
36 private PointInTimeType pointInTimeType;
37 private HistoryBrowserModel model;
38 private JMultilineLabel lblInfo;
39 private UrlLabel lblUser;
40 private UrlLabel lblChangeset;
41
42 protected void build() {
43 JPanel pnl1 = new JPanel();
44 pnl1.setLayout(new BorderLayout());
45 lblInfo = new JMultilineLabel("");
46 pnl1.add(lblInfo, BorderLayout.CENTER);
47
48 JPanel pnlUserAndChangeset = new JPanel();
49 pnlUserAndChangeset.setLayout(new GridLayout(2,2));
50 lblUser = new UrlLabel("", 2);
51 pnlUserAndChangeset.add(new JLabel(tr("User:")));
52 pnlUserAndChangeset.add(lblUser);
53 pnlUserAndChangeset.add(new JLabel(tr("Changeset:")));
54 lblChangeset = new UrlLabel("", 2);
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 String getInfoText() {
76 HistoryOsmPrimitive primitive = getPrimitive();
77 if (primitive == null)
78 return "";
79 String text;
80 if (model.isLatest(primitive)) {
81 OsmDataLayer editLayer = Main.main.getEditLayer();
82 text = tr("<html>Version <strong>{0}</strong> currently edited in layer ''{1}''</html>",
83 Long.toString(primitive.getVersion()),
84 editLayer == null ? tr("unknown") : editLayer.getName()
85 );
86 } else {
87 String date = "?";
88 if (primitive.getTimestamp() != null) {
89 date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(primitive.getTimestamp());
90 }
91 text = tr(
92 "<html>Version <strong>{0}</strong> created on <strong>{1}</strong></html>",
93 Long.toString(primitive.getVersion()), date);
94 }
95 return text;
96 }
97
98 /**
99 * Constructs a new {@code VersionInfoPanel}.
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 @Override
127 public void update(Observable o, Object arg) {
128 lblInfo.setText(getInfoText());
129
130 if (!model.isLatest(getPrimitive())) {
131 String url = AbstractInfoAction.getBaseBrowseUrl() + "/changeset/" + getPrimitive().getChangesetId();
132 lblChangeset.setUrl(url);
133 lblChangeset.setDescription(Long.toString(getPrimitive().getChangesetId()));
134
135 try {
136 if (getPrimitive().getUser() != null && getPrimitive().getUser() != User.getAnonymous()) {
137 url = AbstractInfoAction.getBaseUserUrl() + "/" + URLEncoder.encode(getPrimitive().getUser().getName(), "UTF-8").replaceAll("\\+", "%20");
138 lblUser.setUrl(url);
139 } else {
140 lblUser.setUrl(null);
141 }
142 } catch(UnsupportedEncodingException e) {
143 e.printStackTrace();
144 lblUser.setUrl(null);
145 }
146 String username = "";
147 if (getPrimitive().getUser() != null) {
148 username = getPrimitive().getUser().getName();
149 }
150 lblUser.setDescription(username);
151 } else {
152 String user = JosmUserIdentityManager.getInstance().getUserName();
153 if (user == null) {
154 lblUser.setDescription(tr("anonymous"));
155 lblUser.setUrl(null);
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.