/* * Copyright (C) 2002-2004 Andrea Mazzolini * andreamazzolini@users.sourceforge.net * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package net.sourceforge.sqlexplorer.plugin.wizards; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import net.sourceforge.sqlexplorer.plugin.SQLExplorerPlugin; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IResource; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.IWorkbenchPartReference; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PartInitException; import org.eclipse.ui.dialogs.WizardNewFileCreationPage; import org.eclipse.ui.ide.IDE; import org.eclipse.ui.part.ISetSelectionTarget; public class NewSQLEditorPage extends WizardNewFileCreationPage { private IWorkbench workbench; /** * @param pageName * @param selection */ public NewSQLEditorPage(IWorkbench iworkbench, IStructuredSelection selection) { super("Create a new empty SQL File", selection); //c = null; setTitle("Create a new empty SQL File"); setDescription("Create a new empty SQL File"); workbench = iworkbench; } public boolean performFinish() { IFile file = createNewFile(); if (file == null) return false; selectAndReveal(file,workbench.getActiveWorkbenchWindow()); // Open editor on new file. IWorkbenchWindow dw = workbench.getActiveWorkbenchWindow(); try { if (dw != null) { IWorkbenchPage page = dw.getActivePage(); if (page != null) IDE.openEditor(page,file,true); } } catch (PartInitException e) { MessageDialog.openError(Display.getDefault().getActiveShell(), "File Resource Error", e.getMessage()); } return true; } public static void selectAndReveal(IResource resource, IWorkbenchWindow window) { // validate the input if (window == null || resource == null) return; IWorkbenchPage page = window.getActivePage(); if (page == null) return; // get all the view and editor parts List parts = new ArrayList(); IWorkbenchPartReference refs[] = page.getViewReferences(); for (int i = 0; i < refs.length; i++) { IWorkbenchPart part = refs[i].getPart(false); if(part != null) parts.add(part); } refs = page.getEditorReferences(); for (int i = 0; i < refs.length; i++) { if(refs[i].getPart(false) != null) parts.add(refs[i].getPart(false)); } final ISelection selection = new StructuredSelection(resource); Iterator it = parts.iterator(); while (it.hasNext()) { IWorkbenchPart part = (IWorkbenchPart) it.next(); // get the part's ISetSelectionTarget implementation ISetSelectionTarget target = null; if (part instanceof ISetSelectionTarget) target = (ISetSelectionTarget) part; else target = (ISetSelectionTarget) part.getAdapter(ISetSelectionTarget.class); if (target != null) { // select and reveal resource final ISetSelectionTarget finalTarget = target; window.getShell().getDisplay().asyncExec(new Runnable() { public void run() { finalTarget.selectReveal(selection); } }); } } } /* (non-Javadoc) * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite) */ public void createControl(Composite parent) { super.createControl(parent); setFileName("sql_"+SQLExplorerPlugin.getDefault().getEditorSerialNo()+"_.sql"); } }