KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > Macro


1 /*
2  * Macro.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: Macro.java,v 1.5 2003/07/18 16:32:12 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.util.ArrayList JavaDoc;
25 import javax.swing.undo.CompoundEdit JavaDoc;
26 import org.armedbear.lisp.Lisp;
27 import org.armedbear.lisp.LispObject;
28 import org.armedbear.lisp.LispThread;
29
30 public final class Macro implements Constants
31 {
32     private static Macro macro;
33
34     public static synchronized void recordMacro()
35     {
36         if (Editor.isRecordingMacro())
37             Editor.setRecordingMacro(false);
38         else {
39             final Editor editor = Editor.currentEditor();
40             if (macro != null && !macro.isEmpty()) {
41                 if (!editor.confirm("Record Macro",
42                                     "Overwrite existing keyboard macro?"))
43                     return;
44             }
45             macro = new Macro(editor);
46             Editor.setRecordingMacro(true);
47         }
48     }
49
50     public static synchronized void playbackMacro()
51     {
52         final Editor editor = Editor.currentEditor();
53         if (Editor.isRecordingMacro()) {
54             MessageDialog.showMessageDialog(editor,
55                                             "Command ignored (playbackMacro is not allowed while recording a macro)",
56                                             "Record Macro");
57             return;
58         }
59         if (macro == null || macro.isEmpty()){
60             editor.status("No keyboard macro defined");
61             return;
62         }
63         macro.playback();
64     }
65
66     private final Editor editor;
67     private ArrayList JavaDoc list = new ArrayList JavaDoc();
68
69     private Macro(Editor editor)
70     {
71         this.editor = editor;
72     }
73
74     public Editor getEditor()
75     {
76         return editor;
77     }
78
79     private synchronized boolean isEmpty()
80     {
81         return list.isEmpty();
82     }
83
84     public static synchronized void record(Editor editor, Object JavaDoc command)
85     {
86         if (macro != null && macro.getEditor() == editor)
87             macro.record(command);
88     }
89
90     public static synchronized void record(Editor editor, char c)
91     {
92         if (macro != null && macro.getEditor() == editor)
93             macro.record(c);
94     }
95
96     private synchronized void record(Object JavaDoc command)
97     {
98         list.add(command);
99     }
100
101     private synchronized void record(char c)
102     {
103         list.add(new Character JavaDoc(c));
104     }
105
106     private synchronized void playback()
107     {
108         final Editor editor = Editor.currentEditor();
109         final Buffer buffer = editor.getBuffer();
110         try {
111             buffer.lockWrite();
112         }
113         catch (InterruptedException JavaDoc e) {
114             Log.debug(e);
115             return;
116         }
117         try {
118             CompoundEdit JavaDoc compoundEdit = buffer.beginCompoundEdit();
119             final int size = list.size();
120             for (int i = 0; i < size; i++) {
121                 editor.setCurrentCommand(COMMAND_NOTHING);
122                 Object JavaDoc object = list.get(i);
123                 if (object instanceof String JavaDoc) {
124                     editor.executeCommand((String JavaDoc)object);
125                 } else if (object instanceof LispObject) {
126                     try {
127                         Lisp.funcall0((LispObject)object,
128                                       LispThread.currentThread());
129                     }
130                     catch (Throwable JavaDoc t) {
131                         Log.error(t);
132                     }
133                 } else if (object instanceof Character JavaDoc) {
134                     editor.insertNormalChar(((Character JavaDoc)object).charValue());
135                 }
136                 editor.setLastCommand(editor.getCurrentCommand());
137             }
138             buffer.endCompoundEdit(compoundEdit);
139         }
140         finally {
141             buffer.unlockWrite();
142         }
143     }
144 }
145
Popular Tags