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.BufferedOutputStream;
022: import java.io.File;
023: import java.io.FileNotFoundException;
024: import java.io.FileOutputStream;
025: import java.io.IOException;
026:
027: import javax.swing.JFileChooser;
028:
029: import org.columba.addressbook.folder.AddressbookFolder;
030: import org.columba.addressbook.gui.frame.AddressbookFrameMediator;
031: import org.columba.addressbook.model.IContactModel;
032: import org.columba.addressbook.parser.VCardParser;
033: import org.columba.api.gui.frame.IFrameMediator;
034:
035: /**
036: * @author fdietz
037: *
038: */
039:
040: public class ExportVCardAction extends DefaultTableAction {
041:
042: /**
043: * @param frameMediator
044: * @param name
045: */
046: public ExportVCardAction(IFrameMediator frameMediator) {
047: super (frameMediator, "Export to VCard..");
048: }
049:
050: /**
051: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
052: */
053: public void actionPerformed(ActionEvent arg0) {
054: AddressbookFrameMediator mediator = (AddressbookFrameMediator) frameMediator;
055:
056: // get selected folder
057: AddressbookFolder sourceFolder = (AddressbookFolder) mediator
058: .getTree().getSelectedFolder();
059:
060: // get selected contact/group card
061: String[] uids = mediator.getTable().getUids();
062:
063: // create open file dialog
064: JFileChooser fc = new JFileChooser();
065: fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
066: fc.setApproveButtonText("Select");
067:
068: int returnVal = fc.showOpenDialog(frameMediator.getView()
069: .getFrame());
070:
071: //if user pressed OK button
072: if (returnVal == JFileChooser.APPROVE_OPTION) {
073: File file = fc.getSelectedFile();
074:
075: for (int i = 0; i < uids.length; i++) {
076: try {
077: IContactModel contact = sourceFolder.get(uids[i]);
078: File f = new File(file, "contact"
079: + uids[i].toString() + ".vcf");
080:
081: BufferedOutputStream s = new BufferedOutputStream(
082: new FileOutputStream(f));
083:
084: VCardParser.write(contact, s);
085:
086: } catch (FileNotFoundException e) {
087:
088: e.printStackTrace();
089: } catch (IOException e) {
090:
091: e.printStackTrace();
092: } catch (Exception e) {
093:
094: e.printStackTrace();
095: }
096: }
097: }
098: }
099:
100: }
|