KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > News


1 /*
2  * News.java
3  *
4  * Copyright (C) 2000-2003 Peter Graves
5  * $Id: News.java,v 1.5 2003/06/29 00:19:34 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.mail;
23
24 import java.io.BufferedWriter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStreamWriter JavaDoc;
28 import javax.swing.SwingUtilities JavaDoc;
29 import org.armedbear.j.Buffer;
30 import org.armedbear.j.Directories;
31 import org.armedbear.j.Editor;
32 import org.armedbear.j.EditorIterator;
33 import org.armedbear.j.File;
34 import org.armedbear.j.Line;
35 import org.armedbear.j.Log;
36 import org.armedbear.j.MessageDialog;
37 import org.armedbear.j.ProgressNotifier;
38 import org.armedbear.j.StatusBarProgressNotifier;
39
40 public final class News extends Buffer
41 {
42     private static final File newsDir =
43         File.getInstance(Directories.getEditorDirectory(), "news");
44
45     private final NntpSession session;
46     private boolean error;
47
48     public News(NntpSession session)
49     {
50         this.session = session;
51         supportsUndo = false;
52         mode = NewsGroupsMode.getMode();
53         formatter = mode.getFormatter(this);
54         readOnly = true;
55         title = session.getHost();
56         setInitialized(true);
57     }
58
59     public String JavaDoc getHost()
60     {
61         return session.getHost();
62     }
63
64     public int load()
65     {
66         setBusy(true);
67         new Thread JavaDoc(loadRunnable).start();
68         setLoaded(true);
69         return LOAD_COMPLETED;
70     }
71
72     private Runnable JavaDoc loadRunnable = new Runnable JavaDoc() {
73         public void run()
74         {
75             try {
76                 lockWrite();
77             }
78             catch (InterruptedException JavaDoc e) {
79                 Log.error(e);
80                 return;
81             }
82             try {
83                 _load();
84             }
85             finally {
86                 unlockWrite();
87             }
88             if (error)
89                 SwingUtilities.invokeLater(errorRunnable);
90             else
91                 SwingUtilities.invokeLater(updateDisplayRunnable);
92         }
93     };
94
95     private void _load()
96     {
97         File file = File.getInstance(newsDir, session.getHost());
98         if (newsDir.isDirectory()) {
99             if (file.isFile()) {
100                 try {
101                     InputStream JavaDoc in = file.getInputStream();
102                     if (in != null) {
103                         load(in, null);
104                         in.close();
105                     }
106                 }
107                 catch (IOException JavaDoc e) {
108                     Log.error(e);
109                 }
110             }
111         } else
112             newsDir.mkdirs();
113         if (getFirstLine() == null) {
114             if (session.connect()) {
115                 if (session.writeLine("LIST")) {
116                     String JavaDoc response = session.readLine();
117                     if (response.startsWith("215")) {
118                         session.setEcho(false);
119                         int count = 0;
120                         ProgressNotifier progressNotifier =
121                             new StatusBarProgressNotifier(this);
122                         progressNotifier.progressStart();
123                         while (true) {
124                             String JavaDoc s = session.readLine();
125                             if (s == null)
126                                 break;
127                             if (s.equals("."))
128                                 break;
129                             int index = s.indexOf(' ');
130                             if (index >= 0)
131                                 appendLine(s.substring(0, index));
132                             else
133                                 appendLine(s);
134                             ++count;
135                             progressNotifier.progress(String.valueOf(count));
136                         }
137                         if (newsDir.isDirectory()) {
138                             try {
139                                 BufferedWriter JavaDoc writer = new BufferedWriter JavaDoc(
140                                     new OutputStreamWriter JavaDoc(file.getOutputStream()));
141                                 for (Line line = getFirstLine(); line != null; line = line.next()) {
142                                     writer.write(line.getText());
143                                     writer.write('\n');
144                                 }
145                                 writer.flush();
146                                 writer.close();
147                             }
148                             catch (IOException JavaDoc e) {
149                                 Log.error(e);
150                             }
151                         }
152                         progressNotifier.progressStop();
153                         session.setEcho(NntpSession.DEFAULT_ECHO);
154                     }
155                 }
156                 session.disconnect();
157             }
158         }
159         if (getFirstLine() == null) {
160             error = true;
161             appendLine("");
162         }
163         renumber();
164     }
165
166     private Runnable JavaDoc errorRunnable = new Runnable JavaDoc() {
167         public void run()
168         {
169             kill();
170             String JavaDoc errorText = session.getErrorText();
171             if (errorText != null)
172                 MessageDialog.showMessageDialog(errorText, "Error");
173         }
174     };
175
176     private Runnable JavaDoc updateDisplayRunnable = new Runnable JavaDoc() {
177         public void run()
178         {
179             setBusy(false);
180             invalidate();
181             for (EditorIterator it = new EditorIterator(); it.hasNext();) {
182                 Editor ed = it.nextEditor();
183                 if (ed.getBuffer() == News.this) {
184                     ed.setDot(getFirstLine(), 0);
185                     ed.moveCaretToDotCol();
186                     ed.setTopLine(getFirstLine());
187                     ed.setUpdateFlag(REPAINT);
188                     ed.updateDisplay();
189                 }
190             }
191         }
192     };
193 }
194
Popular Tags