KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-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.jdt.core.IJavaElement;
20 import org.eclipse.jdt.core.ISourceRange;
21 import org.eclipse.jdt.core.ISourceReference;
22
23 import org.eclipse.jface.text.IRegion;
24 import org.eclipse.jface.text.Region;
25 import org.eclipse.jface.util.Assert;
26
27 import org.eclipse.ltk.core.refactoring.Change;
28 import org.eclipse.ltk.core.refactoring.TextChange;
29 import org.eclipse.ltk.core.refactoring.TextEditChangeGroup;
30 import org.eclipse.ltk.ui.refactoring.IChangePreviewViewer;
31
32 /**
33  * TODO should remove dependency to JDT/Core
34  * (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=61312)
35  */

36 /* package */ class PseudoJavaChangeElement extends ChangeElement {
37
38     private IJavaElement fJavaElement;
39     private List JavaDoc fChildren;
40
41     public PseudoJavaChangeElement(ChangeElement parent, IJavaElement element) {
42         super(parent);
43         fJavaElement= element;
44         Assert.isNotNull(fJavaElement);
45     }
46     
47     /**
48      * Returns the Java element.
49      *
50      * @return the Java element managed by this node
51      */

52     public IJavaElement getJavaElement() {
53         return fJavaElement;
54     }
55
56     public Change getChange() {
57         return null;
58     }
59     
60     public ChangePreviewViewerDescriptor getChangePreviewViewerDescriptor() throws CoreException {
61         DefaultChangeElement element= getDefaultChangeElement();
62         if (element == null)
63             return null;
64         return element.getChangePreviewViewerDescriptor();
65     }
66     
67     public void feedInput(IChangePreviewViewer viewer) throws CoreException {
68         DefaultChangeElement element= getDefaultChangeElement();
69         if (element != null) {
70             Change change= element.getChange();
71             if (change instanceof TextChange) {
72                 List JavaDoc edits= collectTextEditChanges();
73                 viewer.setInput(TextChangePreviewViewer.createInput(change,
74                     (TextEditChangeGroup[])edits.toArray(new TextEditChangeGroup[edits.size()]),
75                     getTextRange()));
76             }
77         } else {
78             viewer.setInput(null);
79         }
80     }
81     
82     public void setActive(boolean active) {
83         for (Iterator JavaDoc iter= fChildren.iterator(); iter.hasNext();) {
84             ChangeElement element= (ChangeElement)iter.next();
85             element.setActive(active);
86         }
87     }
88     
89     public int getActive() {
90         Assert.isTrue(fChildren.size() > 0);
91         int result= ((ChangeElement)fChildren.get(0)).getActive();
92         for (int i= 1; i < fChildren.size(); i++) {
93             ChangeElement element= (ChangeElement)fChildren.get(i);
94             result= ACTIVATION_TABLE[element.getActive()][result];
95             if (result == PARTLY_ACTIVE)
96                 break;
97         }
98         return result;
99     }
100     
101     /* non Java-doc
102      * @see ChangeElement.getChildren
103      */

104     public ChangeElement[] getChildren() {
105         if (fChildren == null)
106             return EMPTY_CHILDREN;
107         return (ChangeElement[]) fChildren.toArray(new ChangeElement[fChildren.size()]);
108     }
109     
110     /**
111      * Adds the given <code>TextEditChangeElement<code> as a child to this
112      * <code>PseudoJavaChangeElement</code>
113      *
114      * @param child the child to be added
115      */

116     public void addChild(TextEditChangeElement child) {
117         doAddChild(child);
118     }
119     
120     /**
121      * Adds the given <code>PseudoJavaChangeElement<code> as a child to this
122      * <code>PseudoJavaChangeElement</code>
123      *
124      * @param child the child to be added
125      */

126     public void addChild(PseudoJavaChangeElement child) {
127         doAddChild(child);
128     }
129     
130     private void doAddChild(ChangeElement child) {
131         if (fChildren == null)
132             fChildren= new ArrayList JavaDoc(2);
133         fChildren.add(child);
134     }
135     
136     private DefaultChangeElement getDefaultChangeElement() {
137         ChangeElement element= getParent();
138         while(!(element instanceof DefaultChangeElement) && element != null) {
139             element= element.getParent();
140         }
141         return (DefaultChangeElement)element;
142     }
143     
144     private List JavaDoc collectTextEditChanges() {
145         List JavaDoc result= new ArrayList JavaDoc(10);
146         ChangeElement[] children= getChildren();
147         for (int i= 0; i < children.length; i++) {
148             ChangeElement child= children[i];
149             if (child instanceof TextEditChangeElement) {
150                 result.add(((TextEditChangeElement)child).getTextEditChange());
151             } else if (child instanceof PseudoJavaChangeElement) {
152                 result.addAll(((PseudoJavaChangeElement)child).collectTextEditChanges());
153             }
154         }
155         return result;
156     }
157     
158     public IRegion getTextRange() throws CoreException {
159         ISourceRange range= ((ISourceReference)fJavaElement).getSourceRange();
160         return new Region(range.getOffset(), range.getLength());
161     }
162 }
163
164
Popular Tags