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

Last change on this file since 10670 was 10481, checked in by stoecker, 8 years ago

Don't use SideButton outside toggle dialogs, see #12994, see #13054

  • Property svn:eol-style set to native
File size: 6.6 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.KeyEvent;
12import java.awt.event.WindowAdapter;
13import java.awt.event.WindowEvent;
14
15import javax.swing.AbstractAction;
16import javax.swing.JButton;
17import javax.swing.JComponent;
18import javax.swing.JDialog;
19import javax.swing.JLabel;
20import javax.swing.JPanel;
21import javax.swing.KeyStroke;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.data.osm.PrimitiveId;
25import org.openstreetmap.josm.data.osm.history.History;
26import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
27import org.openstreetmap.josm.data.osm.history.HistoryDataSetListener;
28import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
29import org.openstreetmap.josm.gui.help.HelpUtil;
30import org.openstreetmap.josm.gui.util.GuiHelper;
31import 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 */
38public 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(GuiHelper.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 JButton btn = new JButton(new ReloadAction());
104 btn.setName("btn.reload");
105 pnl.add(btn);
106
107 btn = new JButton(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 JButton(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 new ImageProvider("ok").getResource().attachImageIcon(this);
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 new ImageProvider("dialogs", "refresh").getResource().attachImageIcon(this);
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}
Note: See TracBrowser for help on using the repository browser.