/*
* Copyright (C) 2004 TiongHiang Lee
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Email: thlee@onemindsoft.org
*/
package org.onemind.swingweb.mapinput.peerdelegate.javax.swing;
import java.io.File;
import java.util.Map;
import javax.swing.JFileChooser;
import org.apache.commons.fileupload.DefaultFileItem;
import org.onemind.awtbridge.input.BridgeInputContext;
import org.onemind.awtbridge.input.InputException;
import org.onemind.awtbridge.peer.BridgeComponentPeer;
import org.onemind.commons.java.util.StringUtils;
import org.onemind.swingweb.mapinput.peerdelegate.MapInputDelegate;
/**
* Input delegate for JFileDialog
* @author TiongHiang Lee (thlee@onemindsoft.org)
*
*/
public class JFileChooserInputDelegate extends JComponentInputDelegate
{
/** the instance **/
public static MapInputDelegate INSTANCE = new JFileChooserInputDelegate();
/**
* {@inheritDoc}
*/
public void processInput(BridgeComponentPeer peer, BridgeInputContext context, Map inputForm) throws InputException
{
String okKey = peer.getId() + ".OK";
String cancelKey = peer.getId() + ".CANCEL";
JFileChooser chooser = (JFileChooser) peer.getComponent();
if (!StringUtils.isNullOrEmpty(inputForm.get(okKey)))
{
String fileKey = peer.getId() + ".File.ITEM";
DefaultFileItem item = (DefaultFileItem) inputForm.get(fileKey);
try
{
File f = File.createTempFile("fdi", new File(item.getName()).getName(), new File(System
.getProperty("java.io.tmpdir")));
item.write(f);
chooser.setSelectedFile(f);
chooser.approveSelection();
} catch (Exception e)
{
throw new InputException("Cannot save file", e);
}
} else if (!StringUtils.isNullOrEmpty(inputForm.get(cancelKey)))
{
chooser.cancelSelection();
} else
{
super.processInput(peer, context, inputForm);
}
}
//
// /**
// * Get the dialog containing the chooser
// * @param chooser
// * @return
// */
// private Dialog getChooserDialog(JFileChooser chooser)
// {
// Component current = chooser;
// while (current != null)
// {
// current = current.getParent();
// if (current instanceof Dialog)
// {
// return (Dialog) current;
// }
// }
// return null;
// }
}