Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 12/21/2008
   *
   * AutoCompleteDescWindow.java - A window containing a description of the
   * currently selected completion.
   * Copyright (C) 2008 Robert Futrell
   * robert_futrell at users.sourceforge.net
   * http://fifesoft.com/rsyntaxtextarea
   *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
  */
 package org.fife.ui.autocomplete;
 
 import java.awt.Color;
 import java.awt.Point;
 import java.util.List;
A "tooltip" that displays information on the function or method currently being entered.

Author(s):
Robert Futrell
Version:
1.0
 
The actual tooltip.
 
 	private JWindow tooltip;

The painter to paint borders around the variables.
 
The tags for the highlights around parameters.
 
 	List tags;

The parent AutoCompletion instance.
 
 	private AutoCompletion ac;

The label that holds the description.
 
 	private JLabel descLabel;

The completion being described.
 
Listens for events in the text component while this window is vislble.
 
	private Listener listener;

The minimum offset into the document that the caret can move to before this tooltip disappears.
	private int minPos;

The maximum offset into the document that the caret can move to before this tooltip disappears.
	private Position maxPos// Moves with text inserted.
The currently "selected" parameter in the displayed text.
	private int lastSelectedParam;
	private Object oldTabKey;
	private static final String IM_KEY_TAB = "ParamCompDescTip.Tab";
	private static final String IM_KEY_SHIFT_TAB = "ParamCompDescTip.ShiftTab";
	private static final String IM_KEY_ESCAPE = "ParamCompDescTip.Escape";
	private static final String IM_KEY_ENTER = "ParamCompDescTip.Enter";
	private static final String IM_KEY_CLOSING = "ParamCompDescTip.Closing";


Constructor.

Parameters:
owner The parent window.
ac The parent autocompletion.
pc The completion being described.
		 = new JWindow(owner);
		this. = ac;
		this. = pc;
		 = new JLabel();
					BorderFactory.createEmptyBorder(2, 5, 2, 5)));
		 = new Listener();
		 = new ArrayList(1); // Usually small
	}
		List paramHighlights = new ArrayList(1);
		Highlight[] highlights = tc.getHighlighter().getHighlights();
		for (int i=0; i<highlights.lengthi++) {
			if (highlights[i].getPainter()==) {
				paramHighlights.add(highlights[i]);
			}
		}
		return paramHighlights;
	}


Installs key bindings on the text component that facilitate the user editing this completion's parameters.

	private void installKeyBindings() {
		if (AutoCompletion.getDebug()) {
			..println("ToolTip: Installing keybindings");
		}
		InputMap im = tc.getInputMap();
		ActionMap am = tc.getActionMap();
		KeyStroke ks = KeyStroke.getKeyStroke(., 0);
		 = im.get(ks);
		im.put(ks);
		 = im.get(ks);
		ks = KeyStroke.getKeyStroke(., 0);
		 = im.get(ks);
		im.put(ks);
		ks = KeyStroke.getKeyStroke(., 0);
		 = im.get(ks);
		ks = KeyStroke.getKeyStroke(end);
		 = im.get(ks);
	}


Moves to and selects the next parameter.

	private void moveToNextParam() {
		int dot = tc.getCaretPosition();
		int tagCount = .size();
		if (tagCount==0) {
			setVisible(falsefalse);
		}
		Highlight currentNext = null;
		int pos = -1;
		List highlights = getParameterHighlights();
		for (int i=0; i<highlights.size(); i++) {
			Highlight hl = (Highlight)highlights.get(i);
			if (currentNext==null || currentNext.getStartOffset()<=dot ||
					(hl.getStartOffset()>dot &&
					hl.getStartOffset()<=currentNext.getStartOffset())) {
				currentNext = hl;
				pos = i;
			}
		}
		if (currentNext!=null && dot<currentNext.getStartOffset()) {
			 // "+1" is a workaround for Java Highlight issues.
			tc.setSelectionStart(currentNext.getStartOffset()+1);
			tc.setSelectionEnd(currentNext.getEndOffset());
		}
		else {
			setVisible(falsefalse);
		}
	}


Moves to and selects the previous parameter.

	private void moveToPreviousParam() {
		int tagCount = .size();
		if (tagCount==0) { // Should never happen
			setVisible(falsefalse);
		}
		int dot = tc.getCaretPosition();
		int selStart = tc.getSelectionStart()-1; // Workaround for Java Highlight issues.
		Highlight currentPrev = null;
		int pos = 0;
		Highlight[] highlights = h.getHighlights();
		for (int i=0; i<highlights.lengthi++) {
			Highlight hl = highlights[i];
			if (hl.getPainter()==) { // Only way to identify our own highlights
				if (currentPrev==null || currentPrev.getStartOffset()>=dot ||
						(hl.getStartOffset()<selStart &&
						hl.getStartOffset()>currentPrev.getStartOffset())) {
					currentPrev = hl;
					pos = i;
				}
			}
		}
		if (currentPrev!=null && dot>currentPrev.getStartOffset()) {
			 // "+1" is a workaround for Java Highlight issues.
			tc.setSelectionStart(currentPrev.getStartOffset()+1);
			tc.setSelectionEnd(currentPrev.getEndOffset());
		}
		else {
			setVisible(falsefalse);
		}
	}


Removes the bounding boxes around parameters.
	private void removeParameterHighlights() {
		for (int i=0; i<.size(); i++) {
		}
	}


Sets the location of this tooltip relative to the given rectangle.

Parameters:
r The visual position of the caret (in screen coordinates).
	public void setLocationRelativeTo(Rectangle r) {
		// Try putting our stuff "above" the caret first.
		int y = r.y - 5 - .getHeight();
		if (y<0) {
			y = r.y + r.height + 5;
		}
		// Get x-coordinate of completions.  Try to align left edge with the
		// caret first.
		int x = r.x;
		if (x<0) {
			x = 0;
		}
		else if (x+.getWidth()>screenSize.width) { // completions don't fit
			x = screenSize.width - .getWidth();
		}
	}


Toggles the visibility of this tooltip.

Parameters:
visible Whether the tooltip should be visible.
addParamListStart Whether or not CompletionProvider.getParameterListStart() should be added to the text component. If visible is false, this parameter is ignored.
	public void setVisible(boolean visibleboolean addParamListStart) {
		if (visible!=.isVisible()) {
			if (visible) {
				.install(tcaddParamListStart);
			}
			else {
			}
		}
	}


Removes the key bindings we installed.

	private void uninstallKeyBindings() {
		if (AutoCompletion.getDebug()) {
			..println("ToolTip: Uninstalling keybindings");
		}
		InputMap im = tc.getInputMap();
		ActionMap am = tc.getActionMap();
		KeyStroke ks = KeyStroke.getKeyStroke(., 0);
		im.put(ks);
		ks = KeyStroke.getKeyStroke(., 0);
		im.put(ks);
		ks = KeyStroke.getKeyStroke(., 0);
		im.put(ks);
		ks = KeyStroke.getKeyStroke(end);
	}


Updates the text in the tooltip to have the current parameter disiplayed in bold. The "current parameter" is determined from the current caret position.
	private void updateText() {
		int dot = tc.getCaretPosition();
		if (dot>0) {
			dot--; // Workaround for Java Highlight issues
		}
		int index = -1;
		List paramHighlights = getParameterHighlights();
		for (int i=0; i<paramHighlights.size(); i++) {
			Highlight h = (Highlight)paramHighlights.get(i);
			if (dot>=h.getStartOffset() && dot<h.getEndOffset()) {
				index = i;
				break;
			}
		}
		updateText(index);
	}


Updates the text in the tooltip to have the current parameter displayed in bold.

Parameters:
selectedParam The index of the selected parameter.
	private void updateText(int selectedParam) {
		// Don't redo everything if they're just using the arrow keys to move
		// through each char of a single parameter, for example.
		if (selectedParam==) {
			return;
		}
		 = selectedParam;
		StringBuffer sb = new StringBuffer("<html>");
		int paramCount = .getParamCount();
		for (int i=0; i<paramCounti++) {
			if (i==selectedParam) {
				sb.append("<b>");
			}
			if (i==selectedParam) {
				sb.append("</b>");
			}
			if (i<paramCount-1) {
			}
		}
		if (selectedParam>=0 && selectedParam<paramCount) {
							.getParam(selectedParam);
			String desc = param.getDescription();
			if (desc!=null) {
				sb.append("<br>");
				sb.append(desc);
			}
		}
	}


Updates the LookAndFeel of this window and the description window.
	public void updateUI() {
	}


Called when the user presses Enter while entering parameters.

Author(s):
Robert Futrell
Version:
1.0
	private class GotoEndAction extends AbstractAction {
		public void actionPerformed(ActionEvent e) {
			setVisible(falsefalse);
		}
	}


Called when the user types the character marking the closing of the parameter list, such as ')'.

Author(s):
Robert Futrell
Version:
1.0
	private class ClosingAction extends AbstractAction {
		public void actionPerformed(ActionEvent e) {
			int dot = tc.getCaretPosition();
			// Are they at or past the end of the parameters?
			if (dot>=.getOffset()-1) { // ">=" for overwrite mode
				if (dot==.getOffset()) { // Happens in overwrite mode
					tc.replaceSelection(Character.toString(end));
				}
				else { // Typical case.
					// Try to decide if we're closing a paren that is a part
					// of the (last) arg being typed.
					String text = getArgumentText(dot);
					if (text!=null) {
						int startCount = getCount(textstart);
						int endCount = getCount(textend);
						if (startCount>endCount) { // Just closing a paren
							tc.replaceSelection(Character.toString(end));
							return;
						}
					}
				}
				setVisible(falsefalse);
			}
			// If not (in the middle of parameters), just insert the paren.
			else {
				tc.replaceSelection(Character.toString(end));
			}
		}
		public String getArgumentText(int offs) {
			List paramHighlights = getParameterHighlights();
			if (paramHighlights==null || paramHighlights.size()==0) {
				return null;
			}
			for (int i=0; i<paramHighlights.size(); i++) {
				Highlight h = (Highlight)paramHighlights.get(i);
				if (offs>=h.getStartOffset() && offs<=h.getEndOffset()) {
					int start = h.getStartOffset() + 1;
					int len = h.getEndOffset() - start;
					Document doc = tc.getDocument();
					try {
						return doc.getText(startlen);
catch (BadLocationException ble) {
						return null;
					}
				}
			}
			return null;
		}
		public int getCount(String textchar ch) {
			int count = 0;
			int old = 0;
			int pos = 0;
			while ((pos=text.indexOf(chold))>-1) {
				count++;
				old = pos + 1;
			}
			return count;
		}
	}


Action performed when the user hits the escape key.

Author(s):
Robert Futrell
Version:
1.0
	private class HideAction extends AbstractAction {
		public void actionPerformed(ActionEvent e) {
			setVisible(falsefalse);
		}
	}


Listens for various events in the text component while this tooltip is visible.

Author(s):
Robert Futrell
Version:
1.0
	private class Listener implements FocusListenerCaretListener {

Called when the text component's caret moves.

Parameters:
e The event.
		public void caretUpdate(CaretEvent e) {
			if (==null) { // Sanity check
				setVisible(falsefalse);
			}
			int dot = e.getDot();
			if (dot< || dot>.getOffset()) {
				setVisible(falsefalse);
			}
		}


Called when the text component gains focus.

Parameters:
e The event.
		public void focusGained(FocusEvent e) {
			// Do nothing
		}


Called when the text component loses focus.

Parameters:
e The event.
		public void focusLost(FocusEvent e) {
			setVisible(falsefalse);
		}


Returns the text to insert for a parameter.

Parameters:
param The parameter.
Returns:
The text.
			String text = param.getName();
			if (text==null) {
				text = param.getType();
				if (text==null) { // Shouldn't ever happen
					text = "arg";
				}
			}
			return text;
		}


Installs this listener onto a text component.

Parameters:
tc The text component to install onto.
addParamListStart Whether or not CompletionProvider.getParameterListStart() should be added to the text component.
See also:
uninstall()
		public void install(JTextComponent tcboolean addParamStartList) {
			// Add listeners to the text component.
			StringBuffer sb = new StringBuffer();
			if (addParamStartList) {
			}
			int dot = tc.getCaretPosition() + sb.length();
			int paramCount = .getParamCount();
			List paramLocs = null;
			if (paramCount>0) {
				paramLocs = new ArrayList(paramCount);
			}
			try {
				// Get the range in which the caret can move before we hide
				// this tooltip.
				 = dot;
				int firstParamLen = 0;
				// Create the text to insert (keep it one completion for
				// performance and simplicity of undo/redo).
				int start = dot;
				for (int i=0; i<paramCounti++) {
					String paramText = getParamText(param);
					if (i==0) {
						firstParamLen = paramText.length();
					}
					sb.append(paramText);
					int end = start + paramText.length();
					paramLocs.add(new Point(startend));
					if (i<paramCount-1) {
						start = end + 2;
					}
				}
				// Insert the parameter text and add highlights around the
				// parameters.
				for (int i=0; i<paramCounti++) {
					Point pt = (Point)paramLocs.get(i);
					 // "-1" is a workaround for Java Highlight issues.
					.add(h.addHighlight(pt.x-1, pt.y));
				}
				// Go back and start at the first parameter.
				if (.getParamCount()>0) {
					tc.moveCaretPosition(dot+firstParamLen);
				}
catch (BadLocationException ble) {
				ble.printStackTrace(); // Never happens
			}
		}


Uninstalls this listener from the current text component.
		public void uninstall() {
			// Remove WeakReferences in javax.swing.text.
			 = null;
			 = -1;
		}
	}


Action performed when the user hits the tab key.

Author(s):
Robert Futrell
Version:
1.0
	private class NextParamAction extends AbstractAction {
		public void actionPerformed(ActionEvent e) {
		}
	}


Action performed when the user hits shift+tab.

Author(s):
Robert Futrell
Version:
1.0
	private class PrevParamAction extends AbstractAction {
		public void actionPerformed(ActionEvent e) {
		}
	}
New to GrepCode? Check out our FAQ X