Revision 41258 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 | ||
---|---|---|
3 | 3 |
* |
4 | 4 |
* Copyright (C) 2007-2013 gvSIG Association. |
5 | 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.
|
|
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 | 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.
|
|
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 | 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. |
|
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. |
|
20 | 19 |
* |
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
|
21 |
* gvsig.com, or visit our website www.gvsig.com. |
|
23 | 22 |
*/ |
24 | 23 |
/* |
25 | 24 |
* AUTHORS (In addition to CIT): |
... | ... | |
27 | 26 |
*/ |
28 | 27 |
package org.gvsig.app.extension.dispose; |
29 | 28 |
|
29 |
import java.awt.Dimension; |
|
30 | 30 |
import java.awt.event.ActionEvent; |
31 | 31 |
import java.awt.event.ActionListener; |
32 | 32 |
import java.util.Iterator; |
... | ... | |
35 | 35 |
import javax.swing.DefaultListModel; |
36 | 36 |
import javax.swing.JList; |
37 | 37 |
import javax.swing.JScrollPane; |
38 |
import javax.swing.JTextArea; |
|
39 | 38 |
import javax.swing.JTextPane; |
40 | 39 |
import javax.swing.SwingUtilities; |
41 | 40 |
import javax.swing.event.ListSelectionEvent; |
... | ... | |
55 | 54 |
/** |
56 | 55 |
* An extension to view and manage {@link Disposable} objects which are still |
57 | 56 |
* bound. |
58 |
*
|
|
57 |
* |
|
59 | 58 |
* TODO: remove this extension and replace with a better one, maybe based on |
60 | 59 |
* scripting. |
61 |
*
|
|
60 |
* |
|
62 | 61 |
* @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team |
63 | 62 |
*/ |
64 | 63 |
public class DisposableManagementExtension extends Extension { |
65 | 64 |
|
66 |
public static String SHOW_COMMAND = "tools-devel-disposables-show-pendings";
|
|
67 |
public static String DISPOSE_ALL_COMMAND = "tools-devel-disposables-free-all";
|
|
65 |
public static String SHOW_COMMAND = "tools-devel-disposables-show-pendings";
|
|
66 |
public static String DISPOSE_ALL_COMMAND = "tools-devel-disposables-free-all";
|
|
68 | 67 |
|
69 |
private static final Logger LOG =
|
|
70 |
LoggerFactory.getLogger(DisposableManagementExtension.class);
|
|
68 |
private static final Logger LOG
|
|
69 |
= LoggerFactory.getLogger(DisposableManagementExtension.class);
|
|
71 | 70 |
|
72 |
public void initialize() {
|
|
73 |
// Nothing to do
|
|
74 |
}
|
|
71 |
public void initialize() {
|
|
72 |
// Nothing to do
|
|
73 |
}
|
|
75 | 74 |
|
76 |
@Override
|
|
77 |
public void postInitialize() {
|
|
78 |
super.postInitialize();
|
|
79 |
}
|
|
75 |
@Override
|
|
76 |
public void postInitialize() {
|
|
77 |
super.postInitialize();
|
|
78 |
}
|
|
80 | 79 |
|
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 |
} |
|
80 |
public void execute(String actionCommand) { |
|
92 | 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 |
} |
|
93 | 91 |
|
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 |
}
|
|
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 |
}
|
|
103 | 101 |
|
104 |
public boolean isEnabled() {
|
|
105 |
return true;
|
|
106 |
}
|
|
102 |
public boolean isEnabled() {
|
|
103 |
return true;
|
|
104 |
}
|
|
107 | 105 |
|
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(); |
|
106 |
public boolean isVisible() { |
|
107 |
return true; |
|
108 |
} |
|
185 | 109 |
|
186 |
this.messageLabel.setText("Pending "+disposables.size()); |
|
187 |
if (disposables.size() > 0) { |
|
188 |
DefaultListModel listmodel = new DefaultListModel(); |
|
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(); |
|
110 |
class DisposablesDoList extends DisposablesDoListLayout { |
|
223 | 111 |
|
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 |
} |
|
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 |
} |
Also available in: Unified diff