1 /*******************************************************************************
2 * Copyright (c) 2006, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.refactoring.reorg;
13 import java.util.ArrayList;
14 import java.util.List;
16 import org.eclipse.swt.widgets.Display;
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;
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;
32 import org.eclipse.jdt.internal.ui.JavaPlugin;
35 public class RenameSelectionState {
36 private final Display fDisplay;
37 private final Object fElement;
38 private final List<IWorkbenchPart> fParts;
39 private final List<IStructuredSelection> fSelections;
41 public RenameSelectionState(Object element) {
43 fParts= new ArrayList<IWorkbenchPart>();
44 fSelections= new ArrayList<IStructuredSelection>();
46 IWorkbenchWindow dw = JavaPlugin.getActiveWorkbenchWindow();
51 fDisplay= dw.getShell().getDisplay();
52 IWorkbenchPage page = dw.getActivePage();
55 IViewReference vrefs[]= page.getViewReferences();
56 for(int i= 0; i < vrefs.length; i++) {
57 consider(vrefs[i].getPart(false));
59 IEditorReference refs[]= page.getEditorReferences();
60 for(int i= 0; i < refs.length; i++) {
61 consider(refs[i].getPart(false));
65 private void consider(IWorkbenchPart part) {
68 ISetSelectionTarget target= null;
69 if (!(part instanceof ISetSelectionTarget)) {
70 target= (ISetSelectionTarget)part.getAdapter(ISetSelectionTarget.class);
74 target= (ISetSelectionTarget)part;
76 ISelectionProvider selectionProvider= part.getSite().getSelectionProvider();
77 if (selectionProvider == null)
79 ISelection s= selectionProvider.getSelection();
80 if (!(s instanceof IStructuredSelection))
82 IStructuredSelection selection= (IStructuredSelection)s;
83 if (!selection.toList().contains(fElement))
86 fSelections.add(selection);
89 public void restore(Object newElement) {
92 for (int i= 0; i < fParts.size(); i++) {
93 IStructuredSelection currentSelection= 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);
109 newSelection[0]= new TreeSelection(paths, treeSelection.getElementComparer());
112 Object[] elements= currentSelection.toArray();
113 for (int e= 0; e < elements.length; e++) {
114 if (elements[e].equals(fElement)) {
115 elements[e]= newElement;
120 newSelection[0]= new StructuredSelection(elements);
124 fDisplay.asyncExec(new Runnable() {
126 target.selectReveal(newSelection[0]);
133 // Method assumes that segment count of path > 0.
134 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);
140 newObjects[count - 1]= newElement;
141 return new TreePath(newObjects);