1 11 package org.eclipse.jdt.internal.ui.actions; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 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 [] EMPTY= new Object [0]; 38 39 private List fFields= new ArrayList (); 40 41 private List fSelected= new ArrayList (); 42 43 private ITypeBinding fType= null; 44 45 private final CompilationUnit fUnit; 46 47 public GenerateConstructorUsingFieldsContentProvider(IType type, List fields, List 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 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 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 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 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 103 public void dispose() { 104 } 105 106 public void down(List 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 117 public Object [] getChildren(Object parentElement) { 118 return EMPTY; 119 } 120 121 124 public Object [] getElements(Object inputElement) { 125 return fFields.toArray(); 126 } 127 128 public List getFieldsList() { 129 return fFields; 130 } 131 132 public Object [] getInitiallySelectedElements() { 133 if (fSelected.isEmpty()) 134 return getElements(null); 135 return fSelected.toArray(); 136 } 137 138 141 public Object getParent(Object element) { 142 return null; 143 } 144 145 public ITypeBinding getType() { 146 return fType; 147 } 148 149 152 public boolean hasChildren(Object element) { 153 return getChildren(element).length > 0; 154 } 155 156 159 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 160 } 161 162 private List moveUp(List elements, List move) { 163 List result= new ArrayList (elements.size()); 164 Object floating= null; 165 for (int index= 0; index < elements.size(); index++) { 166 Object 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 reverse(List list) { 183 List reverse= new ArrayList (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 elements, CheckboxTreeViewer tree) { 191 fFields= new ArrayList (elements); 192 if (tree != null) 193 tree.refresh(); 194 } 195 196 public void up(List 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 |