1010import org .eclipse .core .resources .IResource ;
1111import org .eclipse .core .runtime .CoreException ;
1212import org .eclipse .core .runtime .QualifiedName ;
13- import org .eclipse .jdt .core .Flags ;
1413import org .eclipse .jdt .core .ICompilationUnit ;
15- import org .eclipse .jdt .core .IMethod ;
16- import org .eclipse .jdt .core .IType ;
1714import org .eclipse .jdt .core .JavaModelException ;
1815import org .eclipse .jface .dialogs .MessageDialog ;
16+ import org .eclipse .jface .viewers .ISelection ;
1917import org .eclipse .jface .viewers .IStructuredSelection ;
2018import org .eclipse .swt .widgets .DirectoryDialog ;
19+ import org .eclipse .swt .widgets .Shell ;
2120import org .eclipse .ui .handlers .HandlerUtil ;
2221
23- public class Convert extends AbstractHandler {
22+ public class ConvertHandler extends AbstractHandler {
2423 private QualifiedName path = new QualifiedName ("html" , "path" );
2524
2625 @ Override
2726 public Object execute (ExecutionEvent event ) throws ExecutionException {
27+ Shell shell = HandlerUtil .getActiveShell (event );
28+ ISelection sel = HandlerUtil .getActiveMenuSelection (event );
29+ IStructuredSelection selection = (IStructuredSelection ) sel ;
2830
29- IStructuredSelection selection = (IStructuredSelection ) HandlerUtil
30- .getActiveMenuSelection (event );
31-
32- String directory = "" ;
3331 Object firstElement = selection .getFirstElement ();
3432 if (firstElement instanceof ICompilationUnit ) {
35- ICompilationUnit cu = (ICompilationUnit ) firstElement ;
36- IResource res = cu .getResource ();
37- boolean newDirectory = true ;
38- directory = getPersistentProperty (res , path );
39-
40- if (directory != null && directory .length () > 0 ) {
41- newDirectory = !(MessageDialog .openQuestion (
42- HandlerUtil .getActiveShell (event ), "Question" ,
43- "Use the previous output directory?" ));
44- }
45- if (newDirectory ) {
46- DirectoryDialog fileDialog = new DirectoryDialog (
47- HandlerUtil .getActiveShell (event ));
48- directory = fileDialog .open ();
49-
50- }
51- if (directory != null && directory .length () > 0 ) {
52- analyze (cu );
53- setPersistentProperty (res , path , directory );
54- write (directory , cu );
55- }
33+ createOutput (shell , firstElement );
5634
5735 } else {
58- MessageDialog .openInformation (HandlerUtil . getActiveShell ( event ) ,
59- "Information" , " Please select a Java source file" );
36+ MessageDialog .openInformation (shell , "Info" ,
37+ "Please select a Java source file" );
6038 }
6139 return null ;
6240 }
6341
42+ private void createOutput (Shell shell , Object firstElement ) {
43+ String directory ;
44+ ICompilationUnit cu = (ICompilationUnit ) firstElement ;
45+ IResource res = cu .getResource ();
46+ boolean newDirectory = true ;
47+ directory = getPersistentProperty (res , path );
48+
49+ if (directory != null && directory .length () > 0 ) {
50+ newDirectory = !(MessageDialog .openQuestion (shell , "Question" ,
51+ "Use the previous output directory?" ));
52+ }
53+ if (newDirectory ) {
54+ DirectoryDialog fileDialog = new DirectoryDialog (shell );
55+ directory = fileDialog .open ();
56+
57+ }
58+ if (directory != null && directory .length () > 0 ) {
59+ setPersistentProperty (res , path , directory );
60+ write (directory , cu );
61+ }
62+ }
63+
6464 protected String getPersistentProperty (IResource res , QualifiedName qn ) {
6565 try {
6666 return res .getPersistentProperty (qn );
@@ -69,38 +69,6 @@ protected String getPersistentProperty(IResource res, QualifiedName qn) {
6969 }
7070 }
7171
72- // TODO: Include this in the HTML output
73-
74- private void analyze (ICompilationUnit cu ) {
75- // Cool JDT allows you to analyze the code easily
76- // I don't see really a use case here but I just wanted to do this here
77- // as I consider this as cool and
78- // what to have a place where I can store the data
79- try {
80-
81- IType type = null ;
82- IType [] allTypes ;
83- allTypes = cu .getAllTypes ();
84- /**
85- * Search the public class
86- */
87- for (int t = 0 ; t < allTypes .length ; t ++) {
88- if (Flags .isPublic ((allTypes [t ].getFlags ()))) {
89- type = allTypes [t ];
90- break ;
91- }
92- }
93- if (type != null ) {
94- String classname = type .getFullyQualifiedName ();
95- IMethod [] methods = type .getMethods ();
96- }
97-
98- } catch (JavaModelException e ) {
99- e .printStackTrace ();
100- }
101-
102- }
103-
10472 protected void setPersistentProperty (IResource res , QualifiedName qn ,
10573 String value ) {
10674 try {
@@ -116,11 +84,7 @@ private void write(String dir, ICompilationUnit cu) {
11684 String test = cu .getCorrespondingResource ().getName ();
11785 // Need
11886 String [] name = test .split ("\\ ." );
119- System .out .println (test );
120- System .out .println (name .length );
12187 String htmlFile = dir + "\\ " + name [0 ] + ".html" ;
122-
123- System .out .println (htmlFile );
12488 FileWriter output = new FileWriter (htmlFile );
12589 BufferedWriter writer = new BufferedWriter (output );
12690 writer .write ("<html>" );
0 commit comments