KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > imports > JavaFixAllImports


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.editor.imports;
20
21 import com.sun.source.tree.CompilationUnitTree;
22 import java.awt.Dialog JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
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 /**
40  *
41  * @author Jan Lahoda
42  */

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     /** Creates a new instance of JavaFixAllImports */
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 JavaDoc<String JavaDoc, List JavaDoc<TypeElement>>, Map JavaDoc<String JavaDoc, List JavaDoc<TypeElement>>> candidates = new ComputeImports().computeCandidates(wc);
64
65                     Map JavaDoc<String JavaDoc, List JavaDoc<TypeElement>> filteredCandidates = candidates.a;
66                     Map JavaDoc<String JavaDoc, List JavaDoc<TypeElement>> notFilteredCandidates = candidates.b;
67
68                     int size = notFilteredCandidates.size();
69                     String JavaDoc[] names = new String JavaDoc[size];
70                     String JavaDoc[][] variants = new String JavaDoc[size][];
71                     String JavaDoc[] defaults = new String JavaDoc[size];
72                     Map JavaDoc<String JavaDoc, TypeElement> fqn2TE = new HashMap JavaDoc<String JavaDoc, TypeElement>();
73                     Map JavaDoc<String JavaDoc, String JavaDoc> displayName2FQN = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
74
75                     int index = 0;
76
77                     for (String JavaDoc key : notFilteredCandidates.keySet()) {
78                         names[index] = key;
79
80                         List JavaDoc<TypeElement> unfilteredVars = notFilteredCandidates.get(key);
81                         List JavaDoc<TypeElement> filteredVars = filteredCandidates.get(key);
82
83                         if (!unfilteredVars.isEmpty()) {
84                             variants[index] = new String JavaDoc[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 JavaDoc fqn = e.getQualifiedName().toString();
98                                 String JavaDoc 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 JavaDoc[1];
106                             variants[index][0] = "<html><font color='#FF0000'>&lt;cannot be resolved&gt;";
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 JavaDoc 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                         //do imports:
128
List JavaDoc<String JavaDoc> toImport = new ArrayList JavaDoc<String JavaDoc>();
129
130                         for (String JavaDoc dn : panel.getSelections()) {
131                             String JavaDoc 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                             // make the changes to the source
141
CompilationUnitTree cut = SourceUtils.addImports(wc.getCompilationUnit(), toImport, wc.getTreeMaker());
142                             wc.rewrite(wc.getCompilationUnit(), cut);
143                         } catch (IOException JavaDoc ex) {
144                             ErrorManager.getDefault().notify(ex);
145                         }
146                     }
147                 } catch (IOException JavaDoc ex) {
148                     //TODO: ErrorManager
149
ex.printStackTrace();
150                 }
151             }
152         };
153         try {
154             JavaSource javaSource = JavaSource.forFileObject(fo);
155             javaSource.runModificationTask(task).commit();
156         } catch (IOException JavaDoc ioe) {
157             ErrorManager.getDefault().notify(ioe);
158         }
159     }
160     
161 }
162
Popular Tags