/*
 * Copyright (c) 2002-2007 TeamDev Ltd. All rights reserved.
 *
 * Use is subject to license terms.
 *
 * The complete licence text can be found at
 * http://www.jniwrapper.com/pages/macpack/license
 */
package com.jniwrapper.macosx.samples.demo;

import com.jniwrapper.samples.shell.components.LazyPanel;
import com.jniwrapper.samples.shell.components.HTMLText;
import com.jniwrapper.macosx.ui.controls.SelectFolderField;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.util.Vector;

/**
 * @author Vadim Steshenko
 */
public class ChooseFolderSample extends LazyPanel implements PropertyChangeListener
{
    private Vector _folders;
    private JLabel lblAdvisoryText;
    private JLabel lblFolderCaption;
    private SelectFolderField _selectFolderField;
    private JLabel lblSelectedFolders;
    private JLabel lblNote;
    private JList lstSelectedFolders;
    private JCheckBox cbAllowMultipleSelection;
    private JCheckBox cbAllowInvisibleFiles;
    private JCheckBox cbTreatsFilePackagesAsDirectories;
    private JCheckBox cbAllowCreateDiretories;
    JPanel _optionsPanel;

    public ChooseFolderSample(Window parent)
    {
        super(parent);
    }

    public void initialize() throws Exception
    {
        _folders = new Vector();
        lblAdvisoryText = new HTMLText("The page demonstrates Standard Choose Folder file dialogs invoked using a special 'SelectFolder' control.");
        lblFolderCaption = new JLabel("Select Folder:");
        _selectFolderField = new SelectFolderField();
        lblSelectedFolders = new JLabel("Selected Folders:");
        lblNote = new HTMLText("<b>NOTE:</b> Multiple folders selected in dialog are shown in the list above.");
        lstSelectedFolders = new JList(_folders);

        _selectFolderField.addPropertyChangeListener(SelectFolderField.PROPERTY_FOLDER, this);
        _selectFolderField.setAllowMultipleSelection(true);

        cbAllowMultipleSelection = new JCheckBox("Allow Multiple Selection",
                                                 _selectFolderField.isAllowMultipleSelection());
        cbAllowMultipleSelection.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                JCheckBox checkBox = (JCheckBox)event.getSource();
                _selectFolderField.setAllowMultipleSelection(checkBox.isSelected());
            }
        });
        cbAllowInvisibleFiles = new JCheckBox("Show All Files", _selectFolderField.isAllowInvisibleFiles());
        cbAllowInvisibleFiles.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                JCheckBox checkBox = (JCheckBox)event.getSource();
                _selectFolderField.setAllowInvisibleFiles(checkBox.isSelected());
            }
        });
        cbTreatsFilePackagesAsDirectories = new JCheckBox("Treats file packages as directories",
                                                          _selectFolderField.isTreatsFilePackagesAsDirectories());
        cbTreatsFilePackagesAsDirectories.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                JCheckBox checkBox = (JCheckBox)event.getSource();
                _selectFolderField.setTreatsFilePackagesAsDirectories(checkBox.isSelected());
            }
        });
        cbAllowCreateDiretories = new JCheckBox("Allow create directories",
                                                          _selectFolderField.isAllowCreateDirectories());
        cbAllowCreateDiretories.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                JCheckBox checkBox = (JCheckBox)event.getSource();
                _selectFolderField.setAllowCreateDirectories(checkBox.isSelected());
            }
        });

        _optionsPanel = new JPanel(new GridLayout(2, 2));

        _optionsPanel.add(cbAllowMultipleSelection);
        _optionsPanel.add(cbAllowInvisibleFiles);
        _optionsPanel.add(cbTreatsFilePackagesAsDirectories);
        _optionsPanel.add(cbAllowCreateDiretories);

        JScrollPane files = new JScrollPane(lstSelectedFolders);
        files.setPreferredSize(new Dimension(10, 100));

        setLayout(new GridBagLayout());

        add(lblAdvisoryText, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0,
                GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 10, 10), 0, 0));
        add(_optionsPanel, new GridBagConstraints(0, 1, 2, 1, 0.0, 0.0,
                GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 10), 0, 0));
        add(lblFolderCaption, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0));
        add(_selectFolderField, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0));
        add(lblSelectedFolders, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0));
        add(files, new GridBagConstraints(0, 4, 2, 1, 0.0, 0.5,
                GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 10, 0, 10), 0, 0));
        add(lblNote, new GridBagConstraints(0, 5, 2, 1, 0.0, 0.0,
                GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 0, 0), 0, 0));
        add(new JPanel(), new GridBagConstraints(0, 6, 2, 1, 1.0, 1.0,
                GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

        super.initialize();
    }

    public void propertyChange(PropertyChangeEvent event)
    {
        _folders.clear();
        _folders.addAll(_selectFolderField.getFolders());
        lstSelectedFolders.updateUI();
    }
}