KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > view > accounts > AccountsEditor


1 package SnowMailClient.view.accounts;
2
3 import snow.utils.storage.*;
4 import snow.crypto.*;
5 import snow.lookandfeel.*;
6 import snow.utils.gui.*;
7
8 import SnowMailClient.SnowMailClientApp;
9 import SnowMailClient.model.accounts.*;
10 import SnowMailClient.model.Address;
11 import SnowMailClient.view.actions.*;
12 import SnowMailClient.view.dialogs.*;
13 import SnowMailClient.crypto.*;
14 import SnowMailClient.Language.Language;
15
16 import javax.swing.*;
17 import javax.swing.event.*;
18 import java.awt.*;
19 import java.io.*;
20 import java.awt.event.*;
21
22
23
24 public final class AccountsEditor extends JDialog implements ListSelectionListener
25 {
26
27    final int fontSize = UIManager.getFont("Label.font").getSize();
28
29    private final JTable table;
30    private final JPanel accountPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, fontSize, fontSize));
31
32    private final MailAccounts accounts;
33    private final AppProperties properties;
34
35
36
37    public AccountsEditor(Frame modalOwner, MailAccounts _accounts, AppProperties _properties)
38    {
39       super(modalOwner, Language.translate("Accounts Editor"), true);
40
41       this.accounts = _accounts;
42       this.properties = _properties;
43
44       if(accounts.getRowCount()==0)
45       {
46         accounts.addNewAccount();
47         accounts.getAccount(0).setAddress("??????@www.snowraver.org");
48       }
49
50       setContentPane(new SnowBackgroundPanel(new BorderLayout()));
51
52       table = new JTable(accounts);
53       table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
54       table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(new JCheckBox()));
55       table.getColumnModel().getColumn(1).setMaxWidth(50);
56       table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(new JCheckBox()));
57       table.getColumnModel().getColumn(2).setMaxWidth(37);
58
59       final JPanel leftPanel = new JPanel(new BorderLayout());
60       leftPanel.setOpaque(false);
61
62       JScrollPane jsp = new JScrollPane(table);
63       leftPanel.add(jsp, BorderLayout.CENTER);
64       table.setOpaque(false);
65       jsp.setOpaque(false);
66       jsp.getViewport().setOpaque(false);
67
68
69       JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
70           leftPanel,
71           accountPanel );
72       splitPane.setOpaque(false);
73       accountPanel.setOpaque(false);
74
75       splitPane.setOneTouchExpandable(true);
76       splitPane.setDividerLocation(ThemesManager.getLabelFontSize()*32);
77
78       getContentPane().add(splitPane, BorderLayout.CENTER);
79
80
81
82       final JPanel contrlpanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
83
84       final JPanel contrlpanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
85
86       leftPanel.add(contrlpanel2, BorderLayout.SOUTH);
87       leftPanel.add(contrlpanel1, BorderLayout.NORTH);
88
89       JButton addUser = new JButton(Language.translate("New Account"), SnowMailClientApp.loadImageIcon("pics/mailAccounts.PNG"));
90       addUser.setFocusPainted(false);
91       GUIUtils.setSmallDimensions(addUser);
92
93       addUser.setOpaque(true);
94       addUser.setBackground(Color.yellow);
95       contrlpanel1.add(addUser);
96       addUser.addActionListener(new ActionListener( ){
97          public void actionPerformed(ActionEvent e)
98          {
99             AddAccountWizzardDialog adw = new AddAccountWizzardDialog(AccountsEditor.this);
100             if(!adw.wasDialogCancelled())
101             {
102               MailAccount ma = new MailAccount();
103               Address address = adw.getMailAddress();
104               ma.setAddress(address.getMailAddress());
105               ma.setName(address.getName());
106               ma.setAccountPassword(adw.getPassword());
107
108               String JavaDoc mailAddress = address.getMailAddress();
109               int posAt = mailAddress.indexOf("@");
110               if(posAt>=0)
111               {
112                  // Guess pop and smtp accounts
113

114                  ma.setAccountUserName( mailAddress.substring(0,posAt) );
115
116                  //
117
String JavaDoc domain = mailAddress.substring(posAt+1);
118                  if(domain.equalsIgnoreCase("gmail.com"))
119                  {
120                    ma.setPop("pop.googlemail.com");
121                    ma.setSMTP( "smtp.googlemail.com");
122                    ma.setUseSSLPOP(true);
123                    ma.setSSLPOPPort(995);
124                    ma.setSSLSMTPPort(465); // or 587
125
ma.setUseSSLSMTP(true);
126                  }
127                  else
128                  {
129                    ma.setPop(domain );
130                    ma.setSMTP( domain);
131                  }
132               }
133
134               accounts.addAccount(ma);
135             }
136          }
137       });
138
139       final JButton removeuser = new JButton(Language.translate("Remove"),Icons.CrossIcon.shared10);
140       GUIUtils.setSmallDimensions(removeuser);
141       removeuser.setOpaque(true);
142       contrlpanel1.add(removeuser);
143       removeuser.addActionListener(new ActionListener()
144       {
145          public void actionPerformed(ActionEvent e)
146          {
147             int sel = table.getSelectedRow();
148             if(sel!=-1)
149             {
150               MailAccount ma = accounts.getAccount(sel);
151
152               int rep = JOptionPane.showConfirmDialog(leftPanel,
153                 Language.translate("Are you sure ?"),
154                 Language.translate("Removing %",ma.getAddress()),
155                 JOptionPane.OK_CANCEL_OPTION);
156
157               if(rep==JOptionPane.OK_OPTION)
158               {
159                 accounts.remove(ma);
160                 valueChanged(null);
161               }
162             }
163          }
164       });
165 /*
166       final JButton importBT = new JSenseButton(
167          Language.translate("Import from OutlookExpress"), SnowMailClientApp.loadImageIcon("pics/outlookexpress.png"));
168       SnowMailClientApp.MakeButtonNiceSize(importBT);
169       importBT.setOpaque(true);
170       importBT.setToolTipText(
171         Language.translate("Import from an IAF file (outlook express account export)."));
172       contrlpanel2.add(importBT);
173       importBT.addActionListener(new ActionListener( )
174       {
175          public void actionPerformed(ActionEvent e)
176          {
177              importIAFAction();
178          }
179       }); */

180
181       final JButton moveUp = new JButton("", new Icons.UpIcon(10,10,true));
182       //GUIUtils.setNarrowInsets(moveUp);
183
moveUp.setMargin(new Insets(0,0,0,0));
184       moveUp.setToolTipText(Language.translate("Move account up in the list"));
185       moveUp.setOpaque(true);
186       contrlpanel2.add(Box.createHorizontalGlue());
187       contrlpanel2.add(moveUp);
188       moveUp.addActionListener(new ActionListener( )
189       {
190          public void actionPerformed(ActionEvent e)
191          {
192             int sel = table.getSelectedRow();
193             if(sel>0)
194             {
195               saveSelectedMailAccount();
196               MailAccount ma = accounts.getAccount(sel);
197               accounts.remove(ma);
198               accounts.insertAccountAt(ma, sel-1);
199               table.setRowSelectionInterval(sel-1, sel-1);
200             }
201          }
202       });
203       final JButton moveDown = new JButton("", new Icons.DownIcon(10,10,true));
204       moveDown.setMargin(new Insets(0,0,0,0));
205       moveDown.setToolTipText(Language.translate("Move account down in the list"));
206       moveDown.setOpaque(true);
207       contrlpanel2.add(moveDown);
208       moveDown.addActionListener(new ActionListener( )
209       {
210          public void actionPerformed(ActionEvent e)
211          {
212             int sel = table.getSelectedRow();
213             if(sel<table.getRowCount()-1)
214             {
215               saveSelectedMailAccount();
216               MailAccount ma = accounts.getAccount(sel);
217               accounts.remove(ma);
218               accounts.insertAccountAt(ma, sel+1);
219               table.setRowSelectionInterval(sel+1, sel+1);
220             }
221          }
222       });
223
224       table.getSelectionModel().addListSelectionListener(new ListSelectionListener()
225       {
226         public void valueChanged(ListSelectionEvent e)
227         {
228           removeuser.setEnabled(table.getSelectedRowCount()==1);
229           moveUp.setEnabled(table.getSelectedRowCount()==1);
230           moveDown.setEnabled(table.getSelectedRowCount()==1);
231         }
232       });
233
234       JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
235       southPanel.setOpaque(false);
236       getContentPane().add(southPanel, BorderLayout.SOUTH);
237
238       JButton close = new JButton(Language.translate("Save and Close"), Icons.OkIcon.shared10);
239       GUIUtils.setSmallDimensions(close);
240       close.setBackground(Color.orange);
241       southPanel.add(close);
242       close.addActionListener(new ActionListener( ){
243          public void actionPerformed(ActionEvent e)
244          {
245             saveSelectedMailAccount();
246             properties.saveComponentSizeInINIFile(AccountsEditor.this, "AccountsEditor");
247             dispose();
248          }
249       });
250
251       table.getSelectionModel().addListSelectionListener(this);
252       int sel = properties.getInteger("AccountsEditor_table_sel_row", 0);
253       if(sel>=0 && sel<table.getModel().getRowCount())
254       {
255          table.setRowSelectionInterval(sel, sel);
256       }
257
258       pack();
259
260       properties.setComponentSizeFromINIFile(AccountsEditor.this, "AccountsEditor",
261                         750, 480, 100, 100);
262       setVisible(true);
263    }
264
265
266    public void saveSelectedMailAccount()
267    {
268       if(accountPanel.getComponentCount()>0)
269       {
270          // save old
271
AccountEditor ma = (AccountEditor) accountPanel.getComponent(0);
272          ma.save();
273       }
274    }
275    /*
276    private void importIAFAction()
277    {
278       JFileChooser fs = new JFileChooser();
279       String startDir = properties.getProperty( "iaf_last", System.getProperty("user.home", "c:/"));
280       fs.setCurrentDirectory(new File(startDir));
281       fs.setDialogTitle(Language.translate("Select an IAF file to open"));
282       fs.setFileFilter(new javax.swing.filechooser.FileFilter()
283       {
284          public boolean accept(File file)
285          {
286            if(file.isDirectory()) return true;
287            String name = file.getName().toUpperCase();
288            return name.endsWith(".IAF");
289          }
290          public String getDescription() { return Language.translate("Outlook mail account IAF file")+" [*.iaf]"; }
291       });
292       int rep = fs.showOpenDialog(this);
293       if(rep==fs.APPROVE_OPTION)
294       {
295          File file = fs.getSelectedFile();
296          File par = file.getParentFile();
297          if(par!=null && par.exists())
298          {
299            properties.setProperty( "iaf_last", par.getAbsolutePath());
300          }
301
302          try
303          {
304            IAFReader iar = new IAFReader(file);
305            MailAccount ma = new MailAccount();
306            accounts.addAccount(ma);
307
308            ma.setAccountUserName(iar.getUserName());
309            ma.setAddress(iar.getMailAddress());
310            ma.setPop( iar.getPOPHost());
311            ma.setPopPort(iar.getPOPPort() );
312            ma.setUseSSLPOP(iar.getUseSSLonPOP());
313            ma.setSSLPOPPort(iar.getPOPPort() );
314
315            ma.setSMTP( iar.getSMTPHost());
316            ma.setSMTPPort(iar.getSMTPPort() );
317            ma.setUseSSLSMTP(iar.getUseSSLonSMTP());
318            ma.setSSLSMTPPort(iar.getSMTPPort() );
319
320            char[] pass = iar.getPassword();
321            if(pass!=null) ma.setAccountPassword(new String(pass));
322          }
323          catch(Exception e)
324          {
325            e.printStackTrace();
326          }
327       }
328    } */

329
330
331    /** list selection listener
332    */

333    public void valueChanged(ListSelectionEvent e)
334    {
335       int ind = table.getSelectedRow();
336       if(ind>=0)
337       {
338          saveSelectedMailAccount();
339          accountPanel.removeAll();
340          MailAccount ma = accounts.getAccount(ind);
341          accountPanel.add(new AccountEditor(ma), 0);
342          accountPanel.updateUI();
343       }
344       properties.setInteger("AccountsEditor_table_sel_row", ind);
345    }
346
347    /** test
348    */

349    public static void main(String JavaDoc[] aaa)
350    {
351       MailAccounts mailAccounts = new MailAccounts();
352       new AccountsEditor(new JFrame(), mailAccounts, new AppProperties());
353    }
354
355    /** editor for ONE mail account.
356    */

357    final private class AccountEditor extends JPanel
358    {
359       final JTextField mailAddress, identity, userName,
360                  pop, sslPopport, smtp, popport, smtpport, sslSMTPPort; //, name;
361
final JPasswordField password;
362       final JCheckBox sslPOPCB = new JCheckBox(Language.translate("Use SSL")+" (995)", false);
363       final JCheckBox sslSMTPCB = new JCheckBox(Language.translate("Use SSL")+" (465)", false); // 587
364
final JButton viewPass = new JButton(Language.translate("View"));
365       //JCheckBox signOutgoingMails = new JCheckBox(Language.translate("Sign outgoing mails with GnuPG")+"", false);
366
final JCheckBox allowUnsecurePasswordProtocols = new JCheckBox(Language.translate("Allow unsecure password protocols")+"", false);
367
368       public boolean dataHasChanged = false;
369       final private MailAccount account;
370
371       public AccountEditor(final MailAccount account)
372       {
373          super(new BorderLayout());
374          setOpaque(false);
375
376          JPanel cont_ = new JPanel();
377          GridLayout3 gl = new GridLayout3(2, cont_);
378
379          cont_.setOpaque(false);
380          add(cont_, BorderLayout.CENTER);
381          this.account = account;
382
383          mailAddress = new JTextField(account.getAddress(),20);
384          mailAddress.setOpaque(false);
385          identity = new JTextField(account.getName(),20);
386          identity.setOpaque(false);
387          pop = new JTextField(account.getPop(), 20);
388          pop.setOpaque(false);
389          sslPopport = new JTextField(""+account.getSSLPopPort(), 4);
390          sslPopport.setOpaque(false);
391          sslSMTPPort = new JTextField(""+account.getSSLSMTPPort(), 4);
392          sslSMTPPort.setOpaque(false);
393          smtp = new JTextField(account.getSMTP(),20);
394          smtp.setOpaque(false);
395          userName = new JTextField(account.getAccountUserName(),20);
396          userName.setOpaque(false);
397          password = new JPasswordField(account.getAccountPassword(),20);
398          password.setOpaque(false);
399          popport = new JTextField(""+account.getPopPort(),4);
400          popport.setOpaque(false);
401          smtpport = new JTextField(""+account.getSMTPPort(),4);
402          smtpport.setOpaque(false);
403          sslPOPCB.setSelected( account.useSSLPOP());
404          sslSMTPCB.setSelected( account.useSSLSMTP());
405          sslPOPCB.setFocusPainted(false);
406          sslSMTPCB.setFocusPainted(false);
407
408
409          //signOutgoingMails.setSelected( account.doSignOutgoingMails());
410
// signOutgoingMails.setFocusPainted(false);
411
// signOutgoingMails.setOpaque(false);
412

413
414          gl.add(new JLabel(Language.translate("E-Mail Address")), false);
415          gl.add(mailAddress, true);
416
417          gl.add(new JLabel(Language.translate("Username")), false);
418          gl.add(userName, true);
419
420          gl.add(new JLabel(Language.translate("Password")), false);
421          JPanel panPass =new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
422          panPass.add(password);
423          if(SecretKeyManager.getInstance().isUserKeySet())
424          {
425            panPass.add(viewPass);
426          }
427          viewPass.setPreferredSize(new Dimension(
428              (int) viewPass.getPreferredSize().getWidth(), (int) password.getPreferredSize().getHeight()));
429          viewPass.addActionListener(new ActionListener()
430          {
431            public void actionPerformed(ActionEvent ee)
432            {
433               SecretKeyID skid = SecretKeyUtilities.computeSignature(
434                 SecretKeyManager.getInstance().getUserKeyForEncryption());
435               PassphraseDialog pd = new PassphraseDialog(AccountsEditor.this,
436                  Language.translate("Enter the SnowMail password"), true, skid, null,
437                  Language.translate("Security Check"));
438
439               if(pd.matchesID())
440               {
441                 JOptionPane.showMessageDialog(
442                   password, new String JavaDoc(password.getPassword()),
443                   Language.translate("Password for")+" "+mailAddress.getText(),
444                   JOptionPane.INFORMATION_MESSAGE
445                 );
446               }
447            }
448          });
449
450          gl.add(panPass, true);
451
452          gl.add(new JLabel(""), false);
453          gl.add(new JLabel(""), false);
454
455          gl.add(new JLabel(Language.translate("User Identity (optional)")), false);
456          gl.add(identity, true);
457          gl.add(new JLabel(Language.translate("POP3 Host address")), false);
458          gl.add(pop, true);
459          gl.add(new JLabel(Language.translate("POP3 Port (110)")), false);
460          gl.add(wrapLeft(popport), true);
461          gl.add(sslPOPCB, false);
462
463          sslPOPCB.setOpaque(false);
464          gl.add(sslPopport, false);
465
466          gl.add(new JLabel(Language.translate("SMTP Host address")), false);
467          gl.add(smtp, true);
468          gl.add(new JLabel(Language.translate("SMTP Port (25)")), false);
469          gl.add(smtpport, false);
470          gl.add(sslSMTPCB, false);
471          sslSMTPCB.setOpaque(false);
472          gl.add(sslSMTPPort, false);
473
474          //gl.add(signOutgoingMails, false);
475

476          gl.add("");
477          gl.add(allowUnsecurePasswordProtocols);
478          allowUnsecurePasswordProtocols.setSelected(account.getAllowUnsecurePasswordProtocols());
479
480          allowUnsecurePasswordProtocols.addActionListener(new ActionListener()
481          {
482            public void actionPerformed(ActionEvent ee)
483            {
484              if(allowUnsecurePasswordProtocols.isSelected())
485              {
486                allowUnsecurePasswordProtocols.setForeground(ThemesManager.getInstance().getRed());
487              }
488              else
489              {
490                allowUnsecurePasswordProtocols.setForeground(UIManager.getColor("Label.foreground"));
491              }
492            }
493          });
494          if(allowUnsecurePasswordProtocols.isSelected())
495          {
496            allowUnsecurePasswordProtocols.setForeground(ThemesManager.getInstance().getRed());
497          }
498
499
500
501
502          sslPOPCB.addActionListener(new ActionListener()
503          {
504            public void actionPerformed(ActionEvent ee)
505            {
506               sslPopport.setEnabled(sslPOPCB.isSelected());
507               popport.setEnabled(!sslPOPCB.isSelected());
508            }
509          });
510          sslPopport.setEnabled(sslPOPCB.isSelected());
511          popport.setEnabled(!sslPOPCB.isSelected());
512
513          sslSMTPCB.addActionListener(new ActionListener()
514          {
515            public void actionPerformed(ActionEvent ee)
516            {
517               sslSMTPPort.setEnabled(sslSMTPCB.isSelected());
518               smtpport.setEnabled(!sslSMTPCB.isSelected());
519            }
520          });
521          sslSMTPPort.setEnabled(sslSMTPCB.isSelected());
522          smtpport.setEnabled(!sslSMTPCB.isSelected());
523
524          // south panel
525
//
526
JPanel southPanel = new JPanel();
527          southPanel.setOpaque(false);
528          add(southPanel, BorderLayout.SOUTH);
529          southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.Y_AXIS));
530          southPanel.add(Box.createVerticalStrut(fontSize*1));
531
532          // general options
533
//
534
JPanel genOptionsPanel_ = new JPanel();
535          GridLayout3 glo = new GridLayout3(1, genOptionsPanel_);
536          genOptionsPanel_.setOpaque(false);
537          southPanel.add(wrapLeft(genOptionsPanel_));
538
539          JButton testAccountBT = new JButton(new TestAccountSettings(AccountsEditor.this, account));
540          testAccountBT.setFont(ThemesManager.getInstance().getSmallFont());
541          GUIUtils.setSmallDimensions(testAccountBT);
542          glo.add(testAccountBT);
543
544          JButton logtBT = new JButton(new ViewAccountLog(AccountsEditor.this, account));
545          logtBT.setFont(ThemesManager.getInstance().getSmallFont());
546          GUIUtils.setSmallDimensions(logtBT);
547          glo.add(logtBT);
548
549          JButton previewBT = new JButton(new MessagesPreviewAction(AccountsEditor.this, account));
550          previewBT.setFont(ThemesManager.getInstance().getSmallFont());
551          GUIUtils.setSmallDimensions(previewBT);
552          glo.add(previewBT);
553
554
555          // snowmail
556
//
557

558          if(account.isSnowraverAccount())
559          {
560             JButton accessSnowraverFunctions = new JButton(
561                 Language.translate("Snowraver special functions"),
562                 SnowMailClientApp.loadImageIcon("pics/snowraverlogo.PNG"));
563             accessSnowraverFunctions.setFont(ThemesManager.getInstance().getSmallFont());
564             glo.add(wrapLeft(accessSnowraverFunctions));
565             GUIUtils.setSmallDimensions(accessSnowraverFunctions);
566             accessSnowraverFunctions.addActionListener(new ActionListener()
567             {
568               public void actionPerformed(ActionEvent ee)
569               {
570                  new SnowraverAccountDialog(AccountsEditor.this, account);
571               }
572             });
573          }
574
575          mailAddress.addKeyListener(new KeyAdapter()
576          {
577             @Override JavaDoc public void keyReleased(KeyEvent e)
578             {
579                // set the name in the list of the accounts
580
account.setAddress( mailAddress.getText());
581                accounts.updateList();
582             }
583          });
584       }
585
586
587
588       public void save()
589       {
590          account.setAddress( mailAddress.getText());
591          account.setName( identity.getText());
592          account.setPop( pop.getText());
593          account.setSMTP( smtp.getText());
594          account.setAccountUserName( userName.getText());
595          account.setAccountPassword( new String JavaDoc(password.getPassword()));
596          try
597          {
598             int pp = Integer.parseInt( popport.getText() );
599             account.setPopPort(pp);
600          }
601          catch(NumberFormatException JavaDoc e){e.printStackTrace(); }
602          try
603          {
604             int pp = Integer.parseInt( smtpport.getText() );
605             account.setSMTPPort(pp);
606          }
607          catch(NumberFormatException JavaDoc e){e.printStackTrace(); }
608          try
609          {
610             int pp = Integer.parseInt( sslPopport.getText() );
611             account.setSSLPOPPort(pp);
612          }
613          catch(NumberFormatException JavaDoc e){e.printStackTrace(); }
614          try
615          {
616             int pp = Integer.parseInt( sslSMTPPort.getText() );
617             account.setSSLSMTPPort(pp);
618          }
619          catch(NumberFormatException JavaDoc e){ e.printStackTrace(); }
620
621          account.setUseSSLPOP(sslPOPCB.isSelected());
622          account.setUseSSLSMTP(sslSMTPCB.isSelected());
623          //account.setSignOutgoingMails(signOutgoingMails.isSelected());
624

625          account.setAllowUnsecurePasswordProtocols(allowUnsecurePasswordProtocols.isSelected());
626
627       }
628    }
629
630    public final static JPanel wrapLeft(JComponent c)
631    {
632       JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
633       c.setOpaque(false);
634       p.setOpaque(false);
635       p.add(c);
636       return p;
637    }
638
639 }
Popular Tags