KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > echo2example > email > MailScreen


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package echo2example.email;
31
32 import javax.mail.Folder JavaDoc;
33 import javax.mail.Message JavaDoc;
34 import javax.mail.MessagingException JavaDoc;
35 import javax.mail.Store JavaDoc;
36
37 import echo2example.email.MessageListTable.MessageSelectionEvent;
38 import echo2example.email.PageNavigator.PageIndexChangeEvent;
39
40 import nextapp.echo2.app.Button;
41 import nextapp.echo2.app.Column;
42 import nextapp.echo2.app.Component;
43 import nextapp.echo2.app.ContentPane;
44 import nextapp.echo2.app.Extent;
45 import nextapp.echo2.app.Label;
46 import nextapp.echo2.app.Row;
47 import nextapp.echo2.app.SelectField;
48 import nextapp.echo2.app.SplitPane;
49 import nextapp.echo2.app.event.ActionEvent;
50 import nextapp.echo2.app.event.ActionListener;
51 import nextapp.echo2.app.list.AbstractListModel;
52
53 /**
54  * The multi-paned main mail-viewing screen of the application.
55  * This component contains the page navigator, message list, and message
56  * preview panes.
57  */

58 public class MailScreen extends ContentPane {
59     
60     // Constants for new dialog positioning algorithm.
61
private static final int DIALOG_POSITION_INITIAL = 80;
62     private static final int DIALOG_POSITION_INCREMENT = 20;
63     private static final int DIALOG_POSITION_MAXIMUM = 200;
64     
65     private Folder JavaDoc[] folders;
66     
67     private MessageListTable messageListTable;
68     private PageNavigator pageNavigator;
69     private MessagePane messagePane;
70     private SelectField folderSelect;
71     private Message JavaDoc selectedMessage;
72     private int dialogPosition = DIALOG_POSITION_INITIAL;
73     
74     /**
75      * Creates a new <code>MailScreen</code>.
76      */

77     public MailScreen() {
78         super();
79         
80         SplitPane mainSplitPane = new SplitPane(SplitPane.ORIENTATION_HORIZONTAL, new Extent(175));
81         mainSplitPane.setSeparatorWidth(new Extent(1, Extent.PX));
82         add(mainSplitPane);
83         
84         SplitPane titleOptionSplitPane = new SplitPane(SplitPane.ORIENTATION_VERTICAL, new Extent(70));
85         titleOptionSplitPane.setSeparatorHeight(new Extent(1, Extent.PX));
86         mainSplitPane.add(titleOptionSplitPane);
87         
88         Column titleColumn = new Column();
89         titleColumn.setStyleName("MailScreen.TitleColumn");
90         titleOptionSplitPane.add(titleColumn);
91         Label label;
92         
93         label = new Label(Messages.getString("Application.Title.Main"));
94         label.setStyleName("Title.Main");
95         titleColumn.add(label);
96         
97         label = new Label(Messages.getString("Application.Title.Sub"));
98         label.setStyleName("Title.Sub");
99         titleColumn.add(label);
100         
101         titleOptionSplitPane.add(createOptionPane());
102         
103         SplitPane mailSplitPane = new SplitPane(SplitPane.ORIENTATION_VERTICAL, new Extent(320));
104         mailSplitPane.setResizable(true);
105         mainSplitPane.add(mailSplitPane);
106         
107         SplitPane messageListSplitPane = new SplitPane(SplitPane.ORIENTATION_VERTICAL, new Extent(32));
108         messageListSplitPane.setSeparatorHeight(new Extent(1, Extent.PX));
109         mailSplitPane.add(messageListSplitPane);
110         
111         Row controlPane = new Row();
112         controlPane.setStyleName("ControlPane");
113         messageListSplitPane.add(controlPane);
114         
115         pageNavigator = new PageNavigator();
116         pageNavigator.addPageIndexChangeListener(new PageNavigator.PageIndexChangeListener() {
117             public void pageIndexChanged(PageIndexChangeEvent e) {
118                 try {
119                     messageListTable.setPageIndex(e.getNewPageIndex());
120                     messagePane.setMessage(null);
121                 } catch (MessagingException JavaDoc ex) {
122                     EmailApp.getApp().processFatalException(ex);
123                 }
124             }
125         });
126         controlPane.add(pageNavigator);
127         
128         messageListTable = new MessageListTable();
129         messageListTable.addMessageSelectionListener(new MessageListTable.MessageSelectionListener() {
130             public void messageSelected(MessageSelectionEvent e) {
131                 try {
132                     selectedMessage = e.getMessage();
133                     messagePane.setMessage(selectedMessage);
134                 } catch (MessagingException JavaDoc ex) {
135                     EmailApp.getApp().processFatalException(ex);
136                 }
137             }
138         });
139         messageListSplitPane.add(messageListTable);
140         
141         messagePane = new MessagePane();
142         mailSplitPane.add(messagePane);
143     }
144     
145     /**
146      * Creates the "option pane".
147      *
148      * @return the option pane <code>Component</code>
149      */

150     private Component createOptionPane() {
151         Button button;
152         Label label;
153
154         Column optionColumn = new Column();
155         optionColumn.setStyleName("MailScreen.OptionColumn");
156         
157         Column folderSelectColumn = new Column();
158         folderSelectColumn.setStyleName("MailScreen.FolderSelectColumn");
159         optionColumn.add(folderSelectColumn);
160         
161         label = new Label(Messages.getString("MailScreen.PromptFolderSelect"));
162         folderSelectColumn.add(label);
163         
164         folderSelect = new SelectField();
165         folderSelect.addActionListener(new ActionListener() {
166             public void actionPerformed(ActionEvent e) {
167                 setFolder(folders[folderSelect.getSelectedIndex()]);
168             }
169         });
170         folderSelectColumn.add(folderSelect);
171         
172         Column actionsColumn = new Column();
173         optionColumn.add(actionsColumn);
174         
175         button = new Button(Messages.getString("MailScreen.ButtonNewMessage"), Styles.ICON_24_MAIL_COMPOSE);
176         button.setStyleName("MailScreen.OptionButton");
177         button.addActionListener(new ActionListener() {
178             public void actionPerformed(ActionEvent e) {
179                 processCompose(null);
180             }
181         });
182         actionsColumn.add(button);
183         
184         button = new Button(Messages.getString("MailScreen.ButtonReplyTo"), Styles.ICON_24_MAIL_REPLY);
185         button.setStyleName("MailScreen.OptionButton");
186         button.addActionListener(new ActionListener() {
187             public void actionPerformed(ActionEvent e) {
188                 if (selectedMessage != null) {
189                     processCompose(selectedMessage);
190                 }
191             }
192         });
193         actionsColumn.add(button);
194         
195         button = new Button(Messages.getString("MailScreen.ButtonLogOut"), Styles.ICON_24_EXIT);
196         button.setStyleName("MailScreen.OptionButton");
197         button.addActionListener(new ActionListener() {
198             public void actionPerformed(ActionEvent e) {
199                 ((EmailApp) getApplicationInstance()).disconnect();
200             }
201         });
202         optionColumn.add(button);
203         
204         return optionColumn;
205     }
206     
207     /**
208      * Processes a user request to compose/reply to a message.
209      *
210      * @param message the message to reply to, or null to a compose a new
211      * message.
212      */

213     private void processCompose(Message JavaDoc message) {
214         ComposeWindow composeWindow = new ComposeWindow(message);
215         Extent dialogPositionExtent = new Extent(dialogPosition);
216         composeWindow.setPositionX(dialogPositionExtent);
217         composeWindow.setPositionY(dialogPositionExtent);
218         dialogPosition += DIALOG_POSITION_INCREMENT;
219         if (dialogPosition > DIALOG_POSITION_MAXIMUM) {
220             dialogPosition = DIALOG_POSITION_INITIAL;
221         }
222         
223         getApplicationInstance().getDefaultWindow().getContent().add(composeWindow);
224     }
225
226     /**
227      * Sets the active folder.
228      *
229      * @param folder the <code>Folder</code>
230      */

231     private void setFolder(Folder JavaDoc folder) {
232         try {
233             messageListTable.setFolder(null);
234             int messageCount = folder.getMessageCount();
235             int totalPages = folder.getMessageCount() / EmailApp.MESSAGES_PER_PAGE;
236             if (messageCount % EmailApp.MESSAGES_PER_PAGE > 0) {
237                 ++totalPages;
238             }
239             pageNavigator.setTotalPages(totalPages);
240             pageNavigator.setPageIndex(totalPages - 1);
241             messageListTable.setFolder(folder);
242             messagePane.setMessage(null);
243         } catch (MessagingException JavaDoc ex) {
244             EmailApp.getApp().processFatalException(ex);
245         }
246     }
247
248     /**
249      * Sets the mail <code>Store</code>.
250      * This method is invoked by the <code>EmailApp</code> instance to
251      * initialize the <code>MailScreen</code>.
252      *
253      * @param store the <code>Store</code>
254      */

255     public void setStore(Store JavaDoc store)
256     throws MessagingException JavaDoc {
257         folders = store.getDefaultFolder().list("*");
258         folderSelect.setModel(new AbstractListModel() {
259         
260             public Object JavaDoc get(int index) {
261                 return folders[index].getName();
262             }
263     
264             public int size() {
265                 return folders.length;
266             }
267         });
268         for (int i = 0; i < folders.length; ++i) {
269             if ("INBOX".equals(folders[i].getName())) {
270                 folderSelect.setSelectedIndex(i);
271                 setFolder(folders[i]);
272                 break;
273             }
274         }
275     }
276 }
277
Popular Tags