Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2010 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
******************************************************************************/ |
11 |
|
12 |
package org.eclipse.ui.internal.ide.application; |
13 |
|
14 |
import java.util.ArrayList; |
15 |
|
16 |
import org.eclipse.core.filesystem.EFS; |
17 |
import org.eclipse.core.filesystem.IFileInfo; |
18 |
import org.eclipse.core.filesystem.IFileStore; |
19 |
import org.eclipse.core.runtime.Path; |
20 |
import org.eclipse.jface.dialogs.MessageDialog; |
21 |
import org.eclipse.osgi.util.NLS; |
22 |
import org.eclipse.swt.SWT; |
23 |
import org.eclipse.swt.widgets.Display; |
24 |
import org.eclipse.swt.widgets.Event; |
25 |
import org.eclipse.swt.widgets.Listener; |
26 |
import org.eclipse.ui.IWorkbenchPage; |
27 |
import org.eclipse.ui.IWorkbenchWindow; |
28 |
import org.eclipse.ui.PartInitException; |
29 |
import org.eclipse.ui.PlatformUI; |
30 |
import org.eclipse.ui.ide.IDE; |
31 |
import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; |
32 |
import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; |
33 |
|
34 |
/** |
35 |
* Helper class used to process delayed events. |
36 |
* Events currently supported: |
37 |
* <ul> |
38 |
* <li>SWT.OpenDoc</li> |
39 |
* </ul> |
40 |
* @since 3.3 |
41 |
*/ |
42 |
public class DelayedEventsProcessor implements Listener { |
43 |
|
44 |
private ArrayList filesToOpen = new ArrayList(1); |
45 |
|
46 |
/** |
47 |
* Constructor. |
48 |
* @param display display used as a source of event |
49 |
*/ |
50 |
public DelayedEventsProcessor(Display display) { |
51 |
display.addListener(SWT.OpenDoc, this); |
52 |
} |
53 |
|
54 |
/* (non-Javadoc) |
55 |
* @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event) |
56 |
*/ |
57 |
public void handleEvent(Event event) { |
58 |
final String path = event.text; |
59 |
if (path == null) |
60 |
return; |
61 |
synchronized (filesToOpen) { |
62 |
filesToOpen.add(path); |
63 |
} |
64 |
} |
65 |
|
66 |
/** |
67 |
* Process delayed events. |
68 |
* @param display display associated with the workbench |
69 |
*/ |
70 |
public void catchUp(Display display) { |
71 |
if (filesToOpen.isEmpty()) // intentionally done outside of the sync block to improve performance |
72 |
return; |
73 |
// make a local copy of the list, clear it, and release the lock ASAP |
74 |
String[] filePaths; |
75 |
synchronized (filesToOpen) { |
76 |
filePaths = new String[filesToOpen.size()]; |
77 |
filesToOpen.toArray(filePaths); |
78 |
filesToOpen.clear(); |
79 |
} |
80 |
for(int i = 0; i < filePaths.length; i++) { |
81 |
openFile(display, filePaths[i]); |
82 |
} |
83 |
} |
84 |
|
85 |
private void openFile(Display display, final String path) { |
86 |
display.asyncExec(new Runnable() { |
87 |
public void run() { |
88 |
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
89 |
if (window == null) |
90 |
return; |
91 |
IFileStore fileStore = EFS.getLocalFileSystem().getStore(new Path(path)); |
92 |
IFileInfo fetchInfo = fileStore.fetchInfo(); |
93 |
if (!fetchInfo.isDirectory() && fetchInfo.exists()) { |
94 |
IWorkbenchPage page = window.getActivePage(); |
95 |
try { |
96 |
IDE.openEditorOnFileStore(page, fileStore); |
97 |
} catch (PartInitException e) { |
98 |
String msg = NLS.bind(IDEWorkbenchMessages.OpenDelayedFileAction_message_errorOnOpen, |
99 |
fileStore.getName()); |
100 |
IDEWorkbenchPlugin.log(msg, e.getStatus()); |
101 |
MessageDialog.open(MessageDialog.ERROR, window.getShell(), |
102 |
IDEWorkbenchMessages.OpenDelayedFileAction_title, |
103 |
msg, SWT.SHEET); |
104 |
} |
105 |
} else { |
106 |
String msg = NLS.bind(IDEWorkbenchMessages.OpenDelayedFileAction_message_fileNotFound, path); |
107 |
MessageDialog.open(MessageDialog.ERROR, window.getShell(), |
108 |
IDEWorkbenchMessages.OpenDelayedFileAction_title, |
109 |
msg, SWT.SHEET); |
110 |
} |
111 |
} |
112 |
}); |
113 |
} |
114 |
|
115 |
} |