/*
 * #%L
 * SciJava UI components for Java Swing.
 * %%
 * Copyright (C) 2010 - 2014 Board of Regents of the University of
 * Wisconsin-Madison.
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * #L%
 */

package org.scijava.ui.swing.widget;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import org.scijava.plugin.Plugin;
import org.scijava.widget.FileWidget;
import org.scijava.widget.InputWidget;
import org.scijava.widget.WidgetModel;

/**
 * Swing implementation of file selector widget.
 * 
 * @author Curtis Rueden
 */
@Plugin(type = InputWidget.class)
public class SwingFileWidget extends SwingInputWidget<File> implements
	FileWidget<JPanel>, ActionListener, DocumentListener
{

	private JTextField path;
	private JButton browse;

	// -- InputWidget methods --

	@Override
	public File getValue() {
		final String text = path.getText();
		return text.isEmpty() ? null : new File(text);
	}

	// -- WrapperPlugin methods --

	@Override
	public void set(final WidgetModel model) {
		super.set(model);

		path = new JTextField(16);
		setToolTip(path);
		getComponent().add(path);
		path.getDocument().addDocumentListener(this);

		getComponent().add(Box.createHorizontalStrut(3));

		browse = new JButton("Browse");
		setToolTip(browse);
		getComponent().add(browse);
		browse.addActionListener(this);

		refreshWidget();
	}

	// -- Typed methods --

	@Override
	public boolean supports(final WidgetModel model) {
		return super.supports(model) && model.isType(File.class);
	}

	// -- ActionListener methods --

	@Override
	public void actionPerformed(final ActionEvent e) {
		File file = new File(path.getText());
		if (!file.isDirectory()) {
			file = file.getParentFile();
		}

		// display file chooser in appropriate mode
		final String style = get().getItem().getWidgetStyle();
		// TODO: Use uiService.chooseFile(file, style) instead.
		final JFileChooser chooser = new JFileChooser(file);
		if (FileWidget.DIRECTORY_STYLE.equals(style)) {
			chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		}
		final int rval;
		if (FileWidget.SAVE_STYLE.equals(style)) {
			rval = chooser.showSaveDialog(getComponent());
		}
		else { // default behavior
			rval = chooser.showOpenDialog(getComponent());
		}
		if (rval != JFileChooser.APPROVE_OPTION) return;
		file = chooser.getSelectedFile();
		if (file == null) return;

		path.setText(file.getAbsolutePath());
	}

	// -- DocumentListener methods --

	@Override
	public void changedUpdate(final DocumentEvent e) {
		updateModel();
	}

	@Override
	public void insertUpdate(final DocumentEvent e) {
		updateModel();
	}

	@Override
	public void removeUpdate(final DocumentEvent e) {
		updateModel();
	}

	// -- AbstractUIInputWidget methods ---

	@Override
	public void doRefresh() {
		final String text = get().getText();
		if (text.equals(path.getText())) return; // no change
		path.setText(text);
	}
}

@@ -1651,41 +1651,8 @@
 on;%0A
-import javax.swing.JFileChooser;%0A
 impo
@@ -1789,16 +1789,53 @@
 tener;%0A%0A
+import org.scijava.plugin.Parameter;%0A
 import o
@@ -1860,16 +1860,49 @@
 Plugin;%0A
+import org.scijava.ui.UIService;%0A
 import o
@@ -2258,16 +2258,59 @@
 ener%0A%7B%0A%0A
+%09@Parameter%0A%09private UIService uiService;%0A%0A
 %09private
@@ -3357,572 +3357,328 @@
 nal 
-String style = get().getItem().getWidgetStyle();%0A%09%09// TODO: Use uiService.chooseFile(file, style) instead.%0A%09%09final JFileChooser chooser = new JFileChooser(file);%0A%09%09if (FileWidget.DIRECTORY_STYLE.equals(style)) %7B%0A%09%09%09chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);%0A%09%09%7D%0A%09%09final int rval;%0A%09%09if (FileWidget.SAVE_STYLE.equals(style)) %7B%0A%09%09%09rval = chooser.showSaveDialog(getComponent());%0A%09%09%7D%0A%09%09else %7B // default behavior%0A%09%09%09rval = chooser.showOpenDialog(getComponent());%0A%09%09%7D%0A%09%09if (rval != JFileChooser.APPROVE_OPTION) return;%0A%09%09file = chooser.getSelectedFi
+WidgetModel model = get();%0A%09%09final String style;%0A%09%09if (model.isStyle(FileWidget.DIRECTORY_STYLE)) %7B%0A%09%09%09style = FileWidget.DIRECTORY_STYLE;%0A%09%09%7D%0A%09%09else if (model.isStyle(FileWidget.SAVE_STYLE)) %7B%0A%09%09%09style = FileWidget.SAVE_STYLE;%0A%09%09%7D%0A%09%09else %7B%0A%09%09%09style = FileWidget.OPEN_STYLE;%0A%09%09%7D%0A%09%09file = uiService.chooseFile(file, sty
 le
-(
 );%0A%09

use isStyle(String) method