1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.history;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
7 |
|
---|
8 | import java.awt.BorderLayout;
|
---|
9 | import java.awt.FlowLayout;
|
---|
10 | import java.awt.event.ActionEvent;
|
---|
11 | import java.awt.event.WindowAdapter;
|
---|
12 | import java.awt.event.WindowEvent;
|
---|
13 |
|
---|
14 | import javax.swing.AbstractAction;
|
---|
15 | import javax.swing.JButton;
|
---|
16 | import javax.swing.JDialog;
|
---|
17 | import javax.swing.JLabel;
|
---|
18 | import javax.swing.JPanel;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.Main;
|
---|
21 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
22 | import org.openstreetmap.josm.data.osm.history.History;
|
---|
23 | import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
|
---|
24 | import org.openstreetmap.josm.data.osm.history.HistoryDataSetListener;
|
---|
25 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
26 | import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
|
---|
27 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
28 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
29 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
30 | import org.openstreetmap.josm.tools.InputMapUtils;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * This is non-modal dialog, always showing on top, which displays history information
|
---|
34 | * about a given {@link org.openstreetmap.josm.data.osm.OsmPrimitive}.
|
---|
35 | * @since 1709
|
---|
36 | */
|
---|
37 | public class HistoryBrowserDialog extends JDialog implements HistoryDataSetListener {
|
---|
38 |
|
---|
39 | /** the embedded browser */
|
---|
40 | private final HistoryBrowser browser = new HistoryBrowser();
|
---|
41 | private final CloseAction closeAction = new CloseAction();
|
---|
42 | private final JLabel titleLabel = new JLabel("", JLabel.CENTER);
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Constructs a new {@code HistoryBrowserDialog}.
|
---|
46 | *
|
---|
47 | * @param history the history to be displayed
|
---|
48 | */
|
---|
49 | public HistoryBrowserDialog(History history) {
|
---|
50 | super(GuiHelper.getFrameForComponent(Main.parent), false);
|
---|
51 | build();
|
---|
52 | setHistory(history);
|
---|
53 | setTitle(buildTitle(history));
|
---|
54 | pack();
|
---|
55 | if (getInsets().top > 0) {
|
---|
56 | titleLabel.setVisible(false);
|
---|
57 | }
|
---|
58 | HistoryDataSet.getInstance().addHistoryDataSetListener(this);
|
---|
59 | addWindowListener(new WindowClosingAdapter());
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Constructs the title for this dialog
|
---|
64 | *
|
---|
65 | * @param h the current history
|
---|
66 | * @return the title for this dialog
|
---|
67 | */
|
---|
68 | static String buildTitle(History h) {
|
---|
69 | String title;
|
---|
70 | switch (h.getEarliest().getType()) {
|
---|
71 | case NODE: title = marktr("History for node {0}");
|
---|
72 | break;
|
---|
73 | case WAY: title = marktr("History for way {0}");
|
---|
74 | break;
|
---|
75 | case RELATION: title = marktr("History for relation {0}");
|
---|
76 | break;
|
---|
77 | default: title = "";
|
---|
78 | }
|
---|
79 | return tr(title, Long.toString(h.getId()));
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public void setTitle(String title) {
|
---|
84 | super.setTitle(title);
|
---|
85 | if (titleLabel != null) {
|
---|
86 | titleLabel.setText(title);
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * builds the GUI
|
---|
92 | */
|
---|
93 | protected void build() {
|
---|
94 | setLayout(new BorderLayout());
|
---|
95 |
|
---|
96 | add(titleLabel, BorderLayout.NORTH);
|
---|
97 |
|
---|
98 | add(browser, BorderLayout.CENTER);
|
---|
99 |
|
---|
100 | JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
|
---|
101 |
|
---|
102 | JButton btn = new JButton(new ReloadAction());
|
---|
103 | btn.setName("btn.reload");
|
---|
104 | pnl.add(btn);
|
---|
105 |
|
---|
106 | btn = new JButton(closeAction);
|
---|
107 | btn.setName("btn.close");
|
---|
108 | pnl.add(btn);
|
---|
109 | InputMapUtils.addEscapeAction(getRootPane(), closeAction);
|
---|
110 |
|
---|
111 | btn = new JButton(new ContextSensitiveHelpAction(ht("/Action/ObjectHistory")));
|
---|
112 | btn.setName("btn.help");
|
---|
113 | pnl.add(btn);
|
---|
114 | add(pnl, BorderLayout.SOUTH);
|
---|
115 |
|
---|
116 | HelpUtil.setHelpContext(getRootPane(), ht("/Action/ObjectHistory"));
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Sets the current history.
|
---|
121 | * @param history current history
|
---|
122 | */
|
---|
123 | protected void setHistory(History history) {
|
---|
124 | browser.populate(history);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Removes this history browser model as listener for data change and layer change events.
|
---|
129 | */
|
---|
130 | public void unlinkAsListener() {
|
---|
131 | getHistoryBrowser().getModel().unlinkAsListener();
|
---|
132 | }
|
---|
133 |
|
---|
134 | /* ---------------------------------------------------------------------------------- */
|
---|
135 | /* interface HistoryDataSetListener */
|
---|
136 | /* ---------------------------------------------------------------------------------- */
|
---|
137 |
|
---|
138 | @Override
|
---|
139 | public void historyUpdated(HistoryDataSet source, PrimitiveId primitiveId) {
|
---|
140 | if (primitiveId == null || primitiveId.equals(browser.getHistory().getPrimitiveId())) {
|
---|
141 | History history = source.getHistory(browser.getHistory().getPrimitiveId());
|
---|
142 | if (history != null) {
|
---|
143 | browser.populate(history);
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | @Override
|
---|
149 | public void historyDataSetCleared(HistoryDataSet source) {
|
---|
150 | if (isVisible()) {
|
---|
151 | closeAction.run();
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | class CloseAction extends AbstractAction {
|
---|
156 | CloseAction() {
|
---|
157 | putValue(NAME, tr("Close"));
|
---|
158 | putValue(SHORT_DESCRIPTION, tr("Close the dialog"));
|
---|
159 | new ImageProvider("ok").getResource().attachImageIcon(this);
|
---|
160 | }
|
---|
161 |
|
---|
162 | void run() {
|
---|
163 | getHistoryBrowser().getModel().unlinkAsListener();
|
---|
164 | HistoryDataSet.getInstance().removeHistoryDataSetListener(HistoryBrowserDialog.this);
|
---|
165 | HistoryBrowserDialogManager.getInstance().hide(HistoryBrowserDialog.this);
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Override
|
---|
169 | public void actionPerformed(ActionEvent e) {
|
---|
170 | run();
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | class ReloadAction extends AbstractAction {
|
---|
175 | ReloadAction() {
|
---|
176 | putValue(NAME, tr("Reload"));
|
---|
177 | putValue(SHORT_DESCRIPTION, tr("Reload the history from the server"));
|
---|
178 | new ImageProvider("dialogs", "refresh").getResource().attachImageIcon(this);
|
---|
179 | }
|
---|
180 |
|
---|
181 | @Override
|
---|
182 | public void actionPerformed(ActionEvent e) {
|
---|
183 | HistoryLoadTask task = new HistoryLoadTask();
|
---|
184 | task.add(browser.getHistory());
|
---|
185 | MainApplication.worker.submit(task);
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | class WindowClosingAdapter extends WindowAdapter {
|
---|
190 | @Override
|
---|
191 | public void windowClosing(WindowEvent e) {
|
---|
192 | if (isVisible()) {
|
---|
193 | closeAction.run();
|
---|
194 | }
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Replies the history browser.
|
---|
200 | * @return the history browser
|
---|
201 | */
|
---|
202 | public HistoryBrowser getHistoryBrowser() {
|
---|
203 | return browser;
|
---|
204 | }
|
---|
205 | }
|
---|