KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.runtime.CoreException;
14
15 import org.eclipse.jface.text.IRegion;
16 import org.eclipse.jface.util.Assert;
17
18 import org.eclipse.ltk.core.refactoring.Change;
19 import org.eclipse.ltk.core.refactoring.TextChange;
20 import org.eclipse.ltk.core.refactoring.TextEditChangeGroup;
21 import org.eclipse.ltk.ui.refactoring.ChangePreviewViewerInput;
22 import org.eclipse.ltk.ui.refactoring.IChangePreviewViewer;
23
24 public class TextEditChangeElement extends ChangeElement {
25     
26     private static final ChangeElement[] fgChildren= new ChangeElement[0];
27     
28     private TextEditChangeGroup fChangeGroup;
29     
30     public TextEditChangeElement(ChangeElement parent, TextEditChangeGroup changeGroup) {
31         super(parent);
32         fChangeGroup= changeGroup;
33         Assert.isNotNull(fChangeGroup);
34     }
35     
36     /**
37      * Returns the <code>TextEditChange</code> managed by this node.
38      *
39      * @return the <code>TextEditChange</code>
40      */

41     public TextEditChangeGroup getTextEditChange() {
42         return fChangeGroup;
43     }
44     
45     public Object JavaDoc getModifiedElement() {
46         return fChangeGroup;
47     }
48     
49     public Change getChange() {
50         return null;
51     }
52     
53     public ChangePreviewViewerDescriptor getChangePreviewViewerDescriptor() throws CoreException {
54         DefaultChangeElement element= getDefaultChangeElement();
55         if (element == null)
56             return null;
57         return element.getChangePreviewViewerDescriptor();
58     }
59     
60     public void feedInput(IChangePreviewViewer viewer) throws CoreException {
61         DefaultChangeElement element= getDefaultChangeElement();
62         if (element != null) {
63             Change change= element.getChange();
64             if (change instanceof TextChange) {
65                 IRegion range= getTextRange(this);
66                 ChangePreviewViewerInput input= null;
67                 if (range != null) {
68                     input= TextChangePreviewViewer.createInput(change, new TextEditChangeGroup[] {fChangeGroup}, range);
69                 } else {
70                     input= TextChangePreviewViewer.createInput(change, fChangeGroup, 2);
71                 }
72                 viewer.setInput(input);
73             }
74         } else {
75             viewer.setInput(null);
76         }
77     }
78     
79     /* non Java-doc
80      * @see ChangeElement#setActive
81      */

82     public void setEnabled(boolean enabled) {
83         fChangeGroup.setEnabled(enabled);
84     }
85     
86     public void setEnabledShallow(boolean enabled) {
87         fChangeGroup.setEnabled(enabled);
88     }
89     
90     /* non Java-doc
91      * @see ChangeElement.getActive
92      */

93     public int getActive() {
94         return fChangeGroup.isEnabled() ? ACTIVE : INACTIVE;
95     }
96     
97     /* non Java-doc
98      * @see ChangeElement.getChildren
99      */

100     public ChangeElement[] getChildren() {
101         return fgChildren;
102     }
103     
104     private DefaultChangeElement getDefaultChangeElement() {
105         ChangeElement element= getParent();
106         while(!(element instanceof DefaultChangeElement) && element != null) {
107             element= element.getParent();
108         }
109         return (DefaultChangeElement)element;
110     }
111     
112     private static IRegion getTextRange(ChangeElement element) throws CoreException {
113         if (element == null)
114             return null;
115         if (element instanceof PseudoLanguageChangeElement) {
116             return ((PseudoLanguageChangeElement)element).getTextRange();
117         } else if (element instanceof DefaultChangeElement) {
118             return null;
119         }
120         return getTextRange(element.getParent());
121     }
122 }
Popular Tags