001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.mail.gui.message.action;
017:
018: import java.awt.event.ActionEvent;
019:
020: import javax.swing.AbstractAction;
021:
022: import org.columba.addressbook.facade.IContactFacade;
023: import org.columba.addressbook.facade.IContactItem;
024: import org.columba.addressbook.facade.IModelFacade;
025: import org.columba.api.exception.ServiceNotFoundException;
026: import org.columba.api.exception.StoreException;
027: import org.columba.core.resourceloader.IconKeys;
028: import org.columba.core.resourceloader.ImageLoader;
029: import org.columba.mail.connector.FacadeUtil;
030: import org.columba.mail.connector.ServiceConnector;
031: import org.columba.mail.gui.message.util.ColumbaURL;
032: import org.columba.mail.util.MailResourceLoader;
033: import org.columba.ristretto.message.Address;
034: import org.columba.ristretto.parser.ParserException;
035:
036: /**
037: * Add address to addressbook.
038: *
039: * @author fdietz
040: */
041: public class AddToAddressbookAction extends AbstractAction {
042: private String emailAddress;
043:
044: private ColumbaURL url = null;
045:
046: /**
047: *
048: */
049: public AddToAddressbookAction(ColumbaURL url) {
050: super (MailResourceLoader.getString("menu", "mainframe",
051: "viewer_addressbook"));
052:
053: putValue(SMALL_ICON, ImageLoader
054: .getSmallIcon(IconKeys.CONTACT_NEW));
055:
056: this .url = url;
057: setEnabled(url != null);
058: if (url != null)
059: setEnabled(url.isMailTo());
060: }
061:
062: /**
063: *
064: */
065: public AddToAddressbookAction(String emailAddress) {
066: super (MailResourceLoader.getString("menu", "mainframe",
067: "viewer_addressbook"));
068:
069: this .emailAddress = emailAddress;
070: setEnabled(emailAddress != null);
071:
072: putValue(SMALL_ICON, ImageLoader
073: .getSmallIcon(IconKeys.CONTACT_NEW));
074: }
075:
076: /*
077: * (non-Javadoc)
078: *
079: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
080: */
081: public void actionPerformed(ActionEvent evt) {
082:
083: IContactFacade contactFacade = null;
084: IModelFacade modelFacade = null;
085: try {
086: contactFacade = ServiceConnector.getContactFacade();
087: modelFacade = ServiceConnector.getModelFacade();
088: } catch (ServiceNotFoundException e) {
089: e.printStackTrace();
090: return;
091: }
092:
093: try {
094: Address address = null;
095: if (emailAddress != null)
096: address = Address.parse(emailAddress);
097: else
098: // create Address from URL
099: address = Address.parse(url.getSender());
100:
101: // add contact to addressbook
102: IContactItem contactItem = modelFacade.createContactItem();
103: FacadeUtil.getInstance().initContactItem(contactItem,
104: address.getDisplayName(), address.getMailAddress());
105: contactFacade.addContact(contactItem);
106: } catch (ParserException e) {
107: e.printStackTrace();
108: } catch (StoreException e) {
109: e.printStackTrace();
110: }
111: }
112:
113: }
|