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