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

Last change on this file since 8126 was 7528, checked in by Don-vip, 10 years ago

fix #10462 - Include object type and id in history for undecorated windows

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