/*
 * 11/15/2011
 *
 * Copyright (C) 2010 Robert Futrell
 * robert_futrell at users.sourceforge.net
 * http://fifesoft.com/rsyntaxtextarea
 *
 * This library is distributed under a modified BSD license.  See the included
 * RSTALanguageSupport.License.txt file for details.
 */
package org.fife.rsta.ac;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.ActionEvent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.text.TextAction;

import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;


/**
 * Displays a popup dialog with the "Go to member" tree.  Language support
 * implementations that can do in-depth parsing of the source code in an editor
 * can create an {@link AbstractSourceTree} representing that source, and add
 * this action to <code>RSyntaxTextArea</code>'s input/action maps, so users
 * can easily navigate to functions, methods, etc.<p>
 *
 * The preferred keystroke to bind this action to is Ctrl+Shift+O
 * (Cmd+Shift+O on Mac).  Language supports should also be sure to uninstall
 * this shortcut when they are uninstalled themselves.
 *
 * @author Robert Futrell
 * @version 1.0
 * @see GoToMemberWindow
 */
public class GoToMemberAction extends TextAction {

	/**
	 * The outline tree class appropriate for the current language.
	 */
	private Class outlineTreeClass;


	/**
	 * Constructor.
	 *
	 * @param outlineTreeClass A class extending {@link AbstractSourceTree}.
	 *        This class must have a no-argument constructor.
	 */
	public GoToMemberAction(Class outlineTreeClass) {
		super("GoToType");
		this.outlineTreeClass = outlineTreeClass;
	}


	public void actionPerformed(ActionEvent e) {
		AbstractSourceTree tree = createTree();
		if (tree==null) {
			UIManager.getLookAndFeel().provideErrorFeedback(null);
			return;
		}
		RSyntaxTextArea textArea = (RSyntaxTextArea)getTextComponent(e);
		Window parent = SwingUtilities.getWindowAncestor(textArea);
		GoToMemberWindow gtmw = new GoToMemberWindow(parent, textArea, tree);
		setLocationBasedOn(gtmw, textArea);
		gtmw.setVisible(true);
	}


	/**
	 * Creates the outline tree.
	 *
	 * @return An instance of the outline tree.
	 */
	private AbstractSourceTree createTree() {
		AbstractSourceTree tree = null;
		try {
			tree = (AbstractSourceTree)outlineTreeClass.newInstance();
			tree.setSorted(true);
		} catch (RuntimeException re) { // FindBugs
			throw re;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tree;
	}


	/**
	 * Centers the window in the text area.
	 *
	 * @param gtmw The window to center.
	 * @param textArea The parent text area to center it in.
	 */
	private void setLocationBasedOn(GoToMemberWindow gtmw,
									RSyntaxTextArea textArea) {
		Rectangle visibleRect = textArea.getVisibleRect();
		Dimension gtmwPS = gtmw.getPreferredSize();
		int x = visibleRect.x + (visibleRect.width-gtmwPS.width)/2;
		int y = visibleRect.y + (visibleRect.height-gtmwPS.height)/2;
		Point p = new Point(x, y);
		SwingUtilities.convertPointToScreen(p, textArea);
		gtmw.setLocation(p);
	}


}
@@ -500,16 +500,56 @@
 anager;%0A
+import javax.swing.text.JTextComponent;%0A
 import j
@@ -1902,16 +1902,99 @@
 rn;%0A%09%09%7D%0A
+%09%09JTextComponent tc = getTextComponent(e);%0A%09%09if (tc instanceof RSyntaxTextArea) %7B%0A%09
 %09%09RSynta
@@ -2031,37 +2031,21 @@
 extArea)
-getTextComponent(e)
+tc
 ;%0A
+%09
 %09%09Window
@@ -2100,16 +2100,17 @@
 rea);%0A%09%09
+%09
 GoToMemb
@@ -2173,16 +2173,17 @@
 ree);%0A%09%09
+%09
 setLocat
@@ -2204,32 +2204,33 @@
 w, textArea);%0A%09%09
+%09
 gtmw.setVisible(
@@ -2236,16 +2236,91 @@
 (true);%0A
+%09%09%7D%0A%09%09else %7B%0A%09%09%09UIManager.getLookAndFeel().provideErrorFeedback(null);%0A%09%09%7D%0A
 %09%7D%0A%0A%0A%09/*

Allow tool tips to be specified for icons in IconRowHeader.