001 // License: GPL. For details, see LICENSE file.
002 package org.openstreetmap.josm.gui.history;
003
004 import java.awt.BorderLayout;
005 import java.awt.GridBagConstraints;
006 import java.awt.GridBagLayout;
007 import java.awt.GridLayout;
008
009 import static org.openstreetmap.josm.tools.I18n.tr;
010
011 import java.io.UnsupportedEncodingException;
012 import java.net.URLEncoder;
013 import java.text.DateFormat;
014 import java.util.Observable;
015 import java.util.Observer;
016
017 import javax.swing.JLabel;
018 import javax.swing.JPanel;
019
020 import org.openstreetmap.josm.Main;
021 import org.openstreetmap.josm.actions.AbstractInfoAction;
022 import org.openstreetmap.josm.data.osm.OsmPrimitive;
023 import org.openstreetmap.josm.data.osm.User;
024 import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
025 import org.openstreetmap.josm.gui.JMultilineLabel;
026 import org.openstreetmap.josm.gui.JosmUserIdentityManager;
027 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
028 import org.openstreetmap.josm.tools.CheckParameterUtil;
029 import org.openstreetmap.josm.tools.UrlLabel;
030
031 /**
032 * VersionInfoPanel is an UI component which displays the basic properties of a version
033 * of a {@link OsmPrimitive}.
034 *
035 */
036 public class VersionInfoPanel extends JPanel implements Observer{
037 private PointInTimeType pointInTimeType;
038 private HistoryBrowserModel model;
039 private JMultilineLabel lblInfo;
040 private UrlLabel lblUser;
041 private UrlLabel lblChangeset;
042 private JPanel pnlUserAndChangeset;
043
044 protected void build() {
045 JPanel pnl1 = new JPanel();
046 pnl1.setLayout(new BorderLayout());
047 lblInfo = new JMultilineLabel("");
048 //lblInfo.setHorizontalAlignment(JLabel.LEFT);
049 pnl1.add(lblInfo, BorderLayout.CENTER);
050
051 pnlUserAndChangeset = new JPanel();
052 pnlUserAndChangeset.setLayout(new GridLayout(2,2));
053 lblUser = new UrlLabel("", 2);
054 pnlUserAndChangeset.add(new JLabel(tr("User:")));
055 pnlUserAndChangeset.add(lblUser);
056 pnlUserAndChangeset.add(new JLabel(tr("Changeset:")));
057 lblChangeset = new UrlLabel("", 2);
058 pnlUserAndChangeset.add(lblChangeset);
059
060 setLayout(new GridBagLayout());
061 GridBagConstraints gc = new GridBagConstraints();
062 gc.anchor = GridBagConstraints.NORTHWEST;
063 gc.fill = GridBagConstraints.HORIZONTAL;
064 gc.weightx = 1.0;
065 gc.weighty = 1.0;
066 add(pnl1, gc);
067 gc.gridy = 1;
068 gc.weighty = 0.0;
069 add(pnlUserAndChangeset, gc);
070 }
071
072 protected HistoryOsmPrimitive getPrimitive() {
073 if (model == null || pointInTimeType == null)
074 return null;
075 return model.getPointInTime(pointInTimeType);
076 }
077
078 protected OsmDataLayer getEditLayer() {
079 try {
080 return Main.map.mapView.getEditLayer();
081 } catch(NullPointerException e) {
082 return null;
083 }
084 }
085
086 protected String getInfoText() {
087 HistoryOsmPrimitive primitive = getPrimitive();
088 if (primitive == null)
089 return "";
090 String text;
091 if (model.isLatest(primitive)) {
092 text = tr("<html>Version <strong>{0}</strong> currently edited in layer ''{1}''</html>",
093 Long.toString(primitive.getVersion()),
094 getEditLayer() == null ? tr("unknown") : getEditLayer().getName()
095 );
096 } else {
097 text = tr(
098 "<html>Version <strong>{0}</strong> created on <strong>{1}</strong></html>",
099 Long.toString(primitive.getVersion()),
100 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).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().getUser() != null && getPrimitive().getUser() != User.getAnonymous()) {
141 url = AbstractInfoAction.getBaseUserUrl() + "/" + URLEncoder.encode(getPrimitive().getUser().getName(), "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 = "";
151 if (getPrimitive().getUser() != null) {
152 username = getPrimitive().getUser().getName();
153 }
154 lblUser.setDescription(username);
155 } else {
156 String user = JosmUserIdentityManager.getInstance().getUserName();
157 if (user == null) {
158 lblUser.setDescription(tr("anonymous"));
159 lblUser.setUrl(null);
160 } else {
161 try {
162 String url = AbstractInfoAction.getBaseUserUrl() + "/" + URLEncoder.encode(user, "UTF-8").replaceAll("\\+", "%20");
163 lblUser.setUrl(url);
164 } catch(UnsupportedEncodingException e) {
165 e.printStackTrace();
166 lblUser.setUrl(null);
167 }
168 lblUser.setDescription(user);
169 }
170 lblChangeset.setDescription(tr("none"));
171 lblChangeset.setUrl(null);
172 }
173 }
174 }