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

package org.netbeans.modules.tasklist.usertasks;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JEditorPane;
import org.netbeans.modules.tasklist.core.TLUtils;

import org.openide.cookies.EditorCookie;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
import org.openide.nodes.Node;
import org.openide.text.CloneableEditor;
import org.openide.text.CloneableEditorSupport;
import org.openide.text.NbDocument;
import org.openide.util.NbBundle;
import org.openide.windows.Mode;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;

/**
 * Utility methods extracted from TLUtil because
 * non-usertasks usages exists.
 */
public final class UTUtils {
    public static final Logger LOGGER = TLUtils.getLogger(UTUtils.class);
    
    static {
        LOGGER.setLevel(Level.OFF);
    }

    public static String getString(String key) {
        return NbBundle.getBundle(UTUtils.class).getString(key);
    }

    public static char getChar(String key) {
        return NbBundle.getBundle(UTUtils.class).getString(key).charAt(0);
    }

    public static String getMessage(String key, Object obj) {
        return NbBundle.getMessage(UTUtils.class,key, obj);
    }

    public static String getMessage(String key, Object obj, Object obj2) {
        return NbBundle.getMessage(UTUtils.class,key, obj, obj2);
    }

    /**
     * Utility method which attempts to find the activated nodes
     *	for the currently showing topcomponent in the editor window.
     *
     * @return editor nodes or null
     */
    public static Node[] getEditorNodes() {
        // First try to get the editor window itself; if you right click
        // on a node in the Todo Window, that node becomes the activated
        // node (which is good - it makes the properties window show the
        // todo item's properties, etc.) but that means that we can't
        // find the editor position via the normal means.
        // So, we go hunting for the topmosteditor tab, and when we find it,
        // ask for its nodes.
        Node[] nodes = null;
        WindowManager wm = WindowManager.getDefault();

        // HACK ALERT !!! HACK ALERT!!! HACK ALERT!!!
        // Look for the source editor window, and then go through its
        // top components, pick the one that is showing - that's the
        // front one!
        Mode mode  = wm.findMode(CloneableEditorSupport.EDITOR_MODE);
        if (mode == null) {
            return null;
        }
        TopComponent [] tcs = mode.getTopComponents();
        for (int j = 0; j < tcs.length; j++) {
            TopComponent tc = tcs[j];
            if (tc instanceof CloneableEditor) {
                // Found the source editor...
                if (tcs[j].isShowing()) {
                    nodes = tcs[j].getActivatedNodes();
                    break;
                }
            }
        }
        return nodes;
    }

    /**
     * Finds cursor position.
     *
     * @param nodes nodes to search. May be null
     * @return Object[2]:
     *     [0] - File name as String
     *     [1] - line number as Integer
     * Returns null if nothing found.
     */
    public static Object[] findCursorPosition(Node[] nodes) {
        if (nodes == null) {
            return null;
        }

        String filename = null;
        int line = 1;

        for (int i = 0; i < nodes.length; i++) {
            DataObject dao = (DataObject)nodes[i].getCookie(DataObject.class);
            if (dao != null) {
                FileObject fo = dao.getPrimaryFile();
                File file = FileUtil.toFile(fo);
                if (file == null) {
                    return null;
                }
                filename = file.getPath();
                EditorCookie ec = (EditorCookie)nodes[i].getCookie(EditorCookie.class);
                if (ec != null) {
                    JEditorPane[] editorPanes = ec.getOpenedPanes();
                    if ((editorPanes != null) && (editorPanes.length > 0)) {
                        line = NbDocument.findLineNumber(
                        ec.getDocument(),
                        editorPanes[0].getCaret().getDot()) + 1;
                    }
                }
                break;
            }
        }

        if (filename == null)
            return null;

        return new Object[] {filename, new Integer(line)};
    }
}
... 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.