KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > propedit > FirstRunWizardController


1 package net.suberic.pooka.gui.propedit;
2 import net.suberic.pooka.Pooka;
3 import net.suberic.pooka.gui.FolderNode;
4 import net.suberic.pooka.gui.FolderPanel;
5 import net.suberic.pooka.gui.MailTreeNode;
6 import net.suberic.util.VariableBundle;
7 import net.suberic.util.gui.propedit.*;
8 import net.suberic.util.thread.ActionThread;
9
10 import java.io.File JavaDoc;
11 import java.util.*;
12 import javax.mail.MessagingException JavaDoc;
13 import javax.swing.Action JavaDoc;
14 import javax.swing.JLabel JavaDoc;
15 import javax.swing.JOptionPane JavaDoc;
16
17 /**
18  * The controller class for the FirstRunWizard.
19  */

20 public class FirstRunWizardController extends NewStoreWizardController {
21
22   /**
23    * Creates a FirstRunWizardController.
24    */

25   public FirstRunWizardController(String JavaDoc sourceTemplate, WizardEditorPane wep) {
26     super(sourceTemplate, wep);
27   }
28
29   /**
30    * Finsihes the wizard.
31    */

32   public void finishWizard() throws PropertyValueVetoException {
33     saveProperties();
34     getEditorPane().addDisableMask(this);
35     getManager().commit();
36     setupFolders();
37     Pooka.getUIFactory().showStatusMessage(Pooka.getProperty("Pooka._firstRunWizard.status.loadingFolders", "Loading folders..."));
38     Pooka.getStoreManager().loadAllSentFolders();
39     Pooka.getOutgoingMailManager().loadOutboxFolders();
40     Pooka.getPookaManager().getResources().saveProperties();
41
42     Pooka.getUIFactory().showStatusMessage(Pooka.getProperty("Pooka._firstRunWizard.status.openingInbox", "Opening Inbox..."));
43     openInbox();
44
45   }
46
47   /**
48    * Sets up your sent folder and outbox.
49    */

50   public void setupFolders() {
51     boolean useLocalFiles = Pooka.getProperty("Pooka.useLocalFiles", "true").equalsIgnoreCase("true");
52     String JavaDoc storeName = Pooka.getProperty("Store");
53     String JavaDoc protocol = Pooka.getProperty("Store." + storeName + ".protocol");
54     String JavaDoc localStoreName = storeName;
55
56     if (protocol.equalsIgnoreCase("imap") && useLocalFiles) {
57       // if we have an imap connection, then we actually have to do some
58
// work.
59
localStoreName = "local";
60       Pooka.setProperty("Store.local.useInbox", "false");
61       Pooka.setProperty("Store.local.folderList", "sent:outbox");
62       Pooka.setProperty("Store.local.protocol", "maildir");
63       Pooka.setProperty("Store", storeName + ":local");
64     } else {
65       // we're fine if not.
66
Pooka.setProperty("Store." + localStoreName + ".folderList", "INBOX:sent:outbox");
67     }
68     String JavaDoc pookaDirName = Pooka.getProperty("FirstRunWizard.pookaDirectory", System.getProperty("user.home") + File.separator + ".pooka");
69     String JavaDoc mailDirName = pookaDirName + File.separator + localStoreName;
70     String JavaDoc subFolderDirName = mailDirName + File.separator + Pooka.getProperty("Pooka.subFolderName", "folders");
71
72     if (useLocalFiles) {
73       File JavaDoc mailDir = new File JavaDoc(mailDirName);
74       if (! mailDir.exists())
75         mailDir.mkdirs();
76
77       File JavaDoc subFolderDir = new File JavaDoc(subFolderDirName);
78       if (! subFolderDir.exists())
79         subFolderDir.mkdirs();
80
81       File JavaDoc sentFile = new File JavaDoc(subFolderDirName + File.separator + ".sent");
82       if (! sentFile.exists()) {
83         sentFile.mkdir();
84
85         // i should probably have the maildir store do this.
86
new File JavaDoc(sentFile, "cur").mkdir();
87         new File JavaDoc(sentFile, "new").mkdir();
88         new File JavaDoc(sentFile, "tmp").mkdir();
89       }
90     }
91
92     if (useLocalFiles) {
93       File JavaDoc outboxFile = new File JavaDoc(subFolderDirName + File.separator + ".outbox");
94       if (! outboxFile.exists()) {
95         outboxFile.mkdir();
96
97         new File JavaDoc(outboxFile, "cur").mkdir();
98         new File JavaDoc(outboxFile, "new").mkdir();
99         new File JavaDoc(outboxFile, "tmp").mkdir();
100       }
101
102       Pooka.setProperty("Store.local.mailDir", mailDirName);
103
104     }
105
106
107     // actually configure said folders.
108

109     String JavaDoc outgoingServer = Pooka.getProperty("OutgoingServer");
110     Pooka.setProperty("OutgoingServer." + outgoingServer + ".outbox", localStoreName + "/outbox");
111
112     String JavaDoc userName = Pooka.getProperty("UserProfile");
113     Pooka.setProperty("UserProfile." + userName + ".sentFolder", localStoreName + "/sent");
114   }
115
116
117   Exception JavaDoc mOpenInboxException = null;
118   boolean mOpenInboxSuccessful = false;
119   /**
120    * Opens up your inbox.
121    */

122   public void openInbox() {
123     java.util.Vector JavaDoc allStores = Pooka.getStoreManager().getStoreList();
124     net.suberic.pooka.StoreInfo si = null;
125     if (allStores.size() > 0) {
126       si = (net.suberic.pooka.StoreInfo) allStores.get(0);
127     }
128
129     if (si != null) {
130       ActionThread thread = si.getStoreThread();
131       final net.suberic.pooka.StoreInfo storeInfo = si;
132
133       // set our local variables to track what's going on.
134
mOpenInboxException = null;
135       mOpenInboxSuccessful = false;
136
137       javax.swing.Action JavaDoc connectionAction = new javax.swing.AbstractAction JavaDoc() {
138           public void actionPerformed(java.awt.event.ActionEvent JavaDoc ae) {
139             try {
140               storeInfo.connectStore();
141               javax.swing.SwingUtilities.invokeLater( new Runnable JavaDoc() {
142
143                   public void run() {
144                     MailTreeNode mtn = null;
145                     net.suberic.pooka.FolderInfo fi = storeInfo.getChild("INBOX");
146                     if (fi != null) {
147                       FolderNode fn = fi.getFolderNode();
148                       Action JavaDoc openAction = fn.getAction("file-open");
149                       openAction.actionPerformed(new java.awt.event.ActionEvent JavaDoc(this, 0, "file-open"));
150                       mtn = fn;
151                     } else {
152                       mtn = storeInfo.getStoreNode();
153                     }
154                     if (mtn != null) {
155                       javax.swing.JTree JavaDoc folderTree = ((FolderPanel)mtn.getParentContainer()).getFolderTree();
156                       folderTree.scrollPathToVisible(new javax.swing.tree.TreePath JavaDoc(mtn.getPath()));
157                     }
158
159                     openInboxSuccess();
160                   }
161                 });
162             } catch (MessagingException JavaDoc me) {
163               Pooka.getUIFactory().clearStatus();
164               me.printStackTrace();
165               int continueValue = handleInvalidEntry(me.getMessage());
166               if (continueValue == JOptionPane.YES_OPTION) {
167                 getEditorPane().removeDisableMask(FirstRunWizardController.this);
168                 // remove all of the properties we just set.
169
clearProperties();
170                 mState = "storeConfig";
171                 getEditorPane().loadState("storeConfig");
172               } else {
173                 getEditorPane().getWizardContainer().closeWizard();
174               }
175             }
176           }
177         };
178
179       thread.addToQueue(connectionAction, new java.awt.event.ActionEvent JavaDoc(this, 0, "connectStore"));
180     }
181
182   }
183
184   private void openInboxSuccess() {
185     Pooka.getUIFactory().clearStatus();
186     getEditorPane().getWizardContainer().closeWizard();
187   }
188
189   public int handleInvalidEntry(String JavaDoc message) {
190     StringBuffer JavaDoc errorMessage = new StringBuffer JavaDoc(Pooka.getProperty("error.NewAccountPooka.invalidEntry", "invalid first entry."));
191     if (message != null && message.length() > 0) {
192       errorMessage.append("\n");
193       errorMessage.append(message);
194     }
195     errorMessage.append("\n\n");
196     errorMessage.append(Pooka.getProperty("error.NewAccountPooka.continueMessage", "Would you like to re-enter your information?"));
197
198     JLabel JavaDoc jta = new JLabel JavaDoc(errorMessage.toString());
199     int continueResponse = JOptionPane.showOptionDialog(Pooka.getMainPanel(), errorMessage.toString(), "Failed to connect to Store.", javax.swing.JOptionPane.YES_NO_OPTION, javax.swing.JOptionPane.WARNING_MESSAGE, null, new Object JavaDoc[] { "Re-enter", "Continue" }, "Re-enter");
200     if (continueResponse == 0) {
201       return JOptionPane.YES_OPTION;
202     } else {
203       return JOptionPane.NO_OPTION;
204     }
205
206   }
207
208
209   /**
210    * Clears the proprties for this wizard.
211    */

212   private void clearProperties() {
213     // we have to do this in two phases to make sure that we don't
214
// accidentally modify a Store/User/SMTP Server that is being
215
// removed.
216
getManager().removeProperty("Store");
217     getManager().removeProperty("User");
218     getManager().removeProperty("OutgoingServer");
219
220     getManager().commit();
221
222     Set<String JavaDoc> removeProperties = getManager().getPropertyNamesStartingWith("Store");
223     for (String JavaDoc prop: removeProperties) {
224       //System.err.println("removing property " + prop);
225
getManager().removeProperty(prop);
226     }
227     removeProperties = getManager().getPropertyNamesStartingWith("User");
228     for (String JavaDoc prop: removeProperties) {
229       //System.err.println("removing property " + prop);
230
getManager().removeProperty(prop);
231     }
232     removeProperties = getManager().getPropertyNamesStartingWith("OutgoingServer");
233     for (String JavaDoc prop: removeProperties) {
234       //System.err.println("removing property " + prop);
235
getManager().removeProperty(prop);
236     }
237     getManager().commit();
238
239   }
240 }
241
Popular Tags