Skip to content

Commit 35a5e7b

Browse files
yuzawa-sanmickaelistria
authored andcommitted
Opening a directory shows existing projects in Project Explorer
The existing behavior is to always run the import wizard, but this is not necessary if the directory being opened is the root of an existing project. In that case, the project will be shown in the Project Explorer. If the project is open, it will be refreshed. If the project is closed, it will be opened.
1 parent d6dc40c commit 35a5e7b

File tree

2 files changed

+114
-70
lines changed

2 files changed

+114
-70
lines changed

bundles/org.eclipse.ui.ide.application/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %Plugin.name
44
Bundle-SymbolicName: org.eclipse.ui.ide.application;singleton:=true
5-
Bundle-Version: 1.5.100.qualifier
5+
Bundle-Version: 1.5.200.qualifier
66
Bundle-Vendor: %Plugin.providerName
77
Bundle-Localization: plugin
88
Require-Bundle: org.eclipse.ui.ide;bundle-version="[3.21.0,4.0.0)",

bundles/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/DelayedEventsProcessor.java

Lines changed: 113 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2010, 2021 IBM Corporation and others.
2+
* Copyright (c) 2010, 2021, 2023 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -14,6 +14,8 @@
1414
* at a specific line/col
1515
* Nitin Dahyabhai - Bug 567708 - Support for MultiPageEditorParts
1616
* containing a TextEditor page
17+
* James Yuzawa - 'Open Directory' action shows existing projects in
18+
* Project Explorer
1719
******************************************************************************/
1820

1921
package org.eclipse.ui.internal.ide.application;
@@ -25,11 +27,18 @@
2527
import java.net.URISyntaxException;
2628
import java.util.ArrayList;
2729

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;
2835
import org.eclipse.core.runtime.CoreException;
36+
import org.eclipse.core.runtime.IPath;
2937
import org.eclipse.core.runtime.IStatus;
3038
import org.eclipse.core.runtime.Platform;
3139
import org.eclipse.core.runtime.Status;
3240
import org.eclipse.jface.dialogs.MessageDialog;
41+
import org.eclipse.jface.viewers.StructuredSelection;
3342
import org.eclipse.jface.wizard.WizardDialog;
3443
import org.eclipse.osgi.util.NLS;
3544
import org.eclipse.swt.SWT;
@@ -38,14 +47,20 @@
3847
import org.eclipse.swt.widgets.Listener;
3948
import org.eclipse.swt.widgets.Shell;
4049
import org.eclipse.ui.IEditorPart;
50+
import org.eclipse.ui.IViewPart;
51+
import org.eclipse.ui.IWorkbenchCommandConstants;
4152
import org.eclipse.ui.IWorkbenchPage;
4253
import org.eclipse.ui.IWorkbenchWindow;
4354
import org.eclipse.ui.PartInitException;
4455
import org.eclipse.ui.PlatformUI;
56+
import org.eclipse.ui.WorkbenchException;
57+
import org.eclipse.ui.commands.ICommandService;
4558
import org.eclipse.ui.ide.IDE;
4659
import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
4760
import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
4861
import org.eclipse.ui.internal.wizards.datatransfer.SmartImportWizard;
62+
import org.eclipse.ui.navigator.resources.ProjectExplorer;
63+
import org.eclipse.ui.part.ISetSelectionTarget;
4964
import org.eclipse.urischeme.IUriSchemeProcessor;
5065
import org.osgi.framework.Bundle;
5166

@@ -183,90 +198,119 @@ public void run() {
183198
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
184199
if (window == null)
185200
return;
186-
// System.err.println(System.currentTimeMillis());
187201
FileLocationDetails details = FileLocationDetails.resolve(initialPath);
188202
if (details == null || !details.fileInfo.exists()) {
189203
String msg = NLS.bind(IDEWorkbenchMessages.OpenDelayedFileAction_message_fileNotFound, initialPath);
190204
MessageDialog.open(MessageDialog.ERROR, window.getShell(),
191205
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())) {
217229
/*
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.
220233
*/
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());
230243
}
231244
}
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+
}
232277

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$
236279

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$
239281

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 });
255284

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;
260296
}
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).
261305
}
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);
269306
}
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);
270314
}
271315
}
272316

0 commit comments

Comments
 (0)