// The contents of this file are subject to the Mozilla Public License Version
// 1.1
//(the "License"); you may not use this file except in compliance with the
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
//
//Software distributed under the License is distributed on an "AS IS" basis,
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
//for the specific language governing rights and
//limitations under the License.
//
//The Original Code is "The Columba Project"
//
//The Initial Developers of the Original Code are Frederik Dietz and Timo
// Stich.
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
//
//All Rights Reserved.
package org.columba.addressbook.gui.action;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import javax.swing.JFileChooser;
import org.columba.addressbook.folder.AddressbookFolder;
import org.columba.addressbook.gui.frame.AddressbookFrameMediator;
import org.columba.addressbook.model.IContactModel;
import org.columba.addressbook.parser.VCardParser;
import org.columba.addressbook.util.AddressbookResourceLoader;
import org.columba.api.gui.frame.IFrameMediator;
import org.columba.ristretto.io.CharSequenceSource;
import org.columba.ristretto.io.SourceInputStream;
/**
* Import VCARD contact to selected addressbook.
*/
public class AddVCardAction extends DefaultTreeAction {
public AddVCardAction(IFrameMediator frameController) {
super(frameController, AddressbookResourceLoader.getString("menu",
"mainframe", "menu_file_addvcard"));
// tooltip text
putValue(SHORT_DESCRIPTION, AddressbookResourceLoader.getString("menu",
"mainframe", "menu_file_addvcard").replaceAll("&", ""));
}
/**
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent evt) {
AddressbookFrameMediator mediator = (AddressbookFrameMediator) frameMediator;
// get selected folder
AddressbookFolder destinationFolder = (AddressbookFolder) mediator
.getTree().getSelectedFolder();
// create open file dialog
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.setMultiSelectionEnabled(true);
int returnVal = fc.showOpenDialog(frameMediator.getView().getFrame());
//if user pressed OK button
if (returnVal == JFileChooser.APPROVE_OPTION) {
File[] files = fc.getSelectedFiles();
for (int i = 0; i < files.length; i++) {
try {
StringBuffer strbuf = new StringBuffer();
// read VCARD file into string buffer
BufferedReader in = new BufferedReader(new FileReader(
files[i]));
String str;
while ((str = in.readLine()) != null) {
strbuf.append(str + "\n");
}
in.close();
// create contact card
IContactModel[] cards = VCardParser.read(new SourceInputStream(
new CharSequenceSource(strbuf.toString())));
// add to folder
destinationFolder.add(cards);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
// update table
mediator.getTable().getAddressbookModel().update();
}
}
|