View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Felix Berger
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.xnap.commons.gui.completion;
21  
22  import java.awt.Point;
23  import java.awt.Rectangle;
24  import javax.swing.text.BadLocationException;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.xnap.commons.gui.util.GUIHelper;
28  
29  /***
30   * This mode mimics KDE's automatic drop down mode. The longest common 
31   * unique completion is inserted after the cursor while all ambiguous
32   * completions are presented to the user below the text field in a completion
33   * popup window.
34   * 
35   * @author Felix Berger
36   */
37  public class AutomaticDropDownCompletionMode extends AutomaticCompletionMode
38  {
39  	private CompletionPopup popup = new CompletionPopup();
40      
41  	private static Log logger = LogFactory.getLog(AutomaticCompletionMode.class);
42  	
43  	protected void enable()
44  	{
45  		super.enable();
46  		popup.enablePopup(getCompletion());
47  	}
48  
49  	public void disable()
50  	{
51  		super.disable();
52  		popup.disablePopup();
53  	}
54  
55  	protected void complete(String prefix)
56  	{
57  		super.complete(prefix);
58  		if (getModel().getSize() > 0) {
59  			showPopup();
60  		}
61  	}
62  
63  	protected void showPopup()
64  	{
65  		if (!getTextComponent().isShowing()) {
66  			return;
67  		}
68  		
69  		if (getCompletion().isWholeTextCompletion()) {
70  			GUIHelper.showPopupMenu(popup, getTextComponent(), 0, 
71  									getTextComponent().getHeight());
72  		}
73  		else {
74  			try {
75  				int pos = getTextComponent().getCaretPosition() + 1;
76  				Rectangle r = getTextComponent().modelToView(pos);
77  				Point p = r.getLocation();
78  				GUIHelper.showPopupMenu(popup, getTextComponent(),
79  									p.x, p.y + r.height);
80  			}
81  			catch (BadLocationException ble) {
82  				logger.debug("bad location", ble);
83  			}			
84  		}
85  	}
86  	
87  }
88  
89