1 19 package org.netbeans.modules.java.editor.imports; 20 21 import java.awt.Font ; 22 import java.awt.Point ; 23 import java.awt.Rectangle ; 24 import java.awt.Toolkit ; 25 import java.awt.event.ActionEvent ; 26 import java.io.IOException ; 27 import java.util.ArrayList ; 28 import java.util.Collections ; 29 import java.util.List ; 30 import java.util.Map ; 31 import javax.lang.model.element.TypeElement; 32 import javax.swing.SwingUtilities ; 33 import javax.swing.SwingUtilities ; 34 import javax.swing.text.BadLocationException ; 35 import javax.swing.text.Document ; 36 import javax.swing.text.JTextComponent ; 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 53 public class FastImportAction extends BaseAction { 54 55 public static final String NAME = "fast-import"; 56 57 58 public FastImportAction() { 59 super(NAME); 60 } 61 62 public void actionPerformed(ActionEvent evt, JTextComponent target) { 63 try { 64 final Rectangle carretRectangle = target.modelToView(target.getCaretPosition()); 65 final Font font = target.getFont(); 66 final Point where = new Point ( carretRectangle.x, carretRectangle.y + carretRectangle.height ); 67 SwingUtilities.convertPointToScreen( where, target); 68 69 final String 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 { 88 parameter.toPhase(Phase.RESOLVED); 89 final JavaSource javaSource = parameter.getJavaSource(); 90 Pair<Map <String , List <TypeElement>>, Map <String , List <TypeElement>>> result = new ComputeImports().computeCandidates(parameter, Collections.singleton(ident)); 91 92 final List <TypeElement> priviledged = result.a.get(ident); 93 94 if (priviledged == null) { 95 Toolkit.getDefaultToolkit().beep(); 97 return ; 98 } 99 100 final List <TypeElement> denied = new ArrayList (result.b.get(ident)); 101 102 denied.removeAll(priviledged); 103 104 SwingUtilities.invokeLater(new Runnable () { 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 ex) { 113 ErrorManager.getDefault().notify(ex); 114 } catch (BadLocationException ex) { 115 ErrorManager.getDefault().notify(ex); 116 } 117 } 118 119 120 private FileObject getFile(Document 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 |