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  import org.xnap.XNap;
24  import org.xnap.gui.util.*;
25  
26  import javax.swing.text.BadLocationException;
27  import javax.swing.text.JTextComponent;
28  
29  public class AutomaticDropDownCompletionMode extends AutomaticCompletionMode
30  {
31  
32  	private CompletionPopup popup = new CompletionPopup(this);
33  
34      /***
35       * @param textComponent
36       * @param model
37       * @param wholeText
38       */
39      public AutomaticDropDownCompletionMode(JTextComponent textComponent,
40                                             CompletionModel model,
41                                             boolean wholeText)
42      {
43          super(textComponent, model, wholeText);
44      }
45  
46      /***
47       * @param textComponent
48       * @param wholeText
49       */
50      public AutomaticDropDownCompletionMode(JTextComponent textComponent,
51                                             boolean wholeText)
52      {
53          super(textComponent, wholeText);
54      }
55  
56      /***
57       * @param textComponent
58       * @param model
59       */
60      public AutomaticDropDownCompletionMode(JTextComponent textComponent,
61                                             CompletionModel model)
62      {
63          super(textComponent, model);
64      }
65  
66      /***
67       * @param textComponent
68       */
69      public AutomaticDropDownCompletionMode(JTextComponent textComponent)
70      {
71          super(textComponent);
72      }
73  
74      //--- Method(s) ---
75  
76      public String getName()
77      {
78          return XNap.tr("Automatic Dropdown");
79      }
80  	
81  	protected void enable()
82  	{
83  		super.enable();
84  		popup.enablePopup();
85  	}
86  
87  	protected void disable()
88  	{
89  		popup.disablePopup();
90  		super.disable();
91  	}
92  
93  	protected void complete(String prefix)
94  	{
95  		super.complete(prefix);
96  		if (getModel().getSize() > 0) {
97  			showPopup();
98  		}
99  	}
100 
101 	// TODO write single word completion case
102 	protected void showPopup()
103 	{
104 		if (isWholeTextCompletion()) {
105 			GUIHelper.showPopupMenu(popup, getTextComponent(), 0, 
106 									getTextComponent().getHeight());
107 		}
108 		else {
109 			try {
110 				int pos = getTextComponent().getCaretPosition() + 1;
111 				Rectangle r = getTextComponent().modelToView(pos);
112 				Point p = r.getLocation();
113 				GUIHelper.showPopupMenu(popup, getTextComponent(),
114 									p.x, p.y + r.height);
115 			}
116 			catch (BadLocationException ble) {
117 				//logger.debug("bad location", ble);
118 			}			
119 		}
120 		
121 		
122 		// XXX: necessary for JDK 1.4.1; the text component looses focus
123 		// when the popup is shown
124 		getTextComponent().requestFocus();
125 	}
126 	
127 }
128 
129