KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > InternalLanguageElementNode


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ltk.internal.ui.refactoring;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.CoreException;
19
20 import org.eclipse.ltk.core.refactoring.Change;
21 import org.eclipse.ltk.core.refactoring.GroupCategorySet;
22 import org.eclipse.ltk.core.refactoring.TextEditBasedChange;
23 import org.eclipse.ltk.core.refactoring.TextEditBasedChangeGroup;
24
25 import org.eclipse.jface.text.IRegion;
26
27 import org.eclipse.ltk.ui.refactoring.IChangePreviewViewer;
28 import org.eclipse.ltk.ui.refactoring.TextEditChangeNode;
29 import org.eclipse.ltk.ui.refactoring.TextEditChangeNode.ChildNode;
30
31 public abstract class InternalLanguageElementNode extends TextEditChangeNode.ChildNode {
32
33     private List JavaDoc/*<ChildNode>*/ fChildren;
34     private GroupCategorySet fGroupCategories;
35     
36     protected InternalLanguageElementNode(PreviewNode parent) {
37         super(parent);
38     }
39
40     ChangePreviewViewerDescriptor getChangePreviewViewerDescriptor() throws CoreException {
41         InternalTextEditChangeNode element= getTextEditChangeNode();
42         if (element == null)
43             return null;
44         return element.getChangePreviewViewerDescriptor();
45     }
46     
47     void feedInput(IChangePreviewViewer viewer, List JavaDoc categories) throws CoreException {
48         InternalTextEditChangeNode element= getTextEditChangeNode();
49         if (element != null) {
50             Change change= element.getChange();
51             if (change instanceof TextEditBasedChange) {
52                 List JavaDoc groups= collectTextEditBasedChangeGroups(categories);
53                 viewer.setInput(TextEditChangePreviewViewer.createInput(change,
54                     (TextEditBasedChangeGroup[])groups.toArray(new TextEditBasedChangeGroup[groups.size()]),
55                     getTextRange()));
56             }
57         } else {
58             viewer.setInput(null);
59         }
60     }
61     
62     void setEnabled(boolean enabled) {
63         for (Iterator JavaDoc iter= fChildren.iterator(); iter.hasNext();) {
64             PreviewNode element= (PreviewNode)iter.next();
65             element.setEnabled(enabled);
66         }
67     }
68     
69     void setEnabledShallow(boolean enabled) {
70         // do nothing. We don't manage an own enablement state.
71
}
72     
73     int getActive() {
74         Assert.isTrue(fChildren.size() > 0);
75         int result= ((PreviewNode)fChildren.get(0)).getActive();
76         for (int i= 1; i < fChildren.size(); i++) {
77             PreviewNode element= (PreviewNode)fChildren.get(i);
78             result= PreviewNode.ACTIVATION_TABLE[element.getActive()][result];
79             if (result == PreviewNode.PARTLY_ACTIVE)
80                 break;
81         }
82         return result;
83     }
84     
85     PreviewNode[] getChildren() {
86         if (fChildren == null)
87             return PreviewNode.EMPTY_CHILDREN;
88         return (PreviewNode[]) fChildren.toArray(new PreviewNode[fChildren.size()]);
89     }
90     
91     boolean hasOneGroupCategory(List JavaDoc categories) {
92         if (fChildren == null)
93             return false;
94         return getGroupCategorySet().containsOneCategory(categories);
95     }
96     
97     boolean hasDerived() {
98         if (fChildren == null)
99             return false;
100         for (Iterator JavaDoc iter= fChildren.iterator(); iter.hasNext();) {
101             PreviewNode node= (PreviewNode)iter.next();
102             if (node.hasDerived())
103                 return true;
104         }
105         return false;
106     }
107     
108     private GroupCategorySet getGroupCategorySet() {
109         if (fGroupCategories == null) {
110             fGroupCategories= GroupCategorySet.NONE;
111             for (Iterator JavaDoc iter= fChildren.iterator(); iter.hasNext();) {
112                 PreviewNode node= (PreviewNode)iter.next();
113                 GroupCategorySet other= null;
114                 if (node instanceof TextEditGroupNode) {
115                     other= ((TextEditGroupNode)node).getGroupCategorySet();
116                 } else if (node instanceof InternalLanguageElementNode) {
117                     other= ((InternalLanguageElementNode)node).getGroupCategorySet();
118                 } else {
119                     Assert.isTrue(false, "Shouldn't happen"); //$NON-NLS-1$
120
}
121                 fGroupCategories= GroupCategorySet.union(fGroupCategories, other);
122             }
123         }
124         return fGroupCategories;
125     }
126     
127     protected void internalAddChild(ChildNode child) {
128         if (fChildren == null)
129             fChildren= new ArrayList JavaDoc(2);
130         fChildren.add(child);
131     }
132     
133     private List JavaDoc collectTextEditBasedChangeGroups(List JavaDoc categories) {
134         List JavaDoc result= new ArrayList JavaDoc(10);
135         PreviewNode[] children= getChildren();
136         for (int i= 0; i < children.length; i++) {
137             PreviewNode child= children[i];
138             if (child instanceof TextEditGroupNode) {
139                 TextEditBasedChangeGroup changeGroup= ((TextEditGroupNode)child).getChangeGroup();
140                 if (categories == null || changeGroup.getGroupCategorySet().containsOneCategory(categories))
141                     result.add(changeGroup);
142             } else if (child instanceof InternalLanguageElementNode) {
143                 result.addAll(((InternalLanguageElementNode)child).collectTextEditBasedChangeGroups(categories));
144             }
145         }
146         return result;
147     }
148     
149     /**
150      * Returns the text region the of this language element node.
151      *
152      * @return the text region of this language element node
153      * @throws CoreException if the source region can't be obtained
154      */

155     public abstract IRegion getTextRange() throws CoreException;
156 }
157
Popular Tags