/*
 * 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 Protege-2000.
 *
 * The Initial Developer of the Original Code is Stanford University. Portions
 * created by Stanford University are Copyright (C) 2007.  All Rights Reserved.
 *
 * Protege was developed by Stanford Medical Informatics
 * (http://www.smi.stanford.edu) at the Stanford University School of Medicine
 * with support from the National Library of Medicine, the National Science
 * Foundation, and the Defense Advanced Research Projects Agency.  Current
 * information about Protege can be obtained at http://protege.stanford.edu.
 *
 */

package edu.stanford.smi.protege.util;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

import javax.swing.*;
import javax.swing.event.*;

import edu.stanford.smi.protege.resource.*;

/**
 * A text field that accepts a URI.  It allows the user to browse for a file name with the standard file browser.
 *
 * @author    Ray Fergerson <fergerson@smi.stanford.edu>
 */
public class URIField extends JComponent {
    private JTextField _textField;
    private String _description;
    private String _extension;
    private ChangeListener _changeListener;

    public URIField(String label, URI uri, String extension, String description) {
        _description = description;
        _extension = extension;
        setLayout(new BorderLayout());
        LabeledComponent c = new LabeledComponent(label, createComponent(uri));
        c.addHeaderButton(new AbstractAction("Browse for File", Icons.getAddIcon()) {
            public void actionPerformed(ActionEvent event) {
                browse();
            }
        });
        add(c);
    }

    public void addChangeListener(ChangeListener listener) {
        Assert.assertNull("existing change listener", _changeListener);
        _changeListener = listener;
    }

    protected void browse() {
        JFileChooser chooser = ComponentFactory.createFileChooser(_description, _extension);
        chooser.setApproveButtonText("Select");
        int openDialogResult = chooser.showOpenDialog(this);
        switch (openDialogResult) {
            case JFileChooser.ERROR_OPTION:
                // Get this on 'close"
                break;
            case JFileChooser.CANCEL_OPTION:
                break;
            case JFileChooser.APPROVE_OPTION:
                _textField.setText(chooser.getSelectedFile().getPath());
                break;
            default:
                Assert.fail("bad result: " + openDialogResult);
                break;
        }
    }

    private JComponent createComponent(URI uri) {
        _textField = ComponentFactory.createTextField();
        _textField.setColumns(40);
        setURI(uri);
        _textField.getDocument().addDocumentListener(new DocumentChangedListener() {
            public void stateChanged(ChangeEvent event) {
                notifyListener();
            }
        });
        return _textField;
    }

    public URI getAbsoluteURI() {
        String text = _textField.getText();
        return URIUtilities.createURI(text);
    }

    public URI getRelativeURI() {
        URI uri = null;
        String text = _textField.getText().trim();
        if (text.length() > 0) {
            try {
                uri = new URI(text);
            } catch (Exception e) {
                // We assume that is is an absolute file path and strip off the name.
                File file = new File(text);
                try {
                    uri = new URI(file.getName());
                } catch (Exception ex) {
                    // do nothing
                }
            }
        }
        return uri;
    }

    private void notifyListener() {
        if (_changeListener != null) {
            _changeListener.stateChanged(new ChangeEvent(this));
        }
    }

    public void setURI(URI uri) {
        String text = (uri == null) ? null : uri.toString();
        _textField.setText(text);
    }
}
