Gridarta Editor
End.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;
17 
18 public class End implements ActionListener {
19 
20  private final boolean select;
21 
22  public End(final boolean select) {
23  this.select = select;
24  }
25 
26  @Override
27  public void actionPerformed(final ActionEvent e) {
28  final JEditTextArea textArea = InputHandler.getTextArea(e);
29 
30  int caret = textArea.getCaretPosition();
31 
32  final int lastOfLine = textArea.getLineEndOffset(textArea.getCaretLine()) - 1;
33  int lastVisibleLine = textArea.getFirstLine() + textArea.getVisibleLines();
34  if (lastVisibleLine >= textArea.getLineCount()) {
35  lastVisibleLine = Math.min(textArea.getLineCount() - 1, lastVisibleLine);
36  } else {
37  lastVisibleLine -= textArea.getElectricScroll() + 1;
38  }
39 
40  final int lastVisible = textArea.getLineEndOffset(lastVisibleLine) - 1;
41  final int lastDocument = textArea.getDocumentLength();
42 
43  if (caret == lastDocument) {
44  textArea.getToolkit().beep();
45  return;
46  } else if (!Boolean.TRUE.equals(textArea.getClientProperty(InputActions.SMART_HOME_END_PROPERTY))) {
47  caret = lastOfLine;
48  } else if (caret == lastVisible) {
49  caret = lastDocument;
50  } else if (caret == lastOfLine) {
51  caret = lastVisible;
52  } else {
53  caret = lastOfLine;
54  }
55 
56  if (select) {
57  textArea.select(textArea.getMarkPosition(), caret);
58  } else {
59  textArea.setCaretPosition(caret);
60  }
61  }
62 
63 }
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 getLineCount()
Returns the number of lines in the document.
void actionPerformed(final ActionEvent e)
Definition: End.java:27
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.
int getElectricScroll()
Returns the number of lines from the top and button of the text area that are always visible...
int getFirstLine()
Returns the line displayed at the text area's origin.
static JEditTextArea getTextArea(final EventObject evt)
Returns the text area that fired the specified event.
void setCaretPosition(final int caret)
Sets the caret position.
int getLineEndOffset(final int line)
Returns the end offset of the specified line.
int getVisibleLines()
Returns the number of lines visible in this text area.
static final String SMART_HOME_END_PROPERTY
If this client property is set to Boolean.TRUE on the text area, the home/end keys will support 'smar...