svn-gvsig-desktop / 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 @ 41258
History | View | Annotate | Download (9.68 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2009 {} {{Task}}
|
26 |
*/
|
27 |
package org.gvsig.app.extension.dispose; |
28 |
|
29 |
import java.awt.Dimension; |
30 |
import java.awt.event.ActionEvent; |
31 |
import java.awt.event.ActionListener; |
32 |
import java.util.Iterator; |
33 |
import java.util.Set; |
34 |
|
35 |
import javax.swing.DefaultListModel; |
36 |
import javax.swing.JList; |
37 |
import javax.swing.JScrollPane; |
38 |
import javax.swing.JTextPane; |
39 |
import javax.swing.SwingUtilities; |
40 |
import javax.swing.event.ListSelectionEvent; |
41 |
import javax.swing.event.ListSelectionListener; |
42 |
|
43 |
import org.gvsig.andami.plugins.Extension; |
44 |
import org.gvsig.tools.ToolsLocator; |
45 |
import org.gvsig.tools.dispose.Disposable; |
46 |
import org.gvsig.tools.dispose.DisposableInfo; |
47 |
import org.gvsig.tools.dispose.DisposableManager; |
48 |
import org.gvsig.tools.exception.BaseException; |
49 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
50 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
51 |
import org.slf4j.Logger; |
52 |
import org.slf4j.LoggerFactory; |
53 |
|
54 |
/**
|
55 |
* An extension to view and manage {@link Disposable} objects which are still
|
56 |
* bound.
|
57 |
*
|
58 |
* TODO: remove this extension and replace with a better one, maybe based on
|
59 |
* scripting.
|
60 |
*
|
61 |
* @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
62 |
*/
|
63 |
public class DisposableManagementExtension extends Extension { |
64 |
|
65 |
public static String SHOW_COMMAND = "tools-devel-disposables-show-pendings"; |
66 |
public static String DISPOSE_ALL_COMMAND = "tools-devel-disposables-free-all"; |
67 |
|
68 |
private static final Logger LOG |
69 |
= LoggerFactory.getLogger(DisposableManagementExtension.class); |
70 |
|
71 |
public void initialize() { |
72 |
// Nothing to do
|
73 |
} |
74 |
|
75 |
@Override
|
76 |
public void postInitialize() { |
77 |
super.postInitialize();
|
78 |
} |
79 |
|
80 |
public void execute(String actionCommand) { |
81 |
|
82 |
if (DISPOSE_ALL_COMMAND.equals(actionCommand)) {
|
83 |
disposeAll(); |
84 |
} else {
|
85 |
if (SHOW_COMMAND.equals(actionCommand)) {
|
86 |
DisposablesDoList panel = new DisposablesDoList();
|
87 |
panel.showPanel(); |
88 |
} |
89 |
} |
90 |
} |
91 |
|
92 |
private void disposeAll() { |
93 |
DisposableManager manager; |
94 |
manager = ToolsLocator.getDisposableManager(); |
95 |
try {
|
96 |
manager.releaseAll(); |
97 |
} catch (BaseException ex) {
|
98 |
LOG.error("Error disposing all bound disposable objects", ex);
|
99 |
} |
100 |
} |
101 |
|
102 |
public boolean isEnabled() { |
103 |
return true; |
104 |
} |
105 |
|
106 |
public boolean isVisible() { |
107 |
return true; |
108 |
} |
109 |
|
110 |
class DisposablesDoList extends DisposablesDoListLayout { |
111 |
|
112 |
private DisposableManager manager;
|
113 |
private JList disposablesList = null; |
114 |
private JTextPane infoTextArea = null; |
115 |
|
116 |
public DisposablesDoList() {
|
117 |
manager = ToolsLocator.getDisposableManager(); |
118 |
initComponents(); |
119 |
SwingUtilities.invokeLater(new Runnable() { |
120 |
public void run() { |
121 |
fillList(); |
122 |
} |
123 |
}); |
124 |
} |
125 |
|
126 |
private void initComponents() { |
127 |
this.setPreferredSize(new Dimension(600, 550)); |
128 |
this.disposablesList = new JList(); |
129 |
this.infoTextArea = new JTextPane(); |
130 |
this.infoTextArea.setContentType("text/html"); |
131 |
JScrollPane listScroller = new JScrollPane(this.disposablesList); |
132 |
JScrollPane infoScroller = new JScrollPane(this.infoTextArea); |
133 |
this.splitPanel.setLeftComponent(listScroller);
|
134 |
this.splitPanel.setRightComponent(infoScroller);
|
135 |
|
136 |
this.closeButton.addActionListener(new ActionListener() { |
137 |
public void actionPerformed(ActionEvent arg0) { |
138 |
closeWindow(); |
139 |
} |
140 |
}); |
141 |
this.closeButton.setEnabled(true); |
142 |
this.disposeAllButton.addActionListener(new ActionListener() { |
143 |
public void actionPerformed(ActionEvent e) { |
144 |
disposeAll(); |
145 |
refreshList(); |
146 |
} |
147 |
}); |
148 |
this.disposeAllButton.setEnabled(true); |
149 |
this.disposeButton.addActionListener(new ActionListener() { |
150 |
public void actionPerformed(ActionEvent e) { |
151 |
disposeSelecteds(); |
152 |
refreshList(); |
153 |
} |
154 |
}); |
155 |
this.disposeButton.setEnabled(true); |
156 |
this.disposablesList.addListSelectionListener(new ListSelectionListener() { |
157 |
public void valueChanged(ListSelectionEvent arg0) { |
158 |
ListItemSelected(); |
159 |
} |
160 |
}); |
161 |
this.disposablesList.setEnabled(true); |
162 |
this.refreshButton.addActionListener(new ActionListener() { |
163 |
public void actionPerformed(ActionEvent arg0) { |
164 |
refreshList(); |
165 |
} |
166 |
}); |
167 |
this.refreshButton.setEnabled(true); |
168 |
} |
169 |
|
170 |
private void refreshList() { |
171 |
fillList(); |
172 |
} |
173 |
|
174 |
private void ListItemSelected() { |
175 |
DisposableInfoItem item = (DisposableInfoItem) disposablesList.getSelectedValue(); |
176 |
if (item == null) { |
177 |
this.infoTextArea.setText(""); |
178 |
} else {
|
179 |
this.infoTextArea.setText(item.getAllInformation());
|
180 |
this.infoTextArea.setCaretPosition(0); |
181 |
} |
182 |
} |
183 |
|
184 |
private void fillList() { |
185 |
Set<DisposableInfo> disposables
|
186 |
= (Set<DisposableInfo>) manager.getBoundDisposables();
|
187 |
|
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);
|
195 |
this.infoTextArea.setText(""); |
196 |
} |
197 |
|
198 |
class DisposableInfoItem { |
199 |
|
200 |
DisposableInfo disposableInfo = null;
|
201 |
|
202 |
DisposableInfoItem(DisposableInfo disposableInfo) { |
203 |
this.disposableInfo = disposableInfo;
|
204 |
} |
205 |
|
206 |
public Disposable getDisposable() {
|
207 |
return this.disposableInfo.getDisposable(); |
208 |
} |
209 |
|
210 |
public DisposableInfo getDisposableInfo() {
|
211 |
return this.disposableInfo; |
212 |
} |
213 |
|
214 |
public String toString() { |
215 |
if (this.disposableInfo == null) { |
216 |
return "(null)"; |
217 |
} |
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;
|
224 |
} |
225 |
|
226 |
public String getAllInformation() { |
227 |
StringBuffer buffer = new StringBuffer(); |
228 |
Disposable disposable = this.disposableInfo.getDisposable();
|
229 |
|
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"); |
233 |
|
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());
|
243 |
} |
244 |
return buffer.toString();
|
245 |
} |
246 |
} |
247 |
|
248 |
public void closeWindow() { |
249 |
this.setVisible(false); |
250 |
} |
251 |
|
252 |
public void disposeAll() { |
253 |
try {
|
254 |
manager.releaseAll(); |
255 |
} catch (BaseException ex) {
|
256 |
LOG.error("Error disposing all bound disposable objects", ex);
|
257 |
} |
258 |
} |
259 |
|
260 |
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());
|
265 |
} |
266 |
refreshList(); |
267 |
} |
268 |
|
269 |
public void showPanel() { |
270 |
WindowManager wm = ToolsSwingLocator.getWindowManager(); |
271 |
wm.showWindow(this, "Disposable do list", WindowManager.MODE.WINDOW); |
272 |
} |
273 |
} |
274 |
|
275 |
} |