]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/reorg/RenameSelectionState.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui refactoring / org / eclipse / jdt / internal / ui / refactoring / reorg / RenameSelectionState.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
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<IWorkbenchPart> fParts;
39         private final List<IStructuredSelection> fSelections;
40
41         public RenameSelectionState(Object element) {
42                 fElement= element;
43                 fParts= new ArrayList<IWorkbenchPart>();
44                 fSelections= new ArrayList<IStructuredSelection>();
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= 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         // 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);
139                 }
140                 newObjects[count - 1]= newElement;
141                 return new TreePath(newObjects);
142         }
143 }