/* * SerialTcpView.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 serial tcp * settings for the ConnectMe device * * This view can be embedded in other panels as desired. */ public class SerialTcpView extends ConfigViewImpl { /** A KvpNode that holds just the fields used in this view */ KvpNode viewSettingTree; /** listens to the device for changes to settings */ DeviceChangeListener settingChangeListener; /** Panel that holds the view content */ GridContentPanel content; GridContentPanel socketIdContent; GridContentPanel idleTimeoutContent; GridContentPanel tcpClientContent; // Data Fields currently used in this view KvpField tcpSocketIdEnabledKvpField; KvpField tcpSocketIdKvpField; KvpField tcpIdleTimeoutKvpField; KvpField tcpDropDCDEnabledKvpField; KvpField tcpDropDSREnabledKvpField; KvpField tcpAutoConnectEnabledKvpField; KvpField tcpConnectTriggerKvpField; KvpField tcpConnectDestinationKvpField; KvpField tcpConnectServiceKvpField; KvpField tcpConnectPortKvpField; // UI Controls for each field JCheckBox tcpSocketIdEnabled_UIC; JTextField tcpSocketId_UIC; JCheckBox tcpIdleTimeoutEnabled_UIC; JTextField tcpIdleTimeout_UIC; JCheckBox tcpDropDCDEnabled_UIC; JCheckBox tcpDropDSREnabled_UIC; JCheckBox tcpAutoConnectEnabled_UIC; JComboBox tcpConnectTrigger_UIC; JTextField tcpConnectDestination_UIC; JComboBox tcpConnectService_UIC; JTextField tcpConnectPort_UIC; /** * Constructor. Sets up the view controls */ public SerialTcpView() throws Exception { // create the KvpNode and the change listener viewSettingTree = new KvpNode(); settingChangeListener = new DeviceChangeListener() { public void deviceChanged(DeviceChangeEvent e) { //SystemLog.debug("SerialTcpView 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("SerialTcpView was notified that field "+source.toString()+" changed."); if (source==tcpSocketIdEnabled_UIC) { tcpSocketIdEnabledKvpField.setStringValue(RciProtocol.toString(tcpSocketIdEnabled_UIC.isSelected())); socketIdContent.setContainerEnabled(tcpSocketIdEnabled_UIC.isSelected()); } else if (source==tcpSocketId_UIC) { tcpSocketIdKvpField.setStringValue(tcpSocketId_UIC.getText()); } else if (source==tcpIdleTimeoutEnabled_UIC) { tcpIdleTimeoutKvpField.setStringValue(tcpIdleTimeoutEnabled_UIC.isSelected() ? tcpIdleTimeout_UIC.getText() : "0"); idleTimeoutContent.setContainerEnabled(tcpIdleTimeoutEnabled_UIC.isSelected()); } else if (source==tcpIdleTimeout_UIC) { tcpIdleTimeoutKvpField.setStringValue(tcpIdleTimeout_UIC.getText()); } else if (source==tcpDropDCDEnabled_UIC) { tcpDropDCDEnabledKvpField.setStringValue(RciProtocol.toString(tcpDropDCDEnabled_UIC.isSelected())); } else if (source==tcpDropDSREnabled_UIC) { tcpDropDSREnabledKvpField.setStringValue(RciProtocol.toString(tcpDropDSREnabled_UIC.isSelected())); } else if (source==tcpAutoConnectEnabled_UIC) { tcpAutoConnectEnabledKvpField.setStringValue(RciProtocol.toString(tcpAutoConnectEnabled_UIC.isSelected())); tcpClientContent.setContainerEnabled(tcpAutoConnectEnabled_UIC.isSelected()); } else if (source==tcpConnectTrigger_UIC) { tcpConnectTriggerKvpField.setStringValue(tcpConnectTrigger_UIC.getSelectedItem().toString()); } else if (source==tcpConnectDestination_UIC) { tcpConnectDestinationKvpField.setStringValue(tcpConnectDestination_UIC.getText()); } else if (source==tcpConnectService_UIC) { tcpConnectServiceKvpField.setStringValue(tcpConnectService_UIC.getSelectedItem().toString()); } else if (source==tcpConnectPort_UIC) { tcpConnectPortKvpField.setStringValue(tcpConnectPort_UIC.getText()); } } }; // // Create the fields and put them on the view // content.addHeadingBar("TcpSerialHeading"); content.addTextLine("TcpSerialDesc"); tcpSocketIdEnabled_UIC = content.addJCheckBox("TcpSocketIdEnabled", fieldListener); socketIdContent = new GridContentPanel(1); tcpSocketId_UIC = socketIdContent.addJTextField("TcpSocketId", "TcpSocketIdDesc", 100, fieldListener); content.addSubPanel(socketIdContent); tcpIdleTimeoutEnabled_UIC = content.addJCheckBox("TcpIdleTimeoutEnabled", fieldListener); idleTimeoutContent = new GridContentPanel(1); tcpIdleTimeout_UIC = idleTimeoutContent.addJTextField("TcpIdleTimeout", "TcpIdleTimeoutDesc", 100, fieldListener); tcpIdleTimeout_UIC.setDocument(createIntegerDocument()); content.addSubPanel(idleTimeoutContent); tcpDropDCDEnabled_UIC = content.addJCheckBox("TcpDropOnDCDEnabled", fieldListener); tcpDropDSREnabled_UIC = content.addJCheckBox("TcpDropOnDSREnabled", fieldListener); content.addHeadingBar("TcpClientHeading"); content.addTextLine("TcpClientDesc"); tcpAutoConnectEnabled_UIC = content.addJCheckBox("TcpAutoConnectEnabled", fieldListener); tcpClientContent = new GridContentPanel(1); String tcpConnectTriggerChoices[] = {"always", "data", "dsr", "dcd"}; tcpConnectTrigger_UIC = tcpClientContent.addJComboBox("TcpConnectTrigger","TcpConnectTriggerDesc",tcpConnectTriggerChoices,fieldListener); //RegexFormatter ipFormatter = new RegexFormatter("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"); tcpConnectDestination_UIC = tcpClientContent.addJTextField("TcpConnectDestination","TcpConnectDestinationDesc",100,fieldListener); tcpConnectDestination_UIC.setDocument(createNetworkAddressDocument()); String tcpConnectServiceChoices[] = {"raw", "telnet", "ssl"}; tcpConnectService_UIC = tcpClientContent.addJComboBox("TcpConnectService","TcpConnectServiceDesc",tcpConnectServiceChoices,fieldListener); //RegexFormatter portFormatter = new RegexFormatter("\\d{1,5}"); tcpConnectPort_UIC = tcpClientContent.addJTextField("TcpConnectPort","TcpConnectPortDesc",100,fieldListener); tcpConnectPort_UIC.setDocument(createIntegerDocument()); content.addSubPanel(tcpClientContent); content.addVGlue(); } /** * Returns a one word name identifying this view. */ public String getName() { return "SerialTcpView"; } /** * 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(); tcpSocketIdEnabledKvpField = viewSettingTree.mergeFrom(device.getInternalSettingTree(), "tcp_serial", "socketid_state"); tcpSocketIdKvpField = viewSettingTree.mergeFrom(device.getInternalSettingTree(), "tcp_serial", "socketid_string"); tcpIdleTimeoutKvpField = viewSettingTree.mergeFrom(device.getInternalSettingTree(), "tcp_serial", "idle_timeout"); tcpDropDCDEnabledKvpField = viewSettingTree.mergeFrom(device.getInternalSettingTree(), "tcp_serial", "hangup_dcd"); tcpDropDSREnabledKvpField = viewSettingTree.mergeFrom(device.getInternalSettingTree(), "tcp_serial", "hangup_dsr"); tcpAutoConnectEnabledKvpField = viewSettingTree.mergeFrom(device.getInternalSettingTree(), "autoconnect", "state"); tcpConnectTriggerKvpField = viewSettingTree.mergeFrom(device.getInternalSettingTree(), "autoconnect", "trigger"); tcpConnectDestinationKvpField = viewSettingTree.mergeFrom(device.getInternalSettingTree(), "autoconnect", "address"); tcpConnectServiceKvpField = viewSettingTree.mergeFrom(device.getInternalSettingTree(), "autoconnect", "service"); tcpConnectPortKvpField = viewSettingTree.mergeFrom(device.getInternalSettingTree(), "autoconnect", "port"); } /** * Refreshes the state of the controls in the view to match the data in the views kvpGroup. */ private void refreshViewControls() { tcpSocketId_UIC.setEnabled(tcpSocketIdKvpField!=null); if (tcpSocketIdKvpField!=null) { tcpSocketId_UIC.setText(tcpSocketIdKvpField.getStringValue()); } tcpSocketIdEnabled_UIC.setEnabled(tcpSocketIdEnabledKvpField!=null); if (tcpSocketIdEnabledKvpField!=null) { tcpSocketIdEnabled_UIC.setSelected(RciProtocol.booleanValue(tcpSocketIdEnabledKvpField.getStringValue())); socketIdContent.setContainerEnabled(tcpSocketIdEnabled_UIC.isSelected()); } tcpIdleTimeout_UIC.setEnabled(tcpIdleTimeoutKvpField!=null); tcpIdleTimeoutEnabled_UIC.setEnabled(tcpIdleTimeoutKvpField!=null); if (tcpIdleTimeoutKvpField!=null) { if (Integer.parseInt(tcpIdleTimeoutKvpField.getStringValue()) <= 0) { tcpIdleTimeoutEnabled_UIC.setSelected(false); tcpIdleTimeout_UIC.setText(""); } else { tcpIdleTimeoutEnabled_UIC.setSelected(true); tcpIdleTimeout_UIC.setText(tcpIdleTimeoutKvpField.getStringValue()); } idleTimeoutContent.setContainerEnabled(tcpIdleTimeoutEnabled_UIC.isSelected()); } tcpDropDCDEnabled_UIC.setEnabled(tcpDropDCDEnabledKvpField!=null); if (tcpDropDCDEnabledKvpField!=null) { tcpDropDCDEnabled_UIC.setSelected(RciProtocol.booleanValue(tcpDropDCDEnabledKvpField.getStringValue())); } tcpDropDSREnabled_UIC.setEnabled(tcpDropDSREnabledKvpField!=null); if (tcpDropDSREnabledKvpField!=null) { tcpDropDSREnabled_UIC.setSelected(RciProtocol.booleanValue(tcpDropDSREnabledKvpField.getStringValue())); } tcpConnectTrigger_UIC.setEnabled(tcpConnectTriggerKvpField!=null); if (tcpConnectTriggerKvpField!=null) { tcpConnectTrigger_UIC.setSelectedItem(tcpConnectTriggerKvpField.getStringValue()); } tcpConnectDestination_UIC.setEnabled(tcpConnectDestinationKvpField!=null); if (tcpConnectDestinationKvpField!=null) { tcpConnectDestination_UIC.setText(tcpConnectDestinationKvpField.getStringValue()); } tcpConnectService_UIC.setEnabled(tcpConnectServiceKvpField!=null); if (tcpConnectServiceKvpField!=null) { tcpConnectService_UIC.setSelectedItem(tcpConnectServiceKvpField.getStringValue()); } tcpConnectPort_UIC.setEnabled(tcpConnectPortKvpField!=null); if (tcpConnectPortKvpField!=null) { tcpConnectPort_UIC.setText(tcpConnectPortKvpField.getStringValue()); } tcpAutoConnectEnabled_UIC.setEnabled(tcpAutoConnectEnabledKvpField!=null); if (tcpAutoConnectEnabledKvpField!=null) { tcpAutoConnectEnabled_UIC.setSelected(RciProtocol.booleanValue(tcpAutoConnectEnabledKvpField.getStringValue())); tcpClientContent.setContainerEnabled(tcpAutoConnectEnabled_UIC.isSelected()); } // If no device data is not available, disable the entire panel if (tcpAutoConnectEnabledKvpField==null) { content.setContainerEnabled(false); } } /** * 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) { try { // Validate TCP Serial if (tcpSocketIdEnabled_UIC.isSelected()) { if (tcpSocketId_UIC.getText().length() <= 0 || tcpSocketId_UIC.getText().length() > 40) errorList.add(new ValidationError("SerialTcpSocketIdError")); } if (tcpIdleTimeoutEnabled_UIC.isSelected()) { if (!ValidationHelper.isValidUnsigned16(tcpIdleTimeout_UIC.getText()) || (Integer.parseInt(tcpIdleTimeout_UIC.getText()) <= 0)) errorList.add(new ValidationError("SerialTcpIdleTimeoutError")); } // Validate Client if (tcpAutoConnectEnabled_UIC.isSelected()) { if (!ValidationHelper.isValidIPAddress(tcpConnectDestination_UIC.getText()) || InetAddress.getByName(tcpConnectDestination_UIC.getText()).equals(InetAddress.getByName("0.0.0.0"))) errorList.add(new ValidationError("SerialTcpConnectDestinationError")); if (!ValidationHelper.isValidNetworkPort(tcpConnectPort_UIC.getText()) || (Integer.parseInt(tcpConnectPort_UIC.getText()) <= 0)) errorList.add(new ValidationError("SerialTcpConnectPortError")); } } catch (Exception e) { System.out.println("Caught Exception in SerialTcpView::validationChanges()"); e.printStackTrace(); } } /** * 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(); } }