KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IAdaptable;
18
19 import org.eclipse.core.resources.IResource;
20
21
22 import org.eclipse.ltk.core.refactoring.Change;
23 import org.eclipse.ltk.core.refactoring.CompositeChange;
24 import org.eclipse.ltk.core.refactoring.TextEditBasedChange;
25
26 import org.eclipse.jface.resource.ImageDescriptor;
27
28 import org.eclipse.ltk.ui.refactoring.ChangePreviewViewerInput;
29 import org.eclipse.ltk.ui.refactoring.IChangePreviewViewer;
30 import org.eclipse.ltk.ui.refactoring.TextEditChangeNode;
31
32 public abstract class AbstractChangeNode extends PreviewNode {
33
34     private final Change fChange;
35     private PreviewNode[] fChildren;
36
37     public static PreviewNode createNode(PreviewNode parent, RefactoringPreviewChangeFilter filter, Change change) {
38         if (change instanceof CompositeChange) {
39             return new CompositeChangeNode(parent, filter, (CompositeChange)change);
40         } else if (change instanceof TextEditBasedChange) {
41             InternalTextEditChangeNode result= (TextEditChangeNode)change.getAdapter(TextEditChangeNode.class);
42             if (result == null) {
43                 result= new TextEditChangeNode((TextEditBasedChange)change);
44             }
45             result.initialize(parent);
46             return result;
47         }
48         return new DefaultChangeNode(parent, change);
49     }
50     
51     public static PreviewNode createNode(PreviewNode parent, Change change) {
52         return createNode(parent, null, change);
53     }
54     
55     /**
56      * Creates a new <code>AbstractChangeNode</code> for the given
57      * change.
58      *
59      * @param parent the change nodes's parent or <code>null
60      * </code> if the change node doesn't have a parent
61      * @param change the actual change. Argument must not be
62      * <code>null</code>
63      */

64     AbstractChangeNode(PreviewNode parent, Change change) {
65         super(parent);
66         Assert.isNotNull(change);
67         fChange= change;
68     }
69     
70     /**
71      * Returns the underlying <code>Change</code> object.
72      *
73      * @return the underlying change
74      */

75     Change getChange() {
76         return fChange;
77     }
78     
79     PreviewNode[] getChildren() {
80         if (fChildren == null) {
81             fChildren= doCreateChildren();
82         }
83         return fChildren;
84     }
85     
86     abstract PreviewNode[] doCreateChildren();
87     
88     public String JavaDoc getText() {
89         return fChange.getName();
90     }
91     
92     public ImageDescriptor getImageDescriptor() {
93         return RefactoringPluginImages.DESC_OBJS_DEFAULT_CHANGE;
94     }
95     
96     ChangePreviewViewerDescriptor getChangePreviewViewerDescriptor() throws CoreException {
97         return ChangePreviewViewerDescriptor.get(fChange);
98     }
99     
100     void feedInput(IChangePreviewViewer viewer, List JavaDoc categories) throws CoreException {
101         viewer.setInput(new ChangePreviewViewerInput(fChange));
102     }
103     
104     void setEnabled(boolean enabled) {
105         fChange.setEnabled(enabled);
106     }
107     
108     void setEnabledShallow(boolean enabled) {
109         fChange.setEnabledShallow(enabled);
110     }
111     
112     boolean hasOneGroupCategory(List JavaDoc categories) {
113         PreviewNode[] children= getChildren();
114         for (int i= 0; i < children.length; i++) {
115             if (children[i].hasOneGroupCategory(categories))
116                 return true;
117         }
118         return false;
119     }
120     
121     boolean hasDerived() {
122         if (hasDerivedResourceChange(fChange))
123             return true;
124         PreviewNode[] children= getChildren();
125         for (int i= 0; i < children.length; i++) {
126             if (children[i].hasDerived())
127                 return true;
128         }
129         return false;
130     }
131
132     int getDefaultChangeActive() {
133         int result= fChange.isEnabled() ? ACTIVE : INACTIVE;
134         if (fChildren != null) {
135             for (int i= 0; i < fChildren.length; i++) {
136                 result= ACTIVATION_TABLE[fChildren[i].getActive()][result];
137                 if (result == PARTLY_ACTIVE)
138                     break;
139             }
140         }
141         return result;
142     }
143     
144     int getCompositeChangeActive() {
145         if (fChildren != null && fChildren.length > 0) {
146             int result= fChildren[0].getActive();
147             for (int i= 1; i < fChildren.length; i++) {
148                 result= ACTIVATION_TABLE[fChildren[i].getActive()][result];
149                 if (result == PARTLY_ACTIVE)
150                     break;
151             }
152             return result;
153         } else {
154             return ACTIVE;
155         }
156     }
157
158     /**
159      * Returns <code>true</code> iff the change node contains a derived
160      * resource.
161      *
162      * @param change the change
163      * @return whether the change contains a derived resource
164      */

165     static boolean hasDerivedResourceChange(Change change) {
166         Object JavaDoc modifiedElement= change.getModifiedElement();
167         if (modifiedElement instanceof IResource) {
168             return ((IResource) modifiedElement).isDerived();
169         } else if (modifiedElement instanceof IAdaptable) {
170             IAdaptable adaptable= (IAdaptable) modifiedElement;
171             IResource resource= (IResource) adaptable.getAdapter(IResource.class);
172             if (resource != null) {
173                 return resource.isDerived();
174             }
175         }
176         return false;
177     }
178 }
179
Popular Tags