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 javax.swing.SwingUtilities;
22  import javax.swing.event.DocumentEvent;
23  import javax.swing.text.JTextComponent;
24  
25  import org.xnap.XNap;
26  
27  /***
28   *
29   */
30  public class AutomaticCompletionMode extends ShortAutomaticCompletionMode
31  {
32  	
33      /***
34       * @param textComponent
35       * @param model
36       * @param wholeText
37       */
38      public AutomaticCompletionMode(JTextComponent textComponent,
39                                     CompletionModel model, boolean wholeText)
40      {
41          super(textComponent, model, wholeText);
42      }
43  
44      /***
45       * @param textComponent
46       * @param wholeText
47       */
48      public AutomaticCompletionMode(JTextComponent textComponent,
49                                     boolean wholeText)
50      {
51          super(textComponent, wholeText);
52      }
53  
54      /***
55       * @param textComponent
56       * @param model
57       */
58      public AutomaticCompletionMode(JTextComponent textComponent,
59                                     CompletionModel model)
60      {
61          super(textComponent, model);
62      }
63  
64      /***
65       * @param textComponent
66       */
67      public AutomaticCompletionMode(JTextComponent textComponent)
68      {
69          super(textComponent);
70      }
71  
72      /*
73       * @see org.xnap.gui.component.CompletionMode#getName()
74       */
75      public String getName()
76      {
77  		return XNap.tr("Automatic");
78      }
79  
80      protected void complete(String prefix)
81      {
82      	if (getModel().complete(prefix)) {
83      		Object obj = getModel().getElementAt(0);
84      		if (obj != null && obj.toString().length() > prefix.length()) {
85      			setText(obj.toString(), obj.toString().length(), prefix.length());
86      		}
87      	}
88      }
89      
90      private class DocumentHandler extends DocumentAdapter
91      {
92      	public void insertUpdate(DocumentEvent e)
93      	{
94      		if (e.getLength() == 1) {
95      			final String text = getText();
96      			Runnable r = new Runnable()
97      			{
98      				public void run()
99      				{
100     					complete(text);	
101     				}
102     			};
103     			SwingUtilities.invokeLater(r);
104     		}
105     	}
106     }
107 }