|
1 | 1 | /*******************************************************************************
|
2 |
| - * Copyright (c) 2010, 2021 IBM Corporation and others. |
| 2 | + * Copyright (c) 2010, 2021, 2023 IBM Corporation and others. |
3 | 3 | *
|
4 | 4 | * This program and the accompanying materials
|
5 | 5 | * are made available under the terms of the Eclipse Public License 2.0
|
|
14 | 14 | * at a specific line/col
|
15 | 15 | * Nitin Dahyabhai - Bug 567708 - Support for MultiPageEditorParts
|
16 | 16 | * containing a TextEditor page
|
| 17 | + * James Yuzawa - 'Open Directory' action shows existing projects in |
| 18 | + * Project Explorer |
17 | 19 | ******************************************************************************/
|
18 | 20 |
|
19 | 21 | package org.eclipse.ui.internal.ide.application;
|
|
25 | 27 | import java.net.URISyntaxException;
|
26 | 28 | import java.util.ArrayList;
|
27 | 29 |
|
| 30 | +import org.eclipse.core.commands.Command; |
| 31 | +import org.eclipse.core.commands.ExecutionEvent; |
| 32 | +import org.eclipse.core.commands.common.CommandException; |
| 33 | +import org.eclipse.core.resources.IProject; |
| 34 | +import org.eclipse.core.resources.ResourcesPlugin; |
28 | 35 | import org.eclipse.core.runtime.CoreException;
|
| 36 | +import org.eclipse.core.runtime.IPath; |
29 | 37 | import org.eclipse.core.runtime.IStatus;
|
30 | 38 | import org.eclipse.core.runtime.Platform;
|
31 | 39 | import org.eclipse.core.runtime.Status;
|
32 | 40 | import org.eclipse.jface.dialogs.MessageDialog;
|
| 41 | +import org.eclipse.jface.viewers.StructuredSelection; |
33 | 42 | import org.eclipse.jface.wizard.WizardDialog;
|
34 | 43 | import org.eclipse.osgi.util.NLS;
|
35 | 44 | import org.eclipse.swt.SWT;
|
|
38 | 47 | import org.eclipse.swt.widgets.Listener;
|
39 | 48 | import org.eclipse.swt.widgets.Shell;
|
40 | 49 | import org.eclipse.ui.IEditorPart;
|
| 50 | +import org.eclipse.ui.IViewPart; |
| 51 | +import org.eclipse.ui.IWorkbenchCommandConstants; |
41 | 52 | import org.eclipse.ui.IWorkbenchPage;
|
42 | 53 | import org.eclipse.ui.IWorkbenchWindow;
|
43 | 54 | import org.eclipse.ui.PartInitException;
|
44 | 55 | import org.eclipse.ui.PlatformUI;
|
| 56 | +import org.eclipse.ui.WorkbenchException; |
| 57 | +import org.eclipse.ui.commands.ICommandService; |
45 | 58 | import org.eclipse.ui.ide.IDE;
|
46 | 59 | import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
|
47 | 60 | import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
|
48 | 61 | import org.eclipse.ui.internal.wizards.datatransfer.SmartImportWizard;
|
| 62 | +import org.eclipse.ui.navigator.resources.ProjectExplorer; |
| 63 | +import org.eclipse.ui.part.ISetSelectionTarget; |
49 | 64 | import org.eclipse.urischeme.IUriSchemeProcessor;
|
50 | 65 | import org.osgi.framework.Bundle;
|
51 | 66 |
|
@@ -183,90 +198,119 @@ public void run() {
|
183 | 198 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
|
184 | 199 | if (window == null)
|
185 | 200 | return;
|
186 |
| - // System.err.println(System.currentTimeMillis()); |
187 | 201 | FileLocationDetails details = FileLocationDetails.resolve(initialPath);
|
188 | 202 | if (details == null || !details.fileInfo.exists()) {
|
189 | 203 | String msg = NLS.bind(IDEWorkbenchMessages.OpenDelayedFileAction_message_fileNotFound, initialPath);
|
190 | 204 | MessageDialog.open(MessageDialog.ERROR, window.getShell(),
|
191 | 205 | IDEWorkbenchMessages.OpenDelayedFileAction_title, msg, SWT.SHEET);
|
192 |
| - } else if (details.fileInfo.isDirectory()) { |
193 |
| - SmartImportWizard wizard = new SmartImportWizard(); |
194 |
| - wizard.setInitialImportSource(new File(details.fileStore.toURI())); |
195 |
| - WizardDialog dialog = new WizardDialog(window.getShell(), wizard); |
196 |
| - dialog.setBlockOnOpen(false); |
197 |
| - dialog.open(); |
198 |
| - } else { |
199 |
| - IWorkbenchPage page = window.getActivePage(); |
200 |
| - if (page == null) { |
201 |
| - String msg = NLS.bind(IDEWorkbenchMessages.OpenDelayedFileAction_message_noWindow, |
202 |
| - details.path); |
203 |
| - MessageDialog.open(MessageDialog.ERROR, window.getShell(), |
204 |
| - IDEWorkbenchMessages.OpenDelayedFileAction_title, msg, SWT.SHEET); |
205 |
| - } |
206 |
| - try { |
207 |
| - IEditorPart openEditor = IDE.openInternalEditorOnFileStore(page, details.fileStore); |
208 |
| - Shell shell = window.getShell(); |
209 |
| - if (shell != null) { |
210 |
| - if (shell.getMinimized()) |
211 |
| - shell.setMinimized(false); |
212 |
| - shell.forceActive(); |
213 |
| - } |
214 |
| - |
215 |
| - if (details.line >= 1) { |
216 |
| - try { |
| 206 | + return; |
| 207 | + } |
| 208 | + IWorkbenchPage page = window.getActivePage(); |
| 209 | + if (page == null) { |
| 210 | + String msg = NLS.bind(IDEWorkbenchMessages.OpenDelayedFileAction_message_noWindow, |
| 211 | + details.path); |
| 212 | + MessageDialog.open(MessageDialog.ERROR, window.getShell(), |
| 213 | + IDEWorkbenchMessages.OpenDelayedFileAction_title, msg, SWT.SHEET); |
| 214 | + return; |
| 215 | + } |
| 216 | + Shell shell = window.getShell(); |
| 217 | + if (shell != null) { |
| 218 | + if (shell.getMinimized()) |
| 219 | + shell.setMinimized(false); |
| 220 | + shell.forceActive(); |
| 221 | + } |
| 222 | + try { |
| 223 | + if (details.fileInfo.isDirectory()) { |
| 224 | + // Open a directory |
| 225 | + File directory = new File(details.fileStore.toURI()); |
| 226 | + for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) { |
| 227 | + IPath location = project.getLocation(); |
| 228 | + if (location != null && directory.equals(location.toFile())) { |
217 | 229 | /*
|
218 |
| - * Do things with reflection to avoid having to rely on the text editor |
219 |
| - * plug-ins. |
| 230 | + * The directory is the root of an existing project. |
| 231 | + * Select in Project Explorer. |
| 232 | + * If open, then refresh, else open. |
220 | 233 | */
|
221 |
| - Bundle textEditorBundle = Platform.getBundle(TEXTEDITOR_BUNDLE_NAME); |
222 |
| - if (textEditorBundle != null) { |
223 |
| - Class<?> textEditorClass = textEditorBundle.loadClass(TEXTEDITOR_CLASS_NAME); |
224 |
| - if (textEditorClass != null) { |
225 |
| - Object textEditor = invoke(openEditor, "getAdapter", //$NON-NLS-1$ |
226 |
| - new Class[] { Class.class }, new Object[] { textEditorClass }); |
227 |
| - if (textEditor != null) { |
228 |
| - openEditor = (IEditorPart) textEditor; |
229 |
| - } |
| 234 | + IViewPart view = page.showView(ProjectExplorer.VIEW_ID); |
| 235 | + if (view instanceof ISetSelectionTarget setSelectionTarget) { |
| 236 | + setSelectionTarget.selectReveal(new StructuredSelection(project)); |
| 237 | + ICommandService commandService = view.getViewSite().getService(ICommandService.class); |
| 238 | + Command command = commandService.getCommand(project.isOpen() ? |
| 239 | + IWorkbenchCommandConstants.FILE_REFRESH : |
| 240 | + IWorkbenchCommandConstants.PROJECT_OPEN_PROJECT); |
| 241 | + if (command != null && command.isHandled() && command.isEnabled()) { |
| 242 | + command.executeWithChecks(new ExecutionEvent()); |
230 | 243 | }
|
231 | 244 | }
|
| 245 | + return; |
| 246 | + } |
| 247 | + } |
| 248 | + // Fall back to import |
| 249 | + SmartImportWizard wizard = new SmartImportWizard(); |
| 250 | + wizard.setInitialImportSource(directory); |
| 251 | + WizardDialog dialog = new WizardDialog(shell, wizard); |
| 252 | + dialog.setBlockOnOpen(false); |
| 253 | + dialog.open(); |
| 254 | + } else { |
| 255 | + // Open a file |
| 256 | + IEditorPart openEditor = IDE.openInternalEditorOnFileStore(page, details.fileStore); |
| 257 | + if (details.line < 1) { |
| 258 | + // There is no need to jump to a line |
| 259 | + return; |
| 260 | + } |
| 261 | + try { |
| 262 | + /* |
| 263 | + * Do things with reflection to avoid having to rely on the text editor |
| 264 | + * plug-ins. |
| 265 | + */ |
| 266 | + Bundle textEditorBundle = Platform.getBundle(TEXTEDITOR_BUNDLE_NAME); |
| 267 | + if (textEditorBundle != null) { |
| 268 | + Class<?> textEditorClass = textEditorBundle.loadClass(TEXTEDITOR_CLASS_NAME); |
| 269 | + if (textEditorClass != null) { |
| 270 | + Object textEditor = invoke(openEditor, "getAdapter", //$NON-NLS-1$ |
| 271 | + new Class[] { Class.class }, new Object[] { textEditorClass }); |
| 272 | + if (textEditor != null) { |
| 273 | + openEditor = (IEditorPart) textEditor; |
| 274 | + } |
| 275 | + } |
| 276 | + } |
232 | 277 |
|
233 |
| - Object documentProvider = invoke(openEditor, "getDocumentProvider"); //$NON-NLS-1$ |
234 |
| - |
235 |
| - Object editorInput = invoke(openEditor, "getEditorInput"); //$NON-NLS-1$ |
| 278 | + Object documentProvider = invoke(openEditor, "getDocumentProvider"); //$NON-NLS-1$ |
236 | 279 |
|
237 |
| - Object document = invoke(documentProvider, "getDocument", new Class[] { Object.class }, //$NON-NLS-1$ |
238 |
| - new Object[] { editorInput }); |
| 280 | + Object editorInput = invoke(openEditor, "getEditorInput"); //$NON-NLS-1$ |
239 | 281 |
|
240 |
| - int numberOfLines = (Integer) invoke(document, "getNumberOfLines"); //$NON-NLS-1$ |
241 |
| - if (details.line > numberOfLines) { |
242 |
| - details.line = numberOfLines; |
243 |
| - } |
244 |
| - int lineLength = (Integer) invoke(document, "getLineLength", new Class[] { int.class }, //$NON-NLS-1$ |
245 |
| - new Object[] { details.line - 1 }); |
246 |
| - if (details.column > lineLength) { |
247 |
| - details.column = lineLength; |
248 |
| - } |
249 |
| - if (details.column < 1) { |
250 |
| - details.column = 1; |
251 |
| - } |
252 |
| - int offset = (Integer) invoke(document, "getLineOffset", new Class[] { int.class }, //$NON-NLS-1$ |
253 |
| - new Object[] { (details.line - 1) }); |
254 |
| - offset += (details.column - 1); |
| 282 | + Object document = invoke(documentProvider, "getDocument", new Class[] { Object.class }, //$NON-NLS-1$ |
| 283 | + new Object[] { editorInput }); |
255 | 284 |
|
256 |
| - invoke(openEditor, "selectAndReveal", new Class[] { int.class, int.class }, //$NON-NLS-1$ |
257 |
| - new Object[] { offset, 0 }); |
258 |
| - } catch (Exception e) { |
259 |
| - // Ignore (not an ITextEditor nor adaptable to one). |
| 285 | + int numberOfLines = (Integer) invoke(document, "getNumberOfLines"); //$NON-NLS-1$ |
| 286 | + if (details.line > numberOfLines) { |
| 287 | + details.line = numberOfLines; |
| 288 | + } |
| 289 | + int lineLength = (Integer) invoke(document, "getLineLength", new Class[] { int.class }, //$NON-NLS-1$ |
| 290 | + new Object[] { details.line - 1 }); |
| 291 | + if (details.column > lineLength) { |
| 292 | + details.column = lineLength; |
| 293 | + } |
| 294 | + if (details.column < 1) { |
| 295 | + details.column = 1; |
260 | 296 | }
|
| 297 | + int offset = (Integer) invoke(document, "getLineOffset", new Class[] { int.class }, //$NON-NLS-1$ |
| 298 | + new Object[] { (details.line - 1) }); |
| 299 | + offset += (details.column - 1); |
| 300 | + |
| 301 | + invoke(openEditor, "selectAndReveal", new Class[] { int.class, int.class }, //$NON-NLS-1$ |
| 302 | + new Object[] { offset, 0 }); |
| 303 | + } catch (Exception e) { |
| 304 | + // Ignore (not an ITextEditor nor adaptable to one). |
261 | 305 | }
|
262 |
| - } catch (PartInitException e) { |
263 |
| - String msg = NLS.bind(IDEWorkbenchMessages.OpenDelayedFileAction_message_errorOnOpen, |
264 |
| - details.fileStore.getName()); |
265 |
| - CoreException eLog = new PartInitException(e.getMessage()); |
266 |
| - IDEWorkbenchPlugin.log(msg, new Status(IStatus.ERROR, IDEApplication.PLUGIN_ID, msg, eLog)); |
267 |
| - MessageDialog.open(MessageDialog.ERROR, window.getShell(), |
268 |
| - IDEWorkbenchMessages.OpenDelayedFileAction_title, msg, SWT.SHEET); |
269 | 306 | }
|
| 307 | + } catch (CommandException | PartInitException e) { |
| 308 | + String msg = NLS.bind(IDEWorkbenchMessages.OpenDelayedFileAction_message_errorOnOpen, |
| 309 | + details.fileStore.getName()); |
| 310 | + WorkbenchException eLog = new WorkbenchException(e.getMessage()); |
| 311 | + IDEWorkbenchPlugin.log(msg, new Status(IStatus.ERROR, IDEApplication.PLUGIN_ID, msg, eLog)); |
| 312 | + MessageDialog.open(MessageDialog.ERROR, window.getShell(), |
| 313 | + IDEWorkbenchMessages.OpenDelayedFileAction_title, msg, SWT.SHEET); |
270 | 314 | }
|
271 | 315 | }
|
272 | 316 |
|
|
0 commit comments