[autocomplete] 82/143: JavaLanguageSupport: Better support for links in Javadoc. Still a little work to do.

Benjamin Mesing ben at alioth.debian.org
Sat Oct 19 12:53:22 UTC 2013


This is an automated email from the git hooks/post-receive script.

ben pushed a commit to branch master
in repository autocomplete.

commit 60bb8d718dc20858f800323c2a10f59c89965254
Author: bobbylight <robert at fifesoft.com>
Date:   Sat May 12 04:20:46 2012 +0000

    JavaLanguageSupport: Better support for links in Javadoc.  Still a little work to do.
---
 .../ui/autocomplete/AutoCompleteDescWindow.java    |  178 +++++++++++++++-----
 .../fife/ui/autocomplete/DescWindowCallback.java   |   35 ++++
 .../fife/ui/autocomplete/ExternalURLHandler.java   |   17 +-
 3 files changed, 185 insertions(+), 45 deletions(-)

diff --git a/src/org/fife/ui/autocomplete/AutoCompleteDescWindow.java b/src/org/fife/ui/autocomplete/AutoCompleteDescWindow.java
index 2c3fed0..0fac65e 100644
--- a/src/org/fife/ui/autocomplete/AutoCompleteDescWindow.java
+++ b/src/org/fife/ui/autocomplete/AutoCompleteDescWindow.java
@@ -16,14 +16,12 @@ import java.awt.Graphics;
 import java.awt.Insets;
 import java.awt.Window;
 import java.awt.event.ActionEvent;
-import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.ResourceBundle;
-import javax.imageio.ImageIO;
 import javax.swing.AbstractAction;
 import javax.swing.Action;
 import javax.swing.BorderFactory;
@@ -53,7 +51,8 @@ import org.fife.ui.rsyntaxtextarea.PopupWindowDecorator;
  * @author Robert Futrell
  * @version 1.0
  */
-class AutoCompleteDescWindow extends JWindow implements HyperlinkListener {
+class AutoCompleteDescWindow extends JWindow implements HyperlinkListener,
+				DescWindowCallback {
 
 	/**
 	 * The parent AutoCompletion instance.
@@ -208,10 +207,10 @@ class AutoCompleteDescWindow extends JWindow implements HyperlinkListener {
 	/**
 	 * Sets the currently displayed description and updates the history.
 	 *
-	 * @param html The new description.
+	 * @param historyItem The item to add to the history.
 	 */
-	private void addToHistory(String html) {
-		history.add(++historyPos, html);
+	private void addToHistory(HistoryEntry historyItem) {
+		history.add(++historyPos, historyItem);
 		clearHistoryAfterCurrentPos();
 		setActionStates();
 	}
@@ -280,14 +279,20 @@ class AutoCompleteDescWindow extends JWindow implements HyperlinkListener {
 		HyperlinkEvent.EventType type = e.getEventType();
 
 		if (type.equals(HyperlinkEvent.EventType.ACTIVATED)) {
+
+			// Custom hyperlink handler for this completion type
+			ExternalURLHandler handler = ac.getExternalURLHandler();
+			if (handler!=null) {
+				HistoryEntry current = (HistoryEntry)history.
+												get(historyPos);
+				handler.urlClicked(e, current.completion, this);
+				return;
+			}
+
+			// No custom handler...
 			URL url = e.getURL();
 			if (url!=null) {
-				ExternalURLHandler handler = ac.getExternalURLHandler();
-				if (handler!=null) {
-					handler.urlClicked(url);
-					return;
-				}
-				// No handler - try loading in external browser (Java 6+ only).
+				// Try loading in external browser (Java 6+ only).
 				try {
 					Util.browse(new URI(url.toString()));
 				} catch (/*IO*/URISyntaxException ioe) {
@@ -315,6 +320,7 @@ class AutoCompleteDescWindow extends JWindow implements HyperlinkListener {
 					}
 				}
 			}
+
 		}
 
 	}
@@ -324,8 +330,25 @@ class AutoCompleteDescWindow extends JWindow implements HyperlinkListener {
 	 * Enables or disables the back and forward actions as appropriate.
 	 */
 	private void setActionStates() {
-		backAction.setEnabled(historyPos>0);
-		forwardAction.setEnabled(historyPos>-1 && historyPos<history.size()-1);
+		// TODO: Localize this text!
+		String desc = null;
+		if (historyPos>0) {
+			backAction.setEnabled(true);
+			desc = "Back to " + history.get(historyPos-1);
+		}
+		else {
+			backAction.setEnabled(false);
+		}
+		backAction.putValue(Action.SHORT_DESCRIPTION, desc);
+		if (historyPos>-1 && historyPos<history.size()-1) {
+			forwardAction.setEnabled(true);
+			desc = "Forward to " + history.get(historyPos+1);
+		}
+		else {
+			forwardAction.setEnabled(false);
+			desc = null;
+		}
+		forwardAction.putValue(Action.SHORT_DESCRIPTION, desc);
 	}
 
 
@@ -348,26 +371,51 @@ class AutoCompleteDescWindow extends JWindow implements HyperlinkListener {
 	 *        (as opposed to clearing it and starting anew).
 	 */
 	protected void setDescriptionFor(Completion item, boolean addToHistory) {
+		setDescriptionFor(item, null, addToHistory);
+	}
+
+
+	/**
+	 * Sets the description displayed in this window.
+	 *
+	 * @param item The item whose description you want to display.
+	 * @parma anchor The anchor to jump to, or <code>null</code> if none.
+	 * @param addToHistory Whether to add this page to the page history
+	 *        (as opposed to clearing it and starting anew).
+	 */
+	protected void setDescriptionFor(Completion item, String anchor,
+									boolean addToHistory) {
 		timer.stop();
-		timerAction.setCompletion(item, addToHistory);
+		timerAction.setCompletion(item, anchor, addToHistory);
 		timer.start();
 	}
 
 
-	private void setDisplayedDesc(String desc, boolean addToHistory) {
+	private void setDisplayedDesc(Completion completion, final String anchor,
+									boolean addToHistory) {
 
+		String desc = completion==null ? null : completion.getSummary();
 		if (desc==null) {
 			desc = "<html><em>" + getString("NoDescAvailable") + "</em>";
 		}
 		descArea.setText(desc);
-		descArea.setCaretPosition(0); // In case of scrolling
+		if (anchor!=null) {
+			SwingUtilities.invokeLater(new Runnable() {
+				public void run() {
+					descArea.scrollToReference(anchor);
+				}
+			});
+		}
+		else {
+			descArea.setCaretPosition(0); // In case of scrolling
+		}
 
 		if (!addToHistory) {
 			// Remove everything first if this is going to be the only
 			// thing in history.
 			clearHistory();
 		}
-		addToHistory(desc);
+		addToHistory(new HistoryEntry(completion, desc, null));
 
 	}
 
@@ -382,6 +430,19 @@ class AutoCompleteDescWindow extends JWindow implements HyperlinkListener {
 		super.setVisible(visible);
 	}
 
+
+	/**
+	 * Callback for custom <code>ExternalURLHandler</code>s.
+	 *
+	 * @param completion The completion to display.
+	 * @param anchor The anchor in the HTML to jump to, or <code>null</code>
+	 *        if none.
+	 */
+	public void showSummaryFor(Completion completion, String anchor) {
+		setDescriptionFor(completion, anchor, true);
+	}
+
+
 	/**
 	 * Called by the parent completion popup window the LookAndFeel is updated.
 	 */
@@ -396,23 +457,54 @@ class AutoCompleteDescWindow extends JWindow implements HyperlinkListener {
 
 
 	/**
+	 * A completion and its cached summary text.
+	 */
+	private static class HistoryEntry {
+
+		public Completion completion;
+		public String summary;
+		public String anchor;
+
+		public HistoryEntry(Completion completion, String summary,
+									String anchor) {
+			this.completion = completion;
+			this.summary = summary;
+			this.anchor = anchor;
+		}
+
+		/**
+		 * Overridden to display a short name for the completion, since it's
+		 * used in the tool tips for the "back" and "forward" buttons.
+		 *
+		 * @return A string representation of this history entry.
+		 */
+		public String toString() {
+			return completion.getInputText();
+		}
+
+	}
+
+
+	/**
 	 * Action that actually updates the summary text displayed.
 	 */
 	private class TimerAction extends AbstractAction {
 
 		private Completion completion;
+		private String anchor;
 		private boolean addToHistory;
 
 		/**
 		 * Called when the timer is fired.
 		 */
 		public void actionPerformed(ActionEvent e) {
-			String desc = completion==null ? null : completion.getSummary();
-			setDisplayedDesc(desc, addToHistory);
+			setDisplayedDesc(completion, anchor, addToHistory);
 		}
 
-		public void setCompletion(Completion c, boolean addToHistory) {
+		public void setCompletion(Completion c, String anchor,
+									boolean addToHistory) {
 			this.completion = c;
+			this.anchor = anchor;
 			this.addToHistory = addToHistory;
 		}
 
@@ -428,20 +520,22 @@ class AutoCompleteDescWindow extends JWindow implements HyperlinkListener {
 			String img = "org/fife/ui/autocomplete/arrow_" +
 						(ltr ? "left.png" : "right.png");
 			ClassLoader cl = getClass().getClassLoader();
-			URL url = cl.getResource(img);
-			try {
-				Icon icon = new ImageIcon(ImageIO.read(url));
-				putValue(Action.SMALL_ICON, icon);
-			} catch (IOException ioe) { // Never happens
-				ioe.printStackTrace();
-				putValue(Action.SHORT_DESCRIPTION, "Back");
-			}
+			Icon icon = new ImageIcon(cl.getResource(img));
+			putValue(Action.SMALL_ICON, icon);
 		}
 
 		public void actionPerformed(ActionEvent e) {
 			if (historyPos>0) {
-				descArea.setText((String)history.get(--historyPos));
-				descArea.setCaretPosition(0);
+				HistoryEntry pair = (HistoryEntry)history.
+													get(--historyPos);
+				descArea.setText(pair.summary);
+				if (pair.anchor!=null) {
+					System.out.println("Scrolling to: " + pair.anchor);
+					descArea.scrollToReference(pair.anchor);
+				}
+				else {
+					descArea.setCaretPosition(0);
+				}
 				setActionStates();
 			}
 		}
@@ -458,20 +552,22 @@ class AutoCompleteDescWindow extends JWindow implements HyperlinkListener {
 			String img = "org/fife/ui/autocomplete/arrow_" +
 							(ltr ? "right.png" : "left.png");
 			ClassLoader cl = getClass().getClassLoader();
-			URL url = cl.getResource(img);
-			try {
-				Icon icon = new ImageIcon(ImageIO.read(url));
-				putValue(Action.SMALL_ICON, icon);
-			} catch (IOException ioe) { // Never happens
-				ioe.printStackTrace();
-				putValue(Action.SHORT_DESCRIPTION, "Forward");
-			}
+			Icon icon = new ImageIcon(cl.getResource(img));
+			putValue(Action.SMALL_ICON, icon);
 		}
 
 		public void actionPerformed(ActionEvent e) {
 			if (history!=null && historyPos<history.size()-1) {
-				descArea.setText((String)history.get(++historyPos));
-				descArea.setCaretPosition(0);
+				HistoryEntry pair = (HistoryEntry)history.
+											get(++historyPos);
+				descArea.setText(pair.summary);
+				if (pair.anchor!=null) {
+					System.out.println("Scrolling to: " + pair.anchor);
+					descArea.scrollToReference(pair.anchor);
+				}
+				else {
+					descArea.setCaretPosition(0);
+				}
 				setActionStates();
 			}
 		}
diff --git a/src/org/fife/ui/autocomplete/DescWindowCallback.java b/src/org/fife/ui/autocomplete/DescWindowCallback.java
new file mode 100644
index 0000000..37334ca
--- /dev/null
+++ b/src/org/fife/ui/autocomplete/DescWindowCallback.java
@@ -0,0 +1,35 @@
+/*
+ * 05/11/2012
+ *
+ * Copyright (C) 2012 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.ui.autocomplete;
+
+
+/**
+ * Passed to {@link ExternalURLHandler}s as a way for them to display a summary
+ * for a new completion in response to a link event.
+ *
+ * @author Robert Futrell
+ * @version 1.0
+ * @see ExternalURLHandler
+ */
+public interface DescWindowCallback {
+
+
+	/**
+	 * Callback allowing a new code completion's description to be displayed
+	 * in the description window.
+	 *
+	 * @param completion The new completion.
+	 * @param anchor The anchor to scroll to, or <code>null</code> if none.
+	 */
+	public void showSummaryFor(Completion completion, String anchor);
+
+
+}
\ No newline at end of file
diff --git a/src/org/fife/ui/autocomplete/ExternalURLHandler.java b/src/org/fife/ui/autocomplete/ExternalURLHandler.java
index b7c8622..08d4a50 100644
--- a/src/org/fife/ui/autocomplete/ExternalURLHandler.java
+++ b/src/org/fife/ui/autocomplete/ExternalURLHandler.java
@@ -9,7 +9,7 @@
  */
 package org.fife.ui.autocomplete;
 
-import java.net.URL;
+import javax.swing.event.HyperlinkEvent;
 
 
 /**
@@ -17,7 +17,12 @@ import java.net.URL;
  * If no handler is installed, and if running in Java 6, the system default
  * web browser is used to open the URL.  If not running Java 6, nothing will
  * happen.  If you want browser support for pre-Java 6 JRE's, you will need
- * to register one of these callbacks on your {@link AutoCompletion}.
+ * to register one of these callbacks on your {@link AutoCompletion}, and
+ * open the URL in a web browser yourself.<p>
+ *
+ * Alternatively, folks implementing robust code completion support for a
+ * language might install an <code>ExternalURLHandler</code> to handle
+ * navigating through linked documentation of objects, functions, etc.
  *
  * @author Robert Futrell
  * @version 1.0
@@ -29,9 +34,13 @@ public interface ExternalURLHandler {
 	/**
 	 * Called when an external URL is clicked in the description window.
 	 *
-	 * @param url The URL.
+	 * @param e The event containing the hyperlink clicked.
+	 * @param c The completion currently being displayed.
+	 * @param callback Allows you to display new content in the description
+	 *        window.
 	 */
-	public void urlClicked(URL url);
+	public void urlClicked(HyperlinkEvent e, Completion c,
+						DescWindowCallback callback);
 
 
 }
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/autocomplete.git



More information about the pkg-java-commits mailing list