1 package SnowMailClient.view.actions; 2 3 import SnowMailClient.MailEngine.*; 4 import SnowMailClient.SnowMailClientApp; 5 import SnowMailClient.model.*; 6 import SnowMailClient.model.accounts.*; 7 import SnowMailClient.view.*; 8 import SnowMailClient.view.MessagesPreview.*; 9 import SnowMailClient.view.accounts.AccountsEditor; 10 import snow.utils.gui.*; 11 12 import SnowMailClient.utils.*; 13 import SnowMailClient.view.dialogs.*; 14 import SnowMailClient.Language.Language; 15 import snow.lookandfeel.*; 16 17 import java.awt.*; 18 import java.awt.event.*; 19 import javax.swing.*; 20 import java.io.*; 21 import java.text.*; 22 23 25 public final class MessagesPreviewAction extends AbstractAction implements Runnable 26 { 27 final private MailAccount account; 28 final private AccountsEditor ref; 29 30 private PreviewDialog previewDialog = null; 31 private SecurePopConnection spop = null; 32 33 public MessagesPreviewAction(AccountsEditor ref, MailAccount account) 34 { 35 super(Language.translate("Messages preview")); 36 37 this.account = account; 38 this.ref= ref; 39 40 putValue(AbstractAction.SMALL_ICON, SnowMailClientApp.loadImageIcon("pics/messagespreview.PNG")); 41 } 42 43 44 public void actionPerformed(ActionEvent actionEvent) 45 { 46 ref.saveSelectedMailAccount(); 47 48 final PreviewModel previewModel = new PreviewModel(); 49 previewDialog = new PreviewDialog(ref, previewModel, false); previewDialog.addWindowListener(new WindowAdapter() 51 { 52 @Override public void windowClosing(WindowEvent we) 53 { 54 terminateCon(); 55 } 56 @Override public void windowClosed(WindowEvent we) 57 { 58 terminateCon(); 59 } 60 }); 61 62 Thread t = new Thread (this); 63 t.start(); 64 } 65 66 private void terminateCon() 67 { 68 if(spop!=null) 69 { 70 try 71 { 72 spop.terminateSession(); 73 spop = null; 74 } 75 catch(Exception e) 76 { 77 } 79 } 80 } 81 82 83 public void run() 84 { 85 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 87 SnowMailClientApp.getInstance().getProperties().setComponentSizeFromINIFile( 88 previewDialog, "MailPreviewDialog", 89 (int) Math.min(ThemesManager.getLabelFontSize()*80, screen.getWidth()), 90 (int) Math.min(ThemesManager.getLabelFontSize()*55, screen.getHeight()), 91 ThemesManager.getLabelFontSize()*5, 92 ThemesManager.getLabelFontSize()*5 93 ); 94 96 EventQueue.invokeLater(new Runnable () { public void run() 97 { 98 previewDialog.setVisible(true); 100 }}); 101 102 103 try 106 { 107 spop = account.getCheckedPopConnection(); 108 System.out.println("spop is "+spop); 109 } 110 catch(Exception e) 111 { 112 JOptionPane.showMessageDialog(ref.getContentPane(), 113 "Cannot open connection to "+account.getPop() 114 +"\n Error="+e.getMessage(), 115 "Error opening connection", JOptionPane.ERROR_MESSAGE); 116 117 return; 119 } 120 121 122 try 123 { 124 int[] nms = spop.getNumberOfMessages(); 125 126 if(nms[0]==0) 127 { 128 previewDialog.setStatusLabelText("You have no new messages."); 129 } 130 else 131 { 132 previewDialog.setMaximalMessageNumber(nms[0]); 133 previewDialog.setStatusLabelText( 134 Language.translate("You have % new messages", ""+nms[0]) 135 +" ("+MailMessageUtils.formatSize(nms[1])+")"); 136 } 137 138 int maxToDownload = nms[0]; 139 140 if(maxToDownload>50) 141 { 142 String n = JOptionPane.showInputDialog(ref.getContentPane(), 143 Language.translate("You have % messages in your inbox.", ""+maxToDownload) 144 +"\n"+Language.translate("Press enter do download all headers, or the number you want")); 145 if(n!=null &&n.trim().length()>0) 146 { 147 try{ 148 maxToDownload = Integer.parseInt(n.trim()); 149 } 150 catch(Exception e) { 151 e.printStackTrace(); 152 } 153 previewDialog.setMaximalMessageNumber(maxToDownload); 154 } 155 } 156 157 String [] messUIDLS = spop.getMessagesUIDLs(); 158 int[] messSizes = spop.getSizes_from_MessagesLIST(spop.getMessagesLIST_()); 159 160 dm: for(int i=0; i<messSizes.length; i++) 161 { 162 if(i>=maxToDownload) break dm; 163 164 previewDialog.setMessageProgress(i); 165 String messHeaderText = spop.getMessageTop(messUIDLS[i], 0); 166 try 167 { 168 final PreviewHeader header = new PreviewHeader(account, messUIDLS[i], messSizes[i]); 169 Header.parseHeader(new NumberedLineReader(messHeaderText), header); 170 171 EventQueue.invokeLater(new Runnable () 172 { public void run() 173 { 174 previewDialog.getPreviewModel().addMessageHeader(header); 175 } 176 }); 177 } 178 catch(Exception e) 179 { 180 e.printStackTrace(); 181 } 182 } 183 previewDialog.setMessageProgressInvisible(); 184 } 185 catch(Exception e) 186 { 187 e.printStackTrace(); 188 previewDialog.setStatusLabelText("ERROR: "+e.getMessage()); 190 } 191 finally 192 { 193 } 195 196 197 198 } 199 200 } | Popular Tags |