View Javadoc

1   package ca.uhn.hl7v2.testpanel.ui.editor;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Component;
5   import java.awt.Dimension;
6   import java.awt.EventQueue;
7   import java.awt.GridBagConstraints;
8   import java.awt.GridBagLayout;
9   import java.awt.Insets;
10  import java.awt.event.KeyAdapter;
11  import java.awt.event.KeyEvent;
12  import java.awt.event.WindowEvent;
13  import java.awt.event.WindowFocusListener;
14  import java.text.NumberFormat;
15  
16  import javax.swing.Box;
17  import javax.swing.JButton;
18  import javax.swing.JDialog;
19  import javax.swing.JFormattedTextField;
20  import javax.swing.JLabel;
21  import javax.swing.JPanel;
22  import javax.swing.event.DocumentEvent;
23  
24  import ca.uhn.hl7v2.testpanel.model.msg.Hl7V2MessageCollection;
25  import ca.uhn.hl7v2.testpanel.ui.HoverButtonMouseAdapter;
26  import ca.uhn.hl7v2.testpanel.util.SimpleDocumentListener;
27  
28  public class SendOptionsPopupDialog extends JDialog {
29  
30  	private final JPanel mycontentPanel = new JPanel();
31  	private JButton mySendOptionsButton;
32  	private HoverButtonMouseAdapter mySendOptionsHoverAdaptor;
33  	private Hl7V2MessageCollection myMessage;
34  
35  	/**
36  	 * Create the dialog.
37  	 */
38  	public SendOptionsPopupDialog(Hl7V2MessageEditorPanel theParent, Hl7V2MessageCollection theMessage, JButton theSendOptionsButton, HoverButtonMouseAdapter theSendOptionsHoverAdaptor) {
39  		super(theParent.getWindow());
40  
41  		mySendOptionsButton = theSendOptionsButton;
42  		mySendOptionsHoverAdaptor = theSendOptionsHoverAdaptor;
43  		myMessage = theMessage;
44  
45  		mySendOptionsButton.setBorderPainted(true);
46  		mySendOptionsHoverAdaptor.setEnabled(false);
47  
48  		setUndecorated(true);
49  		setBounds(100, 100, 234, 114);
50  		getContentPane().setLayout(new BorderLayout());
51  		mycontentPanel.setBorder(null);
52  		getContentPane().add(mycontentPanel, BorderLayout.CENTER);
53  		GridBagLayout gbl_contentPanel = new GridBagLayout();
54  		gbl_contentPanel.columnWidths = new int[] { 0, 0, 0 };
55  		gbl_contentPanel.rowHeights = new int[] { 0, 0, 0, 0 };
56  		gbl_contentPanel.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
57  		gbl_contentPanel.rowWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
58  		mycontentPanel.setLayout(gbl_contentPanel);
59  		{
60  			JLabel lblSendOptions = new JLabel("Send Options");
61  			GridBagConstraints gbc_lblSendOptions = new GridBagConstraints();
62  			gbc_lblSendOptions.gridwidth = 2;
63  			gbc_lblSendOptions.insets = new Insets(0, 0, 5, 0);
64  			gbc_lblSendOptions.gridx = 0;
65  			gbc_lblSendOptions.gridy = 0;
66  			mycontentPanel.add(lblSendOptions, gbc_lblSendOptions);
67  		}
68  		{
69  			Component verticalStrut = Box.createVerticalStrut(20);
70  			verticalStrut.setPreferredSize(new Dimension(0, 10));
71  			verticalStrut.setMinimumSize(new Dimension(0, 10));
72  			verticalStrut.setMaximumSize(new Dimension(32767, 10));
73  			GridBagConstraints gbc_verticalStrut = new GridBagConstraints();
74  			gbc_verticalStrut.insets = new Insets(0, 0, 5, 5);
75  			gbc_verticalStrut.gridx = 0;
76  			gbc_verticalStrut.gridy = 1;
77  			mycontentPanel.add(verticalStrut, gbc_verticalStrut);
78  		}
79  		{
80  			JLabel lblNumberOfTimes = new JLabel("Send # Times:");
81  			GridBagConstraints gbc_lblNumberOfTimes = new GridBagConstraints();
82  			gbc_lblNumberOfTimes.insets = new Insets(0, 0, 0, 5);
83  			gbc_lblNumberOfTimes.anchor = GridBagConstraints.EAST;
84  			gbc_lblNumberOfTimes.gridx = 0;
85  			gbc_lblNumberOfTimes.gridy = 2;
86  			mycontentPanel.add(lblNumberOfTimes, gbc_lblNumberOfTimes);
87  		}
88  		{
89  			final JFormattedTextField sendTimes = new JFormattedTextField(NumberFormat.getIntegerInstance());
90  			sendTimes.setText(Integer.toString(myMessage.getSendNumberOfTimes()));
91  			GridBagConstraints gbc_mySendTimes = new GridBagConstraints();
92  			gbc_mySendTimes.fill = GridBagConstraints.HORIZONTAL;
93  			gbc_mySendTimes.gridx = 1;
94  			gbc_mySendTimes.gridy = 2;
95  			mycontentPanel.add(sendTimes, gbc_mySendTimes);
96  			sendTimes.addKeyListener(new KeyAdapter() {
97  				@Override
98  				public void keyPressed(KeyEvent theE) {
99  					if (theE.getKeyCode() == KeyEvent.VK_ENTER || theE.getKeyCode() == KeyEvent.VK_ESCAPE) {
100 						doHide();
101 					}
102 				}
103 			});
104 			sendTimes.getDocument().addDocumentListener(new SimpleDocumentListener() {
105 				@Override
106 				public void update(DocumentEvent theE) {
107 					String newTValue = sendTimes.getText();
108 					int newValue = 1;
109 					try {
110 						newValue = Integer.parseInt(newTValue);
111 					} catch (Exception e) {
112 						// ignore
113 					}
114 					if (newValue < 1) {
115 						newValue = 1;
116 					}
117 					myMessage.setSendNumberOfTimes(newValue);
118 					final String repTValue = Integer.toString(newValue);
119 					if (!newTValue.equals(sendTimes.getText())) {
120 						EventQueue.invokeLater(new Runnable() {
121 							public void run() {
122 								sendTimes.setText(repTValue);
123 							}
124 						});
125 					}
126 				}
127 			});
128 		}
129 
130 		addWindowFocusListener(new WindowFocusListener() {
131 
132 			@Override
133 			public void windowGainedFocus(WindowEvent theE) {
134 				ourLog.info("Focus gained");
135 			}
136 
137 			@Override
138 			public void windowLostFocus(WindowEvent theE) {
139 				ourLog.info("Focus lost");
140 				SendOptionsPopupDialog.this.setVisible(false);
141 				mySendOptionsButton.setBorderPainted(false);
142 				mySendOptionsHoverAdaptor.setEnabled(true);
143 			}
144 		});
145 
146 	}
147 
148 	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(SendOptionsPopupDialog.class);
149 
150 	public void doHide() {
151 		setVisible(false);
152 		mySendOptionsButton.setBorderPainted(false);
153 		mySendOptionsHoverAdaptor.setEnabled(false);
154 	}
155 
156 }