Search in sources :

Example 51 with WorkbenchContentProvider

use of org.eclipse.ui.model.WorkbenchContentProvider in project jbosstools-hibernate by jbosstools.

the class DialogSelectionHelper method chooseFileEntries.

/**
 * Shows the UI to select new JAR or ZIP archive entries located in the workspace.
 * The dialog returns the selected entries or <code>null</code> if the dialog has
 * been cancelled. The dialog does not apply any changes.
 * @param shell The parent shell for the dialog.
 * @param initialSelection The path of the element (container or archive) to initially select or <code>null</code> to not select an entry.
 * @param usedEntries An array of paths that are already on the classpath and therefore should not be
 * selected again.
 * @param fileExtensions An array of file extensions.
 * @param allowMultiple allow multiple selections.
 * @param allowFiles TODO
 * @param acceptedTypes TODO
 *
 * @return Returns the new classpath container entry paths or <code>null</code> if the dialog has
 * been cancelled by the user.
 *
 * Inspired by BuildPathDialogAccess.chooseJAREntries from jdt.ui.wizards
 */
public static IPath[] chooseFileEntries(Shell shell, IPath initialSelection, IPath[] usedEntries, String title, String description, String[] fileExtensions, boolean allowMultiple, boolean allowDirectories, boolean allowFiles) {
    if (usedEntries == null) {
        throw new IllegalArgumentException(HibernateConsoleMessages.DialogSelectionHelper_used_entries_must_be_notnull);
    }
    List<Class<?>> clazzes = new ArrayList<Class<?>>();
    if (allowDirectories) {
        clazzes.add(IFolder.class);
        clazzes.add(IProject.class);
    }
    if (allowFiles) {
        clazzes.add(IFile.class);
    }
    Class<?>[] acceptedClasses = clazzes.toArray(new Class[clazzes.size()]);
    TypedElementSelectionValidator validator = new TypedElementSelectionValidator(acceptedClasses, true);
    List<IResource> usedFiles = new ArrayList<IResource>(usedEntries.length);
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    for (int i = 0; i < usedEntries.length; i++) {
        IResource resource = root.findMember(usedEntries[i]);
        if (resource instanceof IFile) {
            usedFiles.add(resource);
        }
    }
    IResource focus = initialSelection != null ? root.findMember(initialSelection) : null;
    ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(shell, new WorkbenchLabelProvider(), new WorkbenchContentProvider());
    dialog.setValidator(validator);
    dialog.setAllowMultiple(allowMultiple);
    dialog.setTitle(title);
    dialog.setMessage(description);
    dialog.addFilter(new FileFilter(fileExtensions, usedFiles, true, allowDirectories));
    dialog.setInput(root);
    dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
    dialog.setInitialSelection(focus);
    if (dialog.open() == Window.OK) {
        Object[] elements = dialog.getResult();
        IPath[] res = new IPath[elements.length];
        for (int i = 0; i < res.length; i++) {
            IResource elem = (IResource) elements[i];
            res[i] = elem.getFullPath();
        }
        return res;
    }
    return null;
}
Also used : ResourceComparator(org.eclipse.ui.views.navigator.ResourceComparator) WorkbenchLabelProvider(org.eclipse.ui.model.WorkbenchLabelProvider) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) WorkbenchContentProvider(org.eclipse.ui.model.WorkbenchContentProvider) ElementTreeSelectionDialog(org.eclipse.ui.dialogs.ElementTreeSelectionDialog) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) FileFilter(org.hibernate.eclipse.console.FileFilter) IResource(org.eclipse.core.resources.IResource)

Example 52 with WorkbenchContentProvider

use of org.eclipse.ui.model.WorkbenchContentProvider in project jbosstools-openshift by jbosstools.

the class ServerSettingsWizardPage method createWorkspaceFolderDialog.

private ElementTreeSelectionDialog createWorkspaceFolderDialog(Shell shell, String selectedFile) {
    ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(shell, new WorkbenchLabelProvider(), new WorkbenchContentProvider());
    dialog.setTitle("Select a workspace folder");
    dialog.setMessage("Select a workspace folder to deploy");
    dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
    dialog.addFilter(new ViewerFilter() {

        @Override
        public boolean select(Viewer viewer, Object parentElement, Object element) {
            if (!(element instanceof IContainer)) {
                return false;
            }
            IContainer container = (IContainer) element;
            return container.isAccessible() && !ProjectUtils.isInternalPde(container.getName()) && !ProjectUtils.isInternalRSE(container.getName());
        }
    });
    dialog.setAllowMultiple(false);
    org.eclipse.core.resources.IResource res = model.getDeployProject();
    if (org.apache.commons.lang.StringUtils.isNotBlank(selectedFile)) {
        String path = VariablesHelper.getWorkspacePath(selectedFile);
        org.eclipse.core.resources.IResource member = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
        if (member != null) {
            res = member;
        }
    }
    if (res != null) {
        dialog.setInitialSelection(res);
    }
    return dialog;
}
Also used : ElementTreeSelectionDialog(org.eclipse.ui.dialogs.ElementTreeSelectionDialog) WorkbenchLabelProvider(org.eclipse.ui.model.WorkbenchLabelProvider) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) TreeViewer(org.eclipse.jface.viewers.TreeViewer) ComboViewer(org.eclipse.jface.viewers.ComboViewer) StructuredViewer(org.eclipse.jface.viewers.StructuredViewer) Viewer(org.eclipse.jface.viewers.Viewer) WorkbenchContentProvider(org.eclipse.ui.model.WorkbenchContentProvider) IContainer(org.eclipse.core.resources.IContainer)

Example 53 with WorkbenchContentProvider

use of org.eclipse.ui.model.WorkbenchContentProvider in project jbosstools-openshift by jbosstools.

the class UIUtils method createFileDialog.

/**
 * Create a file selection dialog.
 *
 * @param selectedFile the initial selected file. May be null
 * @param title the title of the dialog
 * @param message the message of the dialog
 * @param extension the extension to filter. If null or empty, all files are accepted
 * @param initialSelection the initial selection resource (used if selectedFile is null)
 * @return the dialog
 */
public static ElementTreeSelectionDialog createFileDialog(String selectedFile, String title, String message, String extension, IResource initialSelection) {
    ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(getShell(), new WorkbenchLabelProvider(), new WorkbenchContentProvider());
    dialog.setTitle(title);
    dialog.setMessage(message);
    dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
    dialog.addFilter(new ViewerFilter() {

        @Override
        public boolean select(Viewer viewer, Object parentElement, Object element) {
            return element instanceof IContainer || (element instanceof IFile && (StringUtils.isEmpty(extension) || extension.equals(((IFile) element).getFileExtension())));
        }
    });
    dialog.setAllowMultiple(false);
    if (StringUtils.isNotBlank(selectedFile)) {
        String prefix = "${workspace_loc:";
        String path = selectedFile;
        if (selectedFile.startsWith(prefix) && selectedFile.endsWith("}")) {
            path = path.substring(prefix.length(), path.length() - 1);
        }
        initialSelection = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
    }
    if (initialSelection != null) {
        dialog.setInitialSelection(initialSelection);
    }
    return dialog;
}
Also used : ElementTreeSelectionDialog(org.eclipse.ui.dialogs.ElementTreeSelectionDialog) WorkbenchLabelProvider(org.eclipse.ui.model.WorkbenchLabelProvider) IFile(org.eclipse.core.resources.IFile) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) Viewer(org.eclipse.jface.viewers.Viewer) WorkbenchContentProvider(org.eclipse.ui.model.WorkbenchContentProvider) IContainer(org.eclipse.core.resources.IContainer)

Aggregations

WorkbenchContentProvider (org.eclipse.ui.model.WorkbenchContentProvider)53 WorkbenchLabelProvider (org.eclipse.ui.model.WorkbenchLabelProvider)42 ElementTreeSelectionDialog (org.eclipse.ui.dialogs.ElementTreeSelectionDialog)32 IResource (org.eclipse.core.resources.IResource)27 IFile (org.eclipse.core.resources.IFile)18 ResourceComparator (org.eclipse.ui.views.navigator.ResourceComparator)18 ViewerFilter (org.eclipse.jface.viewers.ViewerFilter)16 IContainer (org.eclipse.core.resources.IContainer)15 IPath (org.eclipse.core.runtime.IPath)14 ArrayList (java.util.ArrayList)12 IStatus (org.eclipse.core.runtime.IStatus)12 ILabelProvider (org.eclipse.jface.viewers.ILabelProvider)12 ITreeContentProvider (org.eclipse.jface.viewers.ITreeContentProvider)11 ISelectionStatusValidator (org.eclipse.ui.dialogs.ISelectionStatusValidator)11 CoreException (org.eclipse.core.runtime.CoreException)10 Status (org.eclipse.core.runtime.Status)10 Composite (org.eclipse.swt.widgets.Composite)10 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)9 GridData (org.eclipse.swt.layout.GridData)9 Button (org.eclipse.swt.widgets.Button)9