001: /*
002: * Macro.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: Macro.java,v 1.5 2003/07/18 16:32:12 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.util.ArrayList;
025: import javax.swing.undo.CompoundEdit;
026: import org.armedbear.lisp.Lisp;
027: import org.armedbear.lisp.LispObject;
028: import org.armedbear.lisp.LispThread;
029:
030: public final class Macro implements Constants {
031: private static Macro macro;
032:
033: public static synchronized void recordMacro() {
034: if (Editor.isRecordingMacro())
035: Editor.setRecordingMacro(false);
036: else {
037: final Editor editor = Editor.currentEditor();
038: if (macro != null && !macro.isEmpty()) {
039: if (!editor.confirm("Record Macro",
040: "Overwrite existing keyboard macro?"))
041: return;
042: }
043: macro = new Macro(editor);
044: Editor.setRecordingMacro(true);
045: }
046: }
047:
048: public static synchronized void playbackMacro() {
049: final Editor editor = Editor.currentEditor();
050: if (Editor.isRecordingMacro()) {
051: MessageDialog
052: .showMessageDialog(
053: editor,
054: "Command ignored (playbackMacro is not allowed while recording a macro)",
055: "Record Macro");
056: return;
057: }
058: if (macro == null || macro.isEmpty()) {
059: editor.status("No keyboard macro defined");
060: return;
061: }
062: macro.playback();
063: }
064:
065: private final Editor editor;
066: private ArrayList list = new ArrayList();
067:
068: private Macro(Editor editor) {
069: this .editor = editor;
070: }
071:
072: public Editor getEditor() {
073: return editor;
074: }
075:
076: private synchronized boolean isEmpty() {
077: return list.isEmpty();
078: }
079:
080: public static synchronized void record(Editor editor, Object command) {
081: if (macro != null && macro.getEditor() == editor)
082: macro.record(command);
083: }
084:
085: public static synchronized void record(Editor editor, char c) {
086: if (macro != null && macro.getEditor() == editor)
087: macro.record(c);
088: }
089:
090: private synchronized void record(Object command) {
091: list.add(command);
092: }
093:
094: private synchronized void record(char c) {
095: list.add(new Character(c));
096: }
097:
098: private synchronized void playback() {
099: final Editor editor = Editor.currentEditor();
100: final Buffer buffer = editor.getBuffer();
101: try {
102: buffer.lockWrite();
103: } catch (InterruptedException e) {
104: Log.debug(e);
105: return;
106: }
107: try {
108: CompoundEdit compoundEdit = buffer.beginCompoundEdit();
109: final int size = list.size();
110: for (int i = 0; i < size; i++) {
111: editor.setCurrentCommand(COMMAND_NOTHING);
112: Object object = list.get(i);
113: if (object instanceof String) {
114: editor.executeCommand((String) object);
115: } else if (object instanceof LispObject) {
116: try {
117: Lisp.funcall0((LispObject) object, LispThread
118: .currentThread());
119: } catch (Throwable t) {
120: Log.error(t);
121: }
122: } else if (object instanceof Character) {
123: editor.insertNormalChar(((Character) object)
124: .charValue());
125: }
126: editor.setLastCommand(editor.getCurrentCommand());
127: }
128: buffer.endCompoundEdit(compoundEdit);
129: } finally {
130: buffer.unlockWrite();
131: }
132: }
133: }
|