/*
 * Copyright (c) 2000-2009 TeamDev Ltd. All rights reserved.
 * TeamDev PROPRIETARY and CONFIDENTIAL.
 * Use is subject to license terms.
 */
package com.jniwrapper.win32.samples.demo;

import com.jniwrapper.win32.com.ComFunctions;
import com.jniwrapper.win32.shell.ShellFolder;
import com.jniwrapper.win32.shell.ShellLink;
import com.jniwrapper.win32.ui.controls.SelectFileField;
import com.jniwrapper.samples.shell.components.LazyPanel;
import com.jniwrapper.samples.shell.components.HTMLText;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 @author Serge Piletsky
 */
public class ShellLinksSample extends ComfyJSample
{
    private JLabel lblAdvisoryText;

    private JLabel lblCreateLinkHint;
    private JLabel lblFileNameCaption;
    private SelectFileField _selectFileField;
    private JButton btnCreateLink;

    private JLabel lblResolveLinkHint;
    private JLabel lblLinkCaption;
    private SelectFileField _selectlinkField;
    private JButton btnResolveLink;
    private JLabel lblNotSupported;

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

    public void initialize() throws Exception
    {
        boolean comfyjAvailable = isComfyJAvailable();

        if (comfyjAvailable)
        {
            lblNotSupported = new JLabel();
        }
        else
        {
            lblNotSupported = new HTMLText("<b><FONT color = red>NOTE:</FONT> The ShellLink functionality requires " +
                    "the ComfyJ library and ComfyJ license file.");
        }

        lblAdvisoryText = new HTMLText("The page demonstrates WinPack ability to create and resolve link files."false);
        lblCreateLinkHint = new HTMLText("Select a file that you want to create the link for and click \"Create link\" button. The link will be created on your desktop."false);
        lblFileNameCaption = new JLabel("Select File:");
        _selectFileField = new SelectFileField();
        btnCreateLink = new JButton("Create Link");
        btnCreateLink.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                createLink();
            }
        });

        lblResolveLinkHint = new HTMLText("Also, you can select a link file and press \"Resolve link\" button to see where an original file is located."false);
        lblLinkCaption = new JLabel("Select Link:");
        _selectlinkField = new SelectFileField();
        btnResolveLink = new JButton("Resolve Link");
        btnResolveLink.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                resolveLink();
            }
        });

        if (!comfyjAvailable)
        {
            _selectFileField.setEnabled(false);
            _selectlinkField.setEnabled(false);
            btnCreateLink.setEnabled(false);
            btnResolveLink.setEnabled(false);
        }

        setLayout(new GridBagLayout());

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

        add(lblCreateLinkHint, new GridBagConstraints(01310.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10101010)00));

        add(lblFileNameCaption, new GridBagConstraints(02110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

        add(_selectFileField, new GridBagConstraints(12110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

        add(btnCreateLink, new GridBagConstraints(22110.00.0,
                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

        add(lblResolveLinkHint, new GridBagConstraints(03310.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(20101010)00));

        add(lblLinkCaption, new GridBagConstraints(04110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

        add(_selectlinkField, new GridBagConstraints(14110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

        add(btnResolveLink, new GridBagConstraints(24110.00.0,
                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

        add(lblNotSupported, new GridBagConstraints(05310.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(20101010)00));

        add(new JPanel()new GridBagConstraints(06311.01.0
                , GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0000)00));

        _selectFileField.getDialog().getOptions().setMultiselectionAllowed(false);
        _selectFileField.getDialog().setFilter("All Files (*.*)|*.*");

        _selectlinkField.getDialog().getOptions().setMultiselectionAllowed(false);
        _selectlinkField.getDialog().setFilter("Link Files (*.lnk)|*.lnk");

        super.initialize();
    }

    private void createLink()
    {
        String sourceFileName = _selectFileField.getFileName();

        if (sourceFileName.equals(""))
            return;

        final File sourceFile = new File(sourceFileName);

        sourceFileName = sourceFile.getName();
        final int index = sourceFileName.lastIndexOf('.');
        if (index != -1)
        {
            sourceFileName = sourceFileName.substring(0, index);
        }
        sourceFileName += ".lnk";

        final File linkFile = new File(ShellFolder.DESKTOP.getAbsolutePath(), sourceFileName);

        // initialize COM
        ComFunctions.coInitialize();
        ShellLink.createLink(sourceFile, linkFile, "This link is created by WinPack Demo applicaion");
        // unitilialize COM
        ComFunctions.coUninitialize();
    }

    private void resolveLink()
    {
        String sourceFileName = _selectlinkField.getFileName();

        if (sourceFileName.equals(""))
            return;

        final File sourceFile = new File(sourceFileName);

        // initialize COM
        ComFunctions.coInitialize();
        String result = ShellLink.resolveLink(sourceFile);
        JOptionPane.showMessageDialog(this, "The choosen link file refers to the following file:\n" + result);
        // unitilialize COM
        ComFunctions.coUninitialize();
    }
}