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;
}
};
}
Aggregations