KickJava   Java API By Example, From Geeks To Geeks.

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


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.editor.imports;
20
21 import java.awt.Font JavaDoc;
22 import java.awt.Point JavaDoc;
23 import java.awt.Rectangle JavaDoc;
24 import java.awt.Toolkit JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import javax.lang.model.element.TypeElement;
32 import javax.swing.SwingUtilities JavaDoc;
33 import javax.swing.SwingUtilities JavaDoc;
34 import javax.swing.text.BadLocationException JavaDoc;
35 import javax.swing.text.Document JavaDoc;
36 import javax.swing.text.JTextComponent JavaDoc;
37 import org.netbeans.api.java.source.CancellableTask;
38 import org.netbeans.api.java.source.CompilationController;
39 import org.netbeans.api.java.source.JavaSource;
40 import org.netbeans.api.java.source.JavaSource.Phase;
41 import org.netbeans.editor.BaseAction;
42 import org.netbeans.editor.Utilities;
43 import org.netbeans.modules.java.editor.imports.ComputeImports.Pair;
44 import org.netbeans.modules.java.editor.overridden.PopupUtil;
45 import org.openide.ErrorManager;
46 import org.openide.filesystems.FileObject;
47 import org.openide.loaders.DataObject;
48
49 /**
50  *
51  * @author Jan Lahoda
52  */

53 public class FastImportAction extends BaseAction {
54     
55     public static final String JavaDoc NAME = "fast-import";
56     
57     /** Creates a new instance of FastImportAction */
58     public FastImportAction() {
59         super(NAME);
60     }
61
62     public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
63         try {
64             final Rectangle JavaDoc carretRectangle = target.modelToView(target.getCaretPosition());
65             final Font JavaDoc font = target.getFont();
66             final Point JavaDoc where = new Point JavaDoc( carretRectangle.x, carretRectangle.y + carretRectangle.height );
67             SwingUtilities.convertPointToScreen( where, target);
68
69             final String JavaDoc ident = Utilities.getIdentifier(Utilities.getDocument(target), target.getCaretPosition());
70             FileObject file = getFile(target.getDocument());
71             
72             if (ident == null || file == null) {
73                 Toolkit.getDefaultToolkit().beep();
74                 return ;
75             }
76             
77             JavaSource js = JavaSource.forFileObject(file);
78             
79             if (js == null) {
80                 Toolkit.getDefaultToolkit().beep();
81                 return ;
82             }
83             
84             js.runUserActionTask(new CancellableTask<CompilationController>() {
85                 public void cancel() {
86                 }
87                 public void run(final CompilationController parameter) throws IOException JavaDoc {
88                     parameter.toPhase(Phase.RESOLVED);
89                     final JavaSource javaSource = parameter.getJavaSource();
90                     Pair<Map JavaDoc<String JavaDoc, List JavaDoc<TypeElement>>, Map JavaDoc<String JavaDoc, List JavaDoc<TypeElement>>> result = new ComputeImports().computeCandidates(parameter, Collections.singleton(ident));
91                     
92                     final List JavaDoc<TypeElement> priviledged = result.a.get(ident);
93                     
94                     if (priviledged == null) {
95                         //not found?
96
Toolkit.getDefaultToolkit().beep();
97                         return ;
98                     }
99                     
100                     final List JavaDoc<TypeElement> denied = new ArrayList JavaDoc(result.b.get(ident));
101                     
102                     denied.removeAll(priviledged);
103                     
104                     SwingUtilities.invokeLater(new Runnable JavaDoc() {
105                         public void run() {
106                             ImportClassPanel panel = new ImportClassPanel(priviledged, denied, font, javaSource);
107                             PopupUtil.showPopup(panel, "", where.x, where.y, true, carretRectangle.height );
108                         }
109                     });
110                 }
111             }, true);
112         } catch (IOException JavaDoc ex) {
113             ErrorManager.getDefault().notify(ex);
114         } catch (BadLocationException JavaDoc ex) {
115             ErrorManager.getDefault().notify(ex);
116         }
117     }
118     
119     
120     private FileObject getFile(Document JavaDoc doc) {
121         DataObject od = (DataObject) doc.getProperty(Document.StreamDescriptionProperty);
122         
123         if (od == null)
124             return null;
125         
126         return od.getPrimaryFile();
127     }
128 }
129
Popular Tags