1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.history;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.GridBagConstraints;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.GridLayout;
|
---|
11 | import java.awt.Insets;
|
---|
12 | import java.awt.event.ActionEvent;
|
---|
13 | import java.text.DateFormat;
|
---|
14 | import java.util.Collections;
|
---|
15 | import java.util.Observable;
|
---|
16 | import java.util.Observer;
|
---|
17 |
|
---|
18 | import javax.swing.AbstractAction;
|
---|
19 | import javax.swing.JButton;
|
---|
20 | import javax.swing.JComponent;
|
---|
21 | import javax.swing.JLabel;
|
---|
22 | import javax.swing.JPanel;
|
---|
23 | import javax.swing.JTextArea;
|
---|
24 |
|
---|
25 | import org.openstreetmap.josm.Main;
|
---|
26 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
27 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
28 | import org.openstreetmap.josm.data.osm.User;
|
---|
29 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
---|
30 | import org.openstreetmap.josm.gui.JosmUserIdentityManager;
|
---|
31 | import org.openstreetmap.josm.gui.dialogs.ChangesetDialog;
|
---|
32 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
33 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
34 | import org.openstreetmap.josm.gui.widgets.UrlLabel;
|
---|
35 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
36 | import org.openstreetmap.josm.tools.GBC;
|
---|
37 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
38 | import org.openstreetmap.josm.tools.Utils;
|
---|
39 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * VersionInfoPanel is an UI component which displays the basic properties of a version
|
---|
43 | * of a {@link OsmPrimitive}.
|
---|
44 | * @since 1709
|
---|
45 | */
|
---|
46 | public class VersionInfoPanel extends JPanel implements Observer{
|
---|
47 | private PointInTimeType pointInTimeType;
|
---|
48 | private transient HistoryBrowserModel model;
|
---|
49 | private JMultilineLabel lblInfo;
|
---|
50 | private UrlLabel lblUser;
|
---|
51 | private UrlLabel lblChangeset;
|
---|
52 | private final OpenChangesetDialogAction changesetDialogAction = new OpenChangesetDialogAction();
|
---|
53 | private final JButton changesetButton = new JButton(changesetDialogAction);
|
---|
54 | private JPanel pnlChangesetSource;
|
---|
55 | private JPanel pnlChangesetImageryUsed;
|
---|
56 | private JLabel lblSource;
|
---|
57 | private JLabel lblImageryUsed;
|
---|
58 | private JTextArea lblChangesetComment;
|
---|
59 | private JTextArea lblChangesetSource;
|
---|
60 | private JTextArea lblChangesetImageryUsed;
|
---|
61 |
|
---|
62 | protected static JTextArea buildTextArea(String tooltip) {
|
---|
63 | JTextArea lbl = new JTextArea();
|
---|
64 | lbl.setLineWrap(true);
|
---|
65 | lbl.setWrapStyleWord(true);
|
---|
66 | lbl.setEditable(false);
|
---|
67 | lbl.setOpaque(false);
|
---|
68 | lbl.setToolTipText(tooltip);
|
---|
69 | return lbl;
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected static JLabel buildLabel(String text, String tooltip, JTextArea textArea) {
|
---|
73 | // We need text field to be a JTextArea for line wrapping but cannot put HTML code in here,
|
---|
74 | // so create a separate JLabel with same characteristics (margin, font)
|
---|
75 | JLabel lbl = new JLabel("<html><p style='margin-top:"+textArea.getMargin().top+"'>"+text+"</html>");
|
---|
76 | lbl.setFont(textArea.getFont());
|
---|
77 | lbl.setToolTipText(tooltip);
|
---|
78 | return lbl;
|
---|
79 | }
|
---|
80 |
|
---|
81 | protected static JPanel buildTextPanel(JLabel label, JTextArea textArea) {
|
---|
82 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
83 | pnl.add(label, GBC.std().anchor(GBC.NORTHWEST));
|
---|
84 | pnl.add(textArea, GBC.eol().insets(2, 0, 0, 0).fill());
|
---|
85 | return pnl;
|
---|
86 | }
|
---|
87 |
|
---|
88 | protected void build() {
|
---|
89 | JPanel pnl1 = new JPanel(new BorderLayout());
|
---|
90 | lblInfo = new JMultilineLabel("");
|
---|
91 | pnl1.add(lblInfo, BorderLayout.CENTER);
|
---|
92 |
|
---|
93 | JPanel pnlUserAndChangeset = new JPanel(new GridLayout(2,2));
|
---|
94 | lblUser = new UrlLabel("", 2);
|
---|
95 | pnlUserAndChangeset.add(new JLabel(tr("User:")));
|
---|
96 | pnlUserAndChangeset.add(lblUser);
|
---|
97 | changesetButton.setMargin(new Insets(0, 0, 0, 0));
|
---|
98 | pnlUserAndChangeset.add(changesetButton);
|
---|
99 | lblChangeset = new UrlLabel("", 2);
|
---|
100 | pnlUserAndChangeset.add(lblChangeset);
|
---|
101 |
|
---|
102 | lblChangesetComment = buildTextArea(tr("Changeset comment"));
|
---|
103 | lblChangesetSource = buildTextArea(tr("Changeset source"));
|
---|
104 | lblChangesetImageryUsed = buildTextArea(tr("Imagery used"));
|
---|
105 |
|
---|
106 | lblSource = buildLabel(tr("<b>Source</b>:"), tr("Changeset source"), lblChangesetSource);
|
---|
107 | lblImageryUsed = buildLabel(tr("<b>Imagery</b>:"), tr("Imagery used"), lblChangesetImageryUsed);
|
---|
108 | pnlChangesetSource = buildTextPanel(lblSource, lblChangesetSource);
|
---|
109 | pnlChangesetImageryUsed = buildTextPanel(lblImageryUsed, lblChangesetImageryUsed);
|
---|
110 |
|
---|
111 | setLayout(new GridBagLayout());
|
---|
112 | GridBagConstraints gc = new GridBagConstraints();
|
---|
113 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
114 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
115 | gc.weightx = 1.0;
|
---|
116 | gc.weighty = 1.0;
|
---|
117 | add(pnl1, gc);
|
---|
118 | gc.gridy = 1;
|
---|
119 | gc.weighty = 0.0;
|
---|
120 | add(pnlUserAndChangeset, gc);
|
---|
121 | gc.gridy = 2;
|
---|
122 | add(lblChangesetComment, gc);
|
---|
123 | gc.gridy = 3;
|
---|
124 | add(pnlChangesetSource, gc);
|
---|
125 | gc.gridy = 4;
|
---|
126 | add(pnlChangesetImageryUsed, gc);
|
---|
127 | }
|
---|
128 |
|
---|
129 | protected HistoryOsmPrimitive getPrimitive() {
|
---|
130 | if (model == null || pointInTimeType == null)
|
---|
131 | return null;
|
---|
132 | return model.getPointInTime(pointInTimeType);
|
---|
133 | }
|
---|
134 |
|
---|
135 | protected String getInfoText() {
|
---|
136 | HistoryOsmPrimitive primitive = getPrimitive();
|
---|
137 | if (primitive == null)
|
---|
138 | return "";
|
---|
139 | String text;
|
---|
140 | if (model.isLatest(primitive)) {
|
---|
141 | OsmDataLayer editLayer = Main.main.getEditLayer();
|
---|
142 | text = tr("<html>Version <strong>{0}</strong> currently edited in layer ''{1}''</html>",
|
---|
143 | Long.toString(primitive.getVersion()),
|
---|
144 | editLayer == null ? tr("unknown") : editLayer.getName()
|
---|
145 | );
|
---|
146 | } else {
|
---|
147 | String date = "?";
|
---|
148 | if (primitive.getTimestamp() != null) {
|
---|
149 | date = DateUtils.formatDateTime(primitive.getTimestamp(), DateFormat.SHORT, DateFormat.SHORT);
|
---|
150 | }
|
---|
151 | text = tr(
|
---|
152 | "<html>Version <strong>{0}</strong> created on <strong>{1}</strong></html>",
|
---|
153 | Long.toString(primitive.getVersion()), date);
|
---|
154 | }
|
---|
155 | return text;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Constructs a new {@code VersionInfoPanel}.
|
---|
160 | */
|
---|
161 | public VersionInfoPanel() {
|
---|
162 | pointInTimeType = null;
|
---|
163 | model = null;
|
---|
164 | build();
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * constructor
|
---|
169 | *
|
---|
170 | * @param model the model (must not be null)
|
---|
171 | * @param pointInTimeType the point in time this panel visualizes (must not be null)
|
---|
172 | * @throws IllegalArgumentException if model is null
|
---|
173 | * @throws IllegalArgumentException if pointInTimeType is null
|
---|
174 | */
|
---|
175 | public VersionInfoPanel(HistoryBrowserModel model, PointInTimeType pointInTimeType) {
|
---|
176 | CheckParameterUtil.ensureParameterNotNull(pointInTimeType, "pointInTimeType");
|
---|
177 | CheckParameterUtil.ensureParameterNotNull(model, "model");
|
---|
178 |
|
---|
179 | this.model = model;
|
---|
180 | this.pointInTimeType = pointInTimeType;
|
---|
181 | model.addObserver(this);
|
---|
182 | build();
|
---|
183 | }
|
---|
184 |
|
---|
185 | protected static String getUserUrl(String username) {
|
---|
186 | return Main.getBaseUserUrl() + "/" + Utils.encodeUrl(username).replaceAll("\\+", "%20");
|
---|
187 | }
|
---|
188 |
|
---|
189 | @Override
|
---|
190 | public void update(Observable o, Object arg) {
|
---|
191 | lblInfo.setText(getInfoText());
|
---|
192 |
|
---|
193 | HistoryOsmPrimitive primitive = getPrimitive();
|
---|
194 | Changeset cs = primitive.getChangeset();
|
---|
195 |
|
---|
196 | if (!model.isLatest(primitive)) {
|
---|
197 | User user = primitive.getUser();
|
---|
198 | String url = Main.getBaseBrowseUrl() + "/changeset/" + primitive.getChangesetId();
|
---|
199 | lblChangeset.setUrl(url);
|
---|
200 | lblChangeset.setDescription(Long.toString(primitive.getChangesetId()));
|
---|
201 | changesetDialogAction.setId((int) primitive.getChangesetId());
|
---|
202 | changesetButton.setEnabled(true);
|
---|
203 |
|
---|
204 | String username = "";
|
---|
205 | if (user != null) {
|
---|
206 | username = user.getName();
|
---|
207 | }
|
---|
208 | lblUser.setDescription(username);
|
---|
209 | if (user != null && user != User.getAnonymous()) {
|
---|
210 | lblUser.setUrl(getUserUrl(username));
|
---|
211 | } else {
|
---|
212 | lblUser.setUrl(null);
|
---|
213 | }
|
---|
214 | } else {
|
---|
215 | String username = JosmUserIdentityManager.getInstance().getUserName();
|
---|
216 | if (username == null) {
|
---|
217 | lblUser.setDescription(tr("anonymous"));
|
---|
218 | lblUser.setUrl(null);
|
---|
219 | } else {
|
---|
220 | lblUser.setDescription(username);
|
---|
221 | lblUser.setUrl(getUserUrl(username));
|
---|
222 | }
|
---|
223 | lblChangeset.setDescription(tr("none"));
|
---|
224 | lblChangeset.setUrl(null);
|
---|
225 | changesetDialogAction.setId(null);
|
---|
226 | changesetButton.setEnabled(false);
|
---|
227 | }
|
---|
228 |
|
---|
229 | final Changeset oppCs = model.getPointInTime(pointInTimeType.opposite()).getChangeset();
|
---|
230 | updateText(cs, "comment", lblChangesetComment, null, oppCs, lblChangesetComment);
|
---|
231 | updateText(cs, "source", lblChangesetSource, lblSource, oppCs, pnlChangesetSource);
|
---|
232 | updateText(cs, "imagery_used", lblChangesetImageryUsed, lblImageryUsed, oppCs, pnlChangesetImageryUsed);
|
---|
233 | }
|
---|
234 |
|
---|
235 | protected static void updateText(Changeset cs, String attr, JTextArea textArea, JLabel label, Changeset oppCs, JComponent container) {
|
---|
236 | final String text = cs != null ? cs.get(attr) : null;
|
---|
237 | // Update text, hide prefixing label if empty
|
---|
238 | if (label != null) {
|
---|
239 | label.setVisible(text != null && !Utils.strip(text).isEmpty());
|
---|
240 | }
|
---|
241 | textArea.setText(text);
|
---|
242 | // Hide container if values of both versions are empty
|
---|
243 | container.setVisible(text != null || (oppCs != null && oppCs.get(attr) != null));
|
---|
244 | }
|
---|
245 |
|
---|
246 | static class OpenChangesetDialogAction extends AbstractAction {
|
---|
247 | private Integer id;
|
---|
248 |
|
---|
249 | public OpenChangesetDialogAction() {
|
---|
250 | super(tr("Changeset"), new ImageProvider("dialogs/changeset", "changesetmanager").resetMaxSize(new Dimension(16, 16)).get());
|
---|
251 | putValue(SHORT_DESCRIPTION, tr("Opens the Changeset Manager window for the selected changesets"));
|
---|
252 | }
|
---|
253 |
|
---|
254 | public void setId(Integer id) {
|
---|
255 | this.id = id;
|
---|
256 | }
|
---|
257 |
|
---|
258 | @Override
|
---|
259 | public void actionPerformed(ActionEvent e) {
|
---|
260 | ChangesetDialog.LaunchChangesetManager.displayChangesets(Collections.singleton(id));
|
---|
261 | }
|
---|
262 | }
|
---|
263 | }
|
---|