001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.java.editor.imports;
042:
043: import java.awt.Font;
044: import java.awt.Point;
045: import java.awt.Rectangle;
046: import java.awt.Toolkit;
047: import java.awt.event.ActionEvent;
048: import java.io.IOException;
049: import java.util.ArrayList;
050: import java.util.Collections;
051: import java.util.List;
052: import java.util.Map;
053: import javax.lang.model.element.TypeElement;
054: import javax.swing.SwingUtilities;
055: import javax.swing.SwingUtilities;
056: import javax.swing.text.BadLocationException;
057: import javax.swing.text.Document;
058: import javax.swing.text.JTextComponent;
059: import org.netbeans.api.java.source.Task;
060: import org.netbeans.api.java.source.CompilationController;
061: import org.netbeans.api.java.source.JavaSource;
062: import org.netbeans.api.java.source.JavaSource.Phase;
063: import org.netbeans.editor.BaseAction;
064: import org.netbeans.editor.Utilities;
065: import org.netbeans.modules.java.editor.imports.ComputeImports.Pair;
066: import org.netbeans.modules.java.editor.overridden.PopupUtil;
067: import org.openide.ErrorManager;
068: import org.openide.filesystems.FileObject;
069: import org.openide.loaders.DataObject;
070:
071: /**
072: *
073: * @author Jan Lahoda
074: */
075: public class FastImportAction extends BaseAction {
076:
077: public static final String NAME = "fast-import";
078:
079: /** Creates a new instance of FastImportAction */
080: public FastImportAction() {
081: super (NAME);
082: }
083:
084: public void actionPerformed(ActionEvent evt, JTextComponent target) {
085: try {
086: final Rectangle carretRectangle = target.modelToView(target
087: .getCaretPosition());
088: final Font font = target.getFont();
089: final Point where = new Point(carretRectangle.x,
090: carretRectangle.y + carretRectangle.height);
091: SwingUtilities.convertPointToScreen(where, target);
092:
093: final int position = target.getCaretPosition();
094: final String ident = Utilities.getIdentifier(Utilities
095: .getDocument(target), position);
096: FileObject file = getFile(target.getDocument());
097:
098: if (ident == null || file == null) {
099: Toolkit.getDefaultToolkit().beep();
100: return;
101: }
102:
103: JavaSource js = JavaSource.forFileObject(file);
104:
105: if (js == null) {
106: Toolkit.getDefaultToolkit().beep();
107: return;
108: }
109:
110: js.runUserActionTask(new Task<CompilationController>() {
111:
112: public void run(final CompilationController parameter)
113: throws IOException {
114: parameter.toPhase(Phase.RESOLVED);
115: final JavaSource javaSource = parameter
116: .getJavaSource();
117: Pair<Map<String, List<TypeElement>>, Map<String, List<TypeElement>>> result = new ComputeImports()
118: .computeCandidates(parameter, Collections
119: .singleton(ident));
120:
121: final List<TypeElement> priviledged = result.a
122: .get(ident);
123:
124: if (priviledged == null) {
125: //not found?
126: Toolkit.getDefaultToolkit().beep();
127: return;
128: }
129:
130: final List<TypeElement> denied = new ArrayList<TypeElement>(
131: result.b.get(ident));
132:
133: denied.removeAll(priviledged);
134:
135: SwingUtilities.invokeLater(new Runnable() {
136: public void run() {
137: ImportClassPanel panel = new ImportClassPanel(
138: priviledged, denied, font,
139: javaSource, position);
140: PopupUtil.showPopup(panel, "", where.x,
141: where.y, true,
142: carretRectangle.height);
143: }
144: });
145: }
146: }, true);
147: } catch (IOException ex) {
148: ErrorManager.getDefault().notify(ex);
149: } catch (BadLocationException ex) {
150: ErrorManager.getDefault().notify(ex);
151: }
152: }
153:
154: private FileObject getFile(Document doc) {
155: DataObject od = (DataObject) doc
156: .getProperty(Document.StreamDescriptionProperty);
157:
158: if (od == null)
159: return null;
160:
161: return od.getPrimaryFile();
162: }
163: }
|