/*
 * 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.teamdev.com/winpack/license.jsf
 */
package com.jniwrapper.win32.samples.demo;

import com.jniwrapper.win32.registry.RegistryKey;
import com.jniwrapper.win32.registry.ui.RegistryKeyValuesTableModel;
import com.jniwrapper.win32.registry.ui.RegistryTreeModel;
import com.jniwrapper.samples.shell.components.LazyPanel;
import com.jniwrapper.samples.shell.components.HTMLText;
import com.jniwrapper.samples.shell.components.TreeCellRenderer;
import com.jniwrapper.samples.shell.BasicDemoShell;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.table.TableColumnModel;
import javax.swing.tree.TreePath;
import java.awt.*;

/**
 @author Serge Piletsky
 */
public class RegistryKeySample extends LazyPanel implements TreeSelectionListener
{
    private JLabel lblAdvisoryText;
    private JTree _registry;
    private JTable _registryKeyValues;
    private RegistryKeyValuesTableModel _registryKeyValuesTableModel;
    private RegistryTreeModel _registryTreeModel;

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

    public void initialize() throws Exception
    {
        lblAdvisoryText = new HTMLText("This page demonstrates a simple registry viewer created using RegistryKey class methods.");
        _registryTreeModel = new RegistryTreeModel();
        _registry = new JTree(_registryTreeModel)
        {
            protected void setExpandedState(TreePath path, boolean state)
            {
                if (state)
                {
                    final Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
                    final Cursor defCursor = getCursor();
                    try
                    {
                        setCursor(waitCursor);
                        super.setExpandedState(path, state);
                    }
                    finally
                    {
                        setCursor(defCursor);
                    }
                }
                else
                    super.setExpandedState(path, state);
            }
        };

        _registry.setRootVisible(false);
        _registry.addTreeSelectionListener(this);
        BasicTreeUI basicTreeUI = ((BasicTreeUI)_registry.getUI());
        basicTreeUI.setCollapsedIcon(new ImageIcon(BasicDemoShell.class.getResource("res/Closed.gif")));
        basicTreeUI.setExpandedIcon(new ImageIcon(BasicDemoShell.class.getResource("res/Opened.gif")));
        _registry.putClientProperty("JTree.lineStyle""None");
        _registry.setCellRenderer(new TreeCellRenderer());
        _registry.setShowsRootHandles(true);

        _registryKeyValuesTableModel = new RegistryKeyValuesTableModel();
        _registryKeyValues = new JTable(_registryKeyValuesTableModel);
        _registryKeyValues.setRowSelectionAllowed(true);
        _registryKeyValues.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        _registryKeyValues.setShowHorizontalLines(false);
        _registryKeyValues.setShowVerticalLines(false);
        final TableColumnModel columnModel = _registryKeyValues.getColumnModel();
        columnModel.getColumn(0).setPreferredWidth(80);
        columnModel.getColumn(1).setPreferredWidth(70);
        columnModel.getColumn(2).setPreferredWidth(110);

        setLayout(new GridBagLayout());

        add(lblAdvisoryText, new GridBagConstraints(00110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10101010)00));

        final JSplitPane splitter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true);
        splitter.setBorder(null);
        splitter.setDividerSize(2);
        splitter.setDividerLocation(170);

        JScrollPane registry = new JScrollPane(_registry);
        JScrollPane values = new JScrollPane(_registryKeyValues);

        splitter.add(registry, JSplitPane.LEFT);
        splitter.add(values, JSplitPane.RIGHT);

        add(splitter, new GridBagConstraints(01111.01.0
                , GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(10101010)00));

        super.initialize();
    }

    public void valueChanged(TreeSelectionEvent e)
    {
        TreePath path = e.getPath();
        RegistryTreeModel.RegistryKeyNode node = (RegistryTreeModel.RegistryKeyNode)path.getPathComponent(path.getPathCount() 1);
        RegistryKey key = node.getRegistryKey();
        _registryKeyValuesTableModel.setRegistryKey(key);
        _registryKeyValues.revalidate();
        _registryKeyValues.repaint();
    }
}