KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > model > accounts > MailAccounts


1 package SnowMailClient.model.accounts;
2
3
4 import snow.utils.storage.*;
5 import SnowMailClient.Language.Language;
6
7 import java.util.*;
8 import javax.swing.*;
9 import javax.swing.table.TableModel JavaDoc;
10
11 import javax.swing.table.AbstractTableModel JavaDoc;
12
13
14 /** the mail accounts
15 */

16 public final class MailAccounts extends AbstractTableModel JavaDoc implements Vectorizable
17 {
18    // Data
19
private final Vector<MailAccount> accounts = new Vector<MailAccount>();
20
21
22    public MailAccounts()
23    {
24    }
25
26    public void addNewAccount()
27    {
28       accounts.addElement(new MailAccount());
29       updateList();
30    }
31
32    public void addAccount(MailAccount a)
33    {
34       accounts.addElement(a);
35       updateList();
36    }
37
38    public void remove(MailAccount ma)
39    {
40       accounts.removeElement(ma);
41       updateList();
42    }
43
44    public void insertAccountAt(MailAccount ma, int pos)
45    {
46       accounts.insertElementAt(ma, pos);
47       updateList();
48    }
49
50    public void updateList()
51    {
52       fireTableDataChanged();
53    }
54
55
56    public MailAccount getAccount(int index)
57    {
58       return accounts.elementAt(index);
59    }
60
61
62    /** @return the account of the given address or
63        null if none found.
64    */

65    public MailAccount getMailAccount(String JavaDoc address)
66    {
67       for(MailAccount ma: accounts)
68       {
69           if(ma.getAddress().equalsIgnoreCase(address)) return ma;
70       }
71       return null;
72    }
73
74
75    /** @return the first account that have administrator privileges
76        on the snowraver server,
77        null if none found.
78    */

79    public MailAccount getFirstSnowraverAdministratorMailAccount()
80    {
81       for(MailAccount ma: accounts)
82       {
83           try
84           {
85              if(ma.getCheckedPopConnection().hasAdminPrivileges())
86              {
87                 return ma;
88              }
89           }
90           catch(Exception JavaDoc e) {}
91       }
92       return null;
93
94    }
95
96
97    /** @return the first account that have snowmail extensions,
98        null if none found.
99    */

100    public MailAccount getFirstSnowMailAccount()
101    {
102       for(MailAccount ma: accounts)
103       {
104           // securemode is equivalent to snowmail account
105
if(ma.isSnowraverAccount()) return ma;
106       }
107       return null;
108
109    }
110
111
112    /** @return the first account that is marked to send mails with
113        null if none found.
114    */

115    public MailAccount getSendMailAccount()
116    {
117       for(MailAccount ma: accounts)
118       {
119           if(ma.getUseToSendMails()) return ma;
120       }
121       return null;
122    }
123
124
125    public Vector<String JavaDoc> getAllMailAddresses()
126    {
127       Vector<String JavaDoc> aa = new Vector<String JavaDoc>();
128       for(MailAccount ma: accounts)
129       {
130         aa.addElement(ma.getAddress());
131         //System.out.println("AA>"+ma.getAddress());
132
}
133       return aa;
134    }
135
136
137    /*
138    public Vector<MailAccount> getAllOpenedPOPConnections()
139    {
140       for(MailAccount ma_: accounts)
141       {
142         final MailAccount ma = ma_;
143         Thread t = new Thread() { public void run() {
144           ma.closePOPConnection();
145         }};
146       }
147    } */

148
149
150    public void closeAllOpenedPOPConnections()
151    {
152       for(MailAccount ma_: accounts)
153       {
154         final MailAccount ma = ma_;
155         Thread JavaDoc t = new Thread JavaDoc() { public void run() {
156           ma.closePOPConnection();
157         }};
158       }
159    }
160
161
162    // Vectorizable storage
163
//
164

165    // load stored folders from a vector
166
//
167
public void createFromVectorRepresentation(Vector<Object JavaDoc> v) throws VectorizeException
168    {
169          int version = (Integer JavaDoc) v.get(0);
170          if(version==3)
171          {
172
173            synchronized(accounts)
174            {
175              @SuppressWarnings JavaDoc("unchecked")
176              Vector<Vector<Object JavaDoc>> acv = (Vector<Vector<Object JavaDoc>>) v.get(1);
177              accounts.removeAllElements();
178              for(Vector<Object JavaDoc> av : acv)
179              {
180                 MailAccount ma = new MailAccount();
181                 ma.createFromVectorRepresentation(av);
182                 accounts.add( ma );
183              }
184            }
185          }
186          else
187          {
188            throw new VectorizeException("Bad version "+version);
189          }
190          updateList();
191    }
192
193    // save folders in a vector
194
//
195
public Vector<Object JavaDoc> getVectorRepresentation() throws VectorizeException
196    {
197        Vector<Object JavaDoc> cont = new Vector<Object JavaDoc>();
198        cont.addElement(3); //version
199

200        synchronized(accounts)
201        {
202           Vector<Object JavaDoc> acv=new Vector<Object JavaDoc>();
203           cont.add(acv);
204
205           for(MailAccount ma: accounts)
206           {
207              acv.add(ma.getVectorRepresentation());
208           }
209        }
210
211        return cont;
212    }
213
214
215    // AbstractTableModel implementation
216
//
217
public Object JavaDoc getValueAt(int row, int col)
218    {
219        if(row>=0 || row<accounts.size())
220        {
221           MailAccount ma = accounts.elementAt(row);
222           switch(col)
223           {
224             case 0:
225               return ma.getAddress();
226             case 1: return ma.getUseToReadMails();
227             case 2: return ma.getUseToSendMails();
228           }
229           return "?";
230        }
231        else
232        {
233           return "?";
234        }
235    }
236
237    public int getRowCount() {return accounts.size();}
238
239
240    public int getColumnCount() { return 3; }
241
242    public String JavaDoc getColumnName(int column)
243    {
244       if(column==0) return Language.translate("Address");
245       if(column==1) return Language.translate("Receive");
246       if(column==2) return Language.translate("Send");
247       return "?";
248    }
249
250
251
252    public boolean isCellEditable(int rowIndex, int columnIndex)
253
254    {
255        return columnIndex>0;
256    }
257
258    public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex)
259    {
260          boolean val = true;
261          if(aValue instanceof Boolean JavaDoc)
262          {
263             val = (Boolean JavaDoc) aValue;
264          }
265          else if(aValue instanceof String JavaDoc)
266          {
267             val = "true".equals((String JavaDoc) aValue);
268          }
269          MailAccount ma = accounts.elementAt(rowIndex);
270
271          if(columnIndex==1) ma.setUseToReadMails(val);
272          if(columnIndex==2) ma.setUseToSendMails(val);
273    }
274
275
276    public Class JavaDoc getColumnClass ( int column )
277    {
278       Object JavaDoc obj = getValueAt ( 0, column );
279       return obj.getClass ();
280    }
281
282
283 }
Popular Tags