1   /**
2    * JHylaFax - A java client for HylaFAX.
3    *
4    * Copyright (C) 2005 by Steffen Pingel <steffenp@gmx.de>
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation; either version 2 of the License, or
9    * (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19   */
20  package net.sf.jhylafax.addressbook;
21  
22  import java.awt.datatransfer.DataFlavor;
23  import java.awt.datatransfer.Transferable;
24  import java.io.InputStream;
25  import javax.swing.JComponent;
26  import javax.swing.TransferHandler;
27  import net.wimpi.pim.Pim;
28  import net.wimpi.pim.contact.io.ContactUnmarshaller;
29  import net.wimpi.pim.contact.model.Contact;
30  import net.wimpi.pim.factory.ContactIOFactory;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  public abstract class AbstractContactTransferHandler extends TransferHandler {
35  	
36  	private final static Log logger = LogFactory.getLog(AbstractContactTransferHandler.class);
37  	
38  	public abstract void importData(Contact[] contacts);
39  	
40  	@Override
41  	public boolean importData(JComponent component, Transferable transferable)
42  	{
43          if (canImport(component, transferable.getTransferDataFlavors())) {
44              try {
45      			ContactIOFactory factory = Pim.getContactIOFactory();
46      		    ContactUnmarshaller unmarshaller = factory.createContactUnmarshaller();
47      		    InputStream in = (InputStream)transferable.getTransferData(ContactTransferable.VCARD_FLAVOR);
48      		    unmarshaller.setEncoding("UTF-8");
49      		    Contact[] contacts = unmarshaller.unmarshallContacts(in);
50      		    if (contacts != null && contacts.length > 0) {
51      		    	importData(contacts);
52      		    }
53                  return true;
54              } 
55              catch (Exception e) {
56              	logger.debug("Error during import", e);
57              } 
58          }
59          return false;
60  	}
61  	
62  	@Override
63  	public boolean canImport(JComponent component, DataFlavor[] transferFlavors)
64  	{
65          if (transferFlavors == null) {
66               return false;
67          }
68  
69          for (int i = 0; i < transferFlavors.length; i++) {
70              if (ContactTransferable.VCARD_FLAVOR.equals(transferFlavors[i])) {
71                  return true;
72              }
73          }
74          return false;
75  	}
76  }