/*
 * HomeView.java
 * 
 * Copyright (c) 2003-2004 Digi International
 * This program and the information contained in it is confidential and
 * proprietary to Digi International and may not be used, copied, or re-
 * produced without the prior written permission of Digi International.
 * 
 */

package com.digi.config.ui;

import com.digi.config.core.*;
import com.digi.config.util.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;




/** 
 * This class is a view panel that shows a basic Getting Started 
 * informational panel for the ConnectMe device
 * 
 * This view can be embedded in other panels as desired.
 */


public class HomeView extends ConfigViewImpl {
    /** A KvpNode that holds just the settings used in this view */
    KvpNode viewSettingTree;
    KvpNode viewStateTree;
    /** listens to the device for changes to settings */
    DeviceChangeListener settingChangeListener;
    /** Panel that holds the view content */
    GridContentPanel content;

    // Data Fields currently used in this view
    KvpField ipKvpField;
    KvpField hostNameKvpField;
    KvpField macAddressKvpField;

    // UI Controls for each field
    JLabel model_UIC;
    JLabel ip_UIC;
    JLabel hostName_UIC;
    JLabel macAddress_UIC;

    /**
     * Basic constructor. Sets up the tree view and the edit panel.
     */
    public HomeView() throws Exception {
        // Include State Fields for MAC
        setIncludeStateFields(true);

        // create the KvpNode and the kvpGroup change listener
        viewSettingTree = new KvpNode();
        viewStateTree = new KvpNode();
        settingChangeListener = new DeviceChangeListener() {
            public void deviceChanged(DeviceChangeEvent e) {
                //SystemLog.debug("SerialBasicView was notified that device kvpGroup changed");
                refreshViewData();
                refreshViewControls();
            }
        };
        // Create the panel that will hold the view controls
        content = new GridContentPanel(this.getName(),1);

        //
        // Create the fields and put them on the view
        //

//        content.addHeadingBar("GettingStarted");
//        content.addAction(null, new ConfigAction("Tutorial", this, "doTutorialAction"), "TutorialDesc");
        content.addHeadingBar("SystemSummary");

        model_UIC = content.addJLabel("Model","ModelDesc", 200);
        ip_UIC = content.addJLabel("Home.IPAddress","Home.IPAddressDesc", 200);
//        hostName_UIC = content.addJLabel("HostName","HostNameDesc", 100);
        macAddress_UIC = content.addJLabel("MACAddress","MACAddressDesc", 200);

        content.addVGlue();
    }


//    public void doTutorialAction() {
//        SystemLog.debug("Launching Tutorial!");
//        HelpDisplay.showHelp("Tutorial");
//    }

    /**
     * Returns a one word name identifying this view. 
     */
    public String getName() {
        return "HomeView";
    }

    /** 
     * Return the Component that displays the primary content for this view
     */
    public Component getViewContent() {
        return content;
    }

    /** 
     * Returns the Component that displays the buttons below the view.
     * If this view has no buttons to display this method returns null.
     */
     public Component getViewButtons() {
         // We don't want buttons on this view so just return null
         return null;
     }


    /**
     * Informs view what device to work with. View should flush any
     * cached device data and mark current view content as invalid
     * so that it is refreshed appropriatly.
     */
    public void setDevice(Device device) {
        super.setDevice(device);
        refreshViewData();
        refreshViewControls();
        device.addSettingChangeListener(settingChangeListener);
        device.addStateChangeListener(settingChangeListener);
    }

    /** 
     * Refreshes the local copy of the kvpGroup displayed in the view. The updated kvpGroup come
     * from those currently cached in the Device
     */
    private void refreshViewData() {
        viewSettingTree.clear();
        hostNameKvpField = viewSettingTree.mergeFrom(device.getInternalSettingTree(), "network", "hostName"); 
        
        viewStateTree.clear();
        ipKvpField = viewStateTree.mergeFrom(device.getInternalStateTree(), "boot_stats", "ip"); 
        macAddressKvpField = viewStateTree.mergeFrom(device.getInternalStateTree(), "device_info", "mac"); 
    }

    /** 
     * Refreshes the state of the controls in the view to match the data in the views kvpGroup.
     */
    private void refreshViewControls() {
        if (device!=null) {
            model_UIC.setText(device.getProductName());
        }
        ip_UIC.setEnabled(ipKvpField!=null);
        if (ipKvpField!=null) {
            ip_UIC.setText(ipKvpField.getStringValue());
        }
// hostname not avail in this release
//         hostName_UIC.setEnabled(hostNameKvpField!=null);
//         if (hostNameKvpField!=null) {
//             hostName_UIC.setText(hostNameKvpField.getStringValue());
//         }
        macAddress_UIC.setEnabled(macAddressKvpField!=null);
        if (macAddressKvpField!=null) {
            macAddress_UIC.setText(macAddressKvpField.getStringValue());
        }
    }




    /**
     * This method is called to instruct the view that it is about to be made
     * active. When a view is active it needs to make sure its content is correct.
     * An example of why a panel may be inactive is if it were on a tabbed pane 
     * and was not currently visible or if the user selected some other view.
     */
    public void activate() {
        SystemLog.debug("Activating HomeView");
    }

    /**
     * This method is called to instruct the view that it is about to be made
     * inactive. When a view is inactive, it does not need to worry
     * about maintaining its content in a correct state. An example of why a panel
     * may be inactive is if it were on a tabbed pane and was not currently visible
     * or if the user selected some other view.
     */
    public void deactivate() {
        SystemLog.debug("Deactivating HomeView");
    }

    /**
     * Indicates if any changes have been made by the user that have not yet been
     * saved to the device(ie committed).
     */
    public boolean isChanged() {
        return false;
    }

    /**
     * This method instructs the view to place any changes the user has made within 
     * the view into the provided clusters. Once the changes have been saved to
     * to the device, a subsequent call to commitChanges() will be made.
     */
    public void getChanges(KvpNode settingCluster, KvpNode stateCluster) {
    }

    /**
     * Instructs the view to consider any user changes within the view as 
     * saved to the device (ie committed).
     */
    public void commitChanges() {
    }

    /** 
     * Instructs the view to discard any changes the user has made within the
     * view and revert those fields to the original state. The view is also 
     * free to refresh all its fields to the present cached state of the device at 
     * this time.
     */
    public void cancelChanges() {
    }


}

