001    /**
002    The contents of this file are subject to the Mozilla Public License Version 1.1 
003    (the "License"); you may not use this file except in compliance with the License. 
004    You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005    Software distributed under the License is distributed on an "AS IS" basis, 
006    WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007    specific language governing rights and limitations under the License. 
008    
009    The Original Code is "ConnectionDialog.java".  Description: 
010    "A dialog box for opening a new Connection (used with TestPanel)." 
011    
012    The Initial Developer of the Original Code is University Health Network. Copyright (C) 
013    2001.  All Rights Reserved. 
014    
015    Contributor(s): ______________________________________. 
016    
017    Alternatively, the contents of this file may be used under the terms of the 
018    GNU General Public License (the  ?GPL?), in which case the provisions of the GPL are 
019    applicable instead of those above.  If you wish to allow use of your version of this 
020    file only under the terms of the GPL and not to allow others to use your version 
021    of this file under the MPL, indicate your decision by deleting  the provisions above 
022    and replace  them with the notice and other provisions required by the GPL License.  
023    If you do not delete the provisions above, a recipient may use your version of 
024    this file under either the MPL or the GPL. 
025    
026    */
027    
028    package ca.uhn.hl7v2.app;
029    
030    import javax.swing.*;
031    import java.awt.event.*;
032    import java.awt.*;
033    
034    /**
035     * A dialog box for opening a new Connection (used with TestPanel). 
036     * @author Bryan Tripp
037     */
038    public class ConnectionDialog extends JDialog {
039    
040        private JTextField port;
041        private JTextField inPort;
042        private JTextField outPort;
043        private JTextField host;
044        private JRadioButton onePort;
045        private TestPanel testPanel;
046        
047        /** Creates a new instance of ConnectionDialog */
048        public ConnectionDialog(TestPanel testPanel) {
049            super();
050            this.testPanel = testPanel;
051            initUI();
052        }
053    
054        /** Initialize UI */
055        private void initUI() {
056            Box box = new Box(BoxLayout.Y_AXIS);
057            getContentPane().add(box);
058            
059            host = new JTextField(20);        
060            JPanel hostPanel = new JPanel();
061            hostPanel.add(new JLabel(" Host:  "));
062            hostPanel.add(host);
063            box.add(hostPanel);
064            
065            JRadioButton twoPort = new JRadioButton();
066            JPanel twoPortPanel = new JPanel();
067            ((FlowLayout) twoPortPanel.getLayout()).setAlignment(FlowLayout.LEFT);
068            twoPortPanel.add(twoPort);
069            twoPortPanel.add(new JLabel(" Separate Inbound & Outbound Ports "));
070            box.add(twoPortPanel);
071            
072            JPanel twoPortPanel2 = new JPanel();
073            ((FlowLayout) twoPortPanel2.getLayout()).setAlignment(FlowLayout.LEFT);
074            twoPortPanel2.add(new JLabel("     Inbound: "), FlowLayout.LEFT);
075            inPort = new JTextField(5);
076            twoPortPanel2.add(inPort);
077            twoPortPanel2.add(new JLabel(" Outbound: "));
078            outPort = new JTextField(5);
079            twoPortPanel2.add(outPort);
080            box.add(twoPortPanel2);
081    
082            onePort = new JRadioButton();
083            onePort.setSelected(true);
084            port = new JTextField(5);
085            JPanel onePortPanel = new JPanel();
086            ((FlowLayout) onePortPanel.getLayout()).setAlignment(FlowLayout.LEFT);
087            onePortPanel.add(onePort, FlowLayout.LEFT);
088            onePortPanel.add(new JLabel(" Single Port "));        
089            onePortPanel.add(port);
090            box.add(onePortPanel);
091            
092            JPanel buttonPanel = new JPanel();
093            JButton OK = new JButton("  OK  ");
094            JButton cancel = new JButton(" Cancel ");
095            buttonPanel.add(OK);
096            buttonPanel.add(cancel);
097            box.add(buttonPanel);
098            
099            ButtonGroup portSelect = new ButtonGroup();
100            portSelect.add(twoPort);
101            portSelect.add(onePort);
102            
103            OK.addActionListener(new ActionListener() {
104                public void actionPerformed(ActionEvent e) {
105                    connect();
106                }
107            });
108            
109            cancel.addActionListener(new ActionListener() {
110                public void actionPerformed(ActionEvent e) {
111                    close();
112                }
113            });
114            
115            pack();
116            show();
117        }
118        
119        private void connect() {
120            try {
121                if (onePort.isSelected()) {
122                    testPanel.connect(host.getText(), Integer.parseInt(port.getText()));
123                } else {
124                    testPanel.connect(host.getText(), Integer.parseInt(inPort.getText()), Integer.parseInt(outPort.getText()));
125                }
126                close();
127            } catch (NumberFormatException e) {
128                JOptionPane.showMessageDialog(this, "Invalid port number", "", JOptionPane.ERROR_MESSAGE);
129            } catch (Exception e) {
130                JOptionPane.showMessageDialog(this, e.getMessage(), e.getClass().getName(), JOptionPane.ERROR_MESSAGE);            
131            }
132        }
133        
134        private void close() {
135            this.dispose();
136        }
137        
138    
139        public static void main(String[] args) {             
140            try {
141                ConnectionDialog d = new ConnectionDialog(new TestPanel());
142            } catch (Exception e) {
143                e.printStackTrace();
144            }
145        }
146    }