KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ArchiveMode.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: ArchiveMode.java,v 1.3 2003/07/05 16:03:04 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 gnu.regexp.RE;
25 import gnu.regexp.REMatch;
26 import gnu.regexp.UncheckedRE;
27 import java.awt.AWTEvent JavaDoc;
28 import java.awt.event.KeyEvent JavaDoc;
29 import java.awt.event.MouseEvent JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.text.SimpleDateFormat JavaDoc;
33 import java.util.Date JavaDoc;
34 import java.util.zip.ZipEntry JavaDoc;
35 import java.util.zip.ZipInputStream JavaDoc;
36
37 public final class ArchiveMode extends AbstractMode implements Constants, Mode
38 {
39     private static final ArchiveMode mode = new ArchiveMode();
40     private static final RE moveToFilenameRegExp =
41         new UncheckedRE(":[0-5][0-9] ");
42
43     private ArchiveMode()
44     {
45         super(ARCHIVE_MODE, ARCHIVE_MODE_NAME);
46         setProperty(Property.VERTICAL_RULE, 0);
47         setProperty(Property.SHOW_LINE_NUMBERS, false);
48     }
49
50     public static final ArchiveMode getMode()
51     {
52         return mode;
53     }
54
55     public final Formatter getFormatter(Buffer buffer)
56     {
57         return new PlainTextFormatter(buffer);
58     }
59
60     protected void setKeyMapDefaults(KeyMap km)
61     {
62         km.mapKey(KeyEvent.VK_ENTER, 0, "archiveOpenFile");
63         km.mapKey(KeyEvent.VK_G, CTRL_MASK | SHIFT_MASK, "archiveOpenFile");
64         km.mapKey(VK_DOUBLE_MOUSE_1, 0, "archiveOpenFile");
65         km.mapKey(VK_MOUSE_2, 0, "archiveOpenFile");
66     }
67
68     private static String JavaDoc getName(String JavaDoc s)
69     {
70         REMatch match = moveToFilenameRegExp.getMatch(s);
71         return match == null ? null : s.substring(match.getEndIndex());
72     }
73
74     public static void openFileAtDot(Editor editor)
75     {
76         Buffer buffer = editor.getBuffer();
77         String JavaDoc name = getName(editor.getDotLine().getText());
78         if (name == null)
79             return;
80         String JavaDoc source = null;
81         if (buffer.getFile() != null) {
82             source = "[from " + buffer.getFile().netPath() + "]";
83         } else {
84             Compression compression = buffer.getCompression();
85             if (compression != null && compression.getType() == COMPRESSION_ZIP)
86                 source = "[from " + compression.getEntryName() + " " + compression.getSource() + "]";
87         }
88         String JavaDoc title = name + " " + source;
89         for (BufferIterator it = new BufferIterator(); it.hasNext();) {
90             Buffer maybe = it.nextBuffer();
91             if (title.equals(maybe.getTitle())) {
92                 editor.makeNext(maybe);
93                 editor.activate(maybe);
94                 return;
95             }
96         }
97         File toBeLoaded = null;
98         if (buffer.getCache() != null)
99             toBeLoaded = buffer.getCache();
100         else
101             toBeLoaded = buffer.getFile();
102         ZipInputStream JavaDoc in = null;
103         try {
104             in = new ZipInputStream JavaDoc(toBeLoaded.getInputStream());
105             ZipEntry JavaDoc zipEntry;
106             while((zipEntry = in.getNextEntry()) != null) {
107                 if (zipEntry.getName().equals(name)) {
108                     if (zipEntry.isDirectory()) {
109                         editor.status(name + " is a directory");
110                     } else {
111                         Buffer buf = null;
112                         File cache = cacheEntry(in);
113                         // First see if it's an image...
114
if (Editor.getModeList().modeAccepts(IMAGE_MODE, name))
115                             buf = ImageBuffer.createImageBuffer(null, cache, null);
116                         if (buf != null) {
117                             buf.setCompression(new Compression(COMPRESSION_ZIP,
118                                                                zipEntry,
119                                                                source));
120                             buf.setTitle(title);
121                         } else {
122                             buf = new Buffer();
123                             buf.type = Buffer.TYPE_NORMAL; // Default (may be changed later).
124
buf.initializeUndo();
125                             buf.setCache(cache);
126                             buf.setCompression(new Compression(COMPRESSION_ZIP,
127                                                                zipEntry,
128                                                                source));
129                             buf.initialize(); // May change buffer type.
130
buf.setTitle(title);
131                             buf.readOnly = true;
132                         }
133                         editor.makeNext(buf);
134                         editor.activate(buf);
135                     }
136                     break;
137                 }
138             }
139         }
140         catch (Exception JavaDoc e) {
141             Log.error(e);
142         }
143         try {
144             if (in != null)
145                 in.close();
146         }
147         catch (Exception JavaDoc e) {
148             Log.error(e);
149         }
150     }
151
152     private static File cacheEntry(ZipInputStream JavaDoc in)
153     {
154         File cache = Utilities.getTempFile();
155         if (cache != null) {
156             OutputStream JavaDoc out = null;
157             try {
158                 out = cache.getOutputStream();
159                 byte[] bytes = new byte[16384];
160                 int bytesRead;
161                 while ((bytesRead = in.read(bytes, 0, bytes.length)) > 0)
162                     out.write(bytes, 0, bytesRead);
163             }
164             catch (IOException JavaDoc e) {
165                 Log.error(e);
166             }
167             if (out != null) {
168                 try {
169                     out.close();
170                 }
171                 catch (IOException JavaDoc e) {
172                     Log.error(e);
173                 }
174             }
175         }
176         return cache;
177     }
178
179     public void loadFile(Buffer buffer, File file)
180     {
181         if (!buffer.isLoaded()) {
182             ZipInputStream JavaDoc in = null;
183             try {
184                 in = new ZipInputStream JavaDoc(file.getInputStream());
185                 ZipEntry JavaDoc ze;
186                 while((ze = in.getNextEntry()) != null)
187                     appendLine(buffer, ze);
188                 buffer.renumber();
189
190                 // Is this right if we're loading from a local cache?
191
buffer.setLastModified(file.lastModified());
192                 buffer.setLoaded(true);
193             }
194             catch (Exception JavaDoc e) {
195                 Log.error(e);
196             }
197             try {
198                 if (in != null)
199                     in.close();
200             }
201             catch (Exception JavaDoc e) {
202                 Log.error(e);
203             }
204         }
205     }
206
207     private static final SimpleDateFormat JavaDoc zipEntryDateFormatter =
208         new SimpleDateFormat JavaDoc ("MMM dd yyyy HH:mm");
209
210     private static void appendLine(Buffer buffer, ZipEntry JavaDoc ze)
211     {
212         FastStringBuffer sb = new FastStringBuffer();
213         String JavaDoc sizeString = String.valueOf(ze.getSize());
214         for (int i = 9 - sizeString.length(); i >= 0; i--)
215             sb.append(' ');
216         sb.append(sizeString);
217         sb.append(' ');
218         sb.append(zipEntryDateFormatter.format(new Date JavaDoc(ze.getTime())));
219         sb.append(' ');
220         sb.append(ze.getName());
221         buffer.appendLine(sb.toString());
222     }
223
224     public static void archiveOpenFile()
225     {
226         final Editor editor = Editor.currentEditor();
227         if (editor.getModeId() == ARCHIVE_MODE) {
228             // If this method is invoked via a mouse event mapping, move dot to
229
// location of mouse click first.
230
AWTEvent JavaDoc e = editor.getDispatcher().getLastEvent();
231             if (e instanceof MouseEvent JavaDoc)
232                 editor.mouseMoveDotToPoint((MouseEvent JavaDoc)e);
233             openFileAtDot(editor);
234         }
235     }
236 }
237
Popular Tags