/* * GpioView.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 the current GPIO * configuration settings for the ConnectME device. * * This view can be embedded in other panels as desired. */ public class GpioView extends ConfigViewImpl { /** A KvpNode that holds just the settings used in this view */ KvpNode viewSettingTree; /** 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 pin1KvpField; KvpField pin2KvpField; KvpField pin3KvpField; KvpField pin4KvpField; KvpField pin5KvpField; // UI Controls for each field JComboBox pin1_UIC; JComboBox pin2_UIC; JComboBox pin3_UIC; JComboBox pin4_UIC; JComboBox pin5_UIC; /** * Basic constructor. Sets up the tree view and the edit panel. */ public GpioView() throws Exception { super(true); // create the KvpNode and the change listener viewSettingTree = new KvpNode(); settingChangeListener = new DeviceChangeListener() { public void deviceChanged(DeviceChangeEvent e) { //SystemLog.debug("GpioView was notified that device kvpGroup changed"); refreshViewKvpNode(); refreshViewControls(); } }; // Create the panel that will hold the view controls content = new GridContentPanel(this.getName(),1); // create a listener that will detect when a user edits the field values ControlChangeAdapter fieldListener = new ControlChangeAdapter() { public void changePerformed(Object source, ActionEvent ae, FocusEvent fe) { //SystemLog.debug("GpioView was notified that field "+source.toString()+" changed."); if (source==pin1_UIC) { pin1KvpField.setStringValue(pin1_UIC.getSelectedItem().toString()); } else if (source==pin2_UIC) { pin2KvpField.setStringValue(pin2_UIC.getSelectedItem().toString()); } else if (source==pin3_UIC) { pin3KvpField.setStringValue(pin3_UIC.getSelectedItem().toString()); } else if (source==pin4_UIC) { pin4KvpField.setStringValue(pin4_UIC.getSelectedItem().toString()); } else if (source==pin5_UIC) { pin5KvpField.setStringValue(pin5_UIC.getSelectedItem().toString()); } } }; // // Create the fields and put them on the view // content.addTextLine("GpioViewDesc"); String pinChoices[] = { "in", "out", "serial" }; pin1_UIC = content.addJComboBox("GpioPin1","GpioPin1Desc",pinChoices,fieldListener); pin2_UIC = content.addJComboBox("GpioPin2","GpioPin1Desc",pinChoices,fieldListener); pin3_UIC = content.addJComboBox("GpioPin3","GpioPin1Desc",pinChoices,fieldListener); pin4_UIC = content.addJComboBox("GpioPin4","GpioPin1Desc",pinChoices,fieldListener); pin5_UIC = content.addJComboBox("GpioPin5","GpioPin1Desc",pinChoices,fieldListener); content.addVGlue(); } /** * Returns a one word name identifying this view. */ public String getName() { return "GpioView"; } /** * Return the Component that displays the primary content for this view */ public Component getViewContent() { return content; } /** * 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); refreshViewKvpNode(); refreshViewControls(); device.addSettingChangeListener(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 refreshViewKvpNode() { viewSettingTree.clear(); KvpNode gpioModeSettings = viewSettingTree.mergeChild(device.getInternalSettingTree().getChild("gpio_mode"),true); if (gpioModeSettings!=null) { pin1KvpField = gpioModeSettings.getChild("pin1"); pin2KvpField = gpioModeSettings.getChild("pin2"); pin3KvpField = gpioModeSettings.getChild("pin3"); pin4KvpField = gpioModeSettings.getChild("pin4"); pin5KvpField = gpioModeSettings.getChild("pin5"); } // pin1KvpField = viewSettingTree.mergeFrom(device.getInternalSettingTree(), "gpio_mode", "pin1"); // pin2KvpField = viewSettingTree.mergeFrom(device.getInternalSettingTree(), "gpio_mode", "pin2"); // pin3KvpField = viewSettingTree.mergeFrom(device.getInternalSettingTree(), "gpio_mode", "pin3"); // pin4KvpField = viewSettingTree.mergeFrom(device.getInternalSettingTree(), "gpio_mode", "pin4"); // pin5KvpField = viewSettingTree.mergeFrom(device.getInternalSettingTree(), "gpio_mode", "pin5"); } /** * Refreshes the state of the controls in the view to match the data in the views kvpGroup. */ private void refreshViewControls() { pin1_UIC.setEnabled(pin1KvpField!=null); if (pin1KvpField!=null) { pin1_UIC.setSelectedItem(pin1KvpField.getStringValue()); } pin2_UIC.setEnabled(pin2KvpField!=null); if (pin2KvpField!=null) { pin2_UIC.setSelectedItem(pin2KvpField.getStringValue()); } pin3_UIC.setEnabled(pin3KvpField!=null); if (pin3KvpField!=null) { pin3_UIC.setSelectedItem(pin3KvpField.getStringValue()); } pin4_UIC.setEnabled(pin4KvpField!=null); if (pin4KvpField!=null) { pin4_UIC.setSelectedItem(pin4KvpField.getStringValue()); } pin5_UIC.setEnabled(pin5KvpField!=null); if (pin5KvpField!=null) { pin5_UIC.setSelectedItem(pin5KvpField.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 "+getName()); } /** * 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 "+getName()); } /** * 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 viewSettingTree.hasChanged(); } /** * This method instructs the view to validate any of the changes the user has made * within the view. Any errors are to be added to the provided errorList. * * @param errorList is a collection of ViewValidationError objects */ public void validateChanges(Collection errorList) { // Nothing to Validate // errorList.add(new ValidationError("DummyValidationError")); } /** * 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) { // Merge the view changes into the provided cluster settingCluster.merge(viewSettingTree, false); } /** * Instructs the view to consider any user changes within the view as * saved to the device (ie committed). */ public void commitChanges() { // simply reset flags indicating user changes in the contols viewSettingTree.resetChanged(); } /** * 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() { refreshViewKvpNode(); refreshViewControls(); } }