View Javadoc
1   /**
2    * The contents of this file are subject to the Mozilla Public License Version 1.1
3    * (the "License"); you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at http://www.mozilla.org/MPL/
5    * Software distributed under the License is distributed on an "AS IS" basis,
6    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
7    * specific language governing rights and limitations under the License.
8    *
9    * The Original Code is ""  Description:
10   * ""
11   *
12   * The Initial Developer of the Original Code is University Health Network. Copyright (C)
13   * 2001.  All Rights Reserved.
14   *
15   * Contributor(s): ______________________________________.
16   *
17   * Alternatively, the contents of this file may be used under the terms of the
18   * GNU General Public License (the  "GPL"), in which case the provisions of the GPL are
19   * applicable instead of those above.  If you wish to allow use of your version of this
20   * file only under the terms of the GPL and not to allow others to use your version
21   * of this file under the MPL, indicate your decision by deleting  the provisions above
22   * and replace  them with the notice and other provisions required by the GPL License.
23   * If you do not delete the provisions above, a recipient may use your version of
24   * this file under either the MPL or the GPL.
25   */
26  package ca.uhn.hl7v2.testpanel.ui.conn;
27  
28  import java.awt.Color;
29  import java.awt.GridBagConstraints;
30  import java.awt.GridBagLayout;
31  import java.awt.Insets;
32  import java.awt.event.ActionEvent;
33  import java.awt.event.ActionListener;
34  import java.beans.PropertyChangeEvent;
35  import java.beans.PropertyChangeListener;
36  
37  import javax.swing.JButton;
38  import javax.swing.JCheckBox;
39  import javax.swing.JLabel;
40  import javax.swing.JPanel;
41  import javax.swing.JTextField;
42  import javax.swing.border.EtchedBorder;
43  import javax.swing.border.TitledBorder;
44  import javax.swing.event.DocumentEvent;
45  
46  import org.apache.commons.lang.StringUtils;
47  
48  import ca.uhn.hl7v2.testpanel.model.conn.AbstractConnection;
49  import ca.uhn.hl7v2.testpanel.model.conn.AbstractConnection.StatusEnum;
50  import ca.uhn.hl7v2.testpanel.model.conn.InboundConnection;
51  import ca.uhn.hl7v2.testpanel.model.conn.OutboundConnection;
52  import ca.uhn.hl7v2.testpanel.ui.IDestroyable;
53  import ca.uhn.hl7v2.testpanel.util.SimpleDocumentListener;
54  
55  public class Hl7ConnectionPanelHeader extends JPanel implements IDestroyable {
56  	private JTextField myNameBox;
57  	private AbstractConnection myConnection;
58  	private PropertyChangeListener myNamePropertyChangeListener;
59  	private PropertyChangeListener myStatusPropertyChangeListener;
60  	private boolean myIgnoreNameChanges;
61  	private JButton myStartButton;
62  	private JButton myStopButton;
63  	private PropertyChangeListener myStatusLinePropertyChangeListener;
64  	private JLabel myStatusLabel;
65  	private JCheckBox myRememberAsCheckBox;
66  
67  
68  	public void setConnection(AbstractConnection theConnection) {
69  		myConnection = theConnection;
70  
71  		if (myConnection instanceof InboundConnection) {
72  			setLabelText("Incoming Message Receiver");
73  		} else {
74  			setLabelText("Outgoing Message Sender");
75  		}
76  		
77  		myNameBox.setText(theConnection.getName());
78  
79  		myNamePropertyChangeListener = new PropertyChangeListener() {
80  			public void propertyChange(PropertyChangeEvent theEvt) {
81  				if (!myIgnoreNameChanges) {
82  					myNameBox.setText(myConnection.getName());
83  				}
84  			}
85  		};
86  		myConnection.addPropertyChangeListener(OutboundConnection.NAME_PROPERTY, myNamePropertyChangeListener);
87  
88  		myStatusPropertyChangeListener = new PropertyChangeListener() {
89  
90  			public void propertyChange(PropertyChangeEvent theEvt) {
91  				updateStatus();
92  			}
93  		};
94  		myConnection.addPropertyChangeListener(OutboundConnection.STATUS_PROPERTY, myStatusPropertyChangeListener);
95  
96  		myStatusLinePropertyChangeListener = new PropertyChangeListener() {
97  
98  			public void propertyChange(PropertyChangeEvent theEvt) {
99  				updateStatus();
100 			}
101 		};
102 		myConnection.addPropertyChangeListener(OutboundConnection.STATUS_LINE_PROPERTY, myStatusLinePropertyChangeListener);
103 
104 		updateStatus();
105 		
106 		updateRememberAsUi();
107 		
108 	}
109 
110 	private void updateRememberAsUi() {
111 		boolean newState = myConnection.isPersistent();
112 		myRememberAsCheckBox.setSelected(newState);
113 		myNameBox.setEnabled(newState);
114 	}
115 
116 	private void updateStatus() {
117 
118 		if (myConnection.getStatus() == StatusEnum.FAILED) {
119 			myStatusLabel.setForeground(Color.red);
120 		} else {
121 			myStatusLabel.setForeground(Color.black);
122 		}
123 		if (StringUtils.isNotBlank(myConnection.getStatusLine())) {
124 			myStatusLabel.setText(myConnection.getStatusLine());
125 		} else {
126 			myStatusLabel.setText("");
127 		}
128 		
129 		switch (myConnection.getStatus()) {
130 		case FAILED:
131 		case STOPPED:
132 			myStartButton.setEnabled(true);
133 			myStopButton.setEnabled(false);
134 			break;
135 		case STARTED:
136 		case TRYING_TO_START:
137 			myStartButton.setEnabled(false);
138 			myStopButton.setEnabled(true);
139 			break;
140 		}
141 		
142 	}
143 
144 	/**
145 	 * Don't allow the user to start and stop this interface
146 	 */
147 	public void markDisableStartingAndStopping() {
148 		myStartButton.setEnabled(false);
149 		myStopButton.setEnabled(false);
150 	}
151 	
152 	public void setLabelText(String theText) {
153 		assert StringUtils.isNotBlank(theText);
154 		
155 		TitledBorder titledBorder = (TitledBorder)getBorder();
156 		titledBorder.setTitle(theText);
157 	}
158 	
159 	/**
160 	 * Create the panel.
161 	 */
162 	public Hl7ConnectionPanelHeader() {
163 		setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Outbound Message Sender", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(0, 0, 0)));
164 		GridBagLayout gridBagLayout = new GridBagLayout();
165 		gridBagLayout.columnWidths = new int[] { 138, 0 };
166 		gridBagLayout.rowHeights = new int[] { 0, 0, 0 };
167 		gridBagLayout.columnWeights = new double[] { 0.0, 1.0 };
168 		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
169 		setLayout(gridBagLayout);
170 
171 		myNameBox = new JTextField();
172 		myNameBox.getDocument().addDocumentListener(new SimpleDocumentListener() {
173 			@Override
174 			public void update(DocumentEvent theE) {
175 				myIgnoreNameChanges = true;
176 				try {
177 					myConnection.setNameExplicitly(myNameBox.getText());
178 				} finally {
179 					myIgnoreNameChanges = false;
180 				}
181 			}
182 		});
183 		
184 		myRememberAsCheckBox = new JCheckBox("Save With Name:");
185 		myRememberAsCheckBox.setToolTipText("If checked, this connection will be saved for the next time you start TestPanel");
186 		myRememberAsCheckBox.addActionListener(new ActionListener() {
187 			public void actionPerformed(ActionEvent e) {
188 				myConnection.setPersistent(myRememberAsCheckBox.isSelected());
189 				updateRememberAsUi();
190 			}
191 		});
192 		GridBagConstraints gbc_RememberAsCheckBox = new GridBagConstraints();
193 		gbc_RememberAsCheckBox.insets = new Insets(0, 0, 5, 5);
194 		gbc_RememberAsCheckBox.gridx = 0;
195 		gbc_RememberAsCheckBox.gridy = 0;
196 		add(myRememberAsCheckBox, gbc_RememberAsCheckBox);
197 		GridBagConstraints gbc_NameBox = new GridBagConstraints();
198 		gbc_NameBox.insets = new Insets(0, 0, 5, 0);
199 		gbc_NameBox.fill = GridBagConstraints.HORIZONTAL;
200 		gbc_NameBox.gridx = 1;
201 		gbc_NameBox.gridy = 0;
202 		add(myNameBox, gbc_NameBox);
203 		myNameBox.setColumns(10);
204 
205 		JPanel panel_5 = new JPanel();
206 		GridBagConstraints gbc_panel_5 = new GridBagConstraints();
207 		gbc_panel_5.anchor = GridBagConstraints.WEST;
208 		gbc_panel_5.fill = GridBagConstraints.VERTICAL;
209 		gbc_panel_5.gridx = 1;
210 		gbc_panel_5.gridy = 1;
211 		add(panel_5, gbc_panel_5);
212 
213 		myStartButton = new JButton("Start");
214 		myStartButton.addActionListener(new ActionListener() {
215 			public void actionPerformed(ActionEvent e) {
216 				myConnection.start();
217 			}
218 		});
219 		panel_5.add(myStartButton);
220 
221 		myStopButton = new JButton("Stop");
222 		myStopButton.addActionListener(new ActionListener() {
223 			public void actionPerformed(ActionEvent e) {
224 				myConnection.stop();
225 			}
226 		});
227 		panel_5.add(myStopButton);
228 		
229 		myStatusLabel = new JLabel("New label");
230 		panel_5.add(myStatusLabel);
231 
232 	}
233 
234 	public void destroy() {
235 		myConnection.removePropertyChangeListener(OutboundConnection.NAME_PROPERTY, myNamePropertyChangeListener);
236 		myConnection.removePropertyChangeListener(OutboundConnection.STATUS_PROPERTY, myStatusPropertyChangeListener);
237 		myConnection.removePropertyChangeListener(OutboundConnection.STATUS_LINE_PROPERTY, myStatusLinePropertyChangeListener);
238 	}
239 
240 }