KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > MailEngine > transfer > MailTransferModel


1 package SnowMailClient.MailEngine.transfer;
2
3 import SnowMailClient.SnowMailClientApp;
4 import SnowMailClient.model.*;
5 import SnowMailClient.model.accounts.*;
6 import SnowMailClient.model.folders.*;
7 import SnowMailClient.utils.storage.*;
8 import SnowMailClient.utils.*;
9 import snow.utils.gui.*;
10 import snow.sortabletable.*;
11 import SnowMailClient.Language.Language;
12
13 import java.util.*;
14 import java.text.*;
15 import javax.swing.table.*;
16 import javax.swing.*;
17 import java.awt.*;
18
19 /**
20 */

21 public final class MailTransferModel extends FineGrainTableModel
22 {
23   private final Vector<TransferItem> transferItems = new Vector<TransferItem>();
24   private boolean hasBeenChangedByUser = false;
25   private static final String JavaDoc[] COLUMN_NAMES = new String JavaDoc[] {
26     Language.translate("Type"),
27     Language.translate("Account"),
28     Language.translate("Mails"),
29     Language.translate("Progress")
30   };
31
32   private int[] COLUMN_PREFERED_SIZES = new int[] { 4, 18, 4, 6 };
33
34   public enum TransferType { Send, Receive, SendAndReceive }
35
36   public MailTransferModel(MailAccounts mac, TransferType type)
37   {
38      super();
39
40      // collect accounts to receive mails
41
//
42
for(int i=0; i<mac.getRowCount(); i++)
43      {
44        if(mac.getAccount(i).getUseToReadMails()
45          && (type==TransferType.Receive || type==TransferType.SendAndReceive))
46        {
47           transferItems.add(new TransferItem(mac.getAccount(i), TransferItem.TransferType.Receive));
48        }
49      }
50
51      // collect the mails in the outbox
52
if(type==TransferType.Send || type==TransferType.SendAndReceive)
53      {
54        try
55        {
56          // functionalize this !
57
Map<MailAccount, Vector<MailMessage>> sendAccounts = new Hashtable<MailAccount, Vector<MailMessage>>();
58          final MailFolder folder = SnowMailClientApp.getInstance().getFoldersModel().getOutboxFolder().getMailFolder();
59          for(MailMessage _mess : folder.getAllMessages())
60          {
61             MailMessage mess = _mess;
62             //String toAddress = mess.getToAddressesText();
63
String JavaDoc fromMailAddress = mess.getFromAddress().getMailAddress();
64             MailAccount account = mac.getMailAccount(fromMailAddress);
65
66             if(!sendAccounts.containsKey(account))
67             {
68                 sendAccounts.put(account, new Vector<MailMessage>());
69             }
70             sendAccounts.get(account).add(mess);
71          }
72
73          for(MailAccount maco : sendAccounts.keySet())
74          {
75             TransferItem tri = new TransferItem(maco, TransferItem.TransferType.Send);
76             tri.messagesToSend.addAll( sendAccounts.get(maco) );
77             transferItems.add(tri);
78          }
79        }
80        catch(Exception JavaDoc e)
81        {
82          e.printStackTrace();
83        }
84      }
85      progress.setStringPainted(true);
86   }
87
88   public String JavaDoc getColumnName(int col)
89   {
90      return COLUMN_NAMES[col];
91   }
92
93   public int getPreferredColumnWidth(int column)
94   {
95     if(column>=0 && column<COLUMN_PREFERED_SIZES.length) return COLUMN_PREFERED_SIZES[column];
96     return -1;
97   }
98
99   public int getColumnCount() { return COLUMN_NAMES.length; }
100
101   public int compareForColumnSort(int pos1, int pos2, int col)
102   {
103      return super.compareForColumnSort(pos1, pos2, col);
104   }
105
106   public int getRowCount() { return transferItems.size(); }
107
108   public Vector<TransferItem> getTransferItems()
109   {
110     return transferItems;
111   }
112
113   private final JProgressBar progress = new JProgressBar(0, 100);
114
115   public Object JavaDoc getValueAt(int row, int col)
116   {
117     TransferItem dac = transferItems.elementAt(row);
118     if(col==0)
119     {
120       if(dac.transferType == TransferItem.TransferType.Receive)
121       {
122         return Language.translate("Receive");
123       }
124       else
125       {
126         return Language.translate("Send");
127       }
128     }
129     if(col==1) return dac.ma.getAddress();
130     if(col==2)
131     {
132       if( dac.numberOfMailsToDownload==-1) return "";
133       if( dac.numberOfMailsToDownload== 0) return Language.translate("-");
134       return ""+dac.numberOfDownladedMails+" / "+dac.numberOfMailsToDownload;
135
136     }
137     if(col==3)
138     {
139       if(dac.error!=null) return Language.translate("Error") + ": " + dac.error.getMessage();
140
141       if( dac.totalBytesToDownload==-1) return "";
142       if( dac.totalBytesToDownload== 0) return "";
143
144       progress.setValue( dac.totalDownloadedBytes *100 / dac.totalBytesToDownload);
145       progress.setString(
146       //MailMessageUtils.formatSize(dac.totalDownloadedBytes) + " / "
147
MailMessageUtils.formatSize(dac.totalBytesToDownload));
148       return progress;
149     }
150     return "?";
151   }
152
153   public void updateView()
154   {
155     EventQueue.invokeLater(new Runnable JavaDoc() { public void run() {
156       fireTableModelWillChange();
157       fireTableDataChanged();
158       fireTableModelHasChanged();
159     }});
160   }
161
162   public void setTableRenderer(JTable table)
163   {
164     CellRenderer dt = new CellRenderer();
165     table.setDefaultRenderer(String JavaDoc.class, dt);
166     table.setDefaultRenderer(Integer JavaDoc.class, dt);
167     table.setDefaultRenderer(Component.class, dt);
168   }
169
170   /** just allows JProgressBar to act as CellRenderer
171   */

172   class CellRenderer extends DefaultTableCellRenderer
173   {
174      public Component getTableCellRendererComponent(
175                           JTable table, Object JavaDoc val, boolean sel, boolean foc, int row, int col)
176      {
177         if(val instanceof Component)
178         {
179           return (Component) val;
180         }
181         return super.getTableCellRendererComponent(table,val,sel,foc,row,col);
182      }
183   }
184
185 } // MailTransferModel
Popular Tags