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-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;
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  public 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  public 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  } else if (c == null) {
154  break;
155  }
156  if (c instanceof JPopupMenu) {
157  c = ((JPopupMenu) c).getInvoker();
158  } else {
159  c = c.getParent();
160  }
161  }
162  }
163  }
164 
165  // this shouldn't happen
166  LOG.fatal("BUG: getTextArea() returning null");
167  LOG.fatal("Report this to Slava Pestov <sp@gjt.org>");
168  assert false : "BUG: getTextArea() returning null";
169  throw new Error("BUG: getTextArea() returning null");
170  }
171 
172  // protected members
173 
181  protected boolean handleGrabAction(final KeyEvent evt) {
182  if (grabAction == null) {
183  return false;
184  }
185 
186  // Clear it *before* it is executed so that executeAction()
187  // resets the repeat count
188  final ActionListener grabAction2 = grabAction;
189  grabAction = null;
190  executeAction(grabAction2, evt.getSource(), String.valueOf(evt.getKeyChar()));
191  return true;
192  }
193 
199  public interface NonRepeatable {
200 
201  }
202 
203 }
void setRepeatCount(final int repeatCount)
Sets the number of times the next action will be repeated.
boolean handleGrabAction(final KeyEvent evt)
If a key is being grabbed, this method should be called with the appropriate key event.
If an action implements this interface, it should not be repeated.
An input handler converts the user&#39;s key strokes into concrete actions.
boolean isRepeatEnabled()
Returns if repetition is enabled.
static final Category LOG
The Logger for printing log messages.
static JEditTextArea getTextArea(final EventObject evt)
Returns the text area that fired the specified event.
void setRepeatEnabled(final boolean repeat)
Sets the enabled state of repetition.
int getRepeatCount()
Returns the number of times the next action will be repeated.
abstract void addDefaultKeyBindings()
Adds the default key bindings to this input handler.
void executeAction(final ActionListener listener, final Object source, @Nullable final String actionCommand)
Executes the specified action, repeating and recording it as necessary.
abstract InputHandler copy()
Returns a copy of this input handler that shares the same key bindings.