| 1 | /*- |
|---|
| 2 | * Copyright (c) 2009, Derek Konigsberg |
|---|
| 3 | * All rights reserved. |
|---|
| 4 | * |
|---|
| 5 | * Redistribution and use in source and binary forms, with or without |
|---|
| 6 | * modification, are permitted provided that the following conditions |
|---|
| 7 | * are met: |
|---|
| 8 | * |
|---|
| 9 | * 1. Redistributions of source code must retain the above copyright |
|---|
| 10 | * notice, this list of conditions and the following disclaimer. |
|---|
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the |
|---|
| 13 | * documentation and/or other materials provided with the distribution. |
|---|
| 14 | * 3. Neither the name of the project nor the names of its |
|---|
| 15 | * contributors may be used to endorse or promote products derived |
|---|
| 16 | * from this software without specific prior written permission. |
|---|
| 17 | * |
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|---|
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|---|
| 22 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|---|
| 24 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|---|
| 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|---|
| 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|---|
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
|---|
| 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 30 | */ |
|---|
| 31 | package org.logicprobe.LogicMail.ui; |
|---|
| 32 | |
|---|
| 33 | import net.rim.device.api.i18n.ResourceBundle; |
|---|
| 34 | import net.rim.device.api.ui.Screen; |
|---|
| 35 | import net.rim.device.api.ui.UiApplication; |
|---|
| 36 | import net.rim.device.api.ui.component.Dialog; |
|---|
| 37 | import net.rim.device.api.ui.component.Status; |
|---|
| 38 | |
|---|
| 39 | import org.logicprobe.LogicMail.LogicMailResource; |
|---|
| 40 | import org.logicprobe.LogicMail.mail.MailConnectionListener; |
|---|
| 41 | import org.logicprobe.LogicMail.mail.MailConnectionLoginEvent; |
|---|
| 42 | import org.logicprobe.LogicMail.mail.MailConnectionManager; |
|---|
| 43 | import org.logicprobe.LogicMail.mail.MailConnectionStateEvent; |
|---|
| 44 | import org.logicprobe.LogicMail.mail.MailConnectionStatusEvent; |
|---|
| 45 | import org.logicprobe.LogicMail.model.MailManager; |
|---|
| 46 | import org.logicprobe.LogicMail.model.MailRootNode; |
|---|
| 47 | import org.logicprobe.LogicMail.model.MailboxNode; |
|---|
| 48 | import org.logicprobe.LogicMail.model.MessageNode; |
|---|
| 49 | import org.logicprobe.LogicMail.model.NetworkAccountNode; |
|---|
| 50 | import org.logicprobe.LogicMail.util.EventObjectRunnable; |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | * Controller class for screen creation. |
|---|
| 54 | * This class is intended to hide all screen creation logic |
|---|
| 55 | * from the rest of the UI, and provide a simple interface |
|---|
| 56 | * in terms of viewing data model objects. |
|---|
| 57 | */ |
|---|
| 58 | public final class NavigationController { |
|---|
| 59 | private static ResourceBundle resources = ResourceBundle.getBundle(LogicMailResource.BUNDLE_ID, LogicMailResource.BUNDLE_NAME); |
|---|
| 60 | private ScreenFactory screenFactory; |
|---|
| 61 | private MailRootNode mailRootNode; |
|---|
| 62 | |
|---|
| 63 | private UiApplication uiApplication; |
|---|
| 64 | private StandardScreen mailHomeScreen; |
|---|
| 65 | private String currentStatus; |
|---|
| 66 | |
|---|
| 67 | private MessageActions messageActions; |
|---|
| 68 | |
|---|
| 69 | public NavigationController(UiApplication uiApplication) { |
|---|
| 70 | this.uiApplication = uiApplication; |
|---|
| 71 | this.screenFactory = ScreenFactory.getInstance(); |
|---|
| 72 | this.mailRootNode = MailManager.getInstance().getMailRootNode(); |
|---|
| 73 | this.messageActions = new MessageActions(this); |
|---|
| 74 | |
|---|
| 75 | MailConnectionManager.getInstance().addMailConnectionListener(new MailConnectionListener() { |
|---|
| 76 | public void mailConnectionStateChanged(MailConnectionStateEvent e) { } |
|---|
| 77 | public void mailConnectionStatus(MailConnectionStatusEvent e) { |
|---|
| 78 | handleMailConnectionStatus(e); |
|---|
| 79 | } |
|---|
| 80 | public void mailConnectionError(MailConnectionStatusEvent e) { |
|---|
| 81 | handleMailConnectionError(e); |
|---|
| 82 | } |
|---|
| 83 | public void mailConnectionLogin(MailConnectionLoginEvent e) { |
|---|
| 84 | handleMailConnectionLogin(e); |
|---|
| 85 | } |
|---|
| 86 | }); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | public synchronized void displayMailHome() { |
|---|
| 90 | if(mailHomeScreen == null) { |
|---|
| 91 | mailHomeScreen = screenFactory.getMailHomeScreen(this, mailRootNode); |
|---|
| 92 | } |
|---|
| 93 | uiApplication.pushScreen(mailHomeScreen); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | public synchronized void displayAccountConfigurationWizard() { |
|---|
| 97 | UiApplication.getUiApplication().invokeLater(new Runnable() { |
|---|
| 98 | public void run() { |
|---|
| 99 | // Start the new account configuration wizard |
|---|
| 100 | AccountConfigWizard wizard = new AccountConfigWizard(); |
|---|
| 101 | wizard.start(); |
|---|
| 102 | } |
|---|
| 103 | }); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | //TODO: Figure out where/when/how to clear screen transitions |
|---|
| 107 | |
|---|
| 108 | public synchronized void displayMailbox(MailboxNode mailboxNode) { |
|---|
| 109 | StandardScreen screen = screenFactory.getMailboxScreen(this, mailboxNode); |
|---|
| 110 | ScreenFactory.getInstance().attachScreenTransition(screen, ScreenFactory.TRANSITION_SLIDE); |
|---|
| 111 | uiApplication.pushScreen(screen); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | public synchronized void displayMessage(MessageNode messageNode) { |
|---|
| 115 | StandardScreen screen = screenFactory.getMessageScreen(this, messageNode); |
|---|
| 116 | ScreenFactory.getInstance().attachScreenTransition(screen, ScreenFactory.TRANSITION_SLIDE); |
|---|
| 117 | uiApplication.pushScreen(screen); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | public synchronized void displayComposition(NetworkAccountNode accountNode) { |
|---|
| 121 | StandardScreen screen = screenFactory.getCompositionScreen(this, accountNode); |
|---|
| 122 | ScreenFactory.getInstance().attachScreenTransition(screen, ScreenFactory.TRANSITION_ZOOM); |
|---|
| 123 | uiApplication.pushScreen(screen); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | public synchronized void displayComposition(NetworkAccountNode accountNode, MessageNode messageNode) { |
|---|
| 127 | StandardScreen screen = screenFactory.getCompositionScreen( |
|---|
| 128 | this, |
|---|
| 129 | accountNode, |
|---|
| 130 | messageNode); |
|---|
| 131 | ScreenFactory.getInstance().attachScreenTransition(screen, ScreenFactory.TRANSITION_ZOOM); |
|---|
| 132 | uiApplication.pushScreen(screen); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | public void displayComposition(NetworkAccountNode accountNode, String address) { |
|---|
| 136 | StandardScreen screen = screenFactory.getCompositionScreen(this, accountNode, address); |
|---|
| 137 | ScreenFactory.getInstance().attachScreenTransition(screen, ScreenFactory.TRANSITION_ZOOM); |
|---|
| 138 | uiApplication.pushScreen(screen); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | public synchronized void displayCompositionReply(NetworkAccountNode accountNode, MessageNode messageNode, boolean replyAll) { |
|---|
| 142 | StandardScreen screen = screenFactory.getCompositionReplyScreen( |
|---|
| 143 | this, |
|---|
| 144 | accountNode, |
|---|
| 145 | messageNode, |
|---|
| 146 | replyAll); |
|---|
| 147 | ScreenFactory.getInstance().attachScreenTransition(screen, ScreenFactory.TRANSITION_ZOOM); |
|---|
| 148 | uiApplication.pushScreen(screen); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | public synchronized void displayCompositionForward(NetworkAccountNode accountNode, MessageNode messageNode) { |
|---|
| 152 | StandardScreen screen = screenFactory.getCompositionForwardScreen( |
|---|
| 153 | this, |
|---|
| 154 | accountNode, |
|---|
| 155 | messageNode); |
|---|
| 156 | ScreenFactory.getInstance().attachScreenTransition(screen, ScreenFactory.TRANSITION_ZOOM); |
|---|
| 157 | uiApplication.pushScreen(screen); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | /** |
|---|
| 161 | * Gets the delegate for handling actions on message nodes. |
|---|
| 162 | * |
|---|
| 163 | * @return the message actions delegate instance |
|---|
| 164 | */ |
|---|
| 165 | public MessageActions getMessageActions() { |
|---|
| 166 | return this.messageActions; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /** |
|---|
| 170 | * Gets the current status text. |
|---|
| 171 | * |
|---|
| 172 | * @return the current status text |
|---|
| 173 | */ |
|---|
| 174 | public String getCurrentStatus() { |
|---|
| 175 | return currentStatus; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | private void handleMailConnectionStatus(MailConnectionStatusEvent e) { |
|---|
| 179 | UiApplication.getUiApplication().invokeLater(new EventObjectRunnable(e) { |
|---|
| 180 | public void run() { |
|---|
| 181 | currentStatus = ((MailConnectionStatusEvent)getEvent()).getMessage(); |
|---|
| 182 | Screen activeScreen = |
|---|
| 183 | UiApplication.getUiApplication().getActiveScreen(); |
|---|
| 184 | if(activeScreen instanceof StandardScreen) { |
|---|
| 185 | StandardScreen screen = (StandardScreen)activeScreen; |
|---|
| 186 | screen.updateStatus(currentStatus); |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | }); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | private void handleMailConnectionError(MailConnectionStatusEvent e) { |
|---|
| 193 | UiApplication.getUiApplication().invokeLater(new EventObjectRunnable(e) { |
|---|
| 194 | public void run() { |
|---|
| 195 | String message = ((MailConnectionStatusEvent)getEvent()).getMessage(); |
|---|
| 196 | if(message == null) { message = resources.getString(LogicMailResource.ERROR_UNKNOWN); } |
|---|
| 197 | try { |
|---|
| 198 | Screen activeScreen = |
|---|
| 199 | UiApplication.getUiApplication().getActiveScreen(); |
|---|
| 200 | if(activeScreen instanceof Status) { |
|---|
| 201 | UiApplication.getUiApplication().popScreen(activeScreen); |
|---|
| 202 | } |
|---|
| 203 | } catch (Exception e) { } |
|---|
| 204 | Status.show(message, 5000); |
|---|
| 205 | } |
|---|
| 206 | }); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | private void handleMailConnectionLogin(MailConnectionLoginEvent e) { |
|---|
| 210 | UiApplication.getUiApplication().invokeAndWait(new EventObjectRunnable(e) { |
|---|
| 211 | public void run() { |
|---|
| 212 | MailConnectionLoginEvent e = (MailConnectionLoginEvent)getEvent(); |
|---|
| 213 | LoginDialog dialog = new LoginDialog(e.getUsername(), e.getPassword()); |
|---|
| 214 | if(dialog.doModal() == Dialog.OK) { |
|---|
| 215 | e.setUsername(dialog.getUsername()); |
|---|
| 216 | e.setPassword(dialog.getPassword()); |
|---|
| 217 | } |
|---|
| 218 | else { |
|---|
| 219 | e.setCanceled(true); |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| 222 | }); |
|---|
| 223 | } |
|---|
| 224 | } |
|---|