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

Last change on this file since 9078 was 9059, checked in by Don-vip, 8 years ago

checkstyle

  • 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("/Action/ObjectHistory")));
100 btn.setName("btn.help");
101 pnl.add(btn);
102 add(pnl, BorderLayout.SOUTH);
103
104 HelpUtil.setHelpContext(getRootPane(), ht("/Action/ObjectHistory"));
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 current 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 class CloseAction extends AbstractAction {
156 CloseAction() {
157 putValue(NAME, tr("Close"));
158 putValue(SHORT_DESCRIPTION, tr("Close the dialog"));
159 putValue(SMALL_ICON, ImageProvider.get("ok"));
160 }
161
162 public 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 putValue(SMALL_ICON, ImageProvider.get("dialogs", "refresh"));
179 }
180
181 @Override
182 public void actionPerformed(ActionEvent e) {
183 HistoryLoadTask task = new HistoryLoadTask();
184 task.add(browser.getHistory());
185 Main.worker.submit(task);
186 }
187 }
188
189 class WindowClosingAdapter extends WindowAdapter {
190 @Override
191 public void windowClosing(WindowEvent e) {
192 closeAction.run();
193 }
194 }
195
196 /**
197 * Replies the history browser.
198 * @return the history browser
199 */
200 public HistoryBrowser getHistoryBrowser() {
201 return browser;
202 }
203}
Note: See TracBrowser for help on using the repository browser.