KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > message > action > AddToAddressbookAction


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.mail.gui.message.action;
17
18 import java.awt.event.ActionEvent JavaDoc;
19
20 import javax.swing.AbstractAction JavaDoc;
21
22 import org.columba.addressbook.facade.IContactFacade;
23 import org.columba.addressbook.facade.IContactItem;
24 import org.columba.addressbook.facade.IModelFacade;
25 import org.columba.api.exception.ServiceNotFoundException;
26 import org.columba.api.exception.StoreException;
27 import org.columba.core.resourceloader.IconKeys;
28 import org.columba.core.resourceloader.ImageLoader;
29 import org.columba.mail.connector.FacadeUtil;
30 import org.columba.mail.connector.ServiceConnector;
31 import org.columba.mail.gui.message.util.ColumbaURL;
32 import org.columba.mail.util.MailResourceLoader;
33 import org.columba.ristretto.message.Address;
34 import org.columba.ristretto.parser.ParserException;
35
36 /**
37  * Add address to addressbook.
38  *
39  * @author fdietz
40  */

41 public class AddToAddressbookAction extends AbstractAction JavaDoc {
42     private String JavaDoc emailAddress;
43
44     private ColumbaURL url = null;
45
46     /**
47      *
48      */

49     public AddToAddressbookAction(ColumbaURL url) {
50         super(MailResourceLoader.getString("menu", "mainframe",
51                 "viewer_addressbook"));
52
53         putValue(SMALL_ICON, ImageLoader.getSmallIcon(IconKeys.CONTACT_NEW));
54         
55         this.url = url;
56         setEnabled( url != null);
57         if ( url != null)
58             setEnabled( url.isMailTo());
59     }
60
61     /**
62      *
63      */

64     public AddToAddressbookAction(String JavaDoc emailAddress) {
65         super(MailResourceLoader.getString("menu", "mainframe",
66                 "viewer_addressbook"));
67
68         this.emailAddress = emailAddress;
69         setEnabled(emailAddress != null);
70         
71         putValue(SMALL_ICON, ImageLoader.getSmallIcon(IconKeys.CONTACT_NEW));
72     }
73
74     /*
75      * (non-Javadoc)
76      *
77      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
78      */

79     public void actionPerformed(ActionEvent JavaDoc evt) {
80
81         IContactFacade contactFacade = null;
82         IModelFacade modelFacade = null;
83         try {
84             contactFacade = ServiceConnector.getContactFacade();
85             modelFacade = ServiceConnector.getModelFacade();
86         } catch (ServiceNotFoundException e) {
87             e.printStackTrace();
88             return;
89         }
90
91         try {
92             Address address = null;
93             if (emailAddress != null)
94                 address = Address.parse(emailAddress);
95             else
96                 // create Address from URL
97
address = Address.parse(url.getSender());
98
99             // add contact to addressbook
100
IContactItem contactItem = modelFacade.createContactItem();
101             FacadeUtil.getInstance().initContactItem(contactItem,
102                     address.getDisplayName(), address.getMailAddress());
103             contactFacade.addContact(contactItem);
104         } catch (ParserException e) {
105             e.printStackTrace();
106         } catch (StoreException e) {
107             e.printStackTrace();
108         }
109     }
110
111 }
Popular Tags