1313import java .io .File ;
1414import java .io .IOException ;
1515import java .io .InputStreamReader ;
16- import java .nio .file .Files ;
17- import java .nio .file .Path ;
18- import java .nio .file .Paths ;
1916import java .util .ArrayList ;
2017import java .util .List ;
2118
2522import org .eclipse .core .resources .IProject ;
2623import org .eclipse .core .resources .IResource ;
2724import org .eclipse .core .runtime .IPath ;
25+ import org .eclipse .core .runtime .IProgressMonitor ;
26+ import org .eclipse .core .runtime .IStatus ;
27+ import org .eclipse .core .runtime .Status ;
28+ import org .eclipse .core .runtime .jobs .Job ;
2829import org .eclipse .jdt .core .ICompilationUnit ;
2930import org .eclipse .jdt .core .IJavaProject ;
3031import org .eclipse .jface .dialogs .MessageDialog ;
3132import org .eclipse .jface .viewers .ISelection ;
3233import org .eclipse .jface .viewers .IStructuredSelection ;
3334import org .eclipse .swt .widgets .DirectoryDialog ;
35+ import org .eclipse .swt .widgets .Display ;
3436import org .eclipse .swt .widgets .Shell ;
3537import org .eclipse .ui .IWorkbenchPage ;
38+ import org .eclipse .ui .IWorkbenchWindow ;
3639import org .eclipse .ui .PartInitException ;
3740import org .eclipse .ui .PlatformUI ;
3841import org .eclipse .ui .console .ConsolePlugin ;
@@ -48,52 +51,84 @@ public class MigrateHandler extends AbstractHandler {
4851 public static final String PAYARA_TRANSFORMER = "fish.payara.transformer" ;
4952 public static final String PAYARA_TRANSFORMER_MAVEN = "fish.payara.transformer.maven" ;
5053 public static final String PAYARA_TRANSFORMER_VERSION = "0.2.14" ;
54+
55+ private IPath resourcePath = null ;
56+ private IPath projectPath = null ;
57+ private String name = "" ;
5158
5259 @ Override
5360 public Object execute (ExecutionEvent event ) throws ExecutionException {
54- Shell shell = HandlerUtil .getActiveWorkbenchWindow (event ).getShell ();
61+ IWorkbenchWindow activeWorkbenchWindow = HandlerUtil .getActiveWorkbenchWindow (event );
62+ IWorkbenchPage page = activeWorkbenchWindow .getActivePage ();
63+ Shell shell = activeWorkbenchWindow .getShell ();
5564 IStructuredSelection selection = getSelection (event );
5665 if (selection != null && !selection .isEmpty ()) {
5766 Object firstElement = selection .getFirstElement ();
58- IPath resourcePath = null ;
59- IPath projectPath = null ;
60- String name = "" ;
61- boolean isFile = false ;
62- if (firstElement instanceof IResource ) {
63- IResource resource = (IResource ) firstElement ;
64- resourcePath = resource .getLocation ();
65- projectPath = resourcePath ;
66- name = resource .getName ();
67- } else if (firstElement instanceof IJavaProject ) {
68- IJavaProject javaProject = (IJavaProject ) firstElement ;
69- IProject project = javaProject .getProject ();
70- resourcePath = project .getLocation ();
71- projectPath = resourcePath ;
72- name = project .getName ();
73- } else if (firstElement instanceof ICompilationUnit ) {
74- ICompilationUnit file = (ICompilationUnit ) firstElement ;
75- resourcePath = file .getResource ().getLocation ();
76- projectPath = file .getJavaProject ().getProject ().getLocation ();
77- name = file .getElementName ();
78- isFile = true ;
79- }
67+
68+ final boolean isFile = checkIfFile (firstElement );
8069 if (resourcePath != null && projectPath != null && !"" .equals (name )) {
8170 String srcPath = resourcePath .toOSString ();
8271 String srcProjectPath = projectPath .toOSString ();
8372 String destinationPath = chooseDestinationPath (srcProjectPath , name , isFile );
8473 if ("" .equals (destinationPath )) return null ;
85- int exitCode = runMvnCommand (srcProjectPath , srcPath , destinationPath );
86- if (exitCode == 0 ) {
87- MessageDialog .openInformation (shell , "Success" , (isFile ? "File " : "Project " ) + destinationPath + " created successfully." );
88- } else {
89- MessageDialog .openError (shell , "Error" , "Maven command failed with exit code " + exitCode + "." );
90- }
74+
75+ Job job = new Job ("Running Maven Command" ) {
76+ @ Override
77+ protected IStatus run (IProgressMonitor monitor ) {
78+ Display .getDefault ().asyncExec (() -> {
79+ try {
80+ IWorkbenchPage page = PlatformUI .getWorkbench ().getActiveWorkbenchWindow ().getActivePage ();
81+ IConsoleView consoleView = (IConsoleView ) page .showView (IConsoleConstants .ID_CONSOLE_VIEW );
82+ MessageConsole console = new MessageConsole ("Maven Command Console" , null );
83+ ConsolePlugin .getDefault ().getConsoleManager ().addConsoles (new IConsole [] { console });
84+ consoleView .display (console );
85+
86+ int exitCode = runMvnCommand (console , page , srcProjectPath , srcPath , destinationPath , shell );
87+ if (exitCode == 0 ) {
88+ Display .getDefault ().asyncExec (() -> {
89+ MessageDialog .openInformation (shell , "Success" , (isFile ? "File " : "Project " ) + destinationPath + " created successfully." );
90+ });
91+ } else {
92+ Display .getDefault ().asyncExec (() -> {
93+ MessageDialog .openError (shell , "Error" , "Maven command failed with exit code " + exitCode + "." );
94+ });
95+ }
96+ } catch (PartInitException e ) {
97+ e .printStackTrace ();
98+ }
99+ });
100+ return Status .OK_STATUS ;
101+ }
102+ };
103+ job .schedule ();
91104 }
92105 }
93106 return null ;
94107 }
95108
96- private String chooseDestinationPath (String srcPath , String name , boolean isFile ) {
109+ private boolean checkIfFile (Object firstElement ) {
110+ if (firstElement instanceof IResource ) {
111+ IResource resource = (IResource ) firstElement ;
112+ resourcePath = resource .getLocation ();
113+ projectPath = resourcePath ;
114+ name = resource .getName ();
115+ } else if (firstElement instanceof IJavaProject ) {
116+ IJavaProject javaProject = (IJavaProject ) firstElement ;
117+ IProject project = javaProject .getProject ();
118+ resourcePath = project .getLocation ();
119+ projectPath = resourcePath ;
120+ name = project .getName ();
121+ } else if (firstElement instanceof ICompilationUnit ) {
122+ ICompilationUnit file = (ICompilationUnit ) firstElement ;
123+ resourcePath = file .getResource ().getLocation ();
124+ projectPath = file .getJavaProject ().getProject ().getLocation ();
125+ name = file .getElementName ();
126+ return true ;
127+ }
128+ return false ;
129+ }
130+
131+ private String chooseDestinationPath (String srcPath , String name , boolean isFile ) {
97132 Shell shell = new Shell ();
98133 DirectoryDialog dialog = new DirectoryDialog (shell );
99134 dialog .setText ("Choose a " + (isFile ? "New File" : "" ) + " destination Folder" );
@@ -117,46 +152,34 @@ private IStructuredSelection getSelection(ExecutionEvent event) {
117152 return null ;
118153 }
119154
120- private int runMvnCommand (String srcProjectPath , String srcPath , String targetPath ) {
121- Shell shell = PlatformUI .getWorkbench ().getActiveWorkbenchWindow ().getShell ();
122-
123- MessageConsole console = new MessageConsole ("Maven Command Console" , null );
124- ConsolePlugin .getDefault ().getConsoleManager ().addConsoles (new IConsole [] { console });
125- IConsoleView consoleView = null ;
126-
127- try {
128- IWorkbenchPage page = PlatformUI .getWorkbench ().getActiveWorkbenchWindow ().getActivePage ();
129- consoleView = (IConsoleView ) page .showView (IConsoleConstants .ID_CONSOLE_VIEW );
130- console .activate ();
131- consoleView .display (console );
155+ private int runMvnCommand (MessageConsole console , IWorkbenchPage page , String srcProjectPath , String srcPath , String targetPath , Shell shell ) {
156+ try {
157+ MessageConsoleStream consoleStream = console .newMessageStream ();
132158
133- MessageConsoleStream consoleStream = console .newMessageStream ();
159+ List <String > command = new ArrayList <>();
160+ command .add (getMvnCommand ());
161+ command .add (getTransformCommand ());
162+ command .add ("-DselectedSource=" + srcPath );
163+ command .add ("-DselectedTarget=" + targetPath );
164+ ProcessBuilder builder = new ProcessBuilder (command );
165+ builder .environment ().put ("PATH" , System .getenv ("PATH" ));
166+ builder .directory (new File (srcProjectPath ));
167+ builder .redirectErrorStream (true );
168+ Process process = builder .start ();
134169
135- List <String > command = new ArrayList <>();
136- command .add (getMvnCommand ());
137- command .add (getTransformCommand ());
138- command .add ("-DselectedSource=" + srcPath );
139- command .add ("-DselectedTarget=" + targetPath );
140- ProcessBuilder builder = new ProcessBuilder (command );
141- builder .environment ().put ("PATH" , System .getenv ("PATH" ));
142- builder .directory (new File (srcProjectPath ));
143- builder .redirectErrorStream (true );
144- Process process = builder .start ();
145-
146- BufferedReader reader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
147- String line = null ;
148- while ((line = reader .readLine ()) != null ) {
149- consoleStream .println (line );
150- }
151-
152- return process .waitFor ();
153- } catch (IOException | InterruptedException | PartInitException e ) {
154- MessageDialog .openError (shell , "Error" ,
155- "An error occurred while running the Maven command: " + e .getMessage ());
156- return -1 ;
157- }
158- }
170+ BufferedReader reader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
171+ String line = null ;
172+ while ((line = reader .readLine ()) != null ) {
173+ consoleStream .println (line );
174+ }
159175
176+ return process .waitFor ();
177+ } catch (IOException | InterruptedException e ) {
178+ MessageDialog .openError (shell , "Error" ,
179+ "An error occurred while running the Maven command: " + e .getMessage ());
180+ return -1 ;
181+ }
182+ }
160183
161184 private String getTransformCommand () {
162185 return String .format ("%s:%s:%s:run" ,
0 commit comments