root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / extension / ShowDevelInfoExtension.java @ 33275
History | View | Annotate | Download (10.9 KB)
1 |
package org.gvsig.app.extension; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Dimension; |
5 |
import java.awt.Toolkit; |
6 |
import java.awt.datatransfer.StringSelection; |
7 |
import java.awt.event.ActionEvent; |
8 |
import java.awt.event.ActionListener; |
9 |
import java.awt.event.ComponentEvent; |
10 |
import java.awt.event.ComponentListener; |
11 |
import java.util.ArrayList; |
12 |
import java.util.Collections; |
13 |
import java.util.Comparator; |
14 |
import java.util.List; |
15 |
import java.util.Set; |
16 |
import java.util.TreeSet; |
17 |
|
18 |
import javax.help.Map; |
19 |
import javax.swing.BorderFactory; |
20 |
import javax.swing.Box; |
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.plugins.Extension; |
28 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
29 |
import org.gvsig.andami.ui.mdiManager.WindowInfo; |
30 |
import org.gvsig.app.ApplicationLocator; |
31 |
import org.gvsig.tools.ToolsLocator; |
32 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
33 |
import org.gvsig.tools.dynobject.DynField; |
34 |
import org.gvsig.tools.dynobject.DynStruct; |
35 |
import org.gvsig.tools.persistence.PersistenceFactory; |
36 |
import org.gvsig.tools.persistence.PersistenceManager; |
37 |
import org.gvsig.tools.persistence.Persistent; |
38 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
39 |
import org.slf4j.Logger; |
40 |
import org.slf4j.LoggerFactory; |
41 |
|
42 |
public class ShowDevelInfoExtension extends Extension { |
43 |
public static final int MODE_DIALOG = 1; |
44 |
public static final int MODE_WINDOW = 2; |
45 |
public static final int MODE_TOOL = 3; |
46 |
|
47 |
private Logger LOG = LoggerFactory.getLogger(ShowDevelInfoExtension.class); |
48 |
|
49 |
public void initialize() { |
50 |
// Do nothing
|
51 |
} |
52 |
|
53 |
public void postInitialize() { |
54 |
super.postInitialize();
|
55 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
56 |
|
57 |
try {
|
58 |
manager.setAutoValidation(PersistenceManager.MANDATORY_IF_DECLARED); |
59 |
} catch (PersistenceException e) {
|
60 |
LOG.warn("Error modificando el modo de autovalidacion de persistencia.", e);
|
61 |
} |
62 |
|
63 |
} |
64 |
|
65 |
public void execute(String actionCommand) { |
66 |
if( "ShowPersistenceFactories".equalsIgnoreCase(actionCommand) ) { |
67 |
showInfoPanel("Persistence factories", MODE_WINDOW, this.getPersistenceFactories()); |
68 |
} |
69 |
|
70 |
} |
71 |
|
72 |
public boolean isEnabled() { |
73 |
return true; |
74 |
} |
75 |
|
76 |
public boolean isVisible() { |
77 |
return true; |
78 |
} |
79 |
|
80 |
private void showInfoPanel(String title, int mode, String html) { |
81 |
IWindow win = new JInfoPanel(title, mode, html);
|
82 |
ApplicationLocator.getManager().getUIManager().addCentredWindow(win); |
83 |
} |
84 |
|
85 |
private class JInfoPanel extends JPanel implements IWindow, ComponentListener { |
86 |
/**
|
87 |
*
|
88 |
*/
|
89 |
private static final long serialVersionUID = 7164653790859770568L; |
90 |
|
91 |
|
92 |
private JButton accept = null; |
93 |
private JButton copy = null; |
94 |
private JTextPane text = null; |
95 |
|
96 |
private WindowInfo info;
|
97 |
|
98 |
private Object profile = WindowInfo.TOOL_PROFILE; |
99 |
|
100 |
|
101 |
public JInfoPanel(String title, int mode, final String html) { |
102 |
|
103 |
this.setLayout(new BorderLayout()); |
104 |
this.setPreferredSize(new Dimension(500, 300)); |
105 |
|
106 |
text = new JTextPane(); |
107 |
text.setContentType("text/html");
|
108 |
text.setEditable(false);
|
109 |
text.setText(html); |
110 |
text.setCaretPosition(0);
|
111 |
|
112 |
JScrollPane scrollPane = new JScrollPane(text); |
113 |
scrollPane.setPreferredSize(new Dimension(500, 220)); |
114 |
|
115 |
accept = new JButton("Accept"); |
116 |
accept.addActionListener(new ActionListener() { |
117 |
public void actionPerformed(ActionEvent arg0) { |
118 |
setVisible(false);
|
119 |
} |
120 |
}); |
121 |
copy = new JButton("Copy to clipboard"); |
122 |
copy.addActionListener(new ActionListener() { |
123 |
public void actionPerformed(ActionEvent arg0) { |
124 |
StringSelection ss = new StringSelection(text.getText()); |
125 |
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss,ss);
|
126 |
} |
127 |
}); |
128 |
|
129 |
JPanel optionsPane = new JPanel(); |
130 |
optionsPane.setLayout(new BoxLayout(optionsPane, BoxLayout.LINE_AXIS)); |
131 |
optionsPane.setBorder(BorderFactory.createEmptyBorder(4,4,4,4)); |
132 |
|
133 |
optionsPane.add(copy); |
134 |
optionsPane.add(accept); |
135 |
|
136 |
this.add(scrollPane, BorderLayout.CENTER); |
137 |
this.add(optionsPane, BorderLayout.SOUTH); |
138 |
this.setVisible(true); |
139 |
profile = WindowInfo.DIALOG_PROFILE; |
140 |
|
141 |
int code = WindowInfo.ICONIFIABLE | WindowInfo.MAXIMIZABLE
|
142 |
| WindowInfo.RESIZABLE; |
143 |
switch (mode) {
|
144 |
case MODE_DIALOG:
|
145 |
code |= WindowInfo.MODALDIALOG; |
146 |
profile = WindowInfo.DIALOG_PROFILE; |
147 |
break;
|
148 |
case MODE_TOOL:
|
149 |
code |= WindowInfo.PALETTE; |
150 |
profile = WindowInfo.TOOL_PROFILE; |
151 |
break;
|
152 |
case MODE_WINDOW:
|
153 |
default:
|
154 |
code |= WindowInfo.MODELESSDIALOG; |
155 |
profile = WindowInfo.DIALOG_PROFILE; |
156 |
} |
157 |
info = new WindowInfo(code);
|
158 |
info.setTitle(title); |
159 |
info.setMinimumSize(getPreferredSize()); |
160 |
info.setHeight(400);
|
161 |
info.setWidth(600);
|
162 |
|
163 |
addComponentListener(this);
|
164 |
} |
165 |
|
166 |
public WindowInfo getWindowInfo() {
|
167 |
return info;
|
168 |
} |
169 |
|
170 |
public Object getWindowProfile() { |
171 |
return profile;
|
172 |
} |
173 |
|
174 |
public void componentHidden(ComponentEvent componentEvent) { |
175 |
ApplicationLocator.getManager().getUIManager().closeWindow(this);
|
176 |
} |
177 |
|
178 |
public void componentMoved(ComponentEvent arg0) { |
179 |
// Do nothing
|
180 |
} |
181 |
|
182 |
public void componentResized(ComponentEvent arg0) { |
183 |
// Do nothing
|
184 |
} |
185 |
|
186 |
public void componentShown(ComponentEvent arg0) { |
187 |
// Do nothing
|
188 |
} |
189 |
} |
190 |
|
191 |
public class PersistenceInfo implements Comparable<PersistenceInfo> { |
192 |
PersistenceFactory factory; |
193 |
@SuppressWarnings("rawtypes") |
194 |
Class theClass;
|
195 |
DynStruct definition; |
196 |
|
197 |
PersistenceInfo(PersistenceFactory factory, @SuppressWarnings("rawtypes") Class theClass, DynStruct definition) { |
198 |
this.factory = factory;
|
199 |
this.theClass = theClass;
|
200 |
this.definition = definition;
|
201 |
} |
202 |
|
203 |
String getDefinitionName() {
|
204 |
if( definition == null ) { |
205 |
return ""; |
206 |
} |
207 |
return definition.getFullName();
|
208 |
} |
209 |
|
210 |
String getDefinitionDescription() {
|
211 |
if( definition == null ) { |
212 |
return ""; |
213 |
} |
214 |
return definition.getDescription();
|
215 |
} |
216 |
|
217 |
String getClassName() {
|
218 |
if( theClass == null ) { |
219 |
return ""; |
220 |
} |
221 |
return theClass.getName();
|
222 |
} |
223 |
|
224 |
String getFactoryName() {
|
225 |
if( factory == null ) { |
226 |
return ""; |
227 |
} |
228 |
return factory.getClass().getName();
|
229 |
} |
230 |
|
231 |
|
232 |
public int compareTo(PersistenceInfo other) { |
233 |
int r = this.factory.getClass().getName().compareTo(other.factory.getClass().getName()); |
234 |
if( r == 0 ) { |
235 |
return this.theClass.getName().compareTo(other.theClass.getName()); |
236 |
} |
237 |
return r;
|
238 |
} |
239 |
} |
240 |
|
241 |
@SuppressWarnings("rawtypes") |
242 |
public class ClassComparator implements Comparator { |
243 |
|
244 |
public int compare(Object o1, Object o2) { |
245 |
return ((Class)o1).getName().compareTo(((Class)o2).getName()); |
246 |
} |
247 |
|
248 |
|
249 |
} |
250 |
|
251 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
252 |
private String getPersistenceFactories() { |
253 |
int warningsCounter =0; |
254 |
List<PersistenceInfo> classes = new ArrayList<PersistenceInfo>(); |
255 |
Set<Class> referencedClasses = new TreeSet<Class>(new ClassComparator()); |
256 |
|
257 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
258 |
|
259 |
List<PersistenceFactory> factories = manager.getFactories();
|
260 |
for (PersistenceFactory factory : factories) {
|
261 |
List<Class> theClasses = factory.getManagedClasses(); |
262 |
for (Class theClass : theClasses) { |
263 |
classes.add( new PersistenceInfo(factory, theClass, manager.getDefinition(theClass)));
|
264 |
} |
265 |
} |
266 |
Collections.sort(classes);
|
267 |
|
268 |
|
269 |
StringBuffer buffer= new StringBuffer(); |
270 |
StringBuffer warnings = new StringBuffer(); |
271 |
|
272 |
buffer.append("<html>\n");
|
273 |
buffer.append("<body>\n");
|
274 |
buffer.append("<h2>Supported persistent classes</h2>\n");
|
275 |
buffer.append("<br>\n");
|
276 |
|
277 |
buffer.append("<ol>\n");
|
278 |
for (PersistenceInfo classInfo : classes) {
|
279 |
StringBuffer classBuffer = new StringBuffer(); |
280 |
boolean warning = false; |
281 |
|
282 |
classBuffer.append(" <li>\n ");
|
283 |
classBuffer.append("Class: <i>");
|
284 |
classBuffer.append(classInfo.getClassName()); |
285 |
classBuffer.append("</i><br>\n Persistent: <i>");
|
286 |
if( Persistent.class.isAssignableFrom(classInfo.theClass) ) {
|
287 |
classBuffer.append(" yes.");
|
288 |
} else {
|
289 |
classBuffer.append(" through a factory.");
|
290 |
} |
291 |
classBuffer.append("<br>\n Factory: <i>");
|
292 |
classBuffer.append(classInfo.getFactoryName()).append("</i><br>\n Definition name: <i>");
|
293 |
classBuffer.append(classInfo.getDefinitionName()).append("</i><br>\n Description: \n");
|
294 |
classBuffer.append(classInfo.getDefinitionDescription()).append("<br>\n ");
|
295 |
DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
296 |
DynStruct definition = classInfo.definition; |
297 |
DynField[] fields = definition.getDynFields();
|
298 |
for( int i=0; i < fields.length ; i++ ) { |
299 |
DynField field = fields[i]; |
300 |
if( dataTypesManager.isContainer(field.getType())) {
|
301 |
if( field.getClassOfValue()==null ) { |
302 |
classBuffer.append("Field <b>").append(field.getName()).append("</b>, container, can't has class for value of items.<br>\n "); |
303 |
warningsCounter++; |
304 |
warning = true;
|
305 |
} else if( field.getClassOfValue().getName().equals(Map.class.getName()) || |
306 |
field.getClassOfValue().getName().equals(List.class.getName()) ||
|
307 |
field.getClassOfValue().getName().equals(Set.class.getName()) ) {
|
308 |
classBuffer.append("Field <b>").append(field.getName()).append("</b>, container, probably can't has class for value of items.<br>\n "); |
309 |
warningsCounter++; |
310 |
warning = true;
|
311 |
} else {
|
312 |
referencedClasses.add(field.getClassOfValue()); |
313 |
} |
314 |
} else if( dataTypesManager.isObject(field.getType())) { |
315 |
if( field.getClassOfValue()==null ) { |
316 |
classBuffer.append("Field <b>").append(field.getName()).append("</b> can't has class of value.<br>\n "); |
317 |
warning = true;
|
318 |
warningsCounter++; |
319 |
} else {
|
320 |
referencedClasses.add(field.getClassOfValue()); |
321 |
} |
322 |
} |
323 |
} |
324 |
classBuffer.append("<br>\n </li>\n");
|
325 |
buffer.append(classBuffer); |
326 |
if( warning ) {
|
327 |
warnings.append(classBuffer); |
328 |
} |
329 |
} |
330 |
buffer.append("</ol>\n");
|
331 |
buffer.append("<br>\n");
|
332 |
buffer.append("<br>\n");
|
333 |
|
334 |
buffer.append("<h2>Persistent classes with problems</h2>\n");
|
335 |
buffer.append("<ol>\n");
|
336 |
buffer.append(warnings); |
337 |
buffer.append("</ol>\n");
|
338 |
buffer.append("<br>\n<p>Total warnigs: ").append(warningsCounter).append("</p>"); |
339 |
|
340 |
buffer.append("<h2>Not persistents used classes</h2>\n");
|
341 |
buffer.append("<ol>\n");
|
342 |
for (Class theClass : referencedClasses) { |
343 |
// if( manager.canPersist(theClass) ) {
|
344 |
// continue;
|
345 |
// }
|
346 |
if( Persistent.class.isAssignableFrom(theClass) ) {
|
347 |
continue;
|
348 |
} |
349 |
if( manager.getFactories().get(theClass)!=null ) { |
350 |
continue;
|
351 |
} |
352 |
buffer.append(" <li>\n");
|
353 |
buffer.append(" <i>").append(theClass.getName()).append("</i><br>\n"); |
354 |
buffer.append(" </li>\n");
|
355 |
warningsCounter++; |
356 |
} |
357 |
buffer.append("</ol>\n");
|
358 |
|
359 |
buffer.append("</body>\n");
|
360 |
buffer.append("</html>\n");
|
361 |
|
362 |
return buffer.toString();
|
363 |
} |
364 |
|
365 |
|
366 |
} |