Gridarta Editor
PrevLine.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 PrevLine implements ActionListener {
19 
20  private final boolean select;
21 
22  public PrevLine(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  final int caret1 = textArea.getCaretPosition();
30  final int line = textArea.getCaretLine();
31 
32  if (line == 0) {
33  textArea.getToolkit().beep();
34  return;
35  }
36 
37  int magic = textArea.getMagicCaretPosition();
38  if (magic == -1) {
39  magic = textArea.offsetToX(line, caret1 - textArea.getLineStartOffset(line));
40  }
41 
42  final int caret2 = textArea.getLineStartOffset(line - 1) + textArea.xToOffset(line - 1, magic);
43  if (select) {
44  textArea.select(textArea.getMarkPosition(), caret2);
45  } else {
46  textArea.setCaretPosition(caret2);
47  }
48  textArea.setMagicCaretPosition(magic);
49  }
50 
51 }
int getMarkPosition()
Returns the mark position.
int offsetToX(final int line, final int offset)
Converts an offset in a line into an x co-ordinate.
An input handler converts the user's key strokes into concrete actions.
Base package of all Gridarta classes.
int xToOffset(final int line, final int x)
Converts an x co-ordinate to an offset within a line.
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.
void setMagicCaretPosition(final int magicCaret)
Sets the `magic' caret position.
static JEditTextArea getTextArea(final EventObject evt)
Returns the text area that fired the specified event.
int getMagicCaretPosition()
Returns the `magic' caret position.
void setCaretPosition(final int caret)
Sets the caret position.