source: josm/trunk/src/org/openstreetmap/josm/gui/history/HistoryBrowserDialog.java@ 13146

Last change on this file since 13146 was 12634, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.worker, replace it by gui.MainApplication.worker + code refactoring to make sure only editor packages use it

  • Property svn:eol-style set to native
File size: 6.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.history;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.marktr;
6import static org.openstreetmap.josm.tools.I18n.tr;
7
8import java.awt.BorderLayout;
9import java.awt.FlowLayout;
10import java.awt.event.ActionEvent;
11import java.awt.event.WindowAdapter;
12import java.awt.event.WindowEvent;
13
14import javax.swing.AbstractAction;
15import javax.swing.JButton;
16import javax.swing.JDialog;
17import javax.swing.JLabel;
18import javax.swing.JPanel;
19
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.data.osm.PrimitiveId;
22import org.openstreetmap.josm.data.osm.history.History;
23import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
24import org.openstreetmap.josm.data.osm.history.HistoryDataSetListener;
25import org.openstreetmap.josm.gui.MainApplication;
26import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
27import org.openstreetmap.josm.gui.help.HelpUtil;
28import org.openstreetmap.josm.gui.util.GuiHelper;
29import org.openstreetmap.josm.tools.ImageProvider;
30import 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 */
37public 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}
Note: See TracBrowser for help on using the repository browser.