View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package org.xnap.gui.component;
20  
21  import java.awt.Point;
22  import java.awt.Rectangle;
23  
24  import javax.swing.event.DocumentEvent;
25  import javax.swing.event.DocumentListener;
26  import javax.swing.text.BadLocationException;
27  import javax.swing.text.JTextComponent;
28  
29  import org.apache.log4j.Logger;
30  import org.xnap.XNap;
31  import org.xnap.gui.util.GUIHelper;
32  
33  
34  /***
35   * Uses a {@link org.xnap.gui.component.CompletionPopup} to present
36   * its completions to the user.
37   */
38  public class DropDownListCompletionMode extends CompletionMode
39  {
40      protected CompletionPopup popup = null;
41  	
42  	protected DocumentListener listener = new DocumentHandler();
43  	
44  	private static Logger logger = Logger.getLogger(DropDownListCompletionMode.class);
45  
46      /***
47       * @param textComponent
48       * @param wholeText
49       */
50      public DropDownListCompletionMode(JTextComponent textComponent,
51                                        boolean wholeText)
52      {
53          super(textComponent, wholeText);
54      }
55  
56      /***
57       * @param textComponent
58       */
59      public DropDownListCompletionMode(JTextComponent textComponent)
60      {
61          super(textComponent);
62      }
63  
64      /***
65       * @param textComponent
66       * @param model
67       * @param wholeText
68       */
69      public DropDownListCompletionMode(JTextComponent textComponent,
70                                        CompletionModel model, boolean wholeText)
71      {
72          super(textComponent, model, wholeText);
73      }
74  
75  	public DropDownListCompletionMode(JTextComponent textComponent,
76  									  CompletionModel model)
77  	{
78  		super(textComponent, model);
79  	}
80  
81      /*
82       * @see org.xnap.gui.component.CompletionMode#getName()
83       */
84      public String getName()
85      {
86          return XNap.tr("Dropdown List");
87      }
88  
89      protected void enable()
90      {
91  		if (popup == null) {
92  			popup = new CompletionPopup(this);
93  			popup.enablePopup();
94  		}
95  		else {
96  			popup.enablePopup();
97  		}
98          getTextComponent().getDocument().addDocumentListener(listener);
99      }
100 
101 	protected void disable()
102 	{
103 		if (popup != null) {
104 			popup.disablePopup();
105 		}
106 		getTextComponent().getDocument().removeDocumentListener(listener);
107 	}
108 	
109 	protected void showPopup()
110 	{
111 		if (!getTextComponent().isVisible()) {
112 			return;
113 		}
114 
115 		if (isWholeTextCompletion()) {
116 			GUIHelper.showPopupMenu(popup, getTextComponent(), 0, 
117 									getTextComponent().getHeight());
118 		}
119 		else {
120 			try {
121 				int pos = getTextComponent().getCaretPosition() + 1;
122 				Rectangle r = getTextComponent().modelToView(pos);
123 				Point p = r.getLocation();
124 				GUIHelper.showPopupMenu(popup, getTextComponent(),
125 									p.x, p.y + r.height);
126 			}
127 			catch (BadLocationException ble) {
128 				logger.debug("bad location", ble);
129 			}
130 		}
131 		
132 		// XXX: necessary for JDK 1.4.1; the text component looses focus
133 		// when the popup is shown
134 		getTextComponent().requestFocus();
135 	}
136 
137     private class DocumentHandler extends DocumentAdapter
138     {
139         public void insertUpdate(DocumentEvent e)
140         {
141             if (e.getLength() == 1 && getModel().complete(getText())) {
142                showPopup();
143             }
144             else {
145             	popup.setVisible(false);
146             }
147         }
148 
149         public void removeUpdate(DocumentEvent e)
150         {
151             if (e.getLength() == 1 &&  getModel().complete(getText())) {
152                 showPopup();
153             }
154             else {
155             	popup.setVisible(false);
156             }
157         }
158     }
159 }