How to open a generated file [message #1848254] |
Fri, 26 November 2021 06:08 |
Denis Nikiforov Messages: 346 Registered: August 2013 |
Senior Member |
|
|
Hi
I run a long job to generate an xlsx document (using Gendoc). After generation is finished I need to open the file. I do it as follows:
public class GenerateDesReportHandler extends AbstractHandler {
private static final String TEMPLATE_URL = "platform:/plugin/.../templates/Report.xlsx"; //$NON-NLS-1$
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
try {
DialectEditor editor = (DialectEditor) HandlerUtil.getActiveEditor(event);
DSemanticDecorator repr = (DSemanticDecorator) editor.getRepresentation();
DiscreteEventSimulation analysis = (DiscreteEventSimulation) repr.getTarget();
URI targetFolderUri = analysis.eResource().getURI().trimSegments(1);
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IContainer targetFolder = targetFolderUri.segmentCount() > 2
? root.getFolder(new Path(targetFolderUri.toPlatformString(true)))
: root.getProject(targetFolderUri.lastSegment());
GenerateGendocReportOperation operation = new GenerateGendocReportOperation(
new URL(TEMPLATE_URL),
analysis, analysis.getName(), targetFolder);
IProgressService service = HandlerUtil.getActiveWorkbenchWindow(event).getWorkbench().getProgressService();
service.run(false, true, new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
operation.run(monitor);
IWorkbenchWindow activeWindow = HandlerUtil.getActiveWorkbenchWindow(event);
IWorkbenchPage activePage = activeWindow.getActivePage();
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IFile targetFile = root.getFileForLocation(new Path(operation.getTargetFileName()));
try {
IDE.openEditor(activePage, targetFile);
} catch (PartInitException e) {
throw new InvocationTargetException(e);
}
}
});
} catch (RuntimeException | MalformedURLException | InterruptedException e) {
String message = e.getLocalizedMessage();
if (message == null || message.isEmpty()) {
message = Messages.CalcluateDesResultsHandler_Error;
}
Shell shell = HandlerUtil.getActiveShell(event);
MessageDialog.openError(shell, Messages.CalcluateDesResultsHandler_Error, message);
} catch (InvocationTargetException e) {
String message = e.getCause() != null ? e.getCause().getLocalizedMessage() : e.getLocalizedMessage();
if (message == null || message.isEmpty()) {
message = Messages.CalcluateDesResultsHandler_Error;
}
Shell shell = HandlerUtil.getActiveShell(event);
MessageDialog.openError(shell, Messages.CalcluateDesResultsHandler_Error, message);
}
return null;
}
}
The problem is that I have to run the operation in UI thread (first argument for service.run is set to false). If I run it in a separate thread I get "org.eclipse.swt.SWTException: Invalid thread access" for IDE.openEditor(). Is it possible to run it in a separate thread?
Is it possible to move the code for editor opening to the operation? If I do it then PlatformUI.getWorkbench().getActiveWorkbenchWindow() reutrns null. And I have to call PlatformUI.getWorkbench().getWorkbenchWindows()[0].
Is it possible to replace IDE.openEditor() by something?
|
|
|
Re: How to open a generated file [message #1848255 is a reply to message #1848254] |
Fri, 26 November 2021 06:12 |
Denis Nikiforov Messages: 346 Registered: August 2013 |
Senior Member |
|
|
Maybe the right way is to show a notification to a user that file generation is finished? He could click on the notification and open the file.
How could I implement such a notification? Anyway it seems usefull to have the ability to open the generated file automatically without additional click.
[Updated on: Fri, 26 November 2021 06:13] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.02992 seconds