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