/*
* 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.java.awt;
import java.awt.FileDialog;
import java.io.File;
import java.util.Map;
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;
/**
* The input delegate for FileDialog
* @author TiongHiang Lee (thlee@onemindsoft.org)
*
*/
public class FileDialogInputDelegate extends DialogInputDelegate
{
/** the instance **/
public static MapInputDelegate INSTANCE = new FileDialogInputDelegate();
/**
* {@inheritDoc}
*/
public void processInput(BridgeComponentPeer peer, BridgeInputContext context, Map inputForm) throws InputException
{
String okKey = peer.getId() + ".OK";
String cancelKey = peer.getId() + ".CANCEL";
FileDialog dlg = (FileDialog) 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);
dlg.setFile(f.getName());
dlg.setDirectory(f.getParent());
dlg.hide(); //hide it like the peer implementation do
} catch (Exception e)
{
throw new InputException("Cannot save file", e);
}
} else if (!StringUtils.isNullOrEmpty(inputForm.get(cancelKey)))
{
dlg.hide();
} else
{
super.processInput(peer, context, inputForm);
}
}
}