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.event.DocumentEvent;
25  import javax.swing.event.DocumentListener;
26  import javax.swing.text.BadLocationException;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.xnap.commons.gui.util.GUIHelper;
30  
31  
32  /***
33   * Uses a {@link org.xnap.commons.gui.completion.CompletionPopup} to present
34   * its completions to the user.
35   * 
36   * @author Felix Berger
37   */
38  public class DropDownListCompletionMode extends AbstractCompletionMode
39  {
40      protected CompletionPopup popup = new CompletionPopup();
41  	
42  	protected DocumentListener listener = new DocumentHandler();
43  	
44  	private static Log logger = LogFactory.getLog(DropDownListCompletionMode.class);
45  
46      protected void enable()
47      {
48      	popup.enablePopup(getCompletion());
49          getTextComponent().getDocument().addDocumentListener(listener);
50      }
51  
52  	public void disable()
53  	{
54  		popup.disablePopup();
55  		getTextComponent().getDocument().removeDocumentListener(listener);
56  	}
57  	
58  	protected void showPopup()
59  	{
60  		if (!getTextComponent().isShowing()) {
61  			return;
62  		}
63  
64  		if (getCompletion().isWholeTextCompletion()) {
65  			GUIHelper.showPopupMenu(popup, getTextComponent(), 0, 
66  									getTextComponent().getHeight());
67  		}
68  		else {
69  			try {
70  				int pos = getTextComponent().getCaretPosition() + 1;
71  				Rectangle r = getTextComponent().modelToView(pos);
72  				Point p = r.getLocation();
73  				GUIHelper.showPopupMenu(popup, getTextComponent(),
74  									p.x, p.y + r.height);
75  			}
76  			catch (BadLocationException ble) {
77  				logger.debug("bad location", ble);
78  			}
79  		}
80  	}
81  
82      private class DocumentHandler implements DocumentListener
83      {
84          public void insertUpdate(DocumentEvent e)
85          {
86              if (e.getLength() == 1 && getModel().complete(getText())) {
87                 showPopup();
88              }
89              else {
90              	popup.setVisible(false);
91              }
92          }
93  
94          public void removeUpdate(DocumentEvent e)
95          {
96              if (e.getLength() == 1 &&  getModel().complete(getText())) {
97                  showPopup();
98              }
99              else {
100             	popup.setVisible(false);
101             }
102         }
103 
104 		/*
105 		 * @see javax.swing.event.DocumentListener#changedUpdate(javax.swing.event.DocumentEvent)
106 		 */
107 		public void changedUpdate(DocumentEvent e)
108 		{
109 		}
110     }
111 }