alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.editor.java;

import org.netbeans.api.java.classpath.GlobalPathRegistry;
import org.netbeans.editor.Utilities;
import org.netbeans.editor.ext.java.JavaFastOpen;
import org.netbeans.jmi.javamodel.JavaClass;
import org.netbeans.modules.javacore.ClassIndex;
import org.netbeans.modules.javacore.internalapi.JavaMetamodel;

import org.openide.NotifyDescriptor;
import org.openide.cookies.EditorCookie;
import org.openide.filesystems.FileObject;
import org.openide.nodes.Node;
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;
import org.openide.windows.TopComponent;

import java.text.MessageFormat;
import java.util.*;
import java.awt.event.ActionEvent;
import javax.swing.JEditorPane;

/**
 * Open java source using java JMI interfaces.
 *
 * @author Dusan Balek
 * @version 1.0
 */

public class NbJavaJMIFastOpen extends JavaFastOpen {

    protected List findClasses(String exp) {
        return findAllClasses(exp, true);
    }

    public static void showFastOpen() {
        // set class or class fragment from current cursor position
        // Issue#: 10639
        Node[] arr = TopComponent.getRegistry ().getActivatedNodes ();
        String initSearchText = null;
        if (arr.length > 0) {
            EditorCookie ec = (EditorCookie) arr[0].getCookie (EditorCookie.class);
            if (ec != null) {
                JEditorPane[] openedPanes = ec.getOpenedPanes ();
                if (openedPanes != null) {
                    initSearchText = Utilities.getSelectionOrIdentifier(openedPanes [0]);
                }
            }
        }
        RequestProcessor.getDefault().post(new ShowDialogRunnable(initSearchText));
    }

    public void actionPerformed(ActionEvent evt) {
        Object src = evt.getSource();
        if (src == getButtons()[0] || src == getPanel() ) { // Open
            setDialogVisible(false);
            final int selIndex = getResultList().getSelectedIndex();
            if (selIndex >= 0) {
                RequestProcessor.getDefault().post(new Runnable() {
                    public void run() {
                        Object item = getResultList().getModel().getElementAt(selIndex);
                        JavaClass cls = item instanceof NbJMIResultItem.ClassResultItem ? (JavaClass)((NbJMIResultItem.ClassResultItem)item).getAssociatedObject() : null;
                        if (cls != null) {
                            if (!JMIUtils.openElement(cls)) {
                                String msg = NbBundle.getBundle(JavaKit.class).getString("goto_source_source_not_found"); // NOI18N
                                msg = MessageFormat.format(msg, new Object [] { cls.getName() } );
                                org.openide.DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(msg));
                            }
                        }
                    }
                });
            }
        } else {
            super.actionPerformed(evt);
        }
    }

    private static List findAllClasses(String exp, boolean createResultItems) {
        List ret = new ArrayList();
        Set cpRoots = GlobalPathRegistry.getDefault().getSourceRoots();
        JavaMetamodel.getDefaultRepository().beginTrans(false);
        try {
            List classes = new ArrayList();
            for (Iterator it = cpRoots.iterator(); it.hasNext();) {
                ClassIndex ci = ClassIndex.getIndex(JavaMetamodel.getManager().getJavaExtent((FileObject)it.next()));
                if (ci != null)
                    classes.addAll(ci.getClassesBySNPrefix(exp, JMIUtils.isCaseSensitive()));
            }
            if (!createResultItems)
                return classes;
            for (Iterator it = classes.iterator(); it.hasNext();) {
                JavaClass cls = (JavaClass)it.next();
                if (!cls.isInner())
                    ret.add(new NbJMIResultItem.ClassResultItem(cls, true));
            }
            Collections.sort(ret, JMIUtils.NATURAL_MEMBER_NAME_COMPARATOR);
        } finally {
            JavaMetamodel.getDefaultRepository().endTrans();
        }
        return ret;
    }

    private static class ShowDialogRunnable implements Runnable {

        private String searchText;

        /** Whether to post the request to Event Dispatch Thread */
        private boolean postToEDT;

        ShowDialogRunnable(String searchText) {
            this.searchText = searchText;
            this.postToEDT = true;
        }

        public void run() {
            if (postToEDT) {
                if (searchText != null) {
                    List l = findAllClasses(searchText, false);
                    if (l.size () < 1) searchText = null;
                }
                postToEDT = false;
                Utilities.runInEventDispatchThread(this);
            } else { // execute directly
                if (fastOpen == null) {
                    fastOpen = new NbJavaJMIFastOpen();
                }
                if (searchText != null) {
                    fastOpen.setSearchText (searchText);
                }
                fastOpen.setDialogVisible(true);
            }
        }
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.