1 package org.simantics.workbench.internal;
3 import java.util.ArrayList;
5 import org.eclipse.core.filesystem.EFS;
6 import org.eclipse.core.filesystem.IFileInfo;
7 import org.eclipse.core.filesystem.IFileStore;
8 import org.eclipse.core.runtime.CoreException;
9 import org.eclipse.core.runtime.IStatus;
10 import org.eclipse.core.runtime.Path;
11 import org.eclipse.core.runtime.Status;
12 import org.eclipse.jface.dialogs.MessageDialog;
13 import org.eclipse.osgi.util.NLS;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.widgets.Display;
16 import org.eclipse.swt.widgets.Event;
17 import org.eclipse.swt.widgets.Listener;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.ui.IWorkbenchPage;
20 import org.eclipse.ui.IWorkbenchWindow;
21 import org.eclipse.ui.PartInitException;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.ide.IDE;
24 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
25 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
28 * Helper class used to process delayed events.
29 * Events currently supported:
31 * <li>SWT.OpenDocument</li>
35 public class DelayedEventsProcessor implements Listener {
37 private ArrayList<String> filesToOpen = new ArrayList<String>(1);
41 * @param display display used as a source of event
43 public DelayedEventsProcessor(Display display) {
44 display.addListener(SWT.OpenDocument, this);
48 * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
50 public void handleEvent(Event event) {
51 final String path = event.text;
54 // If we start supporting events that can arrive on a non-UI thread, the following
55 // line will need to be in a "synchronized" block:
56 filesToOpen.add(path);
60 * Process delayed events.
61 * @param display display associated with the workbench
63 public void catchUp(Display display) {
64 if (filesToOpen.isEmpty())
67 // If we start supporting events that can arrive on a non-UI thread, the following
68 // lines will need to be in a "synchronized" block:
69 String[] filePaths = new String[filesToOpen.size()];
70 filesToOpen.toArray(filePaths);
73 for(int i = 0; i < filePaths.length; i++) {
74 openFile(display, filePaths[i]);
78 private void openFile(Display display, final String path) {
79 display.asyncExec(new Runnable() {
81 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
84 IFileStore fileStore = EFS.getLocalFileSystem().getStore(new Path(path));
85 IFileInfo fetchInfo = fileStore.fetchInfo();
86 if (!fetchInfo.isDirectory() && fetchInfo.exists()) {
87 IWorkbenchPage page = window.getActivePage();
89 String msg = NLS.bind(IDEWorkbenchMessages.OpenDelayedFileAction_message_noWindow, path);
90 MessageDialog.open(MessageDialog.ERROR, window.getShell(),
91 IDEWorkbenchMessages.OpenDelayedFileAction_title,
95 IDE.openInternalEditorOnFileStore(page, fileStore);
96 Shell shell = window.getShell();
98 if (shell.getMinimized())
99 shell.setMinimized(false);
102 } catch (PartInitException e) {
103 String msg = NLS.bind(IDEWorkbenchMessages.OpenDelayedFileAction_message_errorOnOpen,
104 fileStore.getName());
105 CoreException eLog = new PartInitException(e.getMessage());
106 IDEWorkbenchPlugin.log(msg, new Status(IStatus.ERROR, Activator.PLUGIN_ID, msg, eLog));
107 MessageDialog.open(MessageDialog.ERROR, window.getShell(),
108 IDEWorkbenchMessages.OpenDelayedFileAction_title,
112 String msg = NLS.bind(IDEWorkbenchMessages.OpenDelayedFileAction_message_fileNotFound, path);
113 MessageDialog.open(MessageDialog.ERROR, window.getShell(),
114 IDEWorkbenchMessages.OpenDelayedFileAction_title,