com.armhold.wicketsource.FileNavigator.java Source code

Java tutorial

Introduction

Here is the source code for com.armhold.wicketsource.FileNavigator.java

Source

/*
 *  Copyright 2012 George Armhold
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.armhold.wicketsource;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.GlobalSearchScope;

/**
 * supports navigation to the java source file in IDEA by classname + line number
 *
 * NB: This code was heavily inspired by the RemoteCall plugin by Alexander Zolotov
 * @see https://github.com/zolotov/RemoteCall
 */
public class FileNavigator {
    private static final Logger log = Logger.getInstance(FileNavigator.class);

    public void navigateTo(final String className, final int line) {
        ApplicationManager.getApplication().invokeLater(new Runnable() {
            public void run() {
                Project[] projects = ProjectManager.getInstance().getOpenProjects();

                for (Project project : projects) {
                    VirtualFile file = classNameToVirtualFile(project, className);
                    if (file != null) {
                        navigate(project, file, line);
                        return;
                    }
                }
            }
        });
    }

    private void navigate(Project project, VirtualFile file, int line) {
        OpenFileDescriptor openFileDescriptor = new OpenFileDescriptor(project, file, line, 0);
        if (openFileDescriptor.canNavigate()) {
            log.info("Trying to navigate to " + file.getPath() + ":" + line);
            openFileDescriptor.navigate(true);
            WindowManager.getInstance().suggestParentWindow(project).toFront();
        } else {
            log.info("Cannot navigate");
        }
    }

    public static VirtualFile classNameToVirtualFile(Project project, String className) {
        GlobalSearchScope scope = GlobalSearchScope.allScope(project);

        PsiClass psiClass = JavaPsiFacade.getInstance(project).findClass(className, scope);
        if (null == psiClass) {
            return null;
        }

        PsiElement psiClassSource = psiClass.getNavigationElement();

        // to avoid viewing the decompiled class
        PsiFile psiFile = psiClassSource.getContainingFile();

        return psiFile.getVirtualFile();
    }

}