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.event.DocumentListener;
24  import javax.swing.text.JTextComponent;
25  
26  import org.xnap.XNap;
27  
28  
29  public class ShortAutomaticCompletionMode extends CompletionMode
30  {
31      //--- Constant(s) ---
32  
33      //--- Data field(s) ---
34  
35      private DocumentListener docListener = new DocumentHandler();
36  
37      /***
38       * @param textComponent
39       * @param wholeText
40       */
41      public ShortAutomaticCompletionMode(JTextComponent textComponent,
42                                          boolean wholeText)
43      {
44          super(textComponent, wholeText);
45      }
46  
47      /***
48       * @param textComponent
49       */
50      public ShortAutomaticCompletionMode(JTextComponent textComponent)
51      {
52          super(textComponent);
53      }
54  
55      /***
56       * @param textComponent
57       * @param model
58       */
59      public ShortAutomaticCompletionMode(JTextComponent textComponent,
60                                          CompletionModel model)
61      {
62          super(textComponent, model);
63      }
64  
65      public ShortAutomaticCompletionMode(JTextComponent textComponent,
66                                          CompletionModel model, boolean wholeText)
67      {
68          super(textComponent, model, wholeText);
69      }
70  
71      //--- Method(s) ---
72  
73      public String getName()
74      {
75          return XNap.tr("Short Automatic");
76      }
77  
78      protected void complete(String prefix)
79      {
80          String comp = getModel().completeUniquePrefix(prefix);
81  
82          // only complete if it's really a completion
83          if (comp.length() > prefix.length()) {
84              setText(comp, comp.length(), prefix.length());
85          }
86      }
87  
88      protected void disable()
89      {
90          getTextComponent().getDocument().removeDocumentListener(docListener);
91      }
92  
93      protected void enable()
94      {
95          getTextComponent().getDocument().addDocumentListener(docListener);
96      }
97  
98      private class DocumentHandler extends DocumentAdapter
99      {
100         public void insertUpdate(DocumentEvent e)
101         {
102             if (e.getLength() == 1) {
103                 final String text = getText();
104                 Runnable r =
105                     new Runnable() {
106                         public void run()
107                         {
108                             complete(text);
109                         }
110                     };
111 
112                 SwingUtilities.invokeLater(r);
113             }
114         }
115     }
116 }