Search in sources :

Example 1 with TypedElementSelectionValidator

use of org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator 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)

Aggregations

IResource (org.eclipse.core.resources.IResource)1 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)1 IPath (org.eclipse.core.runtime.IPath)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 ElementTreeSelectionDialog (org.eclipse.ui.dialogs.ElementTreeSelectionDialog)1 WizardNewFileCreationPage (org.eclipse.ui.dialogs.WizardNewFileCreationPage)1 WorkbenchContentProvider (org.eclipse.ui.model.WorkbenchContentProvider)1 WorkbenchLabelProvider (org.eclipse.ui.model.WorkbenchLabelProvider)1 ResourceComparator (org.eclipse.ui.views.navigator.ResourceComparator)1 FileFilter (org.hibernate.eclipse.console.FileFilter)1 NewConfigurationWizard (org.hibernate.eclipse.console.wizards.NewConfigurationWizard)1 NewConfigurationWizardPage (org.hibernate.eclipse.console.wizards.NewConfigurationWizardPage)1