This is an automated email from the git hooks/post-receive script. ben pushed a commit to branch master in repository autocomplete.
commit 7d67755e0b864533a3e2282ed5ded2f35f074494 Author: bobbylight <[email protected]> Date: Sun Feb 1 05:09:04 2009 +0000 Make text in description window copy-able. Add AutoComplete.setDescriptionWindowSize() method. Minor update to c.xml for demo. --- distfiles/readme.txt | 10 +- .../autocomplete/AbstractCompletionProvider.java | 69 ----------- .../ui/autocomplete/AutoCompleteDescWindow.java | 24 +++- .../ui/autocomplete/AutoCompletePopupWindow.java | 130 +++++++++++++++++--- src/org/fife/ui/autocomplete/AutoCompletion.java | 59 +++++++-- src/org/fife/ui/autocomplete/BasicCompletion.java | 4 +- .../ui/autocomplete/CompletionProviderBase.java | 75 ++++++++++- .../ui/autocomplete/DefaultCompletionProvider.java | 6 +- 8 files changed, 268 insertions(+), 109 deletions(-) diff --git a/distfiles/readme.txt b/distfiles/readme.txt index a728e2d..b6a9a42 100644 --- a/distfiles/readme.txt +++ b/distfiles/readme.txt @@ -1,5 +1,9 @@ AutoComplete Readme ------------------- +Please contact me if you are using AutoComplete in your project! I like to +know when people are finding it useful. Please send mail to: +robert -at- fifesoft dot com. + * About AutoComplete @@ -11,7 +15,7 @@ AutoComplete Readme * Example Usage - TODO + See http://fifesoft.com/autocomplete * License @@ -30,3 +34,7 @@ AutoComplete Readme * http://fifesoft.com/autocomplete Project home page, which contains general information and example source code. + + * http://javadoc.fifesoft.com/autocomplete/ + API documentation for the package. Note that this *will* change as + the library matures. diff --git a/src/org/fife/ui/autocomplete/AbstractCompletionProvider.java b/src/org/fife/ui/autocomplete/AbstractCompletionProvider.java index 0ece74c..1113b50 100644 --- a/src/org/fife/ui/autocomplete/AbstractCompletionProvider.java +++ b/src/org/fife/ui/autocomplete/AbstractCompletionProvider.java @@ -53,21 +53,6 @@ public abstract class AbstractCompletionProvider */ private Comparator comparator; - /** - * Text that marks the beginning of a parameter list, for example, '('. - */ - private char paramListStart; - - /** - * Text that marks the end of a parameter list, for example, ')'. - */ - private char paramListEnd; - - /** - * Text that separates items in a parameter list, for example, ", ". - */ - private String paramListSeparator; - /** * Constructor. @@ -154,15 +139,6 @@ public abstract class AbstractCompletionProvider /** - * {@inheritDoc} - */ - public void clearParameterizedCompletionParams() { - paramListEnd = paramListStart = 0; - paramListSeparator = null; - } - - - /** * Returns a list of <tt>Completion</tt>s in this provider with the * specified input text. * @@ -202,7 +178,6 @@ public abstract class AbstractCompletionProvider String text = getAlreadyEnteredText(comp); int index = Collections.binarySearch(completions, text, comparator); - //System.out.println(index + "(" + completions.size() + ")"); if (index<0) { index = -index - 1; } @@ -224,30 +199,6 @@ public abstract class AbstractCompletionProvider /** - * {@inheritDoc} - */ - public char getParameterListEnd() { - return paramListEnd; - } - - - /** - * {@inheritDoc} - */ - public String getParameterListSeparator() { - return paramListSeparator; - } - - - /** - * {@inheritDoc} - */ - public char getParameterListStart() { - return paramListStart; - } - - - /** * Removes the specified completion from this provider. This method * will not remove completions from the parent provider, if there is one. * @@ -270,26 +221,6 @@ public abstract class AbstractCompletionProvider /** - * {@inheritDoc} - */ - public void setParameterizedCompletionParams(char listStart, - String separator, char listEnd) { - if (listStart<0x20 || listStart==0x7F) { - throw new IllegalArgumentException("Invalid listStart"); - } - if (listEnd<0x20 || listEnd==0x7F) { - throw new IllegalArgumentException("Invalid listEnd"); - } - if (separator==null || separator.length()==0) { - throw new IllegalArgumentException("Invalid separator"); - } - paramListStart = listStart; - paramListSeparator = separator; - paramListEnd = listEnd; - } - - - /** * Returns whether <code>str</code> starts with <code>start</code>, * ignoring case. * diff --git a/src/org/fife/ui/autocomplete/AutoCompleteDescWindow.java b/src/org/fife/ui/autocomplete/AutoCompleteDescWindow.java index 311e4e9..dfed8c5 100644 --- a/src/org/fife/ui/autocomplete/AutoCompleteDescWindow.java +++ b/src/org/fife/ui/autocomplete/AutoCompleteDescWindow.java @@ -125,7 +125,7 @@ class AutoCompleteDescWindow extends JWindow implements HyperlinkListener { * Constructor. * * @param owner The parent window. - * @param ac The parent autocompletion. + * @param ac The parent auto-completion. */ public AutoCompleteDescWindow(Window owner, AutoCompletion ac) { @@ -202,6 +202,22 @@ class AutoCompleteDescWindow extends JWindow implements HyperlinkListener { /** + * Copies from the description text area, if it is visible and there is + * a selection. + * + * @return Whether a copy occurred. + */ + public boolean copy() { + if (isVisible() && + descArea.getSelectionStart()!=descArea.getSelectionEnd()) { + descArea.copy(); + return true; + } + return false; + } + + + /** * Returns the default background color to use for the description * window. * @@ -351,12 +367,12 @@ class AutoCompleteDescWindow extends JWindow implements HyperlinkListener { descArea.setSelectionColor(selBG); } - descArea.setEditable(false); +// descArea.setEditable(false); - // Make selection visible even though we are not editable. + // Make selection visible even though we are not focusable. descArea.getCaret().setSelectionVisible(true); - // Make it use "tooltip" background color. + // Make it use "tool tip" background color. descArea.setBackground(getDefaultBackground()); // Force JEditorPane to use a certain font even in HTML. diff --git a/src/org/fife/ui/autocomplete/AutoCompletePopupWindow.java b/src/org/fife/ui/autocomplete/AutoCompletePopupWindow.java index d256b22..f96284c 100644 --- a/src/org/fife/ui/autocomplete/AutoCompletePopupWindow.java +++ b/src/org/fife/ui/autocomplete/AutoCompletePopupWindow.java @@ -28,6 +28,7 @@ import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Window; import java.awt.event.ActionEvent; +import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; @@ -64,9 +65,43 @@ import javax.swing.text.JTextComponent; class AutoCompletePopupWindow extends JWindow implements CaretListener, ListSelectionListener, MouseListener { + /** + * The parent AutoCompletion instance. + */ private AutoCompletion ac; + + /** + * The list of completion choices. + */ private JList list; - private /*DefaultListModel*/CompletionListModel model; + + /** + * The contents of {@link #list()}. + */ + private CompletionListModel model; + + /** + * Optional popup window containing a description of the currently + * selected completion. + */ + private AutoCompleteDescWindow descWindow; + + /** + * The preferred size of the optional description window. This field + * only exists because the user may (and usually will) set the size of + * the description window before it exists (it must be parented to a + * Window). + */ + private Dimension preferredDescWindowSize; + + /** + * Whether the completion window and the optional description window + * should be displayed above the current caret position (as opposed to + * underneath it, which is preferred unless there is not enough space). + */ + private boolean aboveCaret; + + private int lastLine; private KeyActionPair escapeKap; private KeyActionPair upKap; @@ -79,14 +114,11 @@ class AutoCompletePopupWindow extends JWindow implements CaretListener, private KeyActionPair endKap; private KeyActionPair pageUpKap; private KeyActionPair pageDownKap; + private KeyActionPair ctrlCKap; private KeyActionPair oldEscape, oldUp, oldDown, oldLeft, oldRight, - oldEnter, oldTab, oldHome, oldEnd, oldPageUp, oldPageDown; - - private int lastLine; - - private AutoCompleteDescWindow descWindow; - private boolean aboveCaret; + oldEnter, oldTab, oldHome, oldEnd, oldPageUp, oldPageDown, + oldCtrlC; /** @@ -107,8 +139,9 @@ class AutoCompletePopupWindow extends JWindow implements CaretListener, list.addMouseListener(this); JPanel contentPane = new JPanel(new BorderLayout()); - JScrollPane sp = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, - JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); + JScrollPane sp = new JScrollPane(list, + JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, + JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); // In 1.4, JScrollPane.setCorner() has a bug where it won't accept // JScrollPane.LOWER_TRAILING_CORNER, even though that constant is @@ -136,7 +169,7 @@ class AutoCompletePopupWindow extends JWindow implements CaretListener, if (isVisible()) { // Should always be true int line = ac.getLineOfCaret(); if (line!=lastLine) { -lastLine = -1; + lastLine = -1; setVisible(false); } else { @@ -150,6 +183,22 @@ lastLine = -1; /** + * Creates the description window. + * + * @return The description window. + */ + private AutoCompleteDescWindow createDescriptionWindow() { + AutoCompleteDescWindow dw = new AutoCompleteDescWindow(this, ac); + Dimension size = preferredDescWindowSize; + if (size==null) { + size = getSize(); + } + dw.setSize(size); + return dw; + } + + + /** * Creates the mappings from keys to Actions we'll be putting into the * text component's ActionMap and InputMap. */ @@ -168,6 +217,7 @@ lastLine = -1; endKap = new KeyActionPair("End", new EndAction()); pageUpKap = new KeyActionPair("PageUp", new PageUpAction()); pageDownKap = new KeyActionPair("PageDown", new PageDownAction()); + ctrlCKap = new KeyActionPair("CtrlC", new CopyAction()); // Buffers for the actions we replace. oldEscape = new KeyActionPair(); @@ -181,6 +231,7 @@ lastLine = -1; oldEnd = new KeyActionPair(); oldPageUp = new KeyActionPair(); oldPageDown = new KeyActionPair(); + oldCtrlC = new KeyActionPair(); } @@ -260,6 +311,16 @@ lastLine = -1; replaceAction(im, am, KeyEvent.VK_PAGE_UP, pageUpKap, oldPageUp); replaceAction(im, am, KeyEvent.VK_PAGE_DOWN, pageDownKap, oldPageDown); + // Make Ctrl+C copy from description window. This isn't done + // automagically because the desc. window is not focusable, and copying + // from text components can only be done from focused components. + int key = KeyEvent.VK_C; + KeyStroke ks = KeyStroke.getKeyStroke(key, InputEvent.CTRL_MASK); + oldCtrlC.key = im.get(ks); + im.put(ks, ctrlCKap.key); + oldCtrlC.action = am.get(ctrlCKap.key); + am.put(ctrlCKap.key, ctrlCKap.action); + comp.addCaretListener(this); } @@ -469,6 +530,21 @@ lastLine = -1; /** + * Sets the size of the description window. + * + * @param size The new size. This cannot be <code>null</code>. + */ + public void setDescriptionWindowSize(Dimension size) { + if (descWindow!=null) { + descWindow.setSize(size); + } + else { + preferredDescWindowSize = size; + } + } + + + /** * Sets the default list cell renderer to use when a completion provider * does not supply its own. * @@ -540,9 +616,8 @@ lastLine = -1; lastLine = ac.getLineOfCaret(); selectFirstItem(); if (descWindow==null && ac.getShowDescWindow()) { - descWindow = new AutoCompleteDescWindow(this, ac); - descWindow.setSize(getSize()); - descWindow.setLocation(getX()+getWidth()+5, getY()); + descWindow = createDescriptionWindow(); + positionDescWindow(); // descWindow needs a kick-start the first time it's // displayed. Completion c = (Completion)list.getSelectedValue(); @@ -592,6 +667,12 @@ lastLine = -1; putBackAction(im, am, KeyEvent.VK_PAGE_UP, oldPageUp); putBackAction(im, am, KeyEvent.VK_PAGE_DOWN, oldPageDown); + // Ctrl+C + int key = KeyEvent.VK_C; + KeyStroke ks = KeyStroke.getKeyStroke(key, InputEvent.CTRL_MASK); + am.put(im.get(ks), oldCtrlC.action); // Original action + im.put(ks, oldCtrlC.key); // Original key + comp.removeCaretListener(this); } @@ -624,6 +705,21 @@ lastLine = -1; } + class CopyAction extends AbstractAction { + + public void actionPerformed(ActionEvent e) { + boolean doNormalCopy = false; + if (descWindow!=null && descWindow.isVisible()) { + doNormalCopy = !descWindow.copy(); + } + if (doNormalCopy) { + ac.getTextComponent().copy(); + } + } + + } + + class DownAction extends AbstractAction { public void actionPerformed(ActionEvent e) { @@ -713,7 +809,9 @@ lastLine = -1; // Ensure moving left hasn't moved us up a line, thus // hiding the popup window. if (comp.isVisible()) { -if (lastLine!=-1) doAutocomplete(); + if (lastLine!=-1) { + doAutocomplete(); + } } } } @@ -756,7 +854,9 @@ if (lastLine!=-1) doAutocomplete(); // Ensure moving right hasn't moved us up a line, thus // hiding the popup window. if (comp.isVisible()) { -if (lastLine!=-1) doAutocomplete(); + if (lastLine!=-1) { + doAutocomplete(); + } } } } diff --git a/src/org/fife/ui/autocomplete/AutoCompletion.java b/src/org/fife/ui/autocomplete/AutoCompletion.java index bf9977a..a36bce8 100644 --- a/src/org/fife/ui/autocomplete/AutoCompletion.java +++ b/src/org/fife/ui/autocomplete/AutoCompletion.java @@ -75,6 +75,14 @@ public class AutoCompletion implements HierarchyListener { private AutoCompletePopupWindow popupWindow; /** + * The preferred size of the optional description window. This field + * only exists because the user may (and usually will) set the size of + * the description window before it exists (it must be parented to a + * Window). + */ + private Dimension preferredDescWindowSize; + + /** * A "tooltip" describing a function just entered. */ private ParameterizedCompletionDescriptionToolTip descToolTip; @@ -397,6 +405,15 @@ public class AutoCompletion implements HierarchyListener { /** + * Hides any child windows being displayed by the auto-completion system. + */ + public void hideChildWindows() { + hidePopupWindow(); + hideToolTipWindow(); + } + + + /** * Hides the popup window, if it is visible. */ private void hidePopupWindow() { @@ -408,6 +425,9 @@ public class AutoCompletion implements HierarchyListener { } + /** + * Hides the parameter tool tip, if it is visible. + */ private void hideToolTipWindow() { if (descToolTip!=null) { descToolTip.setVisible(false, false); @@ -584,6 +604,10 @@ try { if (renderer!=null) { popupWindow.setListCellRenderer(renderer); } + if (preferredDescWindowSize!=null) { + popupWindow.setDescriptionWindowSize( + preferredDescWindowSize); + } } popupWindow.setCompletions(completions); @@ -625,9 +649,9 @@ try { /** - * Sets whether autocompletion is enabled. + * Sets whether auto-completion is enabled. * - * @param enabled Whether autocompletion is enabled. + * @param enabled Whether auto-completion is enabled. * @see #isAutoCompleteEnabled() */ public void setAutoCompleteEnabled(boolean enabled) { @@ -639,10 +663,10 @@ try { /** - * Sets whether, if a single autocomplete choice is available, it should + * Sets whether, if a single auto-complete choice is available, it should * be automatically inserted, without displaying the popup menu. * - * @param autoComplete Whether to autocomplete single choices. + * @param autoComplete Whether to auto-complete single choices. * @see #getAutoCompleteSingleChoices() */ public void setAutoCompleteSingleChoices(boolean autoComplete) { @@ -668,6 +692,20 @@ try { /** + * Sets the size of the description window. + * + * @param w The new width. + * @param h The new height. + */ + public void setDescriptionWindowSize(int w, int h) { + preferredDescWindowSize = new Dimension(w, h); + if (popupWindow!=null) { + popupWindow.setDescriptionWindowSize(preferredDescWindowSize); + } + } + + + /** * Sets the handler to use when an external URL is clicked in the * description window. This handler can perform some action, such as * open the URL in a web browser. The default implementation will open @@ -782,6 +820,7 @@ try { } textComponent = null; + popupWindow = null; } @@ -854,18 +893,15 @@ try { } public void componentHidden(ComponentEvent e) { - hidePopupWindow(); - hideToolTipWindow(); + hideChildWindows(); } public void componentMoved(ComponentEvent e) { - hidePopupWindow(); - hideToolTipWindow(); + hideChildWindows(); } public void componentResized(ComponentEvent e) { - hidePopupWindow(); - hideToolTipWindow(); + hideChildWindows(); } public void removeFrom(Window w) { @@ -877,8 +913,7 @@ try { } public void windowLostFocus(WindowEvent e) { - hidePopupWindow(); - hideToolTipWindow(); + hideChildWindows(); } } diff --git a/src/org/fife/ui/autocomplete/BasicCompletion.java b/src/org/fife/ui/autocomplete/BasicCompletion.java index 55f0d5e..4062a18 100644 --- a/src/org/fife/ui/autocomplete/BasicCompletion.java +++ b/src/org/fife/ui/autocomplete/BasicCompletion.java @@ -150,9 +150,7 @@ public class BasicCompletion extends AbstractCompletion { if (shortDesc==null) { return getInputText(); } - else { - return getInputText() + " - " + shortDesc; - } + return getInputText() + " - " + shortDesc; } diff --git a/src/org/fife/ui/autocomplete/CompletionProviderBase.java b/src/org/fife/ui/autocomplete/CompletionProviderBase.java index fa4632d..ac808c1 100644 --- a/src/org/fife/ui/autocomplete/CompletionProviderBase.java +++ b/src/org/fife/ui/autocomplete/CompletionProviderBase.java @@ -7,12 +7,15 @@ import javax.swing.text.JTextComponent; /** - * A base class for all standard completion providers. + * A base class for all standard completion providers. This class implements + * functionality that should be sharable across all <tt>CompletionProvider</tt> + * implementations. * * @author Robert Futrell * @version 1.0 + * @see AbstractCompletionProvider */ -abstract class CompletionProviderBase implements CompletionProvider { +public abstract class CompletionProviderBase implements CompletionProvider { /** * The parent completion provider. @@ -25,12 +28,36 @@ abstract class CompletionProviderBase implements CompletionProvider { */ private ListCellRenderer listCellRenderer; + /** + * Text that marks the beginning of a parameter list, for example, '('. + */ + private char paramListStart; + + /** + * Text that marks the end of a parameter list, for example, ')'. + */ + private char paramListEnd; + + /** + * Text that separates items in a parameter list, for example, ", ". + */ + private String paramListSeparator; + protected static final String EMPTY_STRING = ""; /** * {@inheritDoc} */ + public void clearParameterizedCompletionParams() { + paramListEnd = paramListStart = 0; + paramListSeparator = null; + } + + + /** + * {@inheritDoc} + */ public List getCompletions(JTextComponent comp) { List completions = getCompletionsImpl(comp); if (parent!=null) { @@ -62,6 +89,30 @@ abstract class CompletionProviderBase implements CompletionProvider { /** * {@inheritDoc} */ + public char getParameterListEnd() { + return paramListEnd; + } + + + /** + * {@inheritDoc} + */ + public String getParameterListSeparator() { + return paramListSeparator; + } + + + /** + * {@inheritDoc} + */ + public char getParameterListStart() { + return paramListStart; + } + + + /** + * {@inheritDoc} + */ public CompletionProvider getParent() { return parent; } @@ -78,6 +129,26 @@ abstract class CompletionProviderBase implements CompletionProvider { /** * {@inheritDoc} */ + public void setParameterizedCompletionParams(char listStart, + String separator, char listEnd) { + if (listStart<0x20 || listStart==0x7F) { + throw new IllegalArgumentException("Invalid listStart"); + } + if (listEnd<0x20 || listEnd==0x7F) { + throw new IllegalArgumentException("Invalid listEnd"); + } + if (separator==null || separator.length()==0) { + throw new IllegalArgumentException("Invalid separator"); + } + paramListStart = listStart; + paramListSeparator = separator; + paramListEnd = listEnd; + } + + + /** + * {@inheritDoc} + */ public void setParent(CompletionProvider parent) { this.parent = parent; } diff --git a/src/org/fife/ui/autocomplete/DefaultCompletionProvider.java b/src/org/fife/ui/autocomplete/DefaultCompletionProvider.java index c0b3c1d..f3c8319 100644 --- a/src/org/fife/ui/autocomplete/DefaultCompletionProvider.java +++ b/src/org/fife/ui/autocomplete/DefaultCompletionProvider.java @@ -235,7 +235,7 @@ public class DefaultCompletionProvider extends AbstractCompletionProvider { */ public void loadFromXML(InputStream in) throws IOException { - long start = System.currentTimeMillis(); + //long start = System.currentTimeMillis(); SAXParserFactory factory = SAXParserFactory.newInstance(); CompletionXMLParser handler = new CompletionXMLParser(this); @@ -258,8 +258,8 @@ public class DefaultCompletionProvider extends AbstractCompletionProvider { } catch (ParserConfigurationException pce) { throw new IOException(pce.toString()); } finally { - long time = System.currentTimeMillis() - start; - System.out.println("XML loaded in: " + time + "ms"); + //long time = System.currentTimeMillis() - start; + //System.out.println("XML loaded in: " + time + "ms"); bin.close(); } -- Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/autocomplete.git _______________________________________________ pkg-java-commits mailing list [email protected] http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-java-commits

