PageRenderTime 104ms CodeModel.GetById 96ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/RubyPlugin/src/org/jedit/ruby/structure/TypeAheadPopupKeyHandler.java

#
Java | 145 lines | 98 code | 25 blank | 22 comment | 16 complexity | 448e489c07e8285c4fff17198940859f MD5 | raw file
  1/*
  2 * TypeAheadPopupKeyHandler.java -
  3 *
  4 * Copyright 2005 Robert McKinnon
  5 *
  6 * This program is free software; you can redistribute it and/or
  7 * modify it under the terms of the GNU General Public License
  8 * as published by the Free Software Foundation; either version 2
  9 * of the License, or any later version.
 10 *
 11 * This program 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
 14 * GNU General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU General Public License
 17 * along with this program; if not, write to the Free Software
 18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 19 */
 20package org.jedit.ruby.structure;
 21
 22import org.jedit.ruby.structure.*;
 23import org.jedit.ruby.structure.TypeAheadPopup;
 24import org.jedit.ruby.RubyPlugin;
 25
 26import java.awt.event.KeyAdapter;
 27import java.awt.event.KeyEvent;
 28
 29/**
 30 * @author robmckinnon at users.sourceforge.net
 31 */
 32final class TypeAheadPopupKeyHandler extends KeyAdapter {
 33
 34    private final TypeAheadPopup typeAheadPopup;
 35    private final String validCharacters;
 36
 37    public TypeAheadPopupKeyHandler(TypeAheadPopup typeAheadPopup) {
 38        this.typeAheadPopup = typeAheadPopup;
 39        validCharacters = "_(),.[]";
 40    }
 41
 42    public final void keyPressed(KeyEvent event) {
 43        switch (event.getKeyCode()) {
 44            case KeyEvent.VK_ESCAPE:
 45                handleEscapePressed(event);
 46                break;
 47
 48            case KeyEvent.VK_UP:
 49                incrementSelection(event, -1);
 50                break;
 51
 52            case KeyEvent.VK_DOWN:
 53                incrementSelection(event, 1);
 54                break;
 55
 56            case KeyEvent.VK_PAGE_UP:
 57            case KeyEvent.VK_LEFT:
 58                incrementSelection(event, -1 * (TypeAheadPopup.VISIBLE_LIST_SIZE - 1));
 59                break;
 60
 61            case KeyEvent.VK_PAGE_DOWN:
 62            case KeyEvent.VK_RIGHT:
 63                incrementSelection(event, (org.jedit.ruby.structure.TypeAheadPopup.VISIBLE_LIST_SIZE - 1));
 64                break;
 65
 66            case KeyEvent.VK_HOME:
 67                incrementSelection(event, -1 * getListSize());
 68                break;
 69
 70            case KeyEvent.VK_END:
 71                incrementSelection(event, getListSize());
 72                break;
 73
 74            case KeyEvent.VK_BACK_SPACE:
 75                handleBackSpacePressed(event);
 76                break;
 77
 78            case KeyEvent.VK_F4:
 79                handleSelection(event, false);
 80
 81            default:
 82                handleOtherKeys(event);
 83                break;
 84        }
 85    }
 86
 87    private void handleOtherKeys(KeyEvent event) {
 88        if (event.isAltDown() || event.isMetaDown()) {
 89            char keyChar = event.getKeyChar();
 90            boolean handled = typeAheadPopup.handleAltPressedWith(keyChar);
 91            if (handled) {
 92                event.consume();
 93            }
 94        }
 95    }
 96
 97    public final void keyTyped(KeyEvent event) {
 98        char character = event.getKeyChar();
 99        int keyCode = event.getKeyCode();
100        int keyChar = event.getKeyChar();
101        if (keyCode == KeyEvent.VK_TAB || keyCode == KeyEvent.VK_ENTER ||
102                keyChar == KeyEvent.VK_TAB || keyChar == KeyEvent.VK_ENTER) {
103            handleSelection(event, true);
104
105        } else {
106            if (!RubyPlugin.ignoreKeyTyped(keyCode, character, event)) {
107                handleCharacterTyped(character, event);
108            }
109        }
110    }
111
112    private void handleBackSpacePressed(KeyEvent event) {
113        typeAheadPopup.handleBackSpacePressed();
114        event.consume();
115    }
116
117    private int getListSize() {
118        return typeAheadPopup.getListSize();
119    }
120
121    private void incrementSelection(KeyEvent event, int increment) {
122        typeAheadPopup.incrementSelection(increment);
123        event.consume();
124    }
125
126    private void handleEscapePressed(KeyEvent event) {
127        typeAheadPopup.handleEscapePressed();
128        event.consume();
129    }
130
131    private void handleSelection(KeyEvent event, boolean showMenu) {
132        event.consume();
133        typeAheadPopup.handleSelection(showMenu);
134    }
135
136    private void handleCharacterTyped(char character, KeyEvent event) {
137        boolean valid = Character.isLetterOrDigit(character)
138                || validCharacters.indexOf(character) != -1;
139
140        if (valid) {
141            typeAheadPopup.updateMatchedMembers(character);
142            event.consume();
143        }
144    }
145}