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