/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.utils;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourceAttributes;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWizard;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.wizards.IWizardDescriptor;
import de.innovationgate.eclipse.utils.ui.SingleStructuredSelection;
public class WorkbenchUtils {
/*
@SuppressWarnings("unchecked")
public static List<IEditorPart> findOpenEditors(IWorkbench workbench, Class type) {
List<IEditorPart> editors = new ArrayList<IEditorPart>();
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
IWorkbenchPage[] workbenchPages = window.getPages();
for (int i = 0; i < workbenchPages.length; i++) {
IWorkbenchPage page = workbenchPages[i];
IEditorReference[] references = page.getEditorReferences();
for (int j = 0; j < references.length; j++) {
IEditorReference reference = references[j];
IEditorPart editorPart = reference.getEditor(false);
if (editorPart != null && type.isAssignableFrom(editorPart.getClass())) {
editors.add(editorPart);
}
}
}
return editors;
}*/
/**
* find open editors of given class with editor input is given file
* @param workbench
* @param type
* @param file
* @return
*/
/*
public static List<IEditorPart> findOpenEditors(IWorkbench workbench, Class type, IFile file) {
Iterator<IEditorPart> editors = findOpenEditors(workbench, type).iterator();
List<IEditorPart> filteredEditors = new ArrayList<IEditorPart>();
while (editors.hasNext()) {
IEditorPart editorPart = editors.next();
if (editorPart.getEditorInput() instanceof FileEditorInput && editorPart.getEditorInput() != null) {
if (((FileEditorInput) editorPart.getEditorInput()).getFile().equals(file)) {
filteredEditors.add(editorPart);
}
}
}
return filteredEditors;
}*/
@SuppressWarnings("unchecked")
public static List<IEditorPart> findOpenEditors(IWorkbench workbench, String id) {
List<IEditorPart> editors = new ArrayList<IEditorPart>();
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
IWorkbenchPage[] workbenchPages = window.getPages();
for (int i = 0; i < workbenchPages.length; i++) {
IWorkbenchPage page = workbenchPages[i];
IEditorReference[] references = page.getEditorReferences();
for (int j = 0; j < references.length; j++) {
IEditorReference reference = references[j];
IEditorPart editorPart = reference.getEditor(false);
if (editorPart != null && reference.getId().equals(id)) {
editors.add(editorPart);
}
}
}
return editors;
}
/**
* find open editors of given id where and input is given file
* @param workbench
* @param id
* @param file
* @return
*/
public static List<IEditorPart> findOpenEditors(IWorkbench workbench, String id, IFile file) {
Iterator<IEditorPart> editors = findOpenEditors(workbench, id).iterator();
List<IEditorPart> filteredEditors = new ArrayList<IEditorPart>();
while (editors.hasNext()) {
IEditorPart editorPart = editors.next();
if (editorPart.getEditorInput() instanceof FileEditorInput && editorPart.getEditorInput() != null) {
if (((FileEditorInput) editorPart.getEditorInput()).getFile().equals(file)) {
filteredEditors.add(editorPart);
}
}
}
return filteredEditors;
}
/**
* find open editors of given id where input file is a children of the given container
* @param workbench
* @param id
* @param file
* @return
*/
public static List<IEditorPart> findOpenEditors(IWorkbench workbench, String id, IContainer container) {
Iterator<IEditorPart> editors = findOpenEditors(workbench, id).iterator();
List<IEditorPart> filteredEditors = new ArrayList<IEditorPart>();
while (editors.hasNext()) {
IEditorPart editorPart = editors.next();
if (editorPart.getEditorInput() instanceof FileEditorInput && editorPart.getEditorInput() != null) {
IFile inputFile = ((FileEditorInput) editorPart.getEditorInput()).getFile();
if (container.getLocation().isPrefixOf(inputFile.getLocation())) {
filteredEditors.add(editorPart);
}
}
}
return filteredEditors;
}
public static IEditorPart openEditor(IWorkbench workbench, IFile file, String editorID) throws PartInitException {
IEditorInput editorInput = new FileEditorInput(file);
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
return page.openEditor(editorInput, editorID);
}
public static IEditorPart openEditor(IWorkbench workbench, IFile file) throws PartInitException {
IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(file.getName());
if (desc != null) {
return openEditor(workbench, file, desc.getId());
} else {
return openEditor(workbench, file, org.eclipse.ui.editors.text.EditorsUI.DEFAULT_TEXT_EDITOR_ID);
}
}
public static IWorkbenchWizard openWizardOnCurrentEditorInput(IWorkbench workbench, String id) throws CoreException {
IWizardDescriptor wizardDesc = workbench.getExportWizardRegistry().findWizard(id);
if (wizardDesc == null) {
wizardDesc = workbench.getNewWizardRegistry().findWizard(id);
}
if (wizardDesc != null) {
IWorkbenchWizard wizard = wizardDesc.createWizard();
IEditorPart editorPart = workbench.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (editorPart != null && editorPart.getEditorInput() instanceof FileEditorInput) {
FileEditorInput input = (FileEditorInput) editorPart.getEditorInput();
wizard.init(workbench, new SingleStructuredSelection(input.getFile().getParent()));
WizardDialog dialog = new WizardDialog(workbench.getActiveWorkbenchWindow().getShell(), wizard);
dialog.open();
return wizard;
} else {
return null;
}
} else {
throw new IllegalArgumentException("Wizard with id '" + id + "' not found.");
}
}
public static IWorkbenchWizard openWizard(IWorkbench workbench, String id) throws CoreException {
return openWizard(workbench, id, null);
}
public static IWorkbenchWizard openWizard(IWorkbench workbench, String id, IStructuredSelection selection) throws CoreException {
IWizardDescriptor wizardDesc = workbench.getExportWizardRegistry().findWizard(id);
if (wizardDesc == null) {
wizardDesc = workbench.getNewWizardRegistry().findWizard(id);
}
if (wizardDesc != null) {
IWorkbenchWizard wizard = wizardDesc.createWizard();
wizard.init(workbench, selection);
WizardDialog dialog = new WizardDialog(workbench.getActiveWorkbenchWindow().getShell(), wizard);
dialog.open();
return wizard;
} else {
throw new IllegalArgumentException("Wizard with id '" + id + "' not found.");
}
}
public static void addBuilder(IProject project, String id) throws CoreException {
final IProject fProject = project;
final String fId = id;
IWorkspaceRunnable operation = new IWorkspaceRunnable() {
public void run(IProgressMonitor monitor) throws CoreException {
if (fProject.isAccessible()) {
ResourceAttributes attribs = fProject.getResourceAttributes();
if (attribs != null && attribs.isReadOnly()) {
attribs.setReadOnly(false);
fProject.setResourceAttributes(attribs);
}
IProjectDescription desc = fProject.getDescription();
ICommand[] commands = desc.getBuildSpec();
for (int i = 0; i < commands.length; ++i) {
if (commands[i].getBuilderName().equals(fId))
return;
}
//add builder to project
ICommand command = desc.newCommand();
command.setBuilderName(fId);
ICommand[] nc = new ICommand[commands.length + 1];
// Add it after all other builders.
System.arraycopy(commands, 0, nc, 0, commands.length);
nc[commands.length] = command;
desc.setBuildSpec(nc);
fProject.setDescription(desc, null);
}
}
};
ResourcesPlugin.getWorkspace().run(operation, null);
}
public static void removeBuilder(IProject project, String id) throws CoreException {
final IProject fProject = project;
final String fId = id;
IWorkspaceRunnable operation = new IWorkspaceRunnable() {
public void run(IProgressMonitor monitor) throws CoreException {
if (fProject.isAccessible()) {
ResourceAttributes attribs = fProject.getResourceAttributes();
if (attribs != null && attribs.isReadOnly()) {
attribs.setReadOnly(false);
fProject.setResourceAttributes(attribs);
}
IProjectDescription desc = fProject.getDescription();
ICommand[] commands = desc.getBuildSpec();
List<ICommand> newCommands = new ArrayList<ICommand>();
for (int i = 0; i < commands.length; ++i) {
if (!commands[i].getBuilderName().equals(fId)) {
newCommands.add(commands[i]);
}
}
desc.setBuildSpec(newCommands.toArray(new ICommand[0]));
fProject.setDescription(desc, null);
}
}
};
ResourcesPlugin.getWorkspace().run(operation, null);
}
/**
* checks if the given name is a valid resource name
* only alphanumeric ascii characters and '_' , '-' are allowed
* @param name
* @return true/ false
*/
public static boolean isValidResourceName(String name) {
for (int i=0; i < name.length(); i++) {
char c = name.charAt(i);
if (!(c > 127) && (Character.isLetterOrDigit(c) || c == '-' || c == '_')) {
continue;
} else {
return false;
}
}
return true;
}
/**
* sets the given selection in the view defined by viewID
* @param viewID
* @param selection
*/
public static void setSelectionInView(String viewID, ISelection selection) {
IViewPart view = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(viewID);
if (view != null) {
view.getSite().getSelectionProvider().setSelection(selection);
}
}
/**
* sets the given selection in all relevant navigation views
* @param selection
*/
public static void setNavigationViewSelection(ISelection selection) {
WorkbenchUtils.setSelectionInView(JavaUI.ID_PACKAGES, selection);
WorkbenchUtils.setSelectionInView(IPageLayout.ID_RES_NAV, selection);
WorkbenchUtils.setSelectionInView(IPageLayout.ID_PROJECT_EXPLORER, selection);
}
/**
* raise an error dialog with the given informations - the error will not be logged
* @param shell
* @param title
* @param message
* @param e
*/
public static void showErrorDialog(Shell shell, String title, String message, Throwable e) {
showErrorDialog(null, shell, title, message, e);
}
/**
* raise an error dialog with the given informations
* if 'plugin' is given the error is also added to the error log
* @param plugin might be 'null' (display dialog DONT log)
* @param shell
* @param title
* @param message
* @param e
*/
public static void showErrorDialog(Plugin plugin, Shell shell, String title, String message, Throwable e) {
if (plugin != null) {
String statusMessage = message;
if (statusMessage == null) {
statusMessage = title;
}
if (e != null) {
plugin.getLog().log(new Status(Status.ERROR, plugin.getBundle().getSymbolicName(), statusMessage, e));
}
else {
plugin.getLog().log(new Status(Status.ERROR, plugin.getBundle().getSymbolicName(), statusMessage));
}
}
if (shell != null) {
StringBuffer errorMessage = new StringBuffer();
if (message != null) {
errorMessage.append(message);
}
if (e != null && e.getMessage() != null) {
if (message != null) {
errorMessage.append("\n\n");
}
errorMessage.append(e.getMessage());
}
if (plugin != null) {
if (message != null || (e != null && e.getMessage() != null)) {
errorMessage.append("\n\n");
}
errorMessage.append("See error log for more details.");
}
MessageDialog.openError(shell, title, errorMessage.toString());
}
}
public static void showErrorDialog(Shell shell, String title, String message) {
showErrorDialog(shell, title, message, null);
}
public static void showErrorDialog(Shell shell, String title, Throwable e) {
showErrorDialog(shell, title, null, e);
}
public static void showErrorDialog(Plugin plugin, Shell shell, String title, Throwable e) {
showErrorDialog(plugin, shell, title, null, e);
}
public static void openBrowser(final URL url, Display display, final String dialogTitle) {
display.syncExec(new Runnable() {
public void run() {
internalOpenBrowser(url, dialogTitle);
}
});
}
private static void internalOpenBrowser(final URL url, String title) {
BusyIndicator.showWhile(null, new Runnable() {
public void run() {
PlatformUI.getWorkbench().getHelpSystem().displayHelpResource(url.toExternalForm() + "?noframes=true");
}
});
}
}