/*
 * 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(22));

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

        JScrollPane files = new JScrollPane(lstSelectedFolders);
        files.setPreferredSize(new Dimension(10100));

        setLayout(new GridBagLayout());

        add(lblAdvisoryText, new GridBagConstraints(00210.00.0,
                GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10101010)00));
        add(_optionsPanel, new GridBagConstraints(01210.00.0,
                GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0101010)00));
        add(lblFolderCaption, new GridBagConstraints(02110.00.0,
                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));
        add(_selectFolderField, new GridBagConstraints(12110.00.0,
                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));
        add(lblSelectedFolders, new GridBagConstraints(03110.00.0,
                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));
        add(files, new GridBagConstraints(04210.00.5,
                GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(010010)00));
        add(lblNote, new GridBagConstraints(05210.00.0,
                GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(101000)00));
        add(new JPanel()new GridBagConstraints(06211.01.0,
                GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0000)00));

        super.initialize();
    }

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