KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedReader JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileReader JavaDoc;
24
25 import javax.swing.JFileChooser JavaDoc;
26
27 import org.columba.addressbook.folder.AddressbookFolder;
28 import org.columba.addressbook.gui.frame.AddressbookFrameMediator;
29 import org.columba.addressbook.model.IContactModel;
30 import org.columba.addressbook.parser.VCardParser;
31 import org.columba.addressbook.util.AddressbookResourceLoader;
32 import org.columba.api.gui.frame.IFrameMediator;
33 import org.columba.ristretto.io.CharSequenceSource;
34 import org.columba.ristretto.io.SourceInputStream;
35
36 /**
37  * Import VCARD contact to selected addressbook.
38  */

39
40 public class AddVCardAction extends DefaultTreeAction {
41     public AddVCardAction(IFrameMediator frameController) {
42         super(frameController, AddressbookResourceLoader.getString("menu",
43                 "mainframe", "menu_file_addvcard"));
44
45         // tooltip text
46
putValue(SHORT_DESCRIPTION, AddressbookResourceLoader.getString("menu",
47                 "mainframe", "menu_file_addvcard").replaceAll("&", ""));
48     }
49
50     /**
51      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
52      */

53     public void actionPerformed(ActionEvent JavaDoc evt) {
54         AddressbookFrameMediator mediator = (AddressbookFrameMediator) frameMediator;
55
56         // get selected folder
57
AddressbookFolder destinationFolder = (AddressbookFolder) mediator
58                 .getTree().getSelectedFolder();
59
60         // create open file dialog
61
JFileChooser JavaDoc fc = new JFileChooser JavaDoc();
62         fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
63         fc.setMultiSelectionEnabled(true);
64
65         int returnVal = fc.showOpenDialog(frameMediator.getView().getFrame());
66
67         //if user pressed OK button
68
if (returnVal == JFileChooser.APPROVE_OPTION) {
69             File JavaDoc[] files = fc.getSelectedFiles();
70
71             for (int i = 0; i < files.length; i++) {
72                 try {
73                     StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
74
75                     // read VCARD file into string buffer
76
BufferedReader JavaDoc in = new BufferedReader JavaDoc(new FileReader JavaDoc(
77                             files[i]));
78                     String JavaDoc str;
79
80                     while ((str = in.readLine()) != null) {
81                         strbuf.append(str + "\n");
82                     }
83
84                     in.close();
85
86                     // create contact card
87
IContactModel[] cards = VCardParser.read(new SourceInputStream(
88                             new CharSequenceSource(strbuf.toString())));
89
90                     // add to folder
91
destinationFolder.add(cards);
92                 } catch (Exception JavaDoc ex) {
93                     ex.printStackTrace();
94                 }
95             }
96         }
97
98         // update table
99
mediator.getTable().getAddressbookModel().update();
100     }
101 }
Popular Tags