/* * GpioStateView.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 state of the GPIO lines * and potentially allows the user to set them. * * This view can be embedded in other panels as desired. */ public class GpioStateView extends ConfigViewImpl { /** A KvpNode that holds just the state fields used in this view */ KvpNode viewStateTree; /** listens to the device for changes to state fields */ DeviceChangeListener stateChangeListener; /** 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 JRadioButton pin1Asserted_UIC; JRadioButton pin1Unasserted_UIC; JRadioButton pin2Asserted_UIC; JRadioButton pin2Unasserted_UIC; JRadioButton pin3Asserted_UIC; JRadioButton pin3Unasserted_UIC; JRadioButton pin4Asserted_UIC; JRadioButton pin4Unasserted_UIC; JRadioButton pin5Asserted_UIC; JRadioButton pin5Unasserted_UIC; JButton setGpioPinsBtn; ConfigAction setGpioPinsAction; /** * Basic constructor. Sets up the tree view and the edit panel. */ public GpioStateView() throws Exception { super(false); setIncludeStateFields(true); setIncludeSettingsFields(false); // create the KvpNode and the change listener viewStateTree = new KvpNode(); stateChangeListener = new DeviceChangeListener() { public void deviceChanged(DeviceChangeEvent e) { //SystemLog.debug("GpioStateView 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("GpioStateView was notified that field "+source.toString()+" changed."); if (source==pin1Asserted_UIC || source==pin1Unasserted_UIC) { pin1KvpField.setStringValue(RciProtocol.assertedString(pin1Asserted_UIC.isSelected())); } else if (source==pin2Asserted_UIC || source==pin2Unasserted_UIC) { pin2KvpField.setStringValue(RciProtocol.assertedString(pin2Asserted_UIC.isSelected())); } else if (source==pin3Asserted_UIC || source==pin3Unasserted_UIC) { pin3KvpField.setStringValue(RciProtocol.assertedString(pin3Asserted_UIC.isSelected())); } else if (source==pin4Asserted_UIC || source==pin4Unasserted_UIC) { pin4KvpField.setStringValue(RciProtocol.assertedString(pin4Asserted_UIC.isSelected())); } else if (source==pin5Asserted_UIC || source==pin5Unasserted_UIC) { pin5KvpField.setStringValue(RciProtocol.assertedString(pin5Asserted_UIC.isSelected())); } setGpioPinsBtn.setEnabled(isChanged()); } }; // Set button's apply action for gpio pins (just resuse the save action from our parent). setGpioPinsAction = new ConfigAction("SetGpioPins", this, "doSaveChangesAction"); // // Create the fields and put them on the view // content.addTextLine("GpioStateViewDesc"); GridContentPanel pinContent = new GridContentPanel(3); pinContent.addColumnHeading("", fieldListener, 0, false); pinContent.addColumnHeading("PinAssertedHeading", fieldListener, 1, false); pinContent.addColumnHeading("PinUnassertedHeading", fieldListener, 2, true); ButtonGroup pin1Group = new ButtonGroup(); pinContent.addColumnHeading("GpioPin1", fieldListener, 0, false); pin1Asserted_UIC = pinContent.addJRadioButton(null, fieldListener, pin1Group, 1, false); pin1Unasserted_UIC = pinContent.addJRadioButton(null, fieldListener, pin1Group, 2, true); ButtonGroup pin2Group = new ButtonGroup(); pinContent.addColumnHeading("GpioPin2", fieldListener, 0, false); pin2Asserted_UIC = pinContent.addJRadioButton(null, fieldListener, pin2Group, 1, false); pin2Unasserted_UIC = pinContent.addJRadioButton(null, fieldListener, pin2Group, 2, true); ButtonGroup pin3Group = new ButtonGroup(); pinContent.addColumnHeading("GpioPin3", fieldListener, 0, false); pin3Asserted_UIC = pinContent.addJRadioButton(null, fieldListener, pin3Group, 1, false); pin3Unasserted_UIC = pinContent.addJRadioButton(null, fieldListener, pin3Group, 2, true); ButtonGroup pin4Group = new ButtonGroup(); pinContent.addColumnHeading("GpioPin4", fieldListener, 0, false); pin4Asserted_UIC = pinContent.addJRadioButton(null, fieldListener, pin4Group, 1, false); pin4Unasserted_UIC = pinContent.addJRadioButton(null, fieldListener, pin4Group, 2, true); ButtonGroup pin5Group = new ButtonGroup(); pinContent.addColumnHeading("GpioPin5", fieldListener, 0, false); pin5Asserted_UIC = pinContent.addJRadioButton(null, fieldListener, pin5Group, 1, false); pin5Unasserted_UIC = pinContent.addJRadioButton(null, fieldListener, pin5Group, 2, true); content.addSubPanel(pinContent); // JPanel buttonPanel = new JPanel(); // buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); // buttonPanel.add(Box.createHorizontalGlue()); setGpioPinsBtn = new JButton(setGpioPinsAction); setGpioPinsBtn.setEnabled(false); // buttonPanel.add(setGpioPinsBtn); // buttonPanel.add(Box.createHorizontalGlue()); // content.addSubPanel(buttonPanel); content.addField(null, setGpioPinsBtn, null); content.addVGlue(); } /** * Returns a one word name identifying this view. */ public String getName() { return "GpioStateView"; } /** * 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(); } /** * 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() { viewStateTree.clear(); pin1KvpField = viewStateTree.mergeFrom(device.getInternalStateTree(), "gpio", "pin1"); pin2KvpField = viewStateTree.mergeFrom(device.getInternalStateTree(), "gpio", "pin2"); pin3KvpField = viewStateTree.mergeFrom(device.getInternalStateTree(), "gpio", "pin3"); pin4KvpField = viewStateTree.mergeFrom(device.getInternalStateTree(), "gpio", "pin4"); pin5KvpField = viewStateTree.mergeFrom(device.getInternalStateTree(), "gpio", "pin5"); } /** * Refreshes the state of the controls in the view to match the data in the views kvpGroup. */ private void refreshViewControls() { setPinState(pin1KvpField, pin1Asserted_UIC, RciProtocol.ASSERTED); setPinState(pin1KvpField, pin1Unasserted_UIC, RciProtocol.UNASSERTED); setPinState(pin2KvpField, pin2Asserted_UIC, RciProtocol.ASSERTED); setPinState(pin2KvpField, pin2Unasserted_UIC, RciProtocol.UNASSERTED); setPinState(pin3KvpField, pin3Asserted_UIC, RciProtocol.ASSERTED); setPinState(pin3KvpField, pin3Unasserted_UIC, RciProtocol.UNASSERTED); setPinState(pin4KvpField, pin4Asserted_UIC, RciProtocol.ASSERTED); setPinState(pin4KvpField, pin4Unasserted_UIC, RciProtocol.UNASSERTED); setPinState(pin5KvpField, pin5Asserted_UIC, RciProtocol.ASSERTED); setPinState(pin5KvpField, pin5Unasserted_UIC, RciProtocol.UNASSERTED); setGpioPinsBtn.setEnabled(isChanged()); } // Helper private void setPinState(KvpField field, JRadioButton pinButton, String state) { // If we don't have data for the field disable it. pinButton.setEnabled(field!=null); if (field!=null) { pinButton.setSelected(field.getStringValue().equals(state)); // Only enable if configured for output pinButton.setEnabled(device.getInternalSettingTree().getField("gpio_mode", field.getName()).getStringValue().equals("out")); } } /** * 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()); device.addStateChangeListener(stateChangeListener); device.addSettingChangeListener(stateChangeListener); doRefreshAction(); } /** * 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()); device.removeStateChangeListener(stateChangeListener); device.removeSettingChangeListener(stateChangeListener); } /** * 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 viewStateTree.hasChanged(); } /** * 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 stateCluster.merge(viewStateTree, 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 viewStateTree.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(); } }