source: osm/applications/editors/josm/plugins/NanoLog/src/nanolog/NanoLogPanel.java@ 35978

Last change on this file since 35978 was 33788, checked in by donvip, 7 years ago

update to JOSM 12643

File size: 3.2 KB
Line 
1package nanolog;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.Rectangle;
6import java.text.SimpleDateFormat;
7import java.util.ArrayList;
8import java.util.List;
9
10import javax.swing.AbstractListModel;
11import javax.swing.JList;
12
13import org.openstreetmap.josm.gui.MainApplication;
14import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
15import org.openstreetmap.josm.gui.layer.Layer;
16import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
17import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
18import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
19import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
20
21import nanolog.NanoLogLayer.NanoLogLayerListener;
22
23/**
24 * NanoLog Panel. Displays the selected log item, along with surrounding 30-50 lines.
25 *
26 * @author zverik
27 */
28public class NanoLogPanel extends ToggleDialog implements LayerChangeListener, NanoLogLayerListener {
29 private JList<String> logPanel;
30 private LogListModel listModel;
31
32 public NanoLogPanel() {
33 super(tr("NanoLog"), "nanolog", tr("Open NanoLog panel"), null, 150, false);
34
35 listModel = new LogListModel();
36 logPanel = new JList<>(listModel);
37 createLayout(logPanel, true, null);
38 }
39
40 public void updateMarkers() {
41 List<NanoLogEntry> entries = new ArrayList<>();
42 for (NanoLogLayer l : MainApplication.getLayerManager().getLayersOfType(NanoLogLayer.class)) {
43 entries.addAll(l.getEntries());
44 }
45 listModel.setEntries(entries);
46 }
47
48 @Override
49 public void layerOrderChanged(LayerOrderChangeEvent e) {
50 }
51
52 @Override
53 public void layerAdded(LayerAddEvent e) {
54 Layer newLayer = e.getAddedLayer();
55 if (newLayer instanceof NanoLogLayer)
56 ((NanoLogLayer) newLayer).addListener(this);
57 updateMarkers();
58 }
59
60 @Override
61 public void layerRemoving(LayerRemoveEvent e) {
62 updateMarkers();
63 }
64
65 @Override
66 public void markersUpdated(NanoLogLayer layer) {
67 updateMarkers();
68 }
69
70 @Override
71 public void markerActivated(NanoLogLayer layer, NanoLogEntry entry) {
72 int idx = entry == null ? -1 : listModel.find(entry);
73 if (idx >= 0) {
74 logPanel.setSelectedIndex(idx);
75 Rectangle rect = logPanel.getCellBounds(Math.max(0, idx-2), Math.min(idx+4, listModel.getSize()));
76 logPanel.scrollRectToVisible(rect);
77 }
78 }
79
80 private static class LogListModel extends AbstractListModel<String> {
81 private List<NanoLogEntry> entries;
82 private final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");
83
84 @Override
85 public int getSize() {
86 return entries.size();
87 }
88
89 @Override
90 public String getElementAt(int index) {
91 return TIME_FORMAT.format(entries.get(index).getTime()) + " " + entries.get(index).getMessage();
92 }
93
94 public void setEntries(List<NanoLogEntry> entries) {
95 this.entries = entries;
96 fireContentsChanged(this, 0, entries.size());
97 }
98
99 public int find(NanoLogEntry entry) {
100 return entries.indexOf(entry);
101 }
102 }
103}
Note: See TracBrowser for help on using the repository browser.