Revision 45707 trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/java/org/gvsig/app/extension/dispose/DisposableManagementExtension.java
DisposableManagementExtension.java | ||
---|---|---|
28 | 28 |
|
29 | 29 |
import java.awt.Dimension; |
30 | 30 |
import java.awt.event.ActionEvent; |
31 |
import java.awt.event.ActionListener; |
|
32 |
import java.util.Iterator; |
|
31 |
import java.lang.management.ManagementFactory; |
|
32 |
import java.lang.management.MemoryMXBean; |
|
33 |
import java.util.ArrayList; |
|
34 |
import java.util.Collections; |
|
35 |
import java.util.List; |
|
33 | 36 |
import java.util.Set; |
34 | 37 |
|
35 |
import javax.swing.DefaultListModel; |
|
36 |
import javax.swing.JList; |
|
37 | 38 |
import javax.swing.JScrollPane; |
39 |
import javax.swing.JTable; |
|
38 | 40 |
import javax.swing.JTextPane; |
41 |
import javax.swing.ListSelectionModel; |
|
39 | 42 |
import javax.swing.SwingUtilities; |
40 | 43 |
import javax.swing.event.ListSelectionEvent; |
41 |
import javax.swing.event.ListSelectionListener; |
|
44 |
import javax.swing.table.AbstractTableModel; |
|
45 |
import javax.swing.table.DefaultTableColumnModel; |
|
46 |
import javax.swing.table.TableColumn; |
|
47 |
import javax.swing.table.TableColumnModel; |
|
42 | 48 |
|
43 | 49 |
import org.gvsig.andami.plugins.Extension; |
44 | 50 |
import org.gvsig.tools.ToolsLocator; |
... | ... | |
47 | 53 |
import org.gvsig.tools.dispose.DisposableManager; |
48 | 54 |
import org.gvsig.tools.exception.BaseException; |
49 | 55 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
56 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
|
50 | 57 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
51 | 58 |
import org.slf4j.Logger; |
52 | 59 |
import org.slf4j.LoggerFactory; |
... | ... | |
68 | 75 |
private static final Logger LOG |
69 | 76 |
= LoggerFactory.getLogger(DisposableManagementExtension.class); |
70 | 77 |
|
78 |
@Override |
|
71 | 79 |
public void initialize() { |
72 | 80 |
// Nothing to do |
73 | 81 |
} |
... | ... | |
77 | 85 |
super.postInitialize(); |
78 | 86 |
} |
79 | 87 |
|
88 |
@Override |
|
80 | 89 |
public void execute(String actionCommand) { |
81 | 90 |
|
82 | 91 |
if (DISPOSE_ALL_COMMAND.equals(actionCommand)) { |
... | ... | |
99 | 108 |
} |
100 | 109 |
} |
101 | 110 |
|
111 |
@Override |
|
102 | 112 |
public boolean isEnabled() { |
103 | 113 |
return true; |
104 | 114 |
} |
105 | 115 |
|
116 |
@Override |
|
106 | 117 |
public boolean isVisible() { |
107 | 118 |
return true; |
108 | 119 |
} |
... | ... | |
110 | 121 |
class DisposablesDoList extends DisposablesDoListLayout { |
111 | 122 |
|
112 | 123 |
private DisposableManager manager; |
113 |
private JList disposablesList = null;
|
|
124 |
private JTable disposablesTable = null;
|
|
114 | 125 |
private JTextPane infoTextArea = null; |
115 | 126 |
|
116 | 127 |
public DisposablesDoList() { |
117 | 128 |
manager = ToolsLocator.getDisposableManager(); |
118 | 129 |
initComponents(); |
119 |
SwingUtilities.invokeLater(new Runnable() { |
|
120 |
public void run() { |
|
121 |
fillList(); |
|
122 |
} |
|
130 |
SwingUtilities.invokeLater(() -> { |
|
131 |
refreshList(); |
|
132 |
updateMemoryUsage(); |
|
123 | 133 |
}); |
124 | 134 |
} |
125 | 135 |
|
126 | 136 |
private void initComponents() { |
127 | 137 |
this.setPreferredSize(new Dimension(600, 550)); |
128 |
this.disposablesList = new JList(); |
|
138 |
|
|
139 |
this.disposablesTable = new JTable(new DisposablesTableModel()); |
|
140 |
this.disposablesTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); |
|
141 |
|
|
142 |
// DefaultTableColumnModel columnModel = new DefaultTableColumnModel(); |
|
143 |
// columnModel.addColumn(new TableColumn(0, ToolsSwingUtils.cols2px(5))); |
|
144 |
// columnModel.addColumn(new TableColumn(1, ToolsSwingUtils.cols2px(12))); |
|
145 |
// columnModel.addColumn(new TableColumn(2, ToolsSwingUtils.cols2px(12))); |
|
146 |
// columnModel.addColumn(new TableColumn(3, ToolsSwingUtils.cols2px(25))); |
|
147 |
// this.disposablesTable.setColumnModel(columnModel); |
|
148 |
|
|
129 | 149 |
this.infoTextArea = new JTextPane(); |
130 | 150 |
this.infoTextArea.setContentType("text/html"); |
131 |
JScrollPane listScroller = new JScrollPane(this.disposablesList); |
|
151 |
|
|
152 |
JScrollPane listScroller = new JScrollPane(this.disposablesTable); |
|
132 | 153 |
JScrollPane infoScroller = new JScrollPane(this.infoTextArea); |
133 | 154 |
this.splitPanel.setLeftComponent(listScroller); |
134 | 155 |
this.splitPanel.setRightComponent(infoScroller); |
135 | 156 |
|
136 |
this.closeButton.addActionListener(new ActionListener() { |
|
137 |
public void actionPerformed(ActionEvent arg0) { |
|
138 |
closeWindow(); |
|
139 |
} |
|
157 |
// Dimension dim = this.splitPanel.getPreferredSize(); |
|
158 |
// dim.height = dim.height / 2; |
|
159 |
// this.disposablesTable.setPreferredSize(dim); |
|
160 |
// this.splitPanel.invalidate(); |
|
161 |
|
|
162 |
this.closeButton.addActionListener((ActionEvent arg0) -> { |
|
163 |
closeWindow(); |
|
140 | 164 |
}); |
141 | 165 |
this.closeButton.setEnabled(true); |
142 |
this.disposeAllButton.addActionListener(new ActionListener() { |
|
143 |
public void actionPerformed(ActionEvent e) { |
|
144 |
disposeAll(); |
|
145 |
refreshList(); |
|
146 |
} |
|
166 |
this.disposeAllButton.addActionListener((ActionEvent e) -> { |
|
167 |
disposeAll(); |
|
168 |
refreshList(); |
|
169 |
updateMemoryUsage(); |
|
147 | 170 |
}); |
148 | 171 |
this.disposeAllButton.setEnabled(true); |
149 |
this.disposeButton.addActionListener(new ActionListener() { |
|
150 |
public void actionPerformed(ActionEvent e) { |
|
151 |
disposeSelecteds(); |
|
152 |
refreshList(); |
|
153 |
} |
|
172 |
this.disposeButton.addActionListener((ActionEvent e) -> { |
|
173 |
disposeSelecteds(); |
|
174 |
refreshList(); |
|
175 |
updateMemoryUsage(); |
|
154 | 176 |
}); |
155 | 177 |
this.disposeButton.setEnabled(true); |
156 |
this.disposablesList.addListSelectionListener(new ListSelectionListener() { |
|
157 |
public void valueChanged(ListSelectionEvent arg0) { |
|
158 |
ListItemSelected(); |
|
159 |
} |
|
178 |
this.disposablesTable.getSelectionModel().addListSelectionListener((ListSelectionEvent arg0) -> { |
|
179 |
ListItemSelected(); |
|
180 |
updateMemoryUsage(); |
|
160 | 181 |
}); |
161 |
this.disposablesList.setEnabled(true); |
|
162 |
this.refreshButton.addActionListener(new ActionListener() { |
|
163 |
public void actionPerformed(ActionEvent arg0) { |
|
164 |
refreshList(); |
|
165 |
} |
|
182 |
this.disposablesTable.setEnabled(true); |
|
183 |
this.refreshButton.addActionListener((ActionEvent arg0) -> { |
|
184 |
refreshList(); |
|
185 |
updateMemoryUsage(); |
|
166 | 186 |
}); |
167 | 187 |
this.refreshButton.setEnabled(true); |
168 | 188 |
} |
169 | 189 |
|
170 | 190 |
private void refreshList() { |
171 |
fillList();
|
|
191 |
fillTable();
|
|
172 | 192 |
} |
173 | 193 |
|
174 | 194 |
private void ListItemSelected() { |
175 |
DisposableInfoItem item = (DisposableInfoItem) disposablesList.getSelectedValue(); |
|
176 |
if (item == null) { |
|
195 |
DisposablesTableModel model = (DisposablesTableModel) this.disposablesTable.getModel(); |
|
196 |
DisposableInfo info = model.get(this.disposablesTable.getSelectedRow()); |
|
197 |
if( info==null ) { |
|
177 | 198 |
this.infoTextArea.setText(""); |
178 | 199 |
} else { |
179 |
this.infoTextArea.setText(item.getAllInformation());
|
|
200 |
this.infoTextArea.setText(toHTML(info));
|
|
180 | 201 |
this.infoTextArea.setCaretPosition(0); |
181 | 202 |
} |
182 | 203 |
} |
183 | 204 |
|
184 |
private void fillList() {
|
|
185 |
Set<DisposableInfo> disposables |
|
186 |
= (Set<DisposableInfo>) manager.getBoundDisposables();
|
|
205 |
private void fillTable() {
|
|
206 |
Set<DisposableInfo> disposables = (Set<DisposableInfo>) manager.getBoundDisposables();
|
|
207 |
this.messageLabel.setText("Disposables " + disposables.size());
|
|
187 | 208 |
|
188 |
this.messageLabel.setText("Pending " + disposables.size()); |
|
189 |
DefaultListModel listmodel = new DefaultListModel(); |
|
190 |
for (Iterator<DisposableInfo> iterator = disposables.iterator(); iterator.hasNext();) { |
|
191 |
DisposableInfo disposableInfo = iterator.next(); |
|
192 |
listmodel.addElement(new DisposableInfoItem(disposableInfo)); |
|
193 |
} |
|
194 |
this.disposablesList.setModel(listmodel); |
|
209 |
this.disposablesTable.setModel(new DisposablesTableModel(new ArrayList<>(disposables))); this.messageLabel.setText("Pending " + disposables.size()); |
|
195 | 210 |
this.infoTextArea.setText(""); |
196 | 211 |
} |
212 |
|
|
213 |
private void updateMemoryUsage() { |
|
214 |
Runtime rt = Runtime.getRuntime(); |
|
215 |
this.lblTotalMemory.setText(String.valueOf(rt.totalMemory()/1024)); |
|
216 |
this.lblMaxMemory.setText(String.valueOf(rt.maxMemory()/1024)); |
|
217 |
this.lblFreeMemory.setText(String.valueOf(rt.freeMemory()/1024)); |
|
218 |
this.lblActiveThreads.setText(String.valueOf(Thread.activeCount())); |
|
219 |
|
|
220 |
int objectPendingFinalizationCount = -1; |
|
221 |
try { |
|
222 |
MemoryMXBean memBean = ManagementFactory.getMemoryMXBean() ; |
|
223 |
objectPendingFinalizationCount = memBean.getObjectPendingFinalizationCount(); |
|
224 |
} catch(ThreadDeath th) { |
|
225 |
|
|
226 |
} |
|
227 |
this.lblObjectPendingFinalization.setText(String.valueOf(objectPendingFinalizationCount)); |
|
228 |
} |
|
197 | 229 |
|
198 |
class DisposableInfoItem {
|
|
230 |
private class DisposablesTableModel extends AbstractTableModel {
|
|
199 | 231 |
|
200 |
DisposableInfo disposableInfo = null; |
|
232 |
private List<DisposableInfo> disposables; |
|
233 |
private final String[] columnNames; |
|
234 |
private final Class[] columnClass; |
|
235 |
|
|
236 |
public DisposablesTableModel() { |
|
237 |
this(Collections.EMPTY_LIST); |
|
238 |
} |
|
239 |
|
|
240 |
public DisposablesTableModel(List<DisposableInfo> disposables) { |
|
241 |
this.disposables = disposables; |
|
242 |
this.columnNames = new String[] {"Refs.", "Class", "Full class", "To str."}; |
|
243 |
this.columnClass = new Class[] {Integer.class, String.class, String.class, String.class}; |
|
244 |
} |
|
245 |
|
|
246 |
public DisposableInfo get(int rowIndex) { |
|
247 |
try { |
|
248 |
return this.disposables.get(rowIndex); |
|
249 |
} catch(Throwable th) { |
|
250 |
return null; |
|
251 |
} |
|
252 |
} |
|
253 |
|
|
254 |
@Override |
|
255 |
public int getRowCount() { |
|
256 |
return this.disposables.size(); |
|
257 |
} |
|
201 | 258 |
|
202 |
DisposableInfoItem(DisposableInfo disposableInfo) { |
|
203 |
this.disposableInfo = disposableInfo; |
|
259 |
@Override |
|
260 |
public int getColumnCount() { |
|
261 |
return this.columnNames.length; |
|
204 | 262 |
} |
205 | 263 |
|
206 |
public Disposable getDisposable() { |
|
207 |
return this.disposableInfo.getDisposable(); |
|
264 |
@Override |
|
265 |
public String getColumnName(int columnIndex) { |
|
266 |
return this.columnNames[columnIndex]; |
|
208 | 267 |
} |
209 | 268 |
|
210 |
public DisposableInfo getDisposableInfo() { |
|
211 |
return this.disposableInfo; |
|
269 |
@Override |
|
270 |
public Class<?> getColumnClass(int columnIndex) { |
|
271 |
return this.columnClass[columnIndex]; |
|
212 | 272 |
} |
213 | 273 |
|
214 |
public String toString() { |
|
215 |
if (this.disposableInfo == null) { |
|
216 |
return "(null)"; |
|
274 |
@Override |
|
275 |
public Object getValueAt(int rowIndex, int columnIndex) { |
|
276 |
DisposableInfo info = this.disposables.get(rowIndex); |
|
277 |
switch(columnIndex) { |
|
278 |
case 0: |
|
279 |
return info.getReferencesCount(); |
|
280 |
case 1: |
|
281 |
try { |
|
282 |
return info.getDisposable().getClass().getSimpleName(); |
|
283 |
} catch(Throwable th) { |
|
284 |
return "(unknown)"; |
|
285 |
} |
|
286 |
case 2: |
|
287 |
try { |
|
288 |
return info.getDisposable().getClass().getName(); |
|
289 |
} catch(Throwable th) { |
|
290 |
return "(unknown)"; |
|
291 |
} |
|
292 |
case 3: |
|
293 |
try { |
|
294 |
return info.getDisposable().toString(); |
|
295 |
} catch(Throwable th) { |
|
296 |
return "(unknown)"; |
|
297 |
} |
|
217 | 298 |
} |
218 |
Disposable d = this.disposableInfo.getDisposable(); |
|
219 |
if (d == null) { |
|
220 |
return "(null - disposable)"; |
|
221 |
} |
|
222 |
String msg = d.getClass().getName() + " - " + d.toString(); |
|
223 |
return msg; |
|
299 |
return null; |
|
224 | 300 |
} |
301 |
} |
|
225 | 302 |
|
226 |
public String getAllInformation() {
|
|
227 |
StringBuffer buffer = new StringBuffer();
|
|
228 |
Disposable disposable = this.disposableInfo.getDisposable();
|
|
303 |
public String toHTML(DisposableInfo disposableInfo) {
|
|
304 |
StringBuilder buffer = new StringBuilder();
|
|
305 |
Disposable disposable = disposableInfo.getDisposable();
|
|
229 | 306 |
|
230 |
buffer.append("<b>Class</b>: ").append(disposable.getClass().getName()).append("<br>\n"); |
|
231 |
buffer.append("<b>References</b>: ").append(this.disposableInfo.getReferencesCount()).append("<br>\n"); |
|
232 |
buffer.append("<b>toString</b>: ").append(disposable.toString()).append("<br>\n"); |
|
307 |
buffer.append("<b>Class</b>: ").append(disposable.getClass().getSimpleName()).append("<br>\n"); |
|
308 |
buffer.append("<b>Full class</b>: ").append(disposable.getClass().getName()).append("<br>\n"); |
|
309 |
buffer.append("<b>References</b>: ").append(disposableInfo.getReferencesCount()).append("<br>\n"); |
|
310 |
buffer.append("<b>toString</b>: ").append(disposable.toString()).append("<br>\n"); |
|
233 | 311 |
|
234 |
buffer.append("<b>Binded from (stack)</b>:<br>\n"); |
|
235 |
try { |
|
236 |
StackTraceElement[] stackTrace = disposableInfo |
|
237 |
.getBindDisposableStackTrace(); |
|
238 |
for (int i = 0; i < stackTrace.length; i++) { |
|
239 |
buffer.append(" ").append(stackTrace[i].toString().replaceAll("[<]", "<").replace("[>]", ">")).append("<br>\n"); |
|
240 |
} |
|
241 |
} catch (Exception ex) { |
|
242 |
buffer.append("<br>\n<br>\nError showing stack.<br>\n").append(ex.getMessage()); |
|
312 |
buffer.append("<b>Binded from (stack)</b>:<br>\n"); |
|
313 |
try { |
|
314 |
StackTraceElement[] stackTrace = disposableInfo |
|
315 |
.getBindDisposableStackTrace(); |
|
316 |
for (StackTraceElement stackTrace1 : stackTrace) { |
|
317 |
buffer.append(" ").append(stackTrace1.toString().replaceAll("[<]", "<").replace("[>]", ">")).append("<br>\n"); |
|
243 | 318 |
} |
244 |
return buffer.toString(); |
|
319 |
} catch (Exception ex) { |
|
320 |
buffer.append("<br>\n<br>\nError showing stack.<br>\n").append(ex.getMessage()); |
|
245 | 321 |
} |
322 |
return buffer.toString(); |
|
246 | 323 |
} |
247 | 324 |
|
248 | 325 |
public void closeWindow() { |
... | ... | |
258 | 335 |
} |
259 | 336 |
|
260 | 337 |
public void disposeSelecteds() { |
261 |
Object[] selection = this.disposablesList.getSelectedValues();
|
|
262 |
for (int i = 0; i < selection.length; i++) {
|
|
263 |
DisposableInfoItem item = (DisposableInfoItem) selection[i];
|
|
264 |
this.manager.release(item.getDisposable());
|
|
338 |
DisposablesTableModel model = (DisposablesTableModel) this.disposablesTable.getModel();
|
|
339 |
for (int row : this.disposablesTable.getSelectedRows()) {
|
|
340 |
DisposableInfo info = model.get(row);
|
|
341 |
this.manager.release(info.getDisposable());
|
|
265 | 342 |
} |
266 | 343 |
refreshList(); |
267 | 344 |
} |
... | ... | |
270 | 347 |
WindowManager wm = ToolsSwingLocator.getWindowManager(); |
271 | 348 |
wm.showWindow(this, "Disposable do list", WindowManager.MODE.WINDOW); |
272 | 349 |
} |
350 |
|
|
351 |
// private class ActionListenerImpl implements ActionListener { |
|
352 |
// |
|
353 |
// public ActionListenerImpl() { |
|
354 |
// } |
|
355 |
// |
|
356 |
// @Override |
|
357 |
// public void actionPerformed(ActionEvent e) { |
|
358 |
// disposeSelecteds(); |
|
359 |
// refreshList(); |
|
360 |
// updateMemoryUsage(); |
|
361 |
// } |
|
362 |
// } |
|
273 | 363 |
} |
274 | 364 |
|
275 | 365 |
} |
Also available in: Unified diff