1 19 package org.netbeans.modules.java.editor.imports; 20 21 import com.sun.source.tree.CompilationUnitTree; 22 import java.awt.Dialog ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 import java.util.Map ; 28 import javax.lang.model.element.TypeElement; 29 import org.netbeans.api.java.source.CancellableTask; 30 import org.netbeans.api.java.source.JavaSource; 31 import org.netbeans.api.java.source.JavaSource.Phase; 32 import org.netbeans.api.java.source.SourceUtils; 33 import org.netbeans.api.java.source.WorkingCopy; 34 import org.openide.DialogDescriptor; 35 import org.openide.DialogDisplayer; 36 import org.openide.ErrorManager; 37 import org.openide.filesystems.FileObject; 38 39 43 public class JavaFixAllImports { 44 45 private static final JavaFixAllImports INSTANCE = new JavaFixAllImports(); 46 47 public static JavaFixAllImports getDefault() { 48 return INSTANCE; 49 } 50 51 52 private JavaFixAllImports() { 53 } 54 55 public void fixAllImports(FileObject fo) { 56 CancellableTask<WorkingCopy> task = new CancellableTask<WorkingCopy>() { 57 public void cancel() { 58 } 59 public void run(final WorkingCopy wc) { 60 try { 61 wc.toPhase(Phase.RESOLVED); 62 63 ComputeImports.Pair<Map <String , List <TypeElement>>, Map <String , List <TypeElement>>> candidates = new ComputeImports().computeCandidates(wc); 64 65 Map <String , List <TypeElement>> filteredCandidates = candidates.a; 66 Map <String , List <TypeElement>> notFilteredCandidates = candidates.b; 67 68 int size = notFilteredCandidates.size(); 69 String [] names = new String [size]; 70 String [][] variants = new String [size][]; 71 String [] defaults = new String [size]; 72 Map <String , TypeElement> fqn2TE = new HashMap <String , TypeElement>(); 73 Map <String , String > displayName2FQN = new HashMap <String , String >(); 74 75 int index = 0; 76 77 for (String key : notFilteredCandidates.keySet()) { 78 names[index] = key; 79 80 List <TypeElement> unfilteredVars = notFilteredCandidates.get(key); 81 List <TypeElement> filteredVars = filteredCandidates.get(key); 82 83 if (!unfilteredVars.isEmpty()) { 84 variants[index] = new String [unfilteredVars.size()]; 85 86 int i = 0; 87 88 for (TypeElement e : filteredVars) { 89 variants[index][i++] = e.getQualifiedName().toString(); 90 fqn2TE.put(e.getQualifiedName().toString(), e); 91 } 92 93 for (TypeElement e : unfilteredVars) { 94 if (filteredVars.contains(e)) 95 continue; 96 97 String fqn = e.getQualifiedName().toString(); 98 String dn = "<html><font color='#808080'><s>" + fqn; 99 100 variants[index][i++] = dn; 101 fqn2TE.put(fqn, e); 102 displayName2FQN.put(dn, fqn); 103 } 104 } else { 105 variants[index] = new String [1]; 106 variants[index][0] = "<html><font color='#FF0000'><cannot be resolved>"; 107 } 108 109 defaults[index] = variants[index][0]; 110 111 index++; 112 } 113 114 FixDuplicateImportStmts panel = new FixDuplicateImportStmts(); 115 116 panel.initPanel(names, variants, defaults); 117 118 DialogDescriptor dd = new DialogDescriptor(panel, "Fix All Imports"); 119 Dialog d = DialogDisplayer.getDefault().createDialog(dd); 120 121 d.setVisible(true); 122 123 d.setVisible(false); 124 d.dispose(); 125 126 if (dd.getValue() == DialogDescriptor.OK_OPTION) { 127 List <String > toImport = new ArrayList <String >(); 129 130 for (String dn : panel.getSelections()) { 131 String fqn = displayName2FQN.get(dn); 132 TypeElement el = fqn2TE.get(fqn != null ? fqn : dn); 133 134 if (el != null) { 135 toImport.add(el.getQualifiedName().toString()); 136 } 137 } 138 139 try { 140 CompilationUnitTree cut = SourceUtils.addImports(wc.getCompilationUnit(), toImport, wc.getTreeMaker()); 142 wc.rewrite(wc.getCompilationUnit(), cut); 143 } catch (IOException ex) { 144 ErrorManager.getDefault().notify(ex); 145 } 146 } 147 } catch (IOException ex) { 148 ex.printStackTrace(); 150 } 151 } 152 }; 153 try { 154 JavaSource javaSource = JavaSource.forFileObject(fo); 155 javaSource.runModificationTask(task).commit(); 156 } catch (IOException ioe) { 157 ErrorManager.getDefault().notify(ioe); 158 } 159 } 160 161 } 162 | Popular Tags |