/*
 * SerialView.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 Network KvpNode 
 * informational panel for the ConnectMe device
 * 
 * This view can be embedded in other panels as desired.
 */

public class SerialView extends AggregateConfigView {

    /** Indicates the number of ports available on the device */
    int portCount = 0;
    /** Indicates which port is selected. 0 means no port is selected */
    int portSelected = 0;
    /** The GUI control showing ports available that user can select from */
    JComboBox portUIC;
    /** Listens to changes in the selected port */
    ActionListener portSelectionListener;


    /**
     * Basic constructor. Sets up the tree view and the edit panel.
     */
    public SerialView() throws Exception {
        super(true);
        // create the port selection control and a listener
        portUIC = new JComboBox();
        portSelectionListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    updatePortSelected();
                }};

        // create the views and add them to this aggregate view
        addView(new SerialBasicView());
        addView(new SerialPortServicesView());
        addView(new SerialNetworkServicesView());
        addView(new SerialAdvancedView());
    }

    /**
     * Informs view what device to work with. Subclass Views should flush any
     * cached device data and mark current view content as invalid
     * so that it is refreshed appropriatly.
     */
    public void setDevice(Device newDevice) {
        super.setDevice(newDevice);
        if (device!=null) {
            // update the port count
            updatePortCount();
            // listen for future changes to the port count.
            device.addSettingChangeListener(new DeviceChangeListener() {
                    public void deviceChanged(DeviceChangeEvent e) {
                        updatePortCount();
                    }
            });
        }
    }




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


    /** 
     * Returns the Component that displays the heading of the view.
     * If this view has no heading to display this method returns null.
     */
    public Component getViewHeading() {
        if (heading==null) {
            JPanel headingPanel = new JPanel();
            headingPanel.setBackground(new Color(0, 0, 139));
            headingPanel.setForeground(Color.WHITE);
            headingPanel.setOpaque(true);
            headingPanel.setLayout(new BoxLayout(headingPanel, BoxLayout.X_AXIS));

            JLabel l = new JLabel(ConfigResource.getUiRbString(getName()+"Title"));
            l.setForeground(Color.WHITE);
            headingPanel.add(l);

            headingPanel.add(Box.createHorizontalGlue());

            l = new JLabel(ConfigResource.getUiRbString("PortNumber"));
            l.setForeground(Color.WHITE);
            headingPanel.add(l);

            headingPanel.add(Box.createHorizontalStrut(10));

            //add a port selector box & a listener
            portUIC.addActionListener(portSelectionListener);
            portUIC.setMaximumSize(new Dimension(100,25));
            headingPanel.add(portUIC);
            
            headingPanel.add(Box.createHorizontalStrut(30));

            heading = headingPanel;
        }
        return heading;
    }


    /**
     * Called when the number of ports on the device is known to change. Updates the
     * UI to reflect the available ports
     */
    private void updatePortCount() {
        // we can tell the number of ports by the number of serial groups there are.
        int newPortCount = 0;
        if (device!=null) {
            newPortCount = device.getInternalSettingTree().getChildCount("serial");
        }
        //System.out.println("Device Port Count = "+newPortCount);
        //System.out.println("  oldPortSelected = "+portSelected);

        // has it changed from what it was?
        if (newPortCount!=portCount) {
            // Reset the number of ports available in the portUIC combo box. First
            // disable the port selection listener and then update the combo box
            // based on the new number of ports available.
            portUIC.removeActionListener(portSelectionListener); 
            // if shrinking, remove items from the end of the list
            while (portUIC.getItemCount()>newPortCount) {
                portUIC.removeItemAt(portUIC.getItemCount()-1);
            }
            // if growing, add items to the end of the list
            while (portUIC.getItemCount()<newPortCount) {
                portUIC.addItem(Integer.toString(portUIC.getItemCount()+1));
            }
            portCount = newPortCount;
            portUIC.addActionListener(portSelectionListener);

            // Next update the selected port if necessary
            int newPortSelected = portSelected;
            if (newPortSelected>newPortCount) {
                // we've shrunk below old port selection, move selection down to last port avail
                newPortSelected = newPortCount;
            }
            if ((newPortSelected==0) && (newPortCount>0)) {
                // previously there were no ports available and now there are, automatically select the first port
                newPortSelected = 1;
            }
            //System.out.println("  newPortSelected = "+newPortSelected);
            portUIC.setSelectedIndex(newPortSelected-1);

        }
    }

    /**
     * Change occured in the portSelected UI control. Record the new port
     * and notify sub-views of the change.
     */
    private void updatePortSelected() {
        int newPortSelected = portUIC.getSelectedIndex()+1;
        if (newPortSelected!=portSelected) {
            // update the cached selection value
            portSelected = newPortSelected;
            // notify underlying views to update themselves off the new port #
            //System.out.println(">>>>New Port Selected is "+portSelected+". Notifying views of the change.");
            this.setViewIndex(portSelected);
        }
    }






}

