/*
 * 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.util.ProcessorInfo;
import com.jniwrapper.win32.system.MemoryStatus;
import com.jniwrapper.samples.shell.components.LazyPanel;
import com.jniwrapper.samples.shell.components.HTMLText;
import com.jniwrapper.samples.shell.components.LineBevel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.net.MalformedURLException;

/**
 * @author Serge Piletsky
 */
public class SystemInfoSample extends LazyPanel
{
    private JLabel lblAdvisoryText;
    private JLabel lblProcessorInformation;
    private JLabel lblVendorCaption;
    private JLabel lblVendor;
    private JLabel lblProcessorNameCaption;
    private JLabel lblProcessorName;
    private JLabel lblFrequencyCaption;
    private JLabel lblFrequency;
    private JLabel lblFamilyCaption;
    private JLabel lblFamily;
    private JLabel lblModelCaption;
    private JLabel lblModel;
    private JLabel lblSteppingCaption;
    private JLabel lblStepping;
    private JCheckBox chkFPU;
    private JCheckBox chkMMX;
    private JCheckBox chkMMXPlus;
    private JCheckBox chk3DNow;
    private JCheckBox chk3DNowPlus;
    private JCheckBox chkSSE;
    private JCheckBox chkSSE2;
    private JLabel lblMemoryInformationCaption;
    private JLabel lblPhisicalTotalCaption;
    private JLabel lblPhisicalAvailableCaption;
    private JLabel lblPhisicalTotal;
    private JLabel lblPhisicalAvailable;

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

    public void initialize() throws Exception
    {
        ProcessorInfo info = ProcessorInfo.getInstance();
        lblAdvisoryText = new HTMLText("This page displays information about your processor using ProcessorInfo class and memory information using MemoryStatus class.");
        lblProcessorInformation = new JLabel("Processor Information:");
        lblVendorCaption = new JLabel("Vendor:");
        lblVendor = new JLabel(info.getVendor());
        lblProcessorNameCaption = new JLabel("Processor Name:");
        lblProcessorName = new JLabel(info.getProcessorName());
        lblFrequencyCaption = new JLabel("Frequency:");
        lblFrequency = new JLabel(String.valueOf(info.getFrequency()) + " MHz");
        lblFamilyCaption = new JLabel("Family:");
        lblFamily = new JLabel(String.valueOf(info.getFamily()));
        lblModelCaption = new JLabel("Model:");
        lblModel = new JLabel(String.valueOf(info.getModel()));
        lblSteppingCaption = new JLabel("Stepping:");
        lblStepping = new JLabel(String.valueOf(info.getStepping()));
        chkFPU = new CPUFeature("FPU", info.hasFPU());
        chkMMX = new CPUFeature("MMX", info.hasMMX());
        chkMMXPlus = new CPUFeature("MMXPlus", info.hasMMXPlus());
        chk3DNow = new CPUFeature("3DNow!", info.has3DNow());
        chk3DNowPlus = new CPUFeature("3DNow! Plus", info.has3DNowPlus());
        chkSSE = new CPUFeature("SSE", info.hasSSE());
        chkSSE2 = new CPUFeature("SSE2", info.hasSSE2());
        lblMemoryInformationCaption = new JLabel("Memory Information:");
        lblPhisicalTotalCaption = new JLabel("Physical Total:");
        lblPhisicalTotal = new JLabel();
        lblPhisicalAvailableCaption = new JLabel("Physical Available:");
        lblPhisicalAvailable = new JLabel();

        setLayout(new GridBagLayout());

        add(lblAdvisoryText, new GridBagConstraints(0, 0, 3, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 10, 10), 0, 0));

        add(lblProcessorInformation, new GridBagConstraints(0, 1, 2, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));

        JPanel bevel1 = new LineBevel();

        add(bevel1, new GridBagConstraints(1, 1, 2, 1, 0.0, 0.0
                , GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));

        add(lblVendorCaption, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 20, 0, 10), 0, 0));

        add(lblVendor, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 20, 0, 10), 0, 0));

        add(lblProcessorNameCaption, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 20, 0, 10), 0, 0));

        add(lblProcessorName, new GridBagConstraints(1, 3, 2, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 20, 0, 10), 0, 0));

        add(lblFrequencyCaption, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 20, 0, 10), 0, 0));

        add(lblFrequency, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 20, 0, 10), 0, 0));

        add(lblFamilyCaption, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 20, 0, 10), 0, 0));

        add(lblFamily, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 20, 0, 10), 0, 0));

        add(lblModelCaption, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 20, 0, 10), 0, 0));

        add(lblModel, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 20, 0, 10), 0, 0));

        add(lblSteppingCaption, new GridBagConstraints(0, 7, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 20, 0, 10), 0, 0));

        add(lblStepping, new GridBagConstraints(1, 7, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 20, 0, 10), 0, 0));

        add(chkFPU, new GridBagConstraints(0, 8, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 20, 0, 10), 0, 0));

        add(chkMMX, new GridBagConstraints(1, 8, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 20, 0, 10), 0, 0));

        add(chkMMXPlus, new GridBagConstraints(2, 8, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 20, 0, 10), 0, 0));

        add(chk3DNow, new GridBagConstraints(0, 9, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 20, 0, 10), 0, 0));

        add(chk3DNowPlus, new GridBagConstraints(1, 9, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 20, 0, 10), 0, 0));

        add(chkSSE, new GridBagConstraints(0, 10, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 20, 0, 10), 0, 0));

        add(chkSSE2, new GridBagConstraints(1, 10, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 20, 0, 10), 0, 0));

        add(lblMemoryInformationCaption, new GridBagConstraints(0, 11, 2, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 0, 10), 0, 0));

        JPanel bevel2 = new LineBevel();

        add(bevel2, new GridBagConstraints(1, 11, 2, 1, 0.0, 0.0
                , GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 0, 10), 0, 0));

        add(lblPhisicalTotalCaption, new GridBagConstraints(0, 12, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 20, 0, 10), 0, 0));

        add(lblPhisicalTotal, new GridBagConstraints(1, 12, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 20, 0, 10), 0, 0));

        add(lblPhisicalAvailableCaption, new GridBagConstraints(0, 13, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 20, 0, 10), 0, 0));

        add(lblPhisicalAvailable, new GridBagConstraints(1, 13, 2, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 20, 0, 10), 0, 0));

        add(new JPanel(), new GridBagConstraints(0, 14, 3, 1, 1.0, 1.0
                , GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
        super.initialize();
    }

    private class CPUFeature extends JCheckBox
    {
        public CPUFeature(String text, boolean selected)
        {
            super(text, selected);
            setMargin(new Insets(0, 0, 0, 0));
            setFocusPainted(false);

            setSelectedIcon(new ImageIcon(this.getClass().getResource("res/checked.gif")));
            setIcon(new ImageIcon(this.getClass().getResource("res/unchecked.gif")));

        }

        protected void processMouseEvent(MouseEvent e)
        {
        }

        protected void processKeyEvent(KeyEvent e)
        {
        }
    }

    public void activate() throws Exception
    {
        super.activate();
        MemoryStatus memoryStatus = new MemoryStatus();
        lblPhisicalTotal.setText("" + memoryStatus.getTotalPhys() / 1024 + " Kb");
        lblPhisicalAvailable.setText("" + memoryStatus.getAvailPhys() / 1024 + " Kb");
    }
}