001: package abbot.editor.recorder;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005:
006: import javax.swing.text.JTextComponent;
007:
008: import abbot.Log;
009: import abbot.script.*;
010:
011: /**
012: * Record basic semantic events you might find on an JTextComponent. <p>
013: */
014: public class JTextComponentRecorder extends JComponentRecorder {
015:
016: private JTextComponent target;
017: private int startIndex;
018: private int endIndex;
019:
020: public static final int SE_SELECTION = 30;
021:
022: public JTextComponentRecorder(Resolver resolver) {
023: super (resolver);
024: }
025:
026: protected void init(int rtype) {
027: super .init(rtype);
028: target = null;
029: startIndex = -1;
030: endIndex = -1;
031: }
032:
033: /** Don't store the action "default-typed"; store the key event instead. */
034: protected boolean isMappedEvent(KeyEvent ev) {
035: if (super .isMappedEvent(ev)) {
036: javax.swing.Action action = getAction(ev);
037: return !"default-typed".equals(action
038: .getValue(javax.swing.Action.NAME));
039: }
040: return false;
041: }
042:
043: /** Coalesce initial click with subsequent drags to produce a
044: * selection.
045: */
046: protected boolean dragStarted(Component target, int x, int y,
047: int modifiers, MouseEvent dragEvent) {
048: Log.debug("Tracking text selection");
049: setRecordingType(SE_DROP);
050: this .target = (JTextComponent) target;
051: startIndex = this .target.viewToModel(new Point(x, y));
052: // Concatenate drag/release with the original click
053: return true;
054: }
055:
056: protected boolean parseDrop(AWTEvent event) {
057: boolean consumed = super .parseDrop(event);
058: if (event.getID() == MouseEvent.MOUSE_DRAGGED) {
059: MouseEvent me = (MouseEvent) event;
060: endIndex = target.viewToModel(me.getPoint());
061: } else if (event.getID() == MouseEvent.MOUSE_RELEASED) {
062: endIndex = target.viewToModel(((MouseEvent) event)
063: .getPoint());
064: setFinished(true);
065: }
066: return consumed;
067: }
068:
069: protected Step createStep() {
070: Step step;
071: if (getRecordingType() == SE_DROP) {
072: step = createDrop(target, startIndex, endIndex);
073: } else {
074: step = super .createStep();
075: }
076: return step;
077: }
078:
079: /** The text component click should click on the text index instead of a
080: mouse coordinate. */
081: protected Step createClick(Component comp, int x, int y, int mods,
082: int count) {
083: if (mods == MouseEvent.BUTTON1_MASK && count == 1) {
084: ComponentReference cr = getResolver().addComponent(comp);
085: JTextComponent tc = (JTextComponent) comp;
086: int index = tc.viewToModel(new Point(x, y));
087: return new Action(
088: getResolver(),
089: null,
090: "actionClick",
091: new String[] { cr.getID(), String.valueOf(index), },
092: JTextComponent.class);
093: }
094: return super .createClick(comp, x, y, mods, count);
095: }
096:
097: protected Step createDrop(Component comp, int start, int end) {
098: ComponentReference cr = getResolver().addComponent(comp);
099: return new Action(getResolver(), null, "actionSelectText",
100: new String[] { cr.getID(), String.valueOf(start),
101: String.valueOf(end) }, JTextComponent.class);
102: }
103: }
|