001: /*
002: * Registers.java
003: *
004: * Copyright (C) 2002-2004 Peter Graves
005: * $Id: Registers.java,v 1.2 2004/08/26 17:13:33 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.io.BufferedReader;
025: import java.io.IOException;
026: import java.io.InputStreamReader;
027: import java.io.OutputStreamWriter;
028:
029: public final class Registers {
030: public static final void saveToRegister() {
031: saveToRegister(null);
032: }
033:
034: public static final void saveToRegister(String name) {
035: final Editor editor = Editor.currentEditor();
036: if (editor.getDot() == null)
037: return;
038: if (editor.getMark() == null) {
039: MessageDialog.showMessageDialog(editor,
040: "No region selected", "Error");
041: return;
042: }
043: if (editor.isColumnSelection()) {
044: editor.notSupportedForColumnSelections();
045: return;
046: }
047: if (name == null) {
048: name = getName(editor, "Save To Register");
049: if (name == null)
050: return;
051: }
052: if (!validateName(name))
053: return;
054: Region r = new Region(editor);
055: String text = r.toString();
056: File file = File.getInstance(Directories
057: .getRegistersDirectory(), name);
058: if (file == null)
059: return; // Shouldn't happen.
060: try {
061: OutputStreamWriter writer = new OutputStreamWriter(file
062: .getOutputStream());
063: writer.write(text);
064: writer.flush();
065: writer.close();
066: ListRegistersBuffer buf = findListRegistersBuffer();
067: if (buf != null)
068: buf.reload();
069: editor.status("Region saved to register ".concat(name));
070: } catch (IOException e) {
071: Log.error(e);
072: }
073: }
074:
075: public static final void insertRegister() {
076: insertRegister(null);
077: }
078:
079: public static final void insertRegister(String name) {
080: final Editor editor = Editor.currentEditor();
081: if (!editor.checkReadOnly())
082: return;
083: if (editor.getDot() == null)
084: return;
085: if (name == null) {
086: name = getName(editor, "Insert Register");
087: if (name == null)
088: return;
089: }
090: String text = getText(name);
091: if (text != null)
092: editor.paste(text);
093: }
094:
095: public static final void editRegister() {
096: editRegister(null);
097: }
098:
099: public static final void editRegister(String name) {
100: final Editor editor = Editor.currentEditor();
101: if (name == null) {
102: name = getName(editor, "Edit Register");
103: if (name == null)
104: return;
105: }
106: File file = File.getInstance(Directories
107: .getRegistersDirectory(), name);
108: if (file != null && file.isFile()) {
109: Buffer buf = Editor.getBuffer(file);
110: if (buf != null) {
111: Editor ed = editor.getOtherEditor();
112: if (ed != null)
113: ed.makeNext(buf);
114: else
115: editor.makeNext(buf);
116: editor.activateInOtherWindow(buf);
117: }
118: }
119: }
120:
121: public static final void clearRegister() {
122: clearRegister(null);
123: }
124:
125: public static final void clearRegister(String name) {
126: final Editor editor = Editor.currentEditor();
127: if (name == null) {
128: name = getName(editor, "Clear Register");
129: if (name == null)
130: return;
131: }
132: File file = File.getInstance(Directories
133: .getRegistersDirectory(), name);
134: if (file.isFile()) {
135: file.delete();
136: ListRegistersBuffer buf = findListRegistersBuffer();
137: if (buf != null)
138: buf.reload();
139: }
140: }
141:
142: public static final void listRegisters() {
143: Buffer buf = findListRegistersBuffer();
144: if (buf == null)
145: buf = new ListRegistersBuffer();
146: final Editor editor = Editor.currentEditor();
147: if (editor.getBuffer() == buf)
148: return;
149: Editor other = editor.getOtherEditor();
150: if (other != null)
151: other.makeNext(buf);
152: else
153: editor.makeNext(buf);
154: editor.activateInOtherWindow(buf);
155: }
156:
157: public static final ListRegistersBuffer findListRegistersBuffer() {
158: for (BufferIterator it = new BufferIterator(); it.hasNext();) {
159: Buffer buf = it.nextBuffer();
160: if (buf instanceof ListRegistersBuffer)
161: return (ListRegistersBuffer) buf;
162: }
163: return null;
164: }
165:
166: // No history.
167: private static final String getName(Editor editor, String title) {
168: InputDialog d = new InputDialog(editor, "Register:", title,
169: null);
170: editor.centerDialog(d);
171: d.show();
172: return d.getInput();
173: }
174:
175: private static final boolean validateName(String name) {
176: boolean lenOk = false;
177: boolean charsOk = true;
178: int len = name.length();
179: // Keep the names of the registers between 1 and 25 chars
180: if (len > 0 && len <= 25) {
181: lenOk = true;
182: for (int x = 0; x < len; x++) {
183: char c = name.charAt(x);
184: // Allow alphanumeric chars plus "-" and "_" only, with no spaces.
185: if (!Character.isLetterOrDigit(c) && c != '-'
186: && c != '_') {
187: charsOk = false;
188: break;
189: }
190: }
191: }
192: if (lenOk && charsOk)
193: return true;
194: FastStringBuffer sb = new FastStringBuffer('"');
195: sb.append(name);
196: sb.append("\" is not a valid register name. ");
197: sb
198: .append("Register names must be between 1 and 25 characters long ");
199: sb
200: .append("and contain only the characters \"a-z\", \"A-Z\", \"0-9\", \"-\", or \"_\".");
201: String message = sb.toString();
202: if (message.length() > 65)
203: message = Utilities.wrap(message, 65, 8);
204: MessageDialog.showMessageDialog(message, "Error");
205: return false;
206: }
207:
208: /*package*/static final String getText(String name) {
209: return getText(name, 0);
210: }
211:
212: // If maxLines > 0, return at most maxLines lines of text.
213: /*package*/static final String getText(String name, int maxLines) {
214: File file = File.getInstance(Directories
215: .getRegistersDirectory(), name);
216: if (!file.isFile())
217: return null;
218: try {
219: BufferedReader reader = new BufferedReader(
220: new InputStreamReader(file.getInputStream()));
221: FastStringBuffer sb = new FastStringBuffer();
222: int lineCount = 0;
223: int c;
224: while ((c = reader.read()) > 0) {
225: sb.append((char) c);
226: if (c == '\n')
227: ++lineCount;
228: if (maxLines > 0 && lineCount == maxLines)
229: break;
230: }
231: reader.close();
232: return sb.toString();
233: } catch (IOException e) {
234: Log.error(e);
235: return null;
236: }
237: }
238: }
|