1 29 30 package echo2example.email; 31 32 import javax.mail.Folder ; 33 import javax.mail.Message ; 34 import javax.mail.MessagingException ; 35 import javax.mail.Store ; 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 58 public class MailScreen extends ContentPane { 59 60 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 [] folders; 66 67 private MessageListTable messageListTable; 68 private PageNavigator pageNavigator; 69 private MessagePane messagePane; 70 private SelectField folderSelect; 71 private Message selectedMessage; 72 private int dialogPosition = DIALOG_POSITION_INITIAL; 73 74 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 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 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 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 213 private void processCompose(Message 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 231 private void setFolder(Folder 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 ex) { 244 EmailApp.getApp().processFatalException(ex); 245 } 246 } 247 248 255 public void setStore(Store store) 256 throws MessagingException { 257 folders = store.getDefaultFolder().list("*"); 258 folderSelect.setModel(new AbstractListModel() { 259 260 public Object 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 |