1 11 package org.eclipse.jdt.internal.ui.refactoring.reorg; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.swt.widgets.Display; 17 18 import org.eclipse.jface.viewers.ISelection; 19 import org.eclipse.jface.viewers.ISelectionProvider; 20 import org.eclipse.jface.viewers.IStructuredSelection; 21 import org.eclipse.jface.viewers.StructuredSelection; 22 import org.eclipse.jface.viewers.TreePath; 23 import org.eclipse.jface.viewers.TreeSelection; 24 25 import org.eclipse.ui.IEditorReference; 26 import org.eclipse.ui.IViewReference; 27 import org.eclipse.ui.IWorkbenchPage; 28 import org.eclipse.ui.IWorkbenchPart; 29 import org.eclipse.ui.IWorkbenchWindow; 30 import org.eclipse.ui.part.ISetSelectionTarget; 31 32 import org.eclipse.jdt.internal.ui.JavaPlugin; 33 34 35 public class RenameSelectionState { 36 private final Display fDisplay; 37 private final Object fElement; 38 private final List fParts; 39 private final List fSelections; 40 41 public RenameSelectionState(Object element) { 42 fElement= element; 43 fParts= new ArrayList (); 44 fSelections= new ArrayList (); 45 46 IWorkbenchWindow dw = JavaPlugin.getActiveWorkbenchWindow(); 47 if (dw == null) { 48 fDisplay= null; 49 return; 50 } 51 fDisplay= dw.getShell().getDisplay(); 52 IWorkbenchPage page = dw.getActivePage(); 53 if (page == null) 54 return; 55 IViewReference vrefs[]= page.getViewReferences(); 56 for(int i= 0; i < vrefs.length; i++) { 57 consider(vrefs[i].getPart(false)); 58 } 59 IEditorReference refs[]= page.getEditorReferences(); 60 for(int i= 0; i < refs.length; i++) { 61 consider(refs[i].getPart(false)); 62 } 63 } 64 65 private void consider(IWorkbenchPart part) { 66 if (part == null) 67 return; 68 ISetSelectionTarget target= null; 69 if (!(part instanceof ISetSelectionTarget)) { 70 target= (ISetSelectionTarget)part.getAdapter(ISetSelectionTarget.class); 71 if (target == null) 72 return; 73 } else { 74 target= (ISetSelectionTarget)part; 75 } 76 ISelectionProvider selectionProvider= part.getSite().getSelectionProvider(); 77 if (selectionProvider == null) 78 return; 79 ISelection s= selectionProvider.getSelection(); 80 if (!(s instanceof IStructuredSelection)) 81 return; 82 IStructuredSelection selection= (IStructuredSelection)s; 83 if (!selection.toList().contains(fElement)) 84 return; 85 fParts.add(part); 86 fSelections.add(selection); 87 } 88 89 public void restore(Object newElement) { 90 if (fDisplay == null) 91 return; 92 for (int i= 0; i < fParts.size(); i++) { 93 IStructuredSelection currentSelection= (IStructuredSelection)fSelections.get(i); 94 boolean changed= false; 95 final ISetSelectionTarget target= (ISetSelectionTarget)fParts.get(i); 96 final IStructuredSelection[] newSelection= new IStructuredSelection[1]; 97 newSelection[0]= currentSelection; 98 if (currentSelection instanceof TreeSelection) { 99 TreeSelection treeSelection= (TreeSelection)currentSelection; 100 TreePath[] paths= treeSelection.getPaths(); 101 for (int p= 0; p < paths.length; p++) { 102 TreePath path= paths[p]; 103 if (path.getSegmentCount() > 0 && path.getLastSegment().equals(fElement)) { 104 paths[p]= createTreePath(path, newElement); 105 changed= true; 106 } 107 } 108 if (changed) { 109 newSelection[0]= new TreeSelection(paths, treeSelection.getElementComparer()); 110 } 111 } else { 112 Object [] elements= currentSelection.toArray(); 113 for (int e= 0; e < elements.length; e++) { 114 if (elements[e].equals(fElement)) { 115 elements[e]= newElement; 116 changed= true; 117 } 118 } 119 if (changed) { 120 newSelection[0]= new StructuredSelection(elements); 121 } 122 } 123 if (changed) { 124 fDisplay.asyncExec(new Runnable () { 125 public void run() { 126 target.selectReveal(newSelection[0]); 127 } 128 }); 129 } 130 } 131 } 132 133 private TreePath createTreePath(TreePath old, Object newElement) { 135 int count= old.getSegmentCount(); 136 Object [] newObjects= new Object [count]; 137 for (int i= 0; i < count - 1; i++) { 138 newObjects[i]= old.getSegment(i); 139 } 140 newObjects[count - 1]= newElement; 141 return new TreePath(newObjects); 142 } 143 } | Popular Tags |