PageRenderTime 60ms CodeModel.GetById 53ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/RubyPlugin/src/org/jedit/ruby/utils/CharCaretListener.java

#
Java | 98 lines | 60 code | 16 blank | 22 comment | 8 complexity | 5b08dd4ae6b3f14382593af0da50b7be MD5 | raw file
 1/*
 2 * CharCaretListener.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 */
20
21package org.jedit.ruby.utils;
22
23import org.gjt.sp.jedit.textarea.JEditTextArea;
24import org.jedit.ruby.structure.RubyTokenHandler;
25import org.jedit.ruby.structure.RubyToken;
26import org.jedit.ruby.structure.BufferChangeHandler;
27
28import javax.swing.event.CaretListener;
29import javax.swing.event.CaretEvent;
30
31/**
32 * @author robmckinnon at users.sourceforge.net
33 */
34public final class CharCaretListener implements CaretListener {
35
36    private static final RubyTokenHandler tokenHandler = new RubyTokenHandler();
37    private static final char NONE = (char)-1;
38
39    private static RubyToken lastToken = null;
40    private static RubyToken currentToken = null;
41    private static char charLastBehind = NONE;
42    private static char charBehind = NONE;
43    private static char charAhead = NONE;
44
45    public static boolean hasCharLastBehind() {
46        return charLastBehind != NONE;
47    }
48
49    public static char getCharBehind() {
50        return charBehind;
51    }
52
53    public static char getCharLastBehind() {
54        return charLastBehind;
55    }
56
57    public static char getCharAhead() {
58        return charAhead;
59    }
60
61    public static RubyToken getLastToken() {
62        return lastToken;
63    }
64
65    public static RubyToken getCurrentToken() {
66        return currentToken;
67    }
68
69    public final void caretUpdate(CaretEvent e) {
70        if (!BufferChangeHandler.instance().isGotoPreviousEditAction()) {
71            BufferChangeHandler.instance().resetEditLocationIndex();
72        }
73        int mark = e.getMark();
74        int dot = e.getDot();
75        charLastBehind = charBehind;
76        lastToken = currentToken;
77
78        if (dot == mark && dot > 0) {
79            JEditTextArea textArea = (JEditTextArea)e.getSource();
80            String text = textArea.getText();
81            charBehind = text.charAt(dot - 1);
82            currentToken = tokenHandler.getTokenAtCaret(CommandUtils.getBuffer(textArea), dot);
83
84            if (text.length() > dot) {
85                charAhead = text.charAt(dot);
86            } else {
87                charAhead = NONE;
88            }
89        } else {
90            currentToken = null;
91            lastToken = null;
92            charBehind = NONE;
93            charLastBehind = NONE;
94            charAhead = NONE;
95        }
96    }
97
98}