Gridarta Editor
NextWord.java
Go to the documentation of this file.
1 /*
2  * InputHandler.java - Manages key bindings and executes actions
3  * Copyright (C) 1999 Slava Pestov
4  * Copyright (C) 2000-2015 The Gridarta Developers.
5  *
6  * You may use and modify this package for any purpose. Redistribution is
7  * permitted, in both source and binary form, provided that this notice
8  * remains intact in all source distributions of this package.
9  */
10 
11 package net.sf.gridarta.textedit.textarea.actions;
12 
13 import java.awt.event.ActionEvent;
14 import java.awt.event.ActionListener;
18 
19 public class NextWord implements ActionListener {
20 
21  private final boolean select;
22 
23  public NextWord(final boolean select) {
24  this.select = select;
25  }
26 
27  @Override
28  public void actionPerformed(final ActionEvent e) {
29  final JEditTextArea textArea = InputHandler.getTextArea(e);
30  int caret = textArea.getCaretPosition();
31  final int line = textArea.getCaretLine();
32  final int lineStart = textArea.getLineStartOffset(line);
33  caret -= lineStart;
34 
35  final CharSequence lineText = textArea.getLineText(textArea.getCaretLine());
36 
37  if (caret == lineText.length()) {
38  if (lineStart + caret == textArea.getDocumentLength()) {
39  textArea.getToolkit().beep();
40  return;
41  }
42  caret++;
43  } else {
44  final String noWordSep = (String) textArea.getDocument().getProperty("noWordSep");
45  caret = TextUtilities.findWordEnd(lineText, caret, noWordSep);
46  }
47 
48  if (select) {
49  textArea.select(textArea.getMarkPosition(), lineStart + caret);
50  } else {
51  textArea.setCaretPosition(lineStart + caret);
52  }
53  }
54 
55 }
int getMarkPosition()
Returns the mark position.
int getDocumentLength()
Returns the length of the document.
An input handler converts the user's key strokes into concrete actions.
Base package of all Gridarta classes.
int getLineStartOffset(final int line)
Returns the start offset of the specified line.
void select(final int start, final int end)
Selects from the start offset to the end offset.
This package contains the other part of the script editor.
int getCaretPosition()
Returns the caret position.
CharSequence getLineText(final int lineIndex)
Returns the text on the specified line.
Document getDocument()
Returns the document this text area is editing.
static JEditTextArea getTextArea(final EventObject evt)
Returns the text area that fired the specified event.
void setCaretPosition(final int caret)
Sets the caret position.
Class with several utility functions used by the text area component.
static int findWordEnd(final CharSequence line, final int pos, final String noWordSep)
Locates the end of the word at the specified position.