root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / extension / ShowDevelInfoExtension.java @ 38078
History | View | Annotate | Download (16.5 KB)
1 |
package org.gvsig.app.extension; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Dimension; |
5 |
import java.awt.event.ActionEvent; |
6 |
import java.awt.event.ActionListener; |
7 |
import java.awt.event.ComponentEvent; |
8 |
import java.awt.event.ComponentListener; |
9 |
import java.io.File; |
10 |
import java.io.FileWriter; |
11 |
import java.io.IOException; |
12 |
import java.util.ArrayList; |
13 |
import java.util.Collections; |
14 |
import java.util.Comparator; |
15 |
import java.util.Iterator; |
16 |
import java.util.List; |
17 |
import java.util.Set; |
18 |
import java.util.TreeSet; |
19 |
|
20 |
import javax.swing.BorderFactory; |
21 |
import javax.swing.BoxLayout; |
22 |
import javax.swing.JButton; |
23 |
import javax.swing.JPanel; |
24 |
import javax.swing.JScrollPane; |
25 |
import javax.swing.JTextPane; |
26 |
|
27 |
import org.gvsig.andami.iconthemes.IIconTheme; |
28 |
import org.gvsig.andami.iconthemes.IconThemeManager; |
29 |
import org.gvsig.andami.plugins.Extension; |
30 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
31 |
import org.gvsig.andami.ui.mdiManager.MDIManagerFactory; |
32 |
import org.gvsig.andami.ui.mdiManager.WindowInfo; |
33 |
import org.gvsig.andami.ui.mdiManager.MDIManager.UIActionTool; |
34 |
import org.gvsig.app.ApplicationLocator; |
35 |
import org.gvsig.tools.ToolsLocator; |
36 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
37 |
import org.gvsig.tools.dynobject.DynField; |
38 |
import org.gvsig.tools.dynobject.DynStruct; |
39 |
import org.gvsig.tools.persistence.PersistenceFactory; |
40 |
import org.gvsig.tools.persistence.PersistenceManager; |
41 |
import org.gvsig.tools.persistence.Persistent; |
42 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
43 |
import org.slf4j.Logger; |
44 |
import org.slf4j.LoggerFactory; |
45 |
|
46 |
public class ShowDevelInfoExtension extends Extension { |
47 |
public static final int MODE_DIALOG = 1; |
48 |
public static final int MODE_WINDOW = 2; |
49 |
public static final int MODE_TOOL = 3; |
50 |
|
51 |
private Logger logger = LoggerFactory.getLogger(ShowDevelInfoExtension.class); |
52 |
|
53 |
public void initialize() { |
54 |
// Do nothing
|
55 |
} |
56 |
|
57 |
public void postInitialize() { |
58 |
super.postInitialize();
|
59 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
60 |
|
61 |
try {
|
62 |
manager.setAutoValidation(PersistenceManager.MANDATORY_IF_DECLARED); |
63 |
} catch (PersistenceException e) {
|
64 |
logger.warn("Error modificando el modo de autovalidacion de persistencia.", e);
|
65 |
} |
66 |
|
67 |
} |
68 |
|
69 |
public void execute(String actionCommand) { |
70 |
if( "ShowPersistenceFactories".equalsIgnoreCase(actionCommand) ) { |
71 |
showInfoPanel("Persistence factories", MODE_WINDOW, this.getPersistenceFactories()); |
72 |
} else if( "ShowIconThemeInformation".equalsIgnoreCase(actionCommand) ) { |
73 |
showInfoPanel("Icon theme information", MODE_WINDOW, this.getIconThemeInformation()); |
74 |
} else if( "ShowActionsInformation".equalsIgnoreCase(actionCommand) ) { |
75 |
showInfoPanel("Actions information", MODE_WINDOW, this.getActionsInformation()); |
76 |
} |
77 |
|
78 |
} |
79 |
|
80 |
private String getActionsInformation() { |
81 |
StringBuffer buffer = new StringBuffer(); |
82 |
|
83 |
buffer.append("<html>\n");
|
84 |
buffer.append("<body>\n");
|
85 |
buffer.append("<h2>Actions information</h2>\n");
|
86 |
buffer.append("<br>\n");
|
87 |
|
88 |
buffer.append("<table border=\"0\">\n");
|
89 |
buffer.append(" <tr>\n");
|
90 |
buffer.append(" <td>Name</td>\n");
|
91 |
buffer.append(" <td>Extension</td>\n");
|
92 |
buffer.append(" <td>Position</td>\n");
|
93 |
buffer.append(" <td>Text</td>\n");
|
94 |
buffer.append(" <td>Command</td>\n");
|
95 |
buffer.append(" <td>Icon</td>\n");
|
96 |
buffer.append(" <td>Tip</td>\n");
|
97 |
buffer.append(" <td>Enabled text</td>\n");
|
98 |
buffer.append(" </tr>\n");
|
99 |
Iterator<UIActionTool> actions = MDIManagerFactory.createManager().getActionTools();
|
100 |
while( actions.hasNext() ) {
|
101 |
UIActionTool action = actions.next(); |
102 |
buffer.append(" <tr>\n");
|
103 |
buffer.append(" <td>").append(action.getName()).append("</td>\n"); |
104 |
buffer.append(" <td>").append(action.getExtensionName()).append("</td>\n"); |
105 |
buffer.append(" <td>").append(action.getPosition()).append("</td>\n"); |
106 |
buffer.append(" <td>").append(action.getText()).append("</td>\n"); |
107 |
buffer.append(" <td>").append(action.getCommand()).append("</td>\n"); |
108 |
buffer.append(" <td>").append(action.getIconName()).append("</td>\n"); |
109 |
buffer.append(" <td>").append(action.getTooptip()).append("</td>\n"); |
110 |
buffer.append(" <td>").append(action.getEnableText()).append("</td>\n"); |
111 |
buffer.append(" </tr>\n");
|
112 |
} |
113 |
buffer.append("</table>\n");
|
114 |
buffer.append("</body>\n");
|
115 |
buffer.append("</html>\n");
|
116 |
|
117 |
save2file("actionsinfo",buffer.toString());
|
118 |
|
119 |
return buffer.toString(); }
|
120 |
|
121 |
private String getIconThemeInformation() { |
122 |
StringBuffer buffer = new StringBuffer(); |
123 |
|
124 |
IconThemeManager manager = IconThemeManager.getIconThemeManager(); |
125 |
IIconTheme theme = manager.getCurrent(); |
126 |
|
127 |
buffer.append("<html>\n");
|
128 |
buffer.append("<body>\n");
|
129 |
buffer.append("<h2>Icon theme information</h2>\n");
|
130 |
buffer.append("<br>\n");
|
131 |
buffer.append("Theme: ");
|
132 |
buffer.append(theme.getName()); |
133 |
buffer.append("<br>\n");
|
134 |
buffer.append("Description: ");
|
135 |
buffer.append(theme.getDescription()); |
136 |
buffer.append("<br>\n");
|
137 |
buffer.append("Version: ");
|
138 |
buffer.append(theme.getVersion()); |
139 |
buffer.append("<br>\n");
|
140 |
|
141 |
buffer.append("<table border=\"0\">\n");
|
142 |
buffer.append(" <tr>\n");
|
143 |
buffer.append(" <td>Name</td>\n");
|
144 |
buffer.append(" <td>Resource id</td>\n");
|
145 |
buffer.append(" </tr>\n");
|
146 |
Iterator<String> names = theme.iterator(); |
147 |
while( names.hasNext() ) {
|
148 |
String name = names.next();
|
149 |
buffer.append(" <tr>\n");
|
150 |
buffer.append(" <td>").append(name).append("</td>\n"); |
151 |
buffer.append(" <td>").append(theme.getResourceID(name)).append("</td>\n"); |
152 |
buffer.append(" </tr>\n");
|
153 |
} |
154 |
buffer.append("</table>\n");
|
155 |
buffer.append("</body>\n");
|
156 |
buffer.append("</html>\n");
|
157 |
|
158 |
save2file("iconthemeinfo",buffer.toString());
|
159 |
|
160 |
return buffer.toString();
|
161 |
} |
162 |
|
163 |
private void save2file(String name, String contents) { |
164 |
File file;
|
165 |
try {
|
166 |
file = File.createTempFile("gvsig-"+name, ".html"); |
167 |
FileWriter fwriter = new FileWriter(file); |
168 |
fwriter.append(contents); |
169 |
fwriter.close(); |
170 |
} catch (IOException e) { |
171 |
logger.warn("Can't save contents to temp file gvsig-"+name, e );
|
172 |
} |
173 |
} |
174 |
|
175 |
public boolean isEnabled() { |
176 |
return true; |
177 |
} |
178 |
|
179 |
public boolean isVisible() { |
180 |
return true; |
181 |
} |
182 |
|
183 |
private void showInfoPanel(String title, int mode, String html) { |
184 |
IWindow win = new JInfoPanel(title, mode, html);
|
185 |
ApplicationLocator.getManager().getUIManager().addCentredWindow(win); |
186 |
} |
187 |
|
188 |
private class JInfoPanel extends JPanel implements IWindow, ComponentListener { |
189 |
/**
|
190 |
*
|
191 |
*/
|
192 |
private static final long serialVersionUID = 7164653790859770568L; |
193 |
|
194 |
|
195 |
private JButton accept = null; |
196 |
private JButton copy = null; |
197 |
private JTextPane text = null; |
198 |
|
199 |
private WindowInfo info;
|
200 |
|
201 |
private Object profile = WindowInfo.TOOL_PROFILE; |
202 |
|
203 |
|
204 |
public JInfoPanel(String title, int mode, final String html) { |
205 |
|
206 |
this.setLayout(new BorderLayout()); |
207 |
this.setPreferredSize(new Dimension(500, 300)); |
208 |
|
209 |
text = new JTextPane(); |
210 |
text.setContentType("text/html");
|
211 |
text.setEditable(false);
|
212 |
text.setText(html); |
213 |
text.setCaretPosition(0);
|
214 |
|
215 |
JScrollPane scrollPane = new JScrollPane(text); |
216 |
scrollPane.setPreferredSize(new Dimension(500, 220)); |
217 |
|
218 |
accept = new JButton("Accept"); |
219 |
accept.addActionListener(new ActionListener() { |
220 |
public void actionPerformed(ActionEvent arg0) { |
221 |
setVisible(false);
|
222 |
} |
223 |
}); |
224 |
copy = new JButton("Copy to clipboard"); |
225 |
copy.addActionListener(new ActionListener() { |
226 |
public void actionPerformed(ActionEvent arg0) { |
227 |
ApplicationLocator.getManager().putInClipboard(text.getText()); |
228 |
} |
229 |
}); |
230 |
|
231 |
JPanel optionsPane = new JPanel(); |
232 |
optionsPane.setLayout(new BoxLayout(optionsPane, BoxLayout.LINE_AXIS)); |
233 |
optionsPane.setBorder(BorderFactory.createEmptyBorder(4,4,4,4)); |
234 |
|
235 |
optionsPane.add(copy); |
236 |
optionsPane.add(accept); |
237 |
|
238 |
this.add(scrollPane, BorderLayout.CENTER); |
239 |
this.add(optionsPane, BorderLayout.SOUTH); |
240 |
this.setVisible(true); |
241 |
profile = WindowInfo.DIALOG_PROFILE; |
242 |
|
243 |
int code = WindowInfo.ICONIFIABLE | WindowInfo.MAXIMIZABLE
|
244 |
| WindowInfo.RESIZABLE; |
245 |
switch (mode) {
|
246 |
case MODE_DIALOG:
|
247 |
code |= WindowInfo.MODALDIALOG; |
248 |
profile = WindowInfo.DIALOG_PROFILE; |
249 |
break;
|
250 |
case MODE_TOOL:
|
251 |
code |= WindowInfo.PALETTE; |
252 |
profile = WindowInfo.TOOL_PROFILE; |
253 |
break;
|
254 |
case MODE_WINDOW:
|
255 |
default:
|
256 |
code |= WindowInfo.MODELESSDIALOG; |
257 |
profile = WindowInfo.DIALOG_PROFILE; |
258 |
} |
259 |
info = new WindowInfo(code);
|
260 |
info.setTitle(title); |
261 |
info.setMinimumSize(getPreferredSize()); |
262 |
info.setHeight(400);
|
263 |
info.setWidth(600);
|
264 |
|
265 |
addComponentListener(this);
|
266 |
} |
267 |
|
268 |
public WindowInfo getWindowInfo() {
|
269 |
return info;
|
270 |
} |
271 |
|
272 |
public Object getWindowProfile() { |
273 |
return profile;
|
274 |
} |
275 |
|
276 |
public void componentHidden(ComponentEvent componentEvent) { |
277 |
ApplicationLocator.getManager().getUIManager().closeWindow(this);
|
278 |
} |
279 |
|
280 |
public void componentMoved(ComponentEvent arg0) { |
281 |
// Do nothing
|
282 |
} |
283 |
|
284 |
public void componentResized(ComponentEvent arg0) { |
285 |
// Do nothing
|
286 |
} |
287 |
|
288 |
public void componentShown(ComponentEvent arg0) { |
289 |
// Do nothing
|
290 |
} |
291 |
} |
292 |
|
293 |
public class PersistenceInfo implements Comparable<PersistenceInfo> { |
294 |
PersistenceFactory factory; |
295 |
@SuppressWarnings("rawtypes") |
296 |
Class theClass;
|
297 |
DynStruct definition; |
298 |
|
299 |
PersistenceInfo(PersistenceFactory factory, @SuppressWarnings("rawtypes") Class theClass, DynStruct definition) { |
300 |
this.factory = factory;
|
301 |
this.theClass = theClass;
|
302 |
this.definition = definition;
|
303 |
} |
304 |
|
305 |
String getDefinitionName() {
|
306 |
if( definition == null ) { |
307 |
return ""; |
308 |
} |
309 |
return definition.getFullName();
|
310 |
} |
311 |
|
312 |
String getDefinitionDescription() {
|
313 |
if( definition == null ) { |
314 |
return ""; |
315 |
} |
316 |
return definition.getDescription();
|
317 |
} |
318 |
|
319 |
String getClassName() {
|
320 |
if( theClass == null ) { |
321 |
return ""; |
322 |
} |
323 |
return theClass.getName();
|
324 |
} |
325 |
|
326 |
String getFactoryName() {
|
327 |
if( factory == null ) { |
328 |
return ""; |
329 |
} |
330 |
return factory.getClass().getName();
|
331 |
} |
332 |
|
333 |
|
334 |
public int compareTo(PersistenceInfo other) { |
335 |
int r = this.factory.getClass().getName().compareTo(other.factory.getClass().getName()); |
336 |
if( r == 0 ) { |
337 |
return this.theClass.getName().compareTo(other.theClass.getName()); |
338 |
} |
339 |
return r;
|
340 |
} |
341 |
} |
342 |
|
343 |
@SuppressWarnings("rawtypes") |
344 |
public class ClassComparator implements Comparator { |
345 |
|
346 |
public int compare(Object o1, Object o2) { |
347 |
if( o1==null || o2==null || ((Class)o1).getName()==null ) { |
348 |
logger.warn("Esto no deberia estar pasando.");
|
349 |
return 0; // FIXME |
350 |
} |
351 |
return ((Class)o1).getName().compareTo(((Class)o2).getName()); |
352 |
} |
353 |
|
354 |
|
355 |
} |
356 |
|
357 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
358 |
private String getPersistenceFactories() { |
359 |
int warningsCounter =0; |
360 |
List<PersistenceInfo> classes = new ArrayList<PersistenceInfo>(); |
361 |
Set<Class> referencedClasses = new TreeSet<Class>(new ClassComparator()); |
362 |
|
363 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
364 |
|
365 |
List<PersistenceFactory> factories = manager.getFactories();
|
366 |
for (PersistenceFactory factory : factories) {
|
367 |
List<Class> theClasses = factory.getManagedClasses(); |
368 |
for (Class theClass : theClasses) { |
369 |
|
370 |
DynStruct definition = manager.getDefinition(theClass); |
371 |
List defs = factory.getDefinitions();
|
372 |
if (definition == null && defs != null && !defs.isEmpty()) { |
373 |
for (int i = 0; i < defs.size(); i++) { |
374 |
definition = (DynStruct) defs.get(i); |
375 |
classes.add(new PersistenceInfo(factory, theClass,
|
376 |
definition)); |
377 |
} |
378 |
} else {
|
379 |
classes.add(new PersistenceInfo(factory, theClass,
|
380 |
definition)); |
381 |
} |
382 |
} |
383 |
} |
384 |
Collections.sort(classes);
|
385 |
|
386 |
|
387 |
StringBuffer buffer= new StringBuffer(); |
388 |
StringBuffer warnings = new StringBuffer(); |
389 |
|
390 |
buffer.append("<html>\n");
|
391 |
buffer.append("<body>\n");
|
392 |
buffer.append("<h2>Supported persistent classes</h2>\n");
|
393 |
buffer.append("<br>\n");
|
394 |
|
395 |
buffer.append("<ol>\n");
|
396 |
for (PersistenceInfo classInfo : classes) {
|
397 |
StringBuffer classBuffer = new StringBuffer(); |
398 |
boolean warning = false; |
399 |
classBuffer.append(" <li>\n ");
|
400 |
classBuffer.append("Class: <i>");
|
401 |
classBuffer.append(classInfo.getClassName()); |
402 |
classBuffer.append("</i><br>\n Persistent: <i>");
|
403 |
if( Persistent.class.isAssignableFrom(classInfo.theClass) ) {
|
404 |
classBuffer.append(" yes.");
|
405 |
} else {
|
406 |
classBuffer.append(" through a factory.");
|
407 |
} |
408 |
classBuffer.append("<br>\n Factory: <i>");
|
409 |
classBuffer.append(classInfo.getFactoryName()).append("</i><br>\n Definition name: <i>");
|
410 |
classBuffer.append(classInfo.getDefinitionName()).append("</i><br>\n Description: \n");
|
411 |
classBuffer.append(classInfo.getDefinitionDescription()).append("<br>\n ");
|
412 |
DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
413 |
DynStruct definition = classInfo.definition; |
414 |
if (definition == null) { |
415 |
classBuffer.append("Definition for ")
|
416 |
.append(classInfo.getClassName()) |
417 |
.append(" is null.<br>\n ");
|
418 |
warningsCounter++; |
419 |
warning = true;
|
420 |
} else {
|
421 |
DynField[] fields = definition.getDynFields();
|
422 |
for (int i = 0; i < fields.length; i++) { |
423 |
DynField field = fields[i]; |
424 |
if (dataTypesManager.isContainer(field.getType())) {
|
425 |
if (field.getClassOfItems() == null) { |
426 |
classBuffer |
427 |
.append("Field <b>")
|
428 |
.append(field.getName()) |
429 |
.append( |
430 |
"</b>, container, can't has class for value of items.<br>\n ");
|
431 |
warningsCounter++; |
432 |
warning = true;
|
433 |
} else {
|
434 |
classBuffer.append("Field ")
|
435 |
.append(field.getName()) |
436 |
.append(", container of '")
|
437 |
.append(field.getClassOfItems().getName()) |
438 |
.append("'.<br>\n ");
|
439 |
referencedClasses.add(field.getClassOfItems()); |
440 |
} |
441 |
} else
|
442 |
if (dataTypesManager.isObject(field.getType())) {
|
443 |
if (field.getClassOfValue() == null) { |
444 |
classBuffer |
445 |
.append("Field <b>")
|
446 |
.append(field.getName()) |
447 |
.append( |
448 |
"</b> can't has class of value.<br>\n ");
|
449 |
warningsCounter++; |
450 |
warning = true;
|
451 |
} else {
|
452 |
classBuffer.append("Field ")
|
453 |
.append(field.getName()).append(" of '")
|
454 |
.append(field.getClassOfValue()) |
455 |
.append("'.<br>\n ");
|
456 |
referencedClasses.add(field.getClassOfValue()); |
457 |
} |
458 |
} |
459 |
} |
460 |
} |
461 |
classBuffer.append("<br>\n </li>\n");
|
462 |
buffer.append(classBuffer); |
463 |
if (warning) {
|
464 |
warnings.append(classBuffer); |
465 |
} |
466 |
} |
467 |
buffer.append("</ol>\n");
|
468 |
buffer.append("<br>\n");
|
469 |
buffer.append("<br>\n");
|
470 |
|
471 |
buffer.append("<h2>Persistent classes with problems</h2>\n");
|
472 |
buffer.append("<ol>\n");
|
473 |
buffer.append(warnings); |
474 |
buffer.append("</ol>\n");
|
475 |
buffer.append("<br>\n<p>Total warnigs: ").append(warningsCounter).append("</p>"); |
476 |
|
477 |
buffer.append("<h2>Not persistents used classes</h2>\n");
|
478 |
buffer.append("<ol>\n");
|
479 |
for (Class theClass : referencedClasses) { |
480 |
// if( manager.canPersist(theClass) ) {
|
481 |
// continue;
|
482 |
// }
|
483 |
if( Persistent.class.isAssignableFrom(theClass) ) {
|
484 |
continue;
|
485 |
} |
486 |
if( manager.getFactories().get(theClass)!=null ) { |
487 |
continue;
|
488 |
} |
489 |
buffer.append(" <li>\n");
|
490 |
buffer.append(" <i>").append(theClass.getName()).append("</i><br>\n"); |
491 |
buffer.append(" </li>\n");
|
492 |
warningsCounter++; |
493 |
} |
494 |
buffer.append("</ol>\n");
|
495 |
|
496 |
buffer.append("</body>\n");
|
497 |
buffer.append("</html>\n");
|
498 |
|
499 |
save2file("persistenceinfo",buffer.toString());
|
500 |
|
501 |
return buffer.toString();
|
502 |
} |
503 |
|
504 |
|
505 |
} |