Gridarta Editor
BackspaceWord.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-2023 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;
15 import javax.swing.text.BadLocationException;
19 
20 public class BackspaceWord implements ActionListener {
21 
22  @Override
23  public void actionPerformed(final ActionEvent e) {
24  final JEditTextArea textArea = InputHandler.getTextArea(e);
25  final int start = textArea.getSelectionStart();
26  if (start != textArea.getSelectionEnd()) {
27  textArea.setSelectedText("");
28  }
29 
30  final int line = textArea.getCaretLine();
31  final int lineStart = textArea.getLineStartOffset(line);
32  int caret = start - lineStart;
33 
34  final CharSequence lineText = textArea.getLineText(textArea.getCaretLine());
35 
36  if (caret == 0) {
37  if (lineStart == 0) {
38  textArea.getToolkit().beep();
39  return;
40  }
41  caret--;
42  } else {
43  final String noWordSep = (String) textArea.getDocument().getProperty("noWordSep");
44  caret = TextUtilities.findWordStart(lineText, caret, noWordSep);
45  }
46 
47  try {
48  textArea.getDocument().remove(caret + lineStart, start - (caret + lineStart));
49  } catch (final BadLocationException bl) {
50  bl.printStackTrace();
51  }
52  }
53 
54 }
net.sf.gridarta.textedit.textarea.JEditTextArea.getDocument
Document getDocument()
Returns the document this text area is editing.
Definition: JEditTextArea.java:500
net.sf.gridarta.textedit.textarea.JEditTextArea
jEdit's text area component.
Definition: JEditTextArea.java:91
net.sf.gridarta.textedit.textarea
This package contains the other part of the script editor.
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.textedit.textarea.JEditTextArea.getCaretLine
int getCaretLine()
Returns the caret line.
Definition: JEditTextArea.java:612
net.sf.gridarta.textedit.textarea.JEditTextArea.getSelectionStart
int getSelectionStart()
Returns the selection start offset.
Definition: JEditTextArea.java:589
net.sf
net.sf.gridarta.textedit.textarea.TextUtilities
Class with several utility functions used by the text area component.
Definition: TextUtilities.java:20
net.sf.gridarta.textedit.textarea.InputHandler
An input handler converts the user's key strokes into concrete actions.
Definition: InputHandler.java:36
net.sf.gridarta.textedit
net.sf.gridarta.textedit.textarea.JEditTextArea.getSelectionEnd
int getSelectionEnd()
Returns the selection end offset.
Definition: JEditTextArea.java:596
net.sf.gridarta.textedit.textarea.actions.BackspaceWord
Definition: BackspaceWord.java:20
net.sf.gridarta.textedit.textarea.actions.BackspaceWord.actionPerformed
void actionPerformed(final ActionEvent e)
Definition: BackspaceWord.java:23
net.sf.gridarta.textedit.textarea.TextUtilities.findWordStart
static int findWordStart(final CharSequence line, final int pos, final String noWordSep)
Locates the start of the word at the specified position.
Definition: TextUtilities.java:149
net
net.sf.gridarta.textedit.textarea.JEditTextArea.setSelectedText
void setSelectedText(@NotNull final String selectedText)
Replaces the selection with the specified text.
Definition: JEditTextArea.java:717
net.sf.gridarta.textedit.textarea.InputHandler.getTextArea
static JEditTextArea getTextArea(final EventObject evt)
Returns the text area that fired the specified event.
Definition: InputHandler.java:144
net.sf.gridarta.textedit.textarea.JEditTextArea.getLineStartOffset
int getLineStartOffset(final int line)
Returns the start offset of the specified line.
Definition: JEditTextArea.java:525
net.sf.gridarta.textedit.textarea.JEditTextArea.getLineText
CharSequence getLineText(final int lineIndex)
Returns the text on the specified line.
Definition: JEditTextArea.java:582