1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 }