1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.imagery;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Map;
|
---|
11 | import java.util.Map.Entry;
|
---|
12 | import java.util.Set;
|
---|
13 | import java.util.concurrent.ConcurrentHashMap;
|
---|
14 |
|
---|
15 | import javax.swing.AbstractAction;
|
---|
16 | import javax.swing.JLabel;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 | import javax.swing.JScrollPane;
|
---|
19 | import javax.swing.JTable;
|
---|
20 | import javax.swing.table.DefaultTableModel;
|
---|
21 | import javax.swing.table.TableColumn;
|
---|
22 | import javax.swing.table.TableModel;
|
---|
23 |
|
---|
24 | import org.apache.commons.jcs.access.CacheAccess;
|
---|
25 | import org.apache.commons.jcs.engine.stats.behavior.ICacheStats;
|
---|
26 | import org.apache.commons.jcs.engine.stats.behavior.IStatElement;
|
---|
27 | import org.apache.commons.jcs.engine.stats.behavior.IStats;
|
---|
28 | import org.openstreetmap.josm.Main;
|
---|
29 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
30 | import org.openstreetmap.josm.gui.layer.TMSLayer;
|
---|
31 | import org.openstreetmap.josm.gui.layer.WMSLayer;
|
---|
32 | import org.openstreetmap.josm.gui.layer.WMTSLayer;
|
---|
33 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
34 | import org.openstreetmap.josm.gui.widgets.ButtonColumn;
|
---|
35 | import org.openstreetmap.josm.tools.GBC;
|
---|
36 | import org.openstreetmap.josm.tools.Pair;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Panel for cache content management.
|
---|
40 | *
|
---|
41 | * @author Wiktor Niesiobędzki
|
---|
42 | *
|
---|
43 | */
|
---|
44 | public class CacheContentsPanel extends JPanel {
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Creates cache content panel
|
---|
48 | */
|
---|
49 | public CacheContentsPanel() {
|
---|
50 | super(new GridBagLayout());
|
---|
51 | Main.worker.submit(() -> {
|
---|
52 | addToPanel(TMSLayer.getCache(), "TMS");
|
---|
53 | addToPanel(WMSLayer.getCache(), "WMS");
|
---|
54 | addToPanel(WMTSLayer.getCache(), "WMTS");
|
---|
55 | });
|
---|
56 | }
|
---|
57 |
|
---|
58 | private void addToPanel(final CacheAccess<String, BufferedImageCacheEntry> cache, final String name) {
|
---|
59 | final Long cacheSize = getCacheSize(cache);
|
---|
60 | final TableModel tableModel = getTableModel(cache);
|
---|
61 |
|
---|
62 | GuiHelper.runInEDT(() -> {
|
---|
63 | add(new JLabel(tr("{0} cache, total cache size: {1} bytes", name, cacheSize)),
|
---|
64 | GBC.eol().insets(5, 5, 0, 0));
|
---|
65 | add(new JScrollPane(getTableForCache(cache, tableModel)),
|
---|
66 | GBC.eol().fill(GBC.BOTH));
|
---|
67 | });
|
---|
68 | }
|
---|
69 |
|
---|
70 | private static Long getCacheSize(CacheAccess<String, BufferedImageCacheEntry> cache) {
|
---|
71 | ICacheStats stats = cache.getStatistics();
|
---|
72 | for (IStats cacheStats: stats.getAuxiliaryCacheStats()) {
|
---|
73 | for (IStatElement<?> statElement: cacheStats.getStatElements()) {
|
---|
74 | if ("Data File Length".equals(statElement.getName())) {
|
---|
75 | Object val = statElement.getData();
|
---|
76 | if (val instanceof Long) {
|
---|
77 | return (Long) val;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 | return Long.valueOf(-1);
|
---|
83 | }
|
---|
84 |
|
---|
85 | public static String[][] getCacheStats(CacheAccess<String, BufferedImageCacheEntry> cache) {
|
---|
86 | Set<String> keySet = cache.getCacheControl().getKeySet();
|
---|
87 | Map<String, int[]> temp = new ConcurrentHashMap<>(); // use int[] as a Object reference to int, gives better performance
|
---|
88 | for (String key: keySet) {
|
---|
89 | String[] keyParts = key.split(":", 2);
|
---|
90 | if (keyParts.length == 2) {
|
---|
91 | int[] counter = temp.get(keyParts[0]);
|
---|
92 | if (counter == null) {
|
---|
93 | temp.put(keyParts[0], new int[]{1});
|
---|
94 | } else {
|
---|
95 | counter[0]++;
|
---|
96 | }
|
---|
97 | } else {
|
---|
98 | Main.warn("Could not parse the key: {0}. No colon found", key);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | List<Pair<String, Integer>> sortedStats = new ArrayList<>();
|
---|
103 | for (Entry<String, int[]> e: temp.entrySet()) {
|
---|
104 | sortedStats.add(new Pair<>(e.getKey(), e.getValue()[0]));
|
---|
105 | }
|
---|
106 | sortedStats.sort((o1, o2) -> -1 * o1.b.compareTo(o2.b));
|
---|
107 | String[][] ret = new String[sortedStats.size()][3];
|
---|
108 | int index = 0;
|
---|
109 | for (Pair<String, Integer> e: sortedStats) {
|
---|
110 | ret[index] = new String[]{e.a, e.b.toString(), tr("Clear")};
|
---|
111 | index++;
|
---|
112 | }
|
---|
113 | return ret;
|
---|
114 | }
|
---|
115 |
|
---|
116 | private static JTable getTableForCache(final CacheAccess<String, BufferedImageCacheEntry> cache, final TableModel tableModel) {
|
---|
117 | final JTable ret = new JTable(tableModel);
|
---|
118 |
|
---|
119 | ButtonColumn buttonColumn = new ButtonColumn(
|
---|
120 | new AbstractAction() {
|
---|
121 | @Override
|
---|
122 | public void actionPerformed(ActionEvent e) {
|
---|
123 | int row = ret.convertRowIndexToModel(ret.getEditingRow());
|
---|
124 | tableModel.setValueAt("0", row, 1);
|
---|
125 | cache.remove(ret.getValueAt(row, 0).toString() + ':');
|
---|
126 | }
|
---|
127 | });
|
---|
128 | TableColumn tableColumn = ret.getColumnModel().getColumn(2);
|
---|
129 | tableColumn.setCellRenderer(buttonColumn);
|
---|
130 | tableColumn.setCellEditor(buttonColumn);
|
---|
131 | return ret;
|
---|
132 | }
|
---|
133 |
|
---|
134 | private static DefaultTableModel getTableModel(final CacheAccess<String, BufferedImageCacheEntry> cache) {
|
---|
135 | return new DefaultTableModel(
|
---|
136 | getCacheStats(cache),
|
---|
137 | new String[]{tr("Cache name"), tr("Object Count"), tr("Clear")}) {
|
---|
138 | @Override
|
---|
139 | public boolean isCellEditable(int row, int column) {
|
---|
140 | return column == 2;
|
---|
141 | }
|
---|
142 | };
|
---|
143 | }
|
---|
144 | }
|
---|