Gridarta Editor
InputHandler.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;
12 
13 import java.awt.Component;
14 import java.awt.event.ActionEvent;
15 import java.awt.event.ActionListener;
16 import java.awt.event.KeyEvent;
17 import java.awt.event.KeyListener;
18 import java.util.EventObject;
19 import javax.swing.JPopupMenu;
20 import org.apache.log4j.Category;
21 import org.apache.log4j.Logger;
22 import org.jetbrains.annotations.NotNull;
23 import org.jetbrains.annotations.Nullable;
24 
36 public abstract class InputHandler implements KeyListener {
37 
41  private static final Category LOG = Logger.getLogger(InputHandler.class);
42 
43  @Nullable
44  private ActionListener grabAction;
45 
46  private boolean repeat;
47 
48  protected int repeatCount;
49 
55  public abstract void addDefaultKeyBindings();
56 
63  protected boolean isRepeatEnabled() {
64  return repeat;
65  }
66 
74  public void setRepeatEnabled(final boolean repeat) {
75  this.repeat = repeat;
76  }
77 
83  public int getRepeatCount() {
84  return repeat ? Math.max(1, repeatCount) : 1;
85  }
86 
91  public void setRepeatCount(final int repeatCount) {
92  this.repeatCount = repeatCount;
93  }
94 
100  public abstract InputHandler copy();
101 
108  protected void executeAction(final ActionListener listener, final Object source, @Nullable final String actionCommand) {
109  // create event
110  final ActionEvent evt = new ActionEvent(source, ActionEvent.ACTION_PERFORMED, actionCommand);
111 
112  // remember old values, in case action changes them
113  final boolean repeatBak = repeat;
114 
115  // execute the action
116  if (listener instanceof NonRepeatable) {
117  listener.actionPerformed(evt);
118  } else {
119  for (int i = 0; i < Math.max(1, repeatCount); i++) {
120  listener.actionPerformed(evt);
121  }
122  }
123 
124  // do recording. Notice that we do no recording whatsoever
125  // for actions that grab keys
126  if (grabAction == null) {
127  // If repeat was true originally, clear it
128  // Otherwise it might have been set by the action, etc
129  if (repeatBak) {
130  repeat = false;
131  repeatCount = 0;
132  }
133  }
134  }
135 
143  @NotNull
144  public static JEditTextArea getTextArea(final EventObject evt) {
145  if (evt != null) {
146  final Object o = evt.getSource();
147  if (o instanceof Component) {
148  // find the parent text area
149  Component c = (Component) o;
150  while (true) {
151  if (c instanceof JEditTextArea) {
152  return (JEditTextArea) c;
153  }
154  if (c == null) {
155  break;
156  }
157  if (c instanceof JPopupMenu) {
158  c = ((JPopupMenu) c).getInvoker();
159  } else {
160  c = c.getParent();
161  }
162  }
163  }
164  }
165 
166  // this shouldn't happen
167  LOG.fatal("BUG: getTextArea() returning null");
168  LOG.fatal("Report this to Slava Pestov <sp@gjt.org>");
169  assert false : "BUG: getTextArea() returning null";
170  throw new Error("BUG: getTextArea() returning null");
171  }
172 
173  // protected members
174 
182  protected boolean handleGrabAction(final KeyEvent evt) {
183  if (grabAction == null) {
184  return false;
185  }
186 
187  // Clear it *before* it is executed so that executeAction()
188  // resets the repeat count
189  final ActionListener grabAction2 = grabAction;
190  grabAction = null;
191  executeAction(grabAction2, evt.getSource(), String.valueOf(evt.getKeyChar()));
192  return true;
193  }
194 
200  public interface NonRepeatable {
201 
202  }
203 
204 }
net.sf.gridarta.textedit.textarea.JEditTextArea
jEdit's text area component.
Definition: JEditTextArea.java:91
net.sf.gridarta.textedit.textarea.InputHandler.addDefaultKeyBindings
abstract void addDefaultKeyBindings()
Adds the default key bindings to this input handler.
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.textarea.InputHandler.repeatCount
int repeatCount
Definition: InputHandler.java:48
net.sf.gridarta.textedit.textarea.InputHandler.setRepeatCount
void setRepeatCount(final int repeatCount)
Sets the number of times the next action will be repeated.
Definition: InputHandler.java:91
net.sf.gridarta.textedit.textarea.InputHandler.isRepeatEnabled
boolean isRepeatEnabled()
Returns if repetition is enabled.
Definition: InputHandler.java:63
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.InputHandler.setRepeatEnabled
void setRepeatEnabled(final boolean repeat)
Sets the enabled state of repetition.
Definition: InputHandler.java:74
net.sf.gridarta.textedit.textarea.InputHandler.repeat
boolean repeat
Definition: InputHandler.java:46
net.sf.gridarta.textedit.textarea.InputHandler.executeAction
void executeAction(final ActionListener listener, final Object source, @Nullable final String actionCommand)
Executes the specified action, repeating and recording it as necessary.
Definition: InputHandler.java:108
net.sf.gridarta.textedit.textarea.InputHandler.grabAction
ActionListener grabAction
Definition: InputHandler.java:44
net.sf.gridarta.textedit.textarea.InputHandler.getRepeatCount
int getRepeatCount()
Returns the number of times the next action will be repeated.
Definition: InputHandler.java:83
net.sf.gridarta.textedit.textarea.InputHandler.handleGrabAction
boolean handleGrabAction(final KeyEvent evt)
If a key is being grabbed, this method should be called with the appropriate key event.
Definition: InputHandler.java:182
net.sf.gridarta.textedit.textarea.InputHandler.LOG
static final Category LOG
The Logger for printing log messages.
Definition: InputHandler.java:41
net.sf.gridarta.textedit.textarea.InputHandler.NonRepeatable
If an action implements this interface, it should not be repeated.
Definition: InputHandler.java:200
net.sf.gridarta.textedit.textarea.InputHandler.copy
abstract InputHandler copy()
Returns a copy of this input handler that shares the same key bindings.