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

Last change on this file since 11553 was 10791, checked in by simon04, 8 years ago

see #13319 - Use InputMapUtils where applicable (VK_ESCAPE)

  • 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.help.ContextSensitiveHelpAction;
26import org.openstreetmap.josm.gui.help.HelpUtil;
27import org.openstreetmap.josm.gui.util.GuiHelper;
28import org.openstreetmap.josm.tools.ImageProvider;
29import org.openstreetmap.josm.tools.InputMapUtils;
30
31/**
32 * This is non-modal dialog, always showing on top, which displays history information
33 * about a given {@link org.openstreetmap.josm.data.osm.OsmPrimitive}.
34 * @since 1709
35 */
36public class HistoryBrowserDialog extends JDialog implements HistoryDataSetListener {
37
38 /** the embedded browser */
39 private final HistoryBrowser browser = new HistoryBrowser();
40 private final CloseAction closeAction = new CloseAction();
41 private final JLabel titleLabel = new JLabel("", JLabel.CENTER);
42
43 /**
44 * Constructs a new {@code HistoryBrowserDialog}.
45 *
46 * @param history the history to be displayed
47 */
48 public HistoryBrowserDialog(History history) {
49 super(GuiHelper.getFrameForComponent(Main.parent), false);
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 *
64 * @param h the current history
65 * @return the title for this dialog
66 */
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 = "";
77 }
78 return tr(title, Long.toString(h.getId()));
79 }
80
81 @Override
82 public void setTitle(String title) {
83 super.setTitle(title);
84 if (titleLabel != null) {
85 titleLabel.setText(title);
86 }
87 }
88
89 /**
90 * builds the GUI
91 */
92 protected void build() {
93 setLayout(new BorderLayout());
94
95 add(titleLabel, BorderLayout.NORTH);
96
97 add(browser, BorderLayout.CENTER);
98
99 JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
100
101 JButton btn = new JButton(new ReloadAction());
102 btn.setName("btn.reload");
103 pnl.add(btn);
104
105 btn = new JButton(closeAction);
106 final String closeHistoryBrowserDialogKey = "CloseHistoryBrowserDialog";
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 closeAction.run();
151 }
152
153 class CloseAction extends AbstractAction {
154 CloseAction() {
155 putValue(NAME, tr("Close"));
156 putValue(SHORT_DESCRIPTION, tr("Close the dialog"));
157 new ImageProvider("ok").getResource().attachImageIcon(this);
158 }
159
160 public void run() {
161 getHistoryBrowser().getModel().unlinkAsListener();
162 HistoryDataSet.getInstance().removeHistoryDataSetListener(HistoryBrowserDialog.this);
163 HistoryBrowserDialogManager.getInstance().hide(HistoryBrowserDialog.this);
164 }
165
166 @Override
167 public void actionPerformed(ActionEvent e) {
168 run();
169 }
170 }
171
172 class ReloadAction extends AbstractAction {
173 ReloadAction() {
174 putValue(NAME, tr("Reload"));
175 putValue(SHORT_DESCRIPTION, tr("Reload the history from the server"));
176 new ImageProvider("dialogs", "refresh").getResource().attachImageIcon(this);
177 }
178
179 @Override
180 public void actionPerformed(ActionEvent e) {
181 HistoryLoadTask task = new HistoryLoadTask();
182 task.add(browser.getHistory());
183 Main.worker.submit(task);
184 }
185 }
186
187 class WindowClosingAdapter extends WindowAdapter {
188 @Override
189 public void windowClosing(WindowEvent e) {
190 closeAction.run();
191 }
192 }
193
194 /**
195 * Replies the history browser.
196 * @return the history browser
197 */
198 public HistoryBrowser getHistoryBrowser() {
199 return browser;
200 }
201}
Note: See TracBrowser for help on using the repository browser.