KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Registers.java
3  *
4  * Copyright (C) 2002-2004 Peter Graves
5  * $Id: Registers.java,v 1.2 2004/08/26 17:13:33 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.io.BufferedReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.io.OutputStreamWriter JavaDoc;
28
29 public final class Registers
30 {
31     public static final void saveToRegister()
32     {
33         saveToRegister(null);
34     }
35
36     public static final void saveToRegister(String JavaDoc name)
37     {
38         final Editor editor = Editor.currentEditor();
39         if (editor.getDot() == null)
40             return;
41         if (editor.getMark() == null) {
42             MessageDialog.showMessageDialog(editor, "No region selected",
43                 "Error");
44             return;
45         }
46         if (editor.isColumnSelection()) {
47             editor.notSupportedForColumnSelections();
48             return;
49         }
50         if (name == null) {
51             name = getName(editor, "Save To Register");
52             if (name == null)
53                 return;
54         }
55         if (!validateName(name))
56             return;
57         Region r = new Region(editor);
58         String JavaDoc text = r.toString();
59         File file =
60             File.getInstance(Directories.getRegistersDirectory(), name);
61         if (file == null)
62             return; // Shouldn't happen.
63
try {
64             OutputStreamWriter JavaDoc writer =
65                 new OutputStreamWriter JavaDoc(file.getOutputStream());
66             writer.write(text);
67             writer.flush();
68             writer.close();
69             ListRegistersBuffer buf = findListRegistersBuffer();
70             if (buf != null)
71                 buf.reload();
72             editor.status("Region saved to register ".concat(name));
73         }
74         catch (IOException JavaDoc e) {
75             Log.error(e);
76         }
77     }
78
79     public static final void insertRegister()
80     {
81         insertRegister(null);
82     }
83
84     public static final void insertRegister(String JavaDoc name)
85     {
86         final Editor editor = Editor.currentEditor();
87         if (!editor.checkReadOnly())
88             return;
89         if (editor.getDot() == null)
90             return;
91         if (name == null) {
92             name = getName(editor, "Insert Register");
93             if (name == null)
94                 return;
95         }
96         String JavaDoc text = getText(name);
97         if (text != null)
98             editor.paste(text);
99     }
100
101     public static final void editRegister()
102     {
103         editRegister(null);
104     }
105
106     public static final void editRegister(String JavaDoc name)
107     {
108         final Editor editor = Editor.currentEditor();
109         if (name == null) {
110             name = getName(editor, "Edit Register");
111             if (name == null)
112                 return;
113         }
114         File file =
115             File.getInstance(Directories.getRegistersDirectory(), name);
116         if (file != null && file.isFile()) {
117             Buffer buf = Editor.getBuffer(file);
118             if (buf != null) {
119                 Editor ed = editor.getOtherEditor();
120                 if (ed != null)
121                     ed.makeNext(buf);
122                 else
123                     editor.makeNext(buf);
124                 editor.activateInOtherWindow(buf);
125             }
126         }
127     }
128
129     public static final void clearRegister()
130     {
131         clearRegister(null);
132     }
133
134     public static final void clearRegister(String JavaDoc name)
135     {
136         final Editor editor = Editor.currentEditor();
137         if (name == null) {
138             name = getName(editor, "Clear Register");
139             if (name == null)
140                 return;
141         }
142         File file =
143             File.getInstance(Directories.getRegistersDirectory(), name);
144         if (file.isFile()) {
145             file.delete();
146             ListRegistersBuffer buf = findListRegistersBuffer();
147             if (buf != null)
148                 buf.reload();
149         }
150     }
151
152     public static final void listRegisters()
153     {
154         Buffer buf = findListRegistersBuffer();
155         if (buf == null)
156             buf = new ListRegistersBuffer();
157         final Editor editor = Editor.currentEditor();
158         if (editor.getBuffer() == buf)
159             return;
160         Editor other = editor.getOtherEditor();
161         if (other != null)
162             other.makeNext(buf);
163         else
164             editor.makeNext(buf);
165         editor.activateInOtherWindow(buf);
166     }
167
168     public static final ListRegistersBuffer findListRegistersBuffer()
169     {
170         for (BufferIterator it = new BufferIterator(); it.hasNext();) {
171             Buffer buf = it.nextBuffer();
172             if (buf instanceof ListRegistersBuffer)
173                 return (ListRegistersBuffer) buf;
174         }
175         return null;
176     }
177
178     // No history.
179
private static final String JavaDoc getName(Editor editor, String JavaDoc title)
180     {
181         InputDialog d = new InputDialog(editor, "Register:", title, null);
182         editor.centerDialog(d);
183         d.show();
184         return d.getInput();
185     }
186
187     private static final boolean validateName(String JavaDoc name)
188     {
189         boolean lenOk = false;
190         boolean charsOk = true;
191         int len = name.length();
192         // Keep the names of the registers between 1 and 25 chars
193
if (len > 0 && len <= 25) {
194             lenOk = true;
195             for (int x = 0; x < len; x++) {
196                 char c = name.charAt(x);
197                 // Allow alphanumeric chars plus "-" and "_" only, with no spaces.
198
if (!Character.isLetterOrDigit(c) &&
199                     c != '-' && c != '_' ) {
200                     charsOk = false;
201                     break;
202                 }
203             }
204         }
205         if (lenOk && charsOk)
206             return true;
207         FastStringBuffer sb = new FastStringBuffer('"');
208         sb.append(name);
209         sb.append("\" is not a valid register name. ");
210         sb.append("Register names must be between 1 and 25 characters long ");
211         sb.append("and contain only the characters \"a-z\", \"A-Z\", \"0-9\", \"-\", or \"_\".");
212         String JavaDoc message = sb.toString();
213         if (message.length() > 65)
214             message = Utilities.wrap(message, 65, 8);
215         MessageDialog.showMessageDialog(message, "Error");
216         return false;
217     }
218
219     /*package*/ static final String JavaDoc getText(String JavaDoc name)
220     {
221         return getText(name, 0);
222     }
223
224     // If maxLines > 0, return at most maxLines lines of text.
225
/*package*/ static final String JavaDoc getText(String JavaDoc name, int maxLines)
226     {
227         File file =
228             File.getInstance(Directories.getRegistersDirectory(), name);
229         if (!file.isFile())
230             return null;
231         try {
232             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
233                 file.getInputStream()));
234             FastStringBuffer sb = new FastStringBuffer();
235             int lineCount = 0;
236             int c;
237             while ((c = reader.read()) > 0) {
238                 sb.append((char)c);
239                 if (c == '\n')
240                     ++lineCount;
241                 if (maxLines > 0 && lineCount == maxLines)
242                     break;
243             }
244             reader.close();
245             return sb.toString();
246         }
247         catch (IOException JavaDoc e) {
248             Log.error(e);
249             return null;
250         }
251     }
252 }
253
254
Popular Tags