KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > folder > mailboximport > AbstractMailboxImporter


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16

17 package org.columba.mail.folder.mailboximport;
18
19 import java.io.File JavaDoc;
20
21 import javax.swing.JOptionPane JavaDoc;
22
23 import org.columba.api.command.IWorkerStatusController;
24 import org.columba.api.plugin.IExtensionInterface;
25 import org.columba.core.gui.frame.FrameManager;
26 import org.columba.core.resourceloader.IconKeys;
27 import org.columba.core.resourceloader.ImageLoader;
28 import org.columba.mail.folder.IMailbox;
29 import org.columba.ristretto.io.CharSequenceSource;
30 import org.columba.ristretto.io.SourceInputStream;
31
32 /**
33  * This is the base class for mailbox importers.
34  */

35 public abstract class AbstractMailboxImporter implements IExtensionInterface{
36     public static final int TYPE_FILE = 0;
37     public static final int TYPE_DIRECTORY = 1;
38
39     protected IMailbox destinationFolder;
40     protected File JavaDoc[] sourceFiles;
41     protected int counter = 0;
42
43     public AbstractMailboxImporter(IMailbox destinationFolder, File JavaDoc[] sourceFiles) {
44         this();
45         setDestinationFolder(destinationFolder);
46         setSourceFiles(sourceFiles);
47     }
48
49     /**
50      * Default constructor
51      */

52     public AbstractMailboxImporter() {}
53
54     /**
55      * Override this method to specify type.
56      * the wizard dialog will open the correct file/directory dialog automatically
57      */

58     public int getType() {
59         return TYPE_FILE;
60     }
61
62     /**
63      * Override this method to do the actual import work. In here, the messages
64      * should be read and passed to the folder using saveMessage.
65      */

66     public abstract void importMailboxFile(File JavaDoc file,
67         IWorkerStatusController worker, IMailbox destFolder)
68         throws Exception JavaDoc;
69
70     /**
71      * Override this method to provide an adequate description to the user.
72      */

73     public abstract String JavaDoc getDescription();
74
75     /*********** intern methods (no need to overwrite these) ****************/
76     /**
77      * Sets the source files/directories.
78      */

79     public void setSourceFiles(File JavaDoc[] files) {
80         this.sourceFiles = files;
81     }
82
83     /**
84      * Set the destination folder.
85      */

86     public void setDestinationFolder(IMailbox folder) {
87         destinationFolder = folder;
88     }
89
90     /**
91      * Returns the number of successfully imported messages so far.
92      */

93     public int getCount() {
94         return counter;
95     }
96
97     /**
98      * This method calls your overridden importMailbox(File)-method
99      * and handles exceptions.
100      */

101     public void run(IWorkerStatusController worker) {
102         //TODO (@author fdietz): i18n
103
worker.setDisplayText("Importing messages...");
104
105         importMailbox(worker);
106
107         if (getCount() == 0) {
108             //TODO (@author fdietz): i18n
109
JOptionPane.showMessageDialog(FrameManager.getInstance()
110                     .getActiveFrame(),
111                 "Message import failed! No messages were added to the folder.\n" +
112                 "This means that the parser didn't throw any exception even if " +
113                 "it didn't recognize the mailbox format or the messagebox simply " +
114                 "didn't contain any messages.",
115                 "Warning", JOptionPane.WARNING_MESSAGE);
116
117             return;
118         } else {
119             //TODO (@author fdietz): i18n
120
JOptionPane.showMessageDialog(null,
121                 "Message import was successful!", "Information",
122                 JOptionPane.INFORMATION_MESSAGE,
123                 ImageLoader.getIcon(IconKeys.DIALOG_INFO));
124         }
125     }
126
127     /**
128      * Import all mailbox files in Columba. This method makes use of the
129      * importMailbox method you have to override and simply iterates over all
130      * given files/directories.
131      *
132      * @param worker
133      */

134     public void importMailbox(IWorkerStatusController worker) {
135         File JavaDoc[] listing = getSourceFiles();
136
137         for (int i = 0; i < listing.length; i++) {
138             if (worker.cancelled()) {
139                 return;
140             }
141
142             try {
143                 importMailboxFile(listing[i], worker, getDestinationFolder());
144             } catch (Exception JavaDoc ex) {
145                 //TODO (@author fdietz): i18n
146
int result = JOptionPane.showConfirmDialog(
147                         FrameManager.getInstance().getActiveFrame(),
148                     "An error occured while importing a message. Try again?",
149                     "Retry message import?",
150                     JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
151                 if (result == JOptionPane.YES_OPTION) {
152                     i--;
153                 } else if (result == JOptionPane.CANCEL_OPTION) {
154                     worker.cancel();
155                 }
156             }
157         }
158     }
159
160     /**
161      * Use this method to save a message in the specified destination folder.
162      */

163     protected void saveMessage(String JavaDoc rawString, IWorkerStatusController worker,
164             IMailbox destFolder) throws Exception JavaDoc {
165         /*
166          * *20031231, karlpeder* Using InputStream instead of rawString
167          * directly. Ensures size is set correctly by addMessage (bug #843657)
168          */

169
170         SourceInputStream in = new SourceInputStream(new CharSequenceSource(
171                     rawString));
172         destFolder.addMessage(in);
173         in.close();
174
175         counter++;
176
177         //TODO (@author fdietz): i18n
178
worker.setDisplayText("Importing messages: " + getCount());
179     }
180
181     /**
182      * Returns the folder new messages will be added to.
183      */

184     public IMailbox getDestinationFolder() {
185         return destinationFolder;
186     }
187
188     /**
189      * Returns the source files/directories new messages will be read from.
190      */

191     public File JavaDoc[] getSourceFiles() {
192         return sourceFiles;
193     }
194 }
195
Popular Tags