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 ; 10 11 import javax.swing.table.AbstractTableModel ; 12 13 14 16 public final class MailAccounts extends AbstractTableModel implements Vectorizable 17 { 18 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 65 public MailAccount getMailAccount(String address) 66 { 67 for(MailAccount ma: accounts) 68 { 69 if(ma.getAddress().equalsIgnoreCase(address)) return ma; 70 } 71 return null; 72 } 73 74 75 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 e) {} 91 } 92 return null; 93 94 } 95 96 97 100 public MailAccount getFirstSnowMailAccount() 101 { 102 for(MailAccount ma: accounts) 103 { 104 if(ma.isSnowraverAccount()) return ma; 106 } 107 return null; 108 109 } 110 111 112 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 > getAllMailAddresses() 126 { 127 Vector<String > aa = new Vector<String >(); 128 for(MailAccount ma: accounts) 129 { 130 aa.addElement(ma.getAddress()); 131 } 133 return aa; 134 } 135 136 137 148 149 150 public void closeAllOpenedPOPConnections() 151 { 152 for(MailAccount ma_: accounts) 153 { 154 final MailAccount ma = ma_; 155 Thread t = new Thread () { public void run() { 156 ma.closePOPConnection(); 157 }}; 158 } 159 } 160 161 162 165 public void createFromVectorRepresentation(Vector<Object > v) throws VectorizeException 168 { 169 int version = (Integer ) v.get(0); 170 if(version==3) 171 { 172 173 synchronized(accounts) 174 { 175 @SuppressWarnings ("unchecked") 176 Vector<Vector<Object >> acv = (Vector<Vector<Object >>) v.get(1); 177 accounts.removeAllElements(); 178 for(Vector<Object > 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 public Vector<Object > getVectorRepresentation() throws VectorizeException 196 { 197 Vector<Object > cont = new Vector<Object >(); 198 cont.addElement(3); 200 synchronized(accounts) 201 { 202 Vector<Object > acv=new Vector<Object >(); 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 public Object 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 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 aValue, int rowIndex, int columnIndex) 259 { 260 boolean val = true; 261 if(aValue instanceof Boolean ) 262 { 263 val = (Boolean ) aValue; 264 } 265 else if(aValue instanceof String ) 266 { 267 val = "true".equals((String ) 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 getColumnClass ( int column ) 277 { 278 Object obj = getValueAt ( 0, column ); 279 return obj.getClass (); 280 } 281 282 283 } | Popular Tags |