Search in sources :

Example 1 with FileFilter

use of org.hibernate.eclipse.console.FileFilter in project jbosstools-hibernate by jbosstools.

the class HibernatePropertiesComposite method createSetupAction.

private Runnable createSetupAction() {
    return new Runnable() {

        @Override
        public void run() {
            IPath initialPath = getConfigurationFilePath();
            int defaultChoice = 0;
            if (initialPath != null) {
                defaultChoice = 1;
            }
            MessageDialog dialog = createSetupDialog(HibernateConsoleMessages.ConsoleConfigurationMainTab_setup_configuration_file, HibernateConsoleMessages.ConsoleConfigurationMainTab_do_you_want_to_create_new_cfgxml, defaultChoice);
            int answer = dialog.open();
            IPath cfgFile = null;
            if (answer == 0) {
                // create new
                cfgFile = handleConfigurationFileCreate();
            } else if (answer == 1) {
                // use existing
                cfgFile = handleConfigurationFileBrowse();
            }
            if (cfgFile != null) {
                HibernatePropertiesComposite.this.cfgFile.setText(makeClassPathRelative(cfgFile).toString());
            }
        }

        protected IPath makeClassPathRelative(IPath cfgFile) {
            IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
            IResource res = root.findMember(cfgFile);
            if (res != null && res.exists() && res.getType() == IResource.FILE) {
                IPackageFragmentRoot[] allPackageFragmentRoots = getSourcePackageFragmentRoots();
                for (IPackageFragmentRoot iPackageFragmentRoot : allPackageFragmentRoots) {
                    if (iPackageFragmentRoot.getResource().getFullPath().isPrefixOf(cfgFile)) {
                        cfgFile = cfgFile.removeFirstSegments(iPackageFragmentRoot.getResource().getFullPath().segmentCount());
                        return cfgFile;
                    }
                }
            }
            return res.getLocation();
        }

        private MessageDialog createSetupDialog(String title, String question, int defaultChoice) {
            return new MessageDialog(getShell(), title, null, question, MessageDialog.QUESTION, new String[] { HibernateConsoleMessages.ConsoleConfigurationMainTab_create_new, HibernateConsoleMessages.ConsoleConfigurationMainTab_use_existing, IDialogConstants.CANCEL_LABEL }, defaultChoice);
        }

        private IPath handleConfigurationFileBrowse() {
            IPath[] paths = chooseFileEntries();
            if (paths != null && paths.length == 1) {
                return paths[0];
            }
            return null;
        }

        public IPath[] chooseFileEntries() {
            TypedElementSelectionValidator validator = new TypedElementSelectionValidator(new Class[] { IFile.class }, false);
            IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
            IResource focus = getConfigurationFilePath() != null ? root.findMember(getConfigurationFilePath()) : null;
            ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(getShell(), new WorkbenchLabelProvider(), new WorkbenchContentProvider() {

                public Object[] getElements(Object element) {
                    IPackageFragmentRoot[] sourcePackageFragmentRoots = getSourcePackageFragmentRoots();
                    IResource[] ress = new IResource[sourcePackageFragmentRoots.length];
                    for (int i = 0; i < sourcePackageFragmentRoots.length; i++) {
                        ress[i] = sourcePackageFragmentRoots[i].getResource();
                    }
                    return ress;
                }
            });
            dialog.setValidator(validator);
            dialog.setAllowMultiple(false);
            dialog.setTitle(HibernateConsoleMessages.ConsoleConfigurationMainTab_select_hibernate_cfg_xml_file);
            dialog.setMessage(HibernateConsoleMessages.ConsoleConfigurationMainTab_choose_file_to_use_as_hibernate_cfg_xml);
            dialog.addFilter(new FileFilter(new String[] { HibernateConsoleMessages.ConsoleConfigurationMainTab_cfg_xml }, null, true, false));
            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;
        }

        private IPath handleConfigurationFileCreate() {
            NewConfigurationWizard wizard = new NewConfigurationWizard();
            wizard.init(PlatformUI.getWorkbench(), StructuredSelection.EMPTY);
            IWorkbenchWindow win = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
            WizardDialog wdialog = new WizardDialog(win.getShell(), wizard);
            wdialog.create();
            IWizardPage configPage = wizard.getPage(HibernateConsoleMessages.ConsoleConfigurationMainTab_wizard_page);
            if (configPage != null && configPage instanceof NewConfigurationWizardPage) {
                ((NewConfigurationWizardPage) configPage).setCreateConsoleConfigurationVisible(false);
            }
            // This opens a dialog
            if (wdialog.open() == Window.OK) {
                WizardNewFileCreationPage createdFilePath = ((WizardNewFileCreationPage) wizard.getStartingPage());
                if (createdFilePath != null) {
                    // createNewFile() does not creates new file if it was created by wizard (OK was pressed)
                    return createdFilePath.createNewFile().getFullPath();
                }
            }
            return null;
        }
    };
}
Also used : ResourceComparator(org.eclipse.ui.views.navigator.ResourceComparator) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) WorkbenchLabelProvider(org.eclipse.ui.model.WorkbenchLabelProvider) IPath(org.eclipse.core.runtime.IPath) NewConfigurationWizard(org.hibernate.eclipse.console.wizards.NewConfigurationWizard) WizardNewFileCreationPage(org.eclipse.ui.dialogs.WizardNewFileCreationPage) WorkbenchContentProvider(org.eclipse.ui.model.WorkbenchContentProvider) TypedElementSelectionValidator(org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) ElementTreeSelectionDialog(org.eclipse.ui.dialogs.ElementTreeSelectionDialog) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) NewConfigurationWizardPage(org.hibernate.eclipse.console.wizards.NewConfigurationWizardPage) IWizardPage(org.eclipse.jface.wizard.IWizardPage) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) FileFilter(org.hibernate.eclipse.console.FileFilter) WizardDialog(org.eclipse.jface.wizard.WizardDialog) IResource(org.eclipse.core.resources.IResource)

Example 2 with FileFilter

use of org.hibernate.eclipse.console.FileFilter 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)

Aggregations

IResource (org.eclipse.core.resources.IResource)2 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)2 IPath (org.eclipse.core.runtime.IPath)2 ElementTreeSelectionDialog (org.eclipse.ui.dialogs.ElementTreeSelectionDialog)2 WorkbenchContentProvider (org.eclipse.ui.model.WorkbenchContentProvider)2 WorkbenchLabelProvider (org.eclipse.ui.model.WorkbenchLabelProvider)2 ResourceComparator (org.eclipse.ui.views.navigator.ResourceComparator)2 FileFilter (org.hibernate.eclipse.console.FileFilter)2 ArrayList (java.util.ArrayList)1 IFile (org.eclipse.core.resources.IFile)1 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)1 TypedElementSelectionValidator (org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator)1 MessageDialog (org.eclipse.jface.dialogs.MessageDialog)1 IWizardPage (org.eclipse.jface.wizard.IWizardPage)1 WizardDialog (org.eclipse.jface.wizard.WizardDialog)1 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)1 WizardNewFileCreationPage (org.eclipse.ui.dialogs.WizardNewFileCreationPage)1 NewConfigurationWizard (org.hibernate.eclipse.console.wizards.NewConfigurationWizard)1 NewConfigurationWizardPage (org.hibernate.eclipse.console.wizards.NewConfigurationWizardPage)1