KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > addressbook > gui > action > ExportVCardAction


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.addressbook.gui.action;
19
20 import java.awt.event.ActionEvent JavaDoc;
21 import java.io.BufferedOutputStream JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import javax.swing.JFileChooser JavaDoc;
28
29 import org.columba.addressbook.folder.AddressbookFolder;
30 import org.columba.addressbook.gui.frame.AddressbookFrameMediator;
31 import org.columba.addressbook.model.IContactModel;
32 import org.columba.addressbook.parser.VCardParser;
33 import org.columba.api.gui.frame.IFrameMediator;
34
35 /**
36  * @author fdietz
37  *
38  */

39
40 public class ExportVCardAction extends DefaultTableAction {
41
42     /**
43      * @param frameMediator
44      * @param name
45      */

46     public ExportVCardAction(IFrameMediator frameMediator) {
47         super(frameMediator, "Export to VCard..");
48     }
49
50     /**
51      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
52      */

53     public void actionPerformed(ActionEvent JavaDoc arg0) {
54         AddressbookFrameMediator mediator = (AddressbookFrameMediator) frameMediator;
55
56         // get selected folder
57
AddressbookFolder sourceFolder = (AddressbookFolder) mediator.getTree()
58                 .getSelectedFolder();
59
60         // get selected contact/group card
61
String JavaDoc[] uids = mediator.getTable().getUids();
62
63         // create open file dialog
64
JFileChooser JavaDoc fc = new JFileChooser JavaDoc();
65         fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
66         fc.setApproveButtonText("Select");
67
68         int returnVal = fc.showOpenDialog(frameMediator.getView().getFrame());
69
70         //if user pressed OK button
71
if (returnVal == JFileChooser.APPROVE_OPTION) {
72             File JavaDoc file = fc.getSelectedFile();
73
74             for (int i = 0; i < uids.length; i++) {
75                 try {
76                     IContactModel contact = sourceFolder.get(uids[i]);
77                     File JavaDoc f = new File JavaDoc(file, "contact"+uids[i].toString() + ".vcf");
78
79                     BufferedOutputStream JavaDoc s = new BufferedOutputStream JavaDoc(
80                             new FileOutputStream JavaDoc(f));
81
82                     VCardParser.write(contact, s);
83
84                 } catch (FileNotFoundException JavaDoc e) {
85
86                     e.printStackTrace();
87                 } catch (IOException JavaDoc e) {
88
89                     e.printStackTrace();
90                 } catch (Exception JavaDoc e) {
91
92                     e.printStackTrace();
93                 }
94             }
95         }
96     }
97
98 }
Popular Tags