1 11 package org.eclipse.ui.wizards.newresource; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.jface.resource.ImageDescriptor; 19 import org.eclipse.jface.viewers.ISelection; 20 import org.eclipse.jface.viewers.IStructuredSelection; 21 import org.eclipse.jface.viewers.StructuredSelection; 22 import org.eclipse.jface.wizard.Wizard; 23 import org.eclipse.ui.INewWizard; 24 import org.eclipse.ui.IWorkbench; 25 import org.eclipse.ui.IWorkbenchPage; 26 import org.eclipse.ui.IWorkbenchPart; 27 import org.eclipse.ui.IWorkbenchPartReference; 28 import org.eclipse.ui.IWorkbenchWindow; 29 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 30 import org.eclipse.ui.part.ISetSelectionTarget; 31 32 39 public abstract class BasicNewResourceWizard extends Wizard implements 40 INewWizard { 41 42 45 private IWorkbench workbench; 46 47 50 protected IStructuredSelection selection; 51 52 55 protected BasicNewResourceWizard() { 56 super(); 57 } 58 59 64 public IStructuredSelection getSelection() { 65 return selection; 66 } 67 68 73 public IWorkbench getWorkbench() { 74 return workbench; 75 } 76 77 84 public void init(IWorkbench workbench, IStructuredSelection currentSelection) { 85 this.workbench = workbench; 86 this.selection = currentSelection; 87 88 initializeDefaultPageImageDescriptor(); 89 } 90 91 99 protected void initializeDefaultPageImageDescriptor() { 100 ImageDescriptor desc = IDEWorkbenchPlugin.getIDEImageDescriptor("wizban/new_wiz.png"); setDefaultPageImageDescriptor(desc); 102 } 103 104 110 protected void selectAndReveal(IResource newResource) { 111 selectAndReveal(newResource, getWorkbench().getActiveWorkbenchWindow()); 112 } 113 114 128 public static void selectAndReveal(IResource resource, 129 IWorkbenchWindow window) { 130 if (window == null || resource == null) { 132 return; 133 } 134 IWorkbenchPage page = window.getActivePage(); 135 if (page == null) { 136 return; 137 } 138 139 List parts = new ArrayList (); 141 IWorkbenchPartReference refs[] = page.getViewReferences(); 142 for (int i = 0; i < refs.length; i++) { 143 IWorkbenchPart part = refs[i].getPart(false); 144 if (part != null) { 145 parts.add(part); 146 } 147 } 148 refs = page.getEditorReferences(); 149 for (int i = 0; i < refs.length; i++) { 150 if (refs[i].getPart(false) != null) { 151 parts.add(refs[i].getPart(false)); 152 } 153 } 154 155 final ISelection selection = new StructuredSelection(resource); 156 Iterator itr = parts.iterator(); 157 while (itr.hasNext()) { 158 IWorkbenchPart part = (IWorkbenchPart) itr.next(); 159 160 ISetSelectionTarget target = null; 162 if (part instanceof ISetSelectionTarget) { 163 target = (ISetSelectionTarget) part; 164 } else { 165 target = (ISetSelectionTarget) part 166 .getAdapter(ISetSelectionTarget.class); 167 } 168 169 if (target != null) { 170 final ISetSelectionTarget finalTarget = target; 172 window.getShell().getDisplay().asyncExec(new Runnable () { 173 public void run() { 174 finalTarget.selectReveal(selection); 175 } 176 }); 177 } 178 } 179 } 180 } 181 | Popular Tags |