001: /*
002: * ArchiveMode.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: ArchiveMode.java,v 1.3 2003/07/05 16:03:04 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 gnu.regexp.RE;
025: import gnu.regexp.REMatch;
026: import gnu.regexp.UncheckedRE;
027: import java.awt.AWTEvent;
028: import java.awt.event.KeyEvent;
029: import java.awt.event.MouseEvent;
030: import java.io.IOException;
031: import java.io.OutputStream;
032: import java.text.SimpleDateFormat;
033: import java.util.Date;
034: import java.util.zip.ZipEntry;
035: import java.util.zip.ZipInputStream;
036:
037: public final class ArchiveMode extends AbstractMode implements
038: Constants, Mode {
039: private static final ArchiveMode mode = new ArchiveMode();
040: private static final RE moveToFilenameRegExp = new UncheckedRE(
041: ":[0-5][0-9] ");
042:
043: private ArchiveMode() {
044: super (ARCHIVE_MODE, ARCHIVE_MODE_NAME);
045: setProperty(Property.VERTICAL_RULE, 0);
046: setProperty(Property.SHOW_LINE_NUMBERS, false);
047: }
048:
049: public static final ArchiveMode getMode() {
050: return mode;
051: }
052:
053: public final Formatter getFormatter(Buffer buffer) {
054: return new PlainTextFormatter(buffer);
055: }
056:
057: protected void setKeyMapDefaults(KeyMap km) {
058: km.mapKey(KeyEvent.VK_ENTER, 0, "archiveOpenFile");
059: km.mapKey(KeyEvent.VK_G, CTRL_MASK | SHIFT_MASK,
060: "archiveOpenFile");
061: km.mapKey(VK_DOUBLE_MOUSE_1, 0, "archiveOpenFile");
062: km.mapKey(VK_MOUSE_2, 0, "archiveOpenFile");
063: }
064:
065: private static String getName(String s) {
066: REMatch match = moveToFilenameRegExp.getMatch(s);
067: return match == null ? null : s.substring(match.getEndIndex());
068: }
069:
070: public static void openFileAtDot(Editor editor) {
071: Buffer buffer = editor.getBuffer();
072: String name = getName(editor.getDotLine().getText());
073: if (name == null)
074: return;
075: String source = null;
076: if (buffer.getFile() != null) {
077: source = "[from " + buffer.getFile().netPath() + "]";
078: } else {
079: Compression compression = buffer.getCompression();
080: if (compression != null
081: && compression.getType() == COMPRESSION_ZIP)
082: source = "[from " + compression.getEntryName() + " "
083: + compression.getSource() + "]";
084: }
085: String title = name + " " + source;
086: for (BufferIterator it = new BufferIterator(); it.hasNext();) {
087: Buffer maybe = it.nextBuffer();
088: if (title.equals(maybe.getTitle())) {
089: editor.makeNext(maybe);
090: editor.activate(maybe);
091: return;
092: }
093: }
094: File toBeLoaded = null;
095: if (buffer.getCache() != null)
096: toBeLoaded = buffer.getCache();
097: else
098: toBeLoaded = buffer.getFile();
099: ZipInputStream in = null;
100: try {
101: in = new ZipInputStream(toBeLoaded.getInputStream());
102: ZipEntry zipEntry;
103: while ((zipEntry = in.getNextEntry()) != null) {
104: if (zipEntry.getName().equals(name)) {
105: if (zipEntry.isDirectory()) {
106: editor.status(name + " is a directory");
107: } else {
108: Buffer buf = null;
109: File cache = cacheEntry(in);
110: // First see if it's an image...
111: if (Editor.getModeList().modeAccepts(
112: IMAGE_MODE, name))
113: buf = ImageBuffer.createImageBuffer(null,
114: cache, null);
115: if (buf != null) {
116: buf.setCompression(new Compression(
117: COMPRESSION_ZIP, zipEntry, source));
118: buf.setTitle(title);
119: } else {
120: buf = new Buffer();
121: buf.type = Buffer.TYPE_NORMAL; // Default (may be changed later).
122: buf.initializeUndo();
123: buf.setCache(cache);
124: buf.setCompression(new Compression(
125: COMPRESSION_ZIP, zipEntry, source));
126: buf.initialize(); // May change buffer type.
127: buf.setTitle(title);
128: buf.readOnly = true;
129: }
130: editor.makeNext(buf);
131: editor.activate(buf);
132: }
133: break;
134: }
135: }
136: } catch (Exception e) {
137: Log.error(e);
138: }
139: try {
140: if (in != null)
141: in.close();
142: } catch (Exception e) {
143: Log.error(e);
144: }
145: }
146:
147: private static File cacheEntry(ZipInputStream in) {
148: File cache = Utilities.getTempFile();
149: if (cache != null) {
150: OutputStream out = null;
151: try {
152: out = cache.getOutputStream();
153: byte[] bytes = new byte[16384];
154: int bytesRead;
155: while ((bytesRead = in.read(bytes, 0, bytes.length)) > 0)
156: out.write(bytes, 0, bytesRead);
157: } catch (IOException e) {
158: Log.error(e);
159: }
160: if (out != null) {
161: try {
162: out.close();
163: } catch (IOException e) {
164: Log.error(e);
165: }
166: }
167: }
168: return cache;
169: }
170:
171: public void loadFile(Buffer buffer, File file) {
172: if (!buffer.isLoaded()) {
173: ZipInputStream in = null;
174: try {
175: in = new ZipInputStream(file.getInputStream());
176: ZipEntry ze;
177: while ((ze = in.getNextEntry()) != null)
178: appendLine(buffer, ze);
179: buffer.renumber();
180:
181: // Is this right if we're loading from a local cache?
182: buffer.setLastModified(file.lastModified());
183: buffer.setLoaded(true);
184: } catch (Exception e) {
185: Log.error(e);
186: }
187: try {
188: if (in != null)
189: in.close();
190: } catch (Exception e) {
191: Log.error(e);
192: }
193: }
194: }
195:
196: private static final SimpleDateFormat zipEntryDateFormatter = new SimpleDateFormat(
197: "MMM dd yyyy HH:mm");
198:
199: private static void appendLine(Buffer buffer, ZipEntry ze) {
200: FastStringBuffer sb = new FastStringBuffer();
201: String sizeString = String.valueOf(ze.getSize());
202: for (int i = 9 - sizeString.length(); i >= 0; i--)
203: sb.append(' ');
204: sb.append(sizeString);
205: sb.append(' ');
206: sb.append(zipEntryDateFormatter.format(new Date(ze.getTime())));
207: sb.append(' ');
208: sb.append(ze.getName());
209: buffer.appendLine(sb.toString());
210: }
211:
212: public static void archiveOpenFile() {
213: final Editor editor = Editor.currentEditor();
214: if (editor.getModeId() == ARCHIVE_MODE) {
215: // If this method is invoked via a mouse event mapping, move dot to
216: // location of mouse click first.
217: AWTEvent e = editor.getDispatcher().getLastEvent();
218: if (e instanceof MouseEvent)
219: editor.mouseMoveDotToPoint((MouseEvent) e);
220: openFileAtDot(editor);
221: }
222: }
223: }
|