]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.workbench/src/org/simantics/workbench/internal/DelayedEventsProcessor.java
Improve startup time for fresh or rollback'd session in index writing
[simantics/platform.git] / bundles / org.simantics.workbench / src / org / simantics / workbench / internal / DelayedEventsProcessor.java
1 package org.simantics.workbench.internal;
2
3 import java.util.ArrayList;
4
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;
26
27 /**
28  * Helper class used to process delayed events.
29  * Events currently supported:
30  * <ul>
31  * <li>SWT.OpenDocument</li>
32  * </ul>
33  * @since 3.3
34  */
35 public class DelayedEventsProcessor implements Listener {
36
37         private ArrayList<String> filesToOpen = new ArrayList<String>(1);
38
39         /**
40          * Constructor.
41          * @param display display used as a source of event
42          */
43         public DelayedEventsProcessor(Display display) {
44                 display.addListener(SWT.OpenDocument, this);
45         }
46
47         /* (non-Javadoc)
48          * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
49          */
50         public void handleEvent(Event event) {
51                 final String path = event.text;
52                 if (path == null)
53                         return;
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);
57         }
58         
59         /**
60          * Process delayed events.
61          * @param display display associated with the workbench 
62          */
63         public void catchUp(Display display) {
64                 if (filesToOpen.isEmpty())
65                         return;
66                 
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);
71                 filesToOpen.clear();
72
73                 for(int i = 0; i < filePaths.length; i++) {
74                         openFile(display, filePaths[i]);
75                 }
76         }
77
78         private void openFile(Display display, final String path) {
79                 display.asyncExec(new Runnable() {
80                         public void run() {
81                                 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
82                                 if (window == null)
83                                         return;
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();
88                                         if (page == null) {
89                                                 String msg = NLS.bind(IDEWorkbenchMessages.OpenDelayedFileAction_message_noWindow, path);
90                                                 MessageDialog.open(MessageDialog.ERROR, window.getShell(),
91                                                                 IDEWorkbenchMessages.OpenDelayedFileAction_title,
92                                                                 msg, SWT.SHEET);
93                                         }
94                                         try {
95                                                 IDE.openInternalEditorOnFileStore(page, fileStore);
96                                                 Shell shell = window.getShell();
97                                                 if (shell != null) {
98                                                         if (shell.getMinimized())
99                                                                 shell.setMinimized(false);
100                                                         shell.forceActive();
101                                                 }
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,
109                                                                 msg, SWT.SHEET);
110                                         }
111                                 } else {
112                                         String msg = NLS.bind(IDEWorkbenchMessages.OpenDelayedFileAction_message_fileNotFound, path);
113                                         MessageDialog.open(MessageDialog.ERROR, window.getShell(),
114                                                         IDEWorkbenchMessages.OpenDelayedFileAction_title,
115                                                         msg, SWT.SHEET);
116                                 }
117                         }
118                 });
119         }
120
121 }