package edu.princeton.toy; import java.awt.*; import java.awt.event.*; import edu.princeton.swing.*; import edu.princeton.swing.text.*; import edu.princeton.toy.lang.*; /** * TUncommentingAutoCompleter is an subclass of IndentingAutoCompleter which will also automatically * remove an entire autocomment when the user deletes the first or last character of it via the * delete key or backspace key, respectively. * * @version 7.1 * @author btsang */ public class TUncommentingAutoCompleter extends IndentingAutoCompleter { public TUncommentingAutoCompleter() { } /** * The PHighlightedTextArea will pass all key events to its auto-completer before processing * the event for itself. * * @param comp The component which recieved the KeyEvent. * @param e The event which occurred. * @return True if the component should ignore the KeyEvent (because this method already * performed a special action associated with it). In either case, KeyListeners of the * component will still be informed of the event. */ public boolean interceptKeyEvent(PHighlightedTextArea comp, KeyEvent e) { // Allow the indenter to try to intercept the KeyEvent if (super.interceptKeyEvent(comp, e)) return true; // Check for the correct keystroke if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { int selectionDot = comp.getSelectionDot(); // Make sure nothing is selected if (selectionDot != comp.getSelectionMark()) return false; HighlightedDocument document = comp.getDocument(); Point coordinate = document.offsetToCoordinate(selectionDot); // Make sure the caret is in the right location if (coordinate.x != TProgramDocument.COMMENT_COLUMN) return false; String line = document.getText( selectionDot - TProgramDocument.COMMENT_COLUMN, selectionDot ); // Make sure the line is indeed an autocommented line if (!TWord.isCommand(line.substring(0, 8))) return false; // Ok, delete the autocomment document.remove( selectionDot - TProgramDocument.COMMENT_COLUMN + 7, TProgramDocument.COMMENT_COLUMN - 7, false ); return true; } return false; } }