001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.addressbook.gui.action;
019:
020: import java.awt.event.ActionEvent;
021: import java.io.BufferedReader;
022: import java.io.File;
023: import java.io.FileReader;
024:
025: import javax.swing.JFileChooser;
026:
027: import org.columba.addressbook.folder.AddressbookFolder;
028: import org.columba.addressbook.gui.frame.AddressbookFrameMediator;
029: import org.columba.addressbook.model.IContactModel;
030: import org.columba.addressbook.parser.VCardParser;
031: import org.columba.addressbook.util.AddressbookResourceLoader;
032: import org.columba.api.gui.frame.IFrameMediator;
033: import org.columba.ristretto.io.CharSequenceSource;
034: import org.columba.ristretto.io.SourceInputStream;
035:
036: /**
037: * Import VCARD contact to selected addressbook.
038: */
039:
040: public class AddVCardAction extends DefaultTreeAction {
041: public AddVCardAction(IFrameMediator frameController) {
042: super (frameController, AddressbookResourceLoader.getString(
043: "menu", "mainframe", "menu_file_addvcard"));
044:
045: // tooltip text
046: putValue(SHORT_DESCRIPTION, AddressbookResourceLoader
047: .getString("menu", "mainframe", "menu_file_addvcard")
048: .replaceAll("&", ""));
049: }
050:
051: /**
052: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
053: */
054: public void actionPerformed(ActionEvent evt) {
055: AddressbookFrameMediator mediator = (AddressbookFrameMediator) frameMediator;
056:
057: // get selected folder
058: AddressbookFolder destinationFolder = (AddressbookFolder) mediator
059: .getTree().getSelectedFolder();
060:
061: // create open file dialog
062: JFileChooser fc = new JFileChooser();
063: fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
064: fc.setMultiSelectionEnabled(true);
065:
066: int returnVal = fc.showOpenDialog(frameMediator.getView()
067: .getFrame());
068:
069: //if user pressed OK button
070: if (returnVal == JFileChooser.APPROVE_OPTION) {
071: File[] files = fc.getSelectedFiles();
072:
073: for (int i = 0; i < files.length; i++) {
074: try {
075: StringBuffer strbuf = new StringBuffer();
076:
077: // read VCARD file into string buffer
078: BufferedReader in = new BufferedReader(
079: new FileReader(files[i]));
080: String str;
081:
082: while ((str = in.readLine()) != null) {
083: strbuf.append(str + "\n");
084: }
085:
086: in.close();
087:
088: // create contact card
089: IContactModel[] cards = VCardParser
090: .read(new SourceInputStream(
091: new CharSequenceSource(strbuf
092: .toString())));
093:
094: // add to folder
095: destinationFolder.add(cards);
096: } catch (Exception ex) {
097: ex.printStackTrace();
098: }
099: }
100: }
101:
102: // update table
103: mediator.getTable().getAddressbookModel().update();
104: }
105: }
|