KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > GenerateConstructorUsingFieldsContentProvider


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.jdt.internal.ui.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jface.viewers.CheckboxTreeViewer;
18 import org.eclipse.jface.viewers.ITreeContentProvider;
19 import org.eclipse.jface.viewers.StructuredSelection;
20 import org.eclipse.jface.viewers.Viewer;
21
22 import org.eclipse.jdt.core.IField;
23 import org.eclipse.jdt.core.IType;
24 import org.eclipse.jdt.core.JavaModelException;
25 import org.eclipse.jdt.core.dom.AST;
26 import org.eclipse.jdt.core.dom.CompilationUnit;
27 import org.eclipse.jdt.core.dom.ITypeBinding;
28 import org.eclipse.jdt.core.dom.IVariableBinding;
29 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
30
31 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
32 import org.eclipse.jdt.internal.corext.refactoring.structure.ASTNodeSearchUtil;
33 import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
34
35 public class GenerateConstructorUsingFieldsContentProvider implements ITreeContentProvider {
36
37     private static final Object JavaDoc[] EMPTY= new Object JavaDoc[0];
38
39     private List JavaDoc fFields= new ArrayList JavaDoc();
40
41     private List JavaDoc fSelected= new ArrayList JavaDoc();
42
43     private ITypeBinding fType= null;
44
45     private final CompilationUnit fUnit;
46
47     public GenerateConstructorUsingFieldsContentProvider(IType type, List JavaDoc fields, List JavaDoc selected) throws JavaModelException {
48         RefactoringASTParser parser= new RefactoringASTParser(AST.JLS3);
49         fUnit= parser.parse(type.getCompilationUnit(), true);
50         fType= ASTNodes.getTypeBinding(fUnit, type);
51         if (fType != null) {
52             IField field= null;
53             for (Iterator JavaDoc iterator= fields.iterator(); iterator.hasNext();) {
54                 field= (IField) iterator.next();
55                 VariableDeclarationFragment fragment= ASTNodeSearchUtil.getFieldDeclarationFragmentNode(field, fUnit);
56                 if (fragment != null) {
57                     IVariableBinding binding= fragment.resolveBinding();
58                     if (binding != null)
59                         fFields.add(binding);
60                 }
61             }
62             for (Iterator JavaDoc iterator= selected.iterator(); iterator.hasNext();) {
63                 field= (IField) iterator.next();
64                 VariableDeclarationFragment fragment= ASTNodeSearchUtil.getFieldDeclarationFragmentNode(field, fUnit);
65                 if (fragment != null) {
66                     IVariableBinding binding= fragment.resolveBinding();
67                     if (binding != null)
68                         fSelected.add(binding);
69                 }
70             }
71         }
72     }
73
74     public CompilationUnit getCompilationUnit() {
75         return fUnit;
76     }
77
78     public boolean canMoveDown(List JavaDoc selectedElements) {
79         int nSelected= selectedElements.size();
80         for (int index= fFields.size() - 1; index >= 0 && nSelected > 0; index--) {
81             if (!selectedElements.contains(fFields.get(index))) {
82                 return true;
83             }
84             nSelected--;
85         }
86         return false;
87     }
88
89     public boolean canMoveUp(List JavaDoc selected) {
90         int nSelected= selected.size();
91         for (int index= 0; index < fFields.size() && nSelected > 0; index++) {
92             if (!selected.contains(fFields.get(index))) {
93                 return true;
94             }
95             nSelected--;
96         }
97         return false;
98     }
99
100     /*
101      * @see IContentProvider#dispose()
102      */

103     public void dispose() {
104     }
105
106     public void down(List JavaDoc checked, CheckboxTreeViewer tree) {
107         if (checked.size() > 0) {
108             setElements(reverse(moveUp(reverse(fFields), checked)), tree);
109             tree.reveal(checked.get(checked.size() - 1));
110         }
111         tree.setSelection(new StructuredSelection(checked));
112     }
113
114     /*
115      * @see ITreeContentProvider#getChildren(Object)
116      */

117     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
118         return EMPTY;
119     }
120
121     /*
122      * @see IStructuredContentProvider#getElements(Object)
123      */

124     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
125         return fFields.toArray();
126     }
127
128     public List JavaDoc getFieldsList() {
129         return fFields;
130     }
131
132     public Object JavaDoc[] getInitiallySelectedElements() {
133         if (fSelected.isEmpty())
134             return getElements(null);
135         return fSelected.toArray();
136     }
137
138     /*
139      * @see ITreeContentProvider#getParent(Object)
140      */

141     public Object JavaDoc getParent(Object JavaDoc element) {
142         return null;
143     }
144
145     public ITypeBinding getType() {
146         return fType;
147     }
148
149     /*
150      * @see ITreeContentProvider#hasChildren(Object)
151      */

152     public boolean hasChildren(Object JavaDoc element) {
153         return getChildren(element).length > 0;
154     }
155
156     /*
157      * @see IContentProvider#inputChanged(Viewer, Object, Object)
158      */

159     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
160     }
161
162     private List JavaDoc moveUp(List JavaDoc elements, List JavaDoc move) {
163         List JavaDoc result= new ArrayList JavaDoc(elements.size());
164         Object JavaDoc floating= null;
165         for (int index= 0; index < elements.size(); index++) {
166             Object JavaDoc current= elements.get(index);
167             if (move.contains(current)) {
168                 result.add(current);
169             } else {
170                 if (floating != null) {
171                     result.add(floating);
172                 }
173                 floating= current;
174             }
175         }
176         if (floating != null) {
177             result.add(floating);
178         }
179         return result;
180     }
181
182     private List JavaDoc reverse(List JavaDoc list) {
183         List JavaDoc reverse= new ArrayList JavaDoc(list.size());
184         for (int index= list.size() - 1; index >= 0; index--) {
185             reverse.add(list.get(index));
186         }
187         return reverse;
188     }
189
190     public void setElements(List JavaDoc elements, CheckboxTreeViewer tree) {
191         fFields= new ArrayList JavaDoc(elements);
192         if (tree != null)
193             tree.refresh();
194     }
195
196     public void up(List JavaDoc checked, CheckboxTreeViewer tree) {
197         if (checked.size() > 0) {
198             setElements(moveUp(fFields, checked), tree);
199             tree.reveal(checked.get(0));
200         }
201         tree.setSelection(new StructuredSelection(checked));
202     }
203 }
204
Popular Tags