KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > view > actions > MessagesPreviewAction


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 /** preview for a single account. Available from the account menu.
24 */

25 public final class MessagesPreviewAction extends AbstractAction implements Runnable JavaDoc
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); // [Oct2005] no more modal
50
previewDialog.addWindowListener(new WindowAdapter()
51       {
52          @Override JavaDoc public void windowClosing(WindowEvent we)
53          {
54             terminateCon();
55          }
56          @Override JavaDoc public void windowClosed(WindowEvent we)
57          {
58             terminateCon();
59          }
60       });
61
62       Thread JavaDoc t = new Thread JavaDoc(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 JavaDoc e)
76           {
77             //Ok, when stopped e.printStackTrace();
78
}
79         }
80    }
81
82
83    public void run()
84    {
85       // look at screen size...
86
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       //SnowMailClientApp.CenterComponentOnMainFrame(previewDialog);
95

96       EventQueue.invokeLater(new Runnable JavaDoc() { public void run()
97       {
98         // NO MORE modal !!
99
previewDialog.setVisible(true);
100       }});
101
102
103       // 1: get connection
104
//
105
try
106       {
107         spop = account.getCheckedPopConnection();
108         System.out.println("spop is "+spop);
109       }
110       catch(Exception JavaDoc 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         // don't go
118
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 JavaDoc 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 JavaDoc e) {
151                  e.printStackTrace();
152               }
153               previewDialog.setMaximalMessageNumber(maxToDownload);
154            }
155         }
156
157         String JavaDoc[] 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 JavaDoc 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 JavaDoc()
172              { public void run()
173                {
174                  previewDialog.getPreviewModel().addMessageHeader(header);
175                }
176              });
177            }
178            catch(Exception JavaDoc e)
179            {
180              e.printStackTrace();
181            }
182         }
183         previewDialog.setMessageProgressInvisible();
184       }
185       catch(Exception JavaDoc e)
186       {
187         e.printStackTrace();
188         //doc.appendError(" Failed: "+e.getMessage());
189
previewDialog.setStatusLabelText("ERROR: "+e.getMessage());
190       }
191       finally
192       {
193         // connection terminated on close...
194
}
195
196
197
198     }
199
200 } // MessagesPreviewAction
Popular Tags