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

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

fix #14175 - HistoryBrowserModel removes listener twice

  • 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 btn.setName("btn.close");
107 pnl.add(btn);
108 InputMapUtils.addEscapeAction(getRootPane(), closeAction);
109
110 btn = new JButton(new ContextSensitiveHelpAction(ht("/Action/ObjectHistory")));
111 btn.setName("btn.help");
112 pnl.add(btn);
113 add(pnl, BorderLayout.SOUTH);
114
115 HelpUtil.setHelpContext(getRootPane(), ht("/Action/ObjectHistory"));
116 }
117
118 /**
119 * Sets the current history.
120 * @param history current history
121 */
122 protected void setHistory(History history) {
123 browser.populate(history);
124 }
125
126 /**
127 * Removes this history browser model as listener for data change and layer change events.
128 */
129 public void unlinkAsListener() {
130 getHistoryBrowser().getModel().unlinkAsListener();
131 }
132
133 /* ---------------------------------------------------------------------------------- */
134 /* interface HistoryDataSetListener */
135 /* ---------------------------------------------------------------------------------- */
136
137 @Override
138 public void historyUpdated(HistoryDataSet source, PrimitiveId primitiveId) {
139 if (primitiveId == null || primitiveId.equals(browser.getHistory().getPrimitiveId())) {
140 History history = source.getHistory(browser.getHistory().getPrimitiveId());
141 if (history != null) {
142 browser.populate(history);
143 }
144 }
145 }
146
147 @Override
148 public void historyDataSetCleared(HistoryDataSet source) {
149 if (isVisible()) {
150 closeAction.run();
151 }
152 }
153
154 class CloseAction extends AbstractAction {
155 CloseAction() {
156 putValue(NAME, tr("Close"));
157 putValue(SHORT_DESCRIPTION, tr("Close the dialog"));
158 new ImageProvider("ok").getResource().attachImageIcon(this);
159 }
160
161 void run() {
162 getHistoryBrowser().getModel().unlinkAsListener();
163 HistoryDataSet.getInstance().removeHistoryDataSetListener(HistoryBrowserDialog.this);
164 HistoryBrowserDialogManager.getInstance().hide(HistoryBrowserDialog.this);
165 }
166
167 @Override
168 public void actionPerformed(ActionEvent e) {
169 run();
170 }
171 }
172
173 class ReloadAction extends AbstractAction {
174 ReloadAction() {
175 putValue(NAME, tr("Reload"));
176 putValue(SHORT_DESCRIPTION, tr("Reload the history from the server"));
177 new ImageProvider("dialogs", "refresh").getResource().attachImageIcon(this);
178 }
179
180 @Override
181 public void actionPerformed(ActionEvent e) {
182 HistoryLoadTask task = new HistoryLoadTask();
183 task.add(browser.getHistory());
184 Main.worker.submit(task);
185 }
186 }
187
188 class WindowClosingAdapter extends WindowAdapter {
189 @Override
190 public void windowClosing(WindowEvent e) {
191 if (isVisible()) {
192 closeAction.run();
193 }
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.