KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.CoreException;
18
19 import org.eclipse.ltk.core.refactoring.Change;
20 import org.eclipse.ltk.core.refactoring.TextChange;
21 import org.eclipse.ltk.core.refactoring.TextEditChangeGroup;
22 import org.eclipse.ltk.ui.refactoring.IChangePreviewViewer;
23
24 import org.eclipse.jface.text.IRegion;
25 import org.eclipse.jface.util.Assert;
26
27 public abstract class PseudoLanguageChangeElement extends ChangeElement {
28
29     private List JavaDoc fChildren;
30     
31     public PseudoLanguageChangeElement(ChangeElement parent) {
32         super(parent);
33     }
34
35     public Change getChange() {
36         return null;
37     }
38     
39     public ChangePreviewViewerDescriptor getChangePreviewViewerDescriptor() throws CoreException {
40         DefaultChangeElement element= getDefaultChangeElement();
41         if (element == null)
42             return null;
43         return element.getChangePreviewViewerDescriptor();
44     }
45     
46     public void feedInput(IChangePreviewViewer viewer) throws CoreException {
47         DefaultChangeElement element= getDefaultChangeElement();
48         if (element != null) {
49             Change change= element.getChange();
50             if (change instanceof TextChange) {
51                 List JavaDoc edits= collectTextEditChanges();
52                 viewer.setInput(TextChangePreviewViewer.createInput(change,
53                     (TextEditChangeGroup[])edits.toArray(new TextEditChangeGroup[edits.size()]),
54                     getTextRange()));
55             }
56         } else {
57             viewer.setInput(null);
58         }
59     }
60     
61     public void setEnabled(boolean enabled) {
62         for (Iterator JavaDoc iter= fChildren.iterator(); iter.hasNext();) {
63             ChangeElement element= (ChangeElement)iter.next();
64             element.setEnabled(enabled);
65         }
66     }
67     
68     public void setEnabledShallow(boolean enabled) {
69         // do nothing. We don't manage a own enablement state.
70
}
71     
72     public int getActive() {
73         Assert.isTrue(fChildren.size() > 0);
74         int result= ((ChangeElement)fChildren.get(0)).getActive();
75         for (int i= 1; i < fChildren.size(); i++) {
76             ChangeElement element= (ChangeElement)fChildren.get(i);
77             result= ACTIVATION_TABLE[element.getActive()][result];
78             if (result == PARTLY_ACTIVE)
79                 break;
80         }
81         return result;
82     }
83     
84     /* non Java-doc
85      * @see ChangeElement.getChildren
86      */

87     public ChangeElement[] getChildren() {
88         if (fChildren == null)
89             return EMPTY_CHILDREN;
90         return (ChangeElement[]) fChildren.toArray(new ChangeElement[fChildren.size()]);
91     }
92     
93     /**
94      * Adds the given <code>TextEditChangeElement<code> as a child to this
95      * <code>PseudoJavaChangeElement</code>
96      *
97      * @param child the child to be added
98      */

99     public void addChild(TextEditChangeElement child) {
100         doAddChild(child);
101     }
102     
103     /**
104      * Adds the given <code>PseudoJavaChangeElement<code> as a child to this
105      * <code>PseudoJavaChangeElement</code>
106      *
107      * @param child the child to be added
108      */

109     public void addChild(PseudoLanguageChangeElement child) {
110         doAddChild(child);
111     }
112     
113     private void doAddChild(ChangeElement child) {
114         if (fChildren == null)
115             fChildren= new ArrayList JavaDoc(2);
116         fChildren.add(child);
117     }
118     
119     private DefaultChangeElement getDefaultChangeElement() {
120         ChangeElement element= getParent();
121         while(!(element instanceof DefaultChangeElement) && element != null) {
122             element= element.getParent();
123         }
124         return (DefaultChangeElement)element;
125     }
126     
127     private List JavaDoc collectTextEditChanges() {
128         List JavaDoc result= new ArrayList JavaDoc(10);
129         ChangeElement[] children= getChildren();
130         for (int i= 0; i < children.length; i++) {
131             ChangeElement child= children[i];
132             if (child instanceof TextEditChangeElement) {
133                 result.add(((TextEditChangeElement)child).getTextEditChange());
134             } else if (child instanceof PseudoLanguageChangeElement) {
135                 result.addAll(((PseudoLanguageChangeElement)child).collectTextEditChanges());
136             }
137         }
138         return result;
139     }
140     
141     /**
142      * Returns the source region the lanaguage element.
143      *
144      * @return the source region of the language element.
145      * @throws CoreException if the source region can't be optained
146      */

147     public abstract IRegion getTextRange() throws CoreException;
148 }
149
Popular Tags