KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > PullUpTreeViewer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.jdt.core.IJavaElement;
17 import org.eclipse.jdt.core.IMember;
18 import org.eclipse.jdt.core.IMethod;
19 import org.eclipse.jdt.core.IType;
20 import org.eclipse.jdt.core.ITypeHierarchy;
21
22 import org.eclipse.swt.widgets.Item;
23 import org.eclipse.swt.widgets.Tree;
24 import org.eclipse.swt.widgets.TreeItem;
25 import org.eclipse.swt.widgets.Widget;
26
27 import org.eclipse.jface.viewers.CheckStateChangedEvent;
28 import org.eclipse.jface.viewers.CheckboxTreeViewer;
29 import org.eclipse.jface.viewers.ICheckStateListener;
30 import org.eclipse.jface.viewers.ITreeViewerListener;
31 import org.eclipse.jface.viewers.TreeExpansionEvent;
32
33 class PullUpTreeViewer extends CheckboxTreeViewer {
34     
35     private Set JavaDoc fActiveElements;
36     
37     public PullUpTreeViewer(Tree tree) {
38         super(tree);
39         
40         fActiveElements= new HashSet JavaDoc();
41         
42         addCheckStateListener(createCheckStateListener());
43
44         addTreeListener(new ITreeViewerListener() {
45             public void treeCollapsed(TreeExpansionEvent event) {
46             }
47             public void treeExpanded(TreeExpansionEvent event) {
48                 IMember element= (IMember)event.getElement();
49                 initializeChildren(element);
50             }
51         });
52     }
53
54     private ICheckStateListener createCheckStateListener() {
55         return new ICheckStateListener() {
56             public void checkStateChanged(CheckStateChangedEvent event){
57                 setCheckState(((IMember)event.getElement()), event.getChecked());
58             }
59         };
60     }
61     
62     public void setCheckState(IMember element, boolean checked){
63         if (checked)
64             fActiveElements.add(element);
65         else
66             fActiveElements.remove(element);
67
68         setSubtreeChecked(element, checked);
69         setSubtreeGrayed(element, false);
70         IJavaElement parent= getParent(element);
71         if (parent == null)
72             return;
73         while (parent != null) {
74             setChecked(parent, checked);
75             setGrayed(parent, isPartlyActive(parent));
76             parent= getParent(parent);
77         }
78     }
79     
80     private ITypeHierarchy getTypeHierarchyInput(){
81         return (ITypeHierarchy)getInput();
82     }
83     
84     private boolean isPartlyActive(IJavaElement member){
85         if (member instanceof IMethod)
86             return false;
87         if (!(member instanceof IType))
88             return false;
89             
90         boolean checked= getChecked(member);
91         Object JavaDoc[] children=getFilteredChildren(member);
92         for (int i= 0; i < children.length; i++) {
93             if (! (children[i] instanceof IJavaElement))
94                 continue;
95             IJavaElement iJavaElement= (IJavaElement)children[i];
96             if (checked){
97                  if (! getChecked(iJavaElement))
98                     return true;
99                  if (isPartlyActive(iJavaElement))
100                     return true;
101             }
102                 
103             if (! checked){
104                 if (getChecked(iJavaElement))
105                     return true;
106                 if (isPartlyActive(iJavaElement))
107                     return true;
108             }
109         }
110         return false;
111     }
112     
113     private IMember getParent(IJavaElement member){
114         if (member instanceof IType)
115             return getTypeHierarchyInput().getSuperclass((IType)member);
116         if (member instanceof IMethod)
117             return ((IMethod)member).getDeclaringType();
118         return null;
119     }
120     
121     private IMember[] getChildren(IMember member){
122         if (member instanceof IType)
123             return getTypeHierarchyInput().getSubtypes((IType)member);
124         return null;
125     }
126     
127     protected void inputChanged(Object JavaDoc input, Object JavaDoc oldInput) {
128         super.inputChanged(input, oldInput);
129         initializeChildren(((ITypeHierarchy)input).getType());
130     }
131     
132     public void expandToLevel(Object JavaDoc element, int level) {
133         super.expandToLevel(element, level);
134         if (element instanceof IMember)
135             initializeChildren((IMember)element);
136         if (element instanceof ITypeHierarchy)
137             initializeChildren(((ITypeHierarchy)element).getType());
138     }
139     
140     private void initializeChildren(IMember element) {
141         if (element == null)
142             return;
143         IMember[] children= getChildren(element);
144         if (children == null)
145             return;
146         for (int i= 0; i < children.length; i++) {
147             IMember child= children[i];
148             boolean checked= fActiveElements.contains(child);
149             if (checked)
150                 setChecked(child, checked);
151             boolean grayed= isPartlyActive(child);
152             if (grayed)
153                 setGrayed(child, grayed);
154         }
155     }
156     
157     public void setSubtreeGrayed(Object JavaDoc element, boolean grayed) {
158         Widget widget= findItem(element);
159         if (widget instanceof TreeItem) {
160             TreeItem item= (TreeItem)widget;
161             if (item.getGrayed() != grayed) {
162                 item.setGrayed(grayed);
163                 grayChildren(getChildren(item), grayed);
164             }
165         }
166     }
167     
168     private void grayChildren(Item[] items, boolean grayed) {
169         for (int i= 0; i < items.length; i++) {
170             Item element= items[i];
171             if (element instanceof TreeItem) {
172                 TreeItem item= (TreeItem)element;
173                 if (item.getGrayed() != grayed) {
174                     item.setGrayed(grayed);
175                     grayChildren(getChildren(item), grayed);
176                 }
177             }
178         }
179     }
180 }
181
Popular Tags