View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Felix Berger
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.xnap.commons.gui.completion;
21  
22  import static org.xnap.commons.gui.completion.CompletionModeFactory.I18N;
23  import java.awt.event.InputEvent;
24  import java.awt.event.KeyAdapter;
25  import java.awt.event.KeyEvent;
26  import java.awt.event.KeyListener;
27  import javax.swing.KeyStroke;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.xnap.commons.gui.shortcut.DefaultShortcut;
31  import org.xnap.commons.gui.shortcut.ShortcutManager;
32  
33  
34  /***
35   * This mode provides manual completion similar to the one offered by KDE.
36   * 
37   * Completion has to be triggered manually using a shortcut which can be
38   * configured in the {@link org.xnap.commons.gui.shortcut.ShortcutManager} 
39   * framework.
40   *
41   * @author Felix Berger
42   */
43  public class ManualCompletionMode extends AbstractCompletionMode
44  {
45      private static Log logger = LogFactory.getLog(ManualCompletionMode.class);
46     
47  	private static DefaultShortcut manual = new DefaultShortcut
48  		(KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_MASK),
49  		 I18N.tr("Completion"), I18N.tr("Trigger Manual Completion"));
50  		
51  	
52  	static {
53  		ShortcutManager.getInstance().add(manual);
54  	}
55     
56      private KeyListener listener = new KeyHandler();
57  
58      public void disable()
59      {
60          getTextComponent().removeKeyListener(listener);
61      }
62  
63      /*
64       * @see org.xnap.gui.component.CompletionMode#enable()
65       */
66      protected void enable()
67      {
68          getTextComponent().addKeyListener(listener);
69      }
70  
71      private class KeyHandler extends KeyAdapter
72      {
73          public void keyPressed(KeyEvent e)
74          {
75              if (manual.getKeyStroke() != null
76  				&& manual.getKeyStroke().equals
77  				(KeyStroke.getKeyStrokeForEvent(e))) {
78                  String prefix = getText();
79                  String completion = getModel().completeUniquePrefix(prefix);
80  				
81                  if (completion.length() > prefix.length()) {
82                      setText(completion, completion.length(), prefix.length());
83                  }
84  				e.consume();
85              }
86          }
87      }
88  }