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 @ 41043
History | View | Annotate | Download (8.15 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
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
/*
|
25 |
* AUTHORS (In addition to CIT):
|
26 |
* 2009 {} {{Task}}
|
27 |
*/
|
28 |
package org.gvsig.app.extension.dispose; |
29 |
|
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.JTextArea; |
39 |
import javax.swing.JTextPane; |
40 |
import javax.swing.SwingUtilities; |
41 |
import javax.swing.event.ListSelectionEvent; |
42 |
import javax.swing.event.ListSelectionListener; |
43 |
|
44 |
import org.gvsig.andami.plugins.Extension; |
45 |
import org.gvsig.tools.ToolsLocator; |
46 |
import org.gvsig.tools.dispose.Disposable; |
47 |
import org.gvsig.tools.dispose.DisposableInfo; |
48 |
import org.gvsig.tools.dispose.DisposableManager; |
49 |
import org.gvsig.tools.exception.BaseException; |
50 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
51 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
52 |
import org.slf4j.Logger; |
53 |
import org.slf4j.LoggerFactory; |
54 |
|
55 |
/**
|
56 |
* An extension to view and manage {@link Disposable} objects which are still
|
57 |
* bound.
|
58 |
*
|
59 |
* TODO: remove this extension and replace with a better one, maybe based on
|
60 |
* scripting.
|
61 |
*
|
62 |
* @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
63 |
*/
|
64 |
public class DisposableManagementExtension extends Extension { |
65 |
|
66 |
public static String SHOW_COMMAND = "tools-devel-disposables-show-pendings"; |
67 |
public static String DISPOSE_ALL_COMMAND = "tools-devel-disposables-free-all"; |
68 |
|
69 |
private static final Logger LOG = |
70 |
LoggerFactory.getLogger(DisposableManagementExtension.class); |
71 |
|
72 |
public void initialize() { |
73 |
// Nothing to do
|
74 |
} |
75 |
|
76 |
@Override
|
77 |
public void postInitialize() { |
78 |
super.postInitialize();
|
79 |
} |
80 |
|
81 |
public void execute(String actionCommand) { |
82 |
|
83 |
if (DISPOSE_ALL_COMMAND.equals(actionCommand)) {
|
84 |
disposeAll(); |
85 |
} else {
|
86 |
if (SHOW_COMMAND.equals(actionCommand)) {
|
87 |
DisposablesDoList panel = new DisposablesDoList();
|
88 |
panel.showPanel(); |
89 |
} |
90 |
} |
91 |
} |
92 |
|
93 |
|
94 |
private void disposeAll() { |
95 |
DisposableManager manager; |
96 |
manager = ToolsLocator.getDisposableManager(); |
97 |
try {
|
98 |
manager.releaseAll(); |
99 |
} catch (BaseException ex) {
|
100 |
LOG.error("Error disposing all bound disposable objects", ex);
|
101 |
} |
102 |
} |
103 |
|
104 |
public boolean isEnabled() { |
105 |
return true; |
106 |
} |
107 |
|
108 |
public boolean isVisible() { |
109 |
return true; |
110 |
} |
111 |
|
112 |
class DisposablesDoList extends DisposablesDoListLayout { |
113 |
private DisposableManager manager;
|
114 |
private JList disposablesList = null; |
115 |
private JTextPane infoTextArea = null; |
116 |
|
117 |
public DisposablesDoList() {
|
118 |
manager = ToolsLocator.getDisposableManager(); |
119 |
initComponents(); |
120 |
SwingUtilities.invokeLater(new Runnable() { |
121 |
public void run() { |
122 |
fillList(); |
123 |
} |
124 |
}); |
125 |
} |
126 |
|
127 |
private void initComponents() { |
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 |
} |
146 |
}); |
147 |
this.disposeAllButton.setEnabled(true); |
148 |
this.disposeButton.addActionListener(new ActionListener() { |
149 |
public void actionPerformed(ActionEvent e) { |
150 |
disposeSelecteds(); |
151 |
} |
152 |
}); |
153 |
this.disposeButton.setEnabled(true); |
154 |
this.disposablesList.addListSelectionListener( new ListSelectionListener() { |
155 |
public void valueChanged(ListSelectionEvent arg0) { |
156 |
ListItemSelected(); |
157 |
} |
158 |
}); |
159 |
this.disposablesList.setEnabled(true); |
160 |
this.refreshButton.addActionListener(new ActionListener() { |
161 |
public void actionPerformed(ActionEvent arg0) { |
162 |
refreshList(); |
163 |
} |
164 |
}); |
165 |
this.refreshButton.setEnabled(true); |
166 |
} |
167 |
|
168 |
private void refreshList() { |
169 |
fillList(); |
170 |
} |
171 |
|
172 |
private void ListItemSelected() { |
173 |
DisposableInfoItem item = (DisposableInfoItem)disposablesList.getSelectedValue(); |
174 |
if( item == null ) { |
175 |
this.infoTextArea.setText(""); |
176 |
} else {
|
177 |
this.infoTextArea.setText(item.getAllInformation());
|
178 |
this.infoTextArea.setCaretPosition(0); |
179 |
} |
180 |
} |
181 |
|
182 |
private void fillList() { |
183 |
Set<DisposableInfo> disposables =
|
184 |
(Set<DisposableInfo>) manager.getBoundDisposables();
|
185 |
|
186 |
this.messageLabel.setText("Pending "+disposables.size()); |
187 |
if (disposables.size() > 0) { |
188 |
DefaultListModel<DisposableInfoItem> listmodel = new DefaultListModel<DisposableInfoItem>(); |
189 |
for (Iterator<DisposableInfo> iterator = disposables.iterator(); iterator.hasNext();) { |
190 |
DisposableInfo disposableInfo = iterator.next(); |
191 |
listmodel.addElement( new DisposableInfoItem(disposableInfo));
|
192 |
} |
193 |
this.disposablesList.setModel(listmodel);
|
194 |
} |
195 |
this.infoTextArea.setText(""); |
196 |
} |
197 |
|
198 |
class DisposableInfoItem { |
199 |
DisposableInfo disposableInfo = null;
|
200 |
DisposableInfoItem(DisposableInfo disposableInfo) { |
201 |
this.disposableInfo = disposableInfo;
|
202 |
} |
203 |
public Disposable getDisposable() {
|
204 |
return this.disposableInfo.getDisposable(); |
205 |
} |
206 |
public DisposableInfo getDisposableInfo() {
|
207 |
return this.disposableInfo; |
208 |
} |
209 |
public String toString() { |
210 |
if( this.disposableInfo == null ) { |
211 |
return "(null)"; |
212 |
} |
213 |
Disposable d = this.disposableInfo.getDisposable();
|
214 |
if( d == null ) { |
215 |
return "(null - disposable)"; |
216 |
} |
217 |
String msg = d.getClass().getName() + " - " + d.toString(); |
218 |
return msg;
|
219 |
} |
220 |
public String getAllInformation() { |
221 |
StringBuffer buffer = new StringBuffer(); |
222 |
Disposable disposable = this.disposableInfo.getDisposable();
|
223 |
|
224 |
buffer.append("<b>Class</b>: ").append(disposable.getClass().getName()).append("<br>\n"); |
225 |
buffer.append("<b>References</b>: ").append(this.disposableInfo.getReferencesCount()).append("<br>\n"); |
226 |
buffer.append("<b>toString</b>: ").append(disposable.toString()).append("<br>\n"); |
227 |
|
228 |
buffer.append("<b>Binded from (stack)</b>:<br>\n");
|
229 |
try {
|
230 |
StackTraceElement[] stackTrace = disposableInfo |
231 |
.getBindDisposableStackTrace(); |
232 |
for (int i = 0; i < stackTrace.length; i++) { |
233 |
buffer.append(" ").append(stackTrace[i].toString().replaceAll("[<]", "<").replace("[>]", ">")).append("<br>\n"); |
234 |
} |
235 |
} catch (Exception ex) { |
236 |
buffer.append("<br>\n<br>\nError showing stack.<br>\n").append(ex.getMessage());
|
237 |
} |
238 |
return buffer.toString();
|
239 |
} |
240 |
} |
241 |
|
242 |
public void closeWindow() { |
243 |
this.setVisible(false); |
244 |
} |
245 |
|
246 |
public void disposeAll() { |
247 |
try {
|
248 |
manager.releaseAll(); |
249 |
} catch (BaseException ex) {
|
250 |
LOG.error("Error disposing all bound disposable objects", ex);
|
251 |
} |
252 |
} |
253 |
|
254 |
public void disposeSelecteds() { |
255 |
Object[] selection = this.disposablesList.getSelectedValues(); |
256 |
for( int i=0; i<selection.length; i++) { |
257 |
DisposableInfoItem item = (DisposableInfoItem) selection[i]; |
258 |
this.manager.release(item.getDisposable());
|
259 |
} |
260 |
refreshList(); |
261 |
} |
262 |
|
263 |
public void showPanel() { |
264 |
WindowManager wm = ToolsSwingLocator.getWindowManager(); |
265 |
wm.showWindow(this, "Disposable do list", WindowManager.MODE.WINDOW); |
266 |
} |
267 |
} |
268 |
|
269 |
|
270 |
|
271 |
|
272 |
} |