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.event.InputEvent;
22  import java.awt.event.KeyAdapter;
23  import java.awt.event.KeyEvent;
24  import java.awt.event.KeyListener;
25  
26  import javax.swing.KeyStroke;
27  import javax.swing.text.JTextComponent;
28  
29  import org.apache.log4j.Logger;
30  import org.xnap.XNap;
31  import org.xnap.gui.shortcut.DefaultShortcut;
32  import org.xnap.gui.shortcut.ShortcutManager;
33  
34  
35  public class ManualCompletionMode extends CompletionMode
36  {
37      private static Logger logger = Logger.getLogger(ManualCompletionMode.class);
38     
39  	private static DefaultShortcut manual = new DefaultShortcut
40  		(KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_MASK),
41  		 XNap.tr("Completion"), XNap.tr("Trigger Manual Completion"),
42  		 "manuallyComplete");
43  	
44  	static {
45  		ShortcutManager.getInstance().add(manual);
46  	}
47     
48      private KeyListener listener = new KeyHandler();
49  
50      /***
51       * @param textComponent
52       * @param wholeText
53       */
54      public ManualCompletionMode(JTextComponent textComponent, boolean wholeText)
55      {
56          super(textComponent, wholeText);
57      }
58  
59      /***
60       * @param textComponent
61       */
62      public ManualCompletionMode(JTextComponent textComponent)
63      {
64          super(textComponent);
65      }
66  
67      /***
68       * @param textComponent
69       * @param model
70       */
71      public ManualCompletionMode(JTextComponent textComponent,
72                                  CompletionModel model)
73      {
74          super(textComponent, model);
75      }
76  
77      /***
78       * @param textComponent
79       * @param model
80       * @param wholeText
81       */
82      public ManualCompletionMode(JTextComponent textComponent,
83                                  CompletionModel model, boolean wholeText)
84      {
85          super(textComponent, model, wholeText);
86      }
87  
88      public String getName()
89      {
90          return XNap.tr("Manual");
91      }
92  
93      protected void disable()
94      {
95          getTextComponent().removeKeyListener(listener);
96      }
97  
98      /*
99       * @see org.xnap.gui.component.CompletionMode#enable()
100      */
101     protected void enable()
102     {
103         getTextComponent().addKeyListener(listener);
104     }
105 
106     private class KeyHandler extends KeyAdapter
107     {
108         public void keyPressed(KeyEvent e)
109         {
110             if (manual.getKeyStroke() != null
111 				&& manual.getKeyStroke().equals
112 				(KeyStroke.getKeyStrokeForEvent(e))) {
113                 String prefix = getText();
114                 String completion = getModel().completeUniquePrefix(prefix);
115 				
116                 if (completion.length() > prefix.length()) {
117                     setText(completion, completion.length(), prefix.length());
118                 }
119 				e.consume();
120             }
121         }
122     }
123 }