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;
27  
28  import java.awt.Component;
29  import java.awt.Dimension;
30  import java.awt.GridBagConstraints;
31  import java.awt.GridBagLayout;
32  import java.awt.Insets;
33  import java.nio.charset.Charset;
34  import java.util.ArrayList;
35  import java.util.Collections;
36  import java.util.List;
37  
38  import javax.swing.Box;
39  import javax.swing.ButtonGroup;
40  import javax.swing.ComboBoxModel;
41  import javax.swing.DefaultComboBoxModel;
42  import javax.swing.JCheckBox;
43  import javax.swing.JComboBox;
44  import javax.swing.JLabel;
45  import javax.swing.JPanel;
46  import javax.swing.JRadioButton;
47  
48  import ca.uhn.hl7v2.testpanel.controller.Prefs;
49  import ca.uhn.hl7v2.testpanel.util.LineEndingsEnum;
50  
51  public class FileChooserSaveAccessory extends JPanel {
52  	private JCheckBox mySaveCommentsCheckbox;
53  	private JRadioButton myHl7LineEndingsRadio;
54  	private JRadioButton myUnixLineEndingsRadio;
55  	private JRadioButton myWindowsLineEndingsRadio;
56  	private final ButtonGroup myLineEndingsButtonGroup = new ButtonGroup();
57  	private JLabel mylabel;
58  	private Component myverticalStrut;
59  	private JComboBox myCharsetCombo;
60  
61  	/**
62  	 * Create the panel.
63  	 */
64  	public FileChooserSaveAccessory() {
65  		GridBagLayout gridBagLayout = new GridBagLayout();
66  		gridBagLayout.columnWidths = new int[] { 0, 0 };
67  		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
68  		gridBagLayout.columnWeights = new double[] { 0.0, 1.0 };
69  		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
70  		setLayout(gridBagLayout);
71  
72  		JLabel lblComments = new JLabel("Comments");
73  		GridBagConstraints gbc_lblComments = new GridBagConstraints();
74  		gbc_lblComments.insets = new Insets(0, 0, 5, 5);
75  		gbc_lblComments.gridx = 0;
76  		gbc_lblComments.gridy = 0;
77  		add(lblComments, gbc_lblComments);
78  
79  		mySaveCommentsCheckbox = new JCheckBox("Strip Comments");
80  		mySaveCommentsCheckbox.setSelected(Prefs.getInstance().getSaveStripComments());
81  		GridBagConstraints gbc_SaveCommentsCheckbox = new GridBagConstraints();
82  		gbc_SaveCommentsCheckbox.insets = new Insets(0, 0, 5, 0);
83  		gbc_SaveCommentsCheckbox.anchor = GridBagConstraints.WEST;
84  		gbc_SaveCommentsCheckbox.gridx = 1;
85  		gbc_SaveCommentsCheckbox.gridy = 0;
86  		add(mySaveCommentsCheckbox, gbc_SaveCommentsCheckbox);
87  
88  		Component verticalStrut = Box.createVerticalStrut(20);
89  		GridBagConstraints gbc_verticalStrut = new GridBagConstraints();
90  		gbc_verticalStrut.insets = new Insets(0, 0, 5, 5);
91  		gbc_verticalStrut.gridx = 0;
92  		gbc_verticalStrut.gridy = 1;
93  		add(verticalStrut, gbc_verticalStrut);
94  
95  		JLabel lblLineEndings = new JLabel("Line Endings");
96  		GridBagConstraints gbc_lblLineEndings = new GridBagConstraints();
97  		gbc_lblLineEndings.insets = new Insets(0, 0, 5, 5);
98  		gbc_lblLineEndings.gridx = 0;
99  		gbc_lblLineEndings.gridy = 2;
100 		add(lblLineEndings, gbc_lblLineEndings);
101 
102 		myHl7LineEndingsRadio = new JRadioButton("HL7 (ASCII-13)");
103 		myLineEndingsButtonGroup.add(myHl7LineEndingsRadio);
104 		GridBagConstraints gbc_Hl7LineEndingsRadio = new GridBagConstraints();
105 		gbc_Hl7LineEndingsRadio.insets = new Insets(0, 0, 5, 0);
106 		gbc_Hl7LineEndingsRadio.anchor = GridBagConstraints.WEST;
107 		gbc_Hl7LineEndingsRadio.gridx = 1;
108 		gbc_Hl7LineEndingsRadio.gridy = 2;
109 		add(myHl7LineEndingsRadio, gbc_Hl7LineEndingsRadio);
110 
111 		myUnixLineEndingsRadio = new JRadioButton("Unix (ASCII-10)");
112 		myLineEndingsButtonGroup.add(myUnixLineEndingsRadio);
113 		GridBagConstraints gbc_UnixLineEndingsRadio = new GridBagConstraints();
114 		gbc_UnixLineEndingsRadio.insets = new Insets(0, 0, 5, 0);
115 		gbc_UnixLineEndingsRadio.anchor = GridBagConstraints.WEST;
116 		gbc_UnixLineEndingsRadio.gridx = 1;
117 		gbc_UnixLineEndingsRadio.gridy = 3;
118 		add(myUnixLineEndingsRadio, gbc_UnixLineEndingsRadio);
119 
120 		myWindowsLineEndingsRadio = new JRadioButton("WIndows (ASCII-13,10)");
121 		myLineEndingsButtonGroup.add(myWindowsLineEndingsRadio);
122 		GridBagConstraints gbc_WindowsLineEndingsRadio = new GridBagConstraints();
123 		gbc_WindowsLineEndingsRadio.insets = new Insets(0, 0, 5, 0);
124 		gbc_WindowsLineEndingsRadio.anchor = GridBagConstraints.WEST;
125 		gbc_WindowsLineEndingsRadio.gridx = 1;
126 		gbc_WindowsLineEndingsRadio.gridy = 4;
127 		add(myWindowsLineEndingsRadio, gbc_WindowsLineEndingsRadio);
128 
129 		myverticalStrut = Box.createVerticalStrut(20);
130 		GridBagConstraints gbc_verticalStrut2 = new GridBagConstraints();
131 		gbc_verticalStrut2.insets = new Insets(0, 0, 5, 5);
132 		gbc_verticalStrut2.gridx = 0;
133 		gbc_verticalStrut2.gridy = 5;
134 		add(myverticalStrut, gbc_verticalStrut2);
135 
136 		mylabel = new JLabel("Charset");
137 		GridBagConstraints gbc_label = new GridBagConstraints();
138 		gbc_label.anchor = GridBagConstraints.EAST;
139 		gbc_label.insets = new Insets(0, 0, 0, 5);
140 		gbc_label.gridx = 0;
141 		gbc_label.gridy = 6;
142 		add(mylabel, gbc_label);
143 
144 		myCharsetCombo = new JComboBox();
145 		myCharsetCombo.setMaximumSize(new Dimension(180, 32767));
146 		myCharsetCombo.setPreferredSize(new Dimension(180, 27));
147 		myCharsetCombo.setMinimumSize(new Dimension(180, 27));
148 		GridBagConstraints gbc_CharsetCombo = new GridBagConstraints();
149 		gbc_CharsetCombo.anchor = GridBagConstraints.WEST;
150 		gbc_CharsetCombo.gridx = 1;
151 		gbc_CharsetCombo.gridy = 6;
152 		add(myCharsetCombo, gbc_CharsetCombo);
153 
154 		List<String> charSets = new ArrayList<String>(Charset.availableCharsets().keySet());
155 		Collections.sort(charSets);
156 		ComboBoxModel charsetModel = new DefaultComboBoxModel(charSets.toArray(new String[0]));
157 		myCharsetCombo.setModel(charsetModel);
158 		Charset openOrSaveCharset = Prefs.getInstance().getOpenOrSaveCharset();
159 		myCharsetCombo.setSelectedItem(openOrSaveCharset.name());
160 
161 		initLocal();
162 	}
163 
164 	private void initLocal() {
165 		LineEndingsEnum lineEndings = Prefs.getInstance().getSaveLineEndings();
166 		switch (lineEndings) {
167 		case HL7:
168 			myHl7LineEndingsRadio.setSelected(true);
169 			break;
170 		case UNIX:
171 			myUnixLineEndingsRadio.setSelected(true);
172 			break;
173 		case WINDOWS:
174 			myWindowsLineEndingsRadio.setSelected(true);
175 			break;
176 		}
177 
178 	}
179 
180 	/**
181 	 * Note: Saves the value as a preference
182 	 */
183 	public Charset getSelectedCharset() {
184 		Charset retVal = Charset.forName((String) myCharsetCombo.getSelectedItem());
185 		Prefs.getInstance().setOpenOrSaveCharset(retVal);
186 		return retVal;
187 	}
188 
189 	/**
190 	 * Note: Saves the value as a preference
191 	 */
192 	public LineEndingsEnum getSelectedLineEndings() {
193 		LineEndingsEnum retVal = LineEndingsEnum.WINDOWS;
194 		if (myHl7LineEndingsRadio.isSelected()) {
195 			retVal = LineEndingsEnum.HL7;
196 		} else if (myUnixLineEndingsRadio.isSelected()) {
197 			retVal = LineEndingsEnum.UNIX;
198 		}
199 		Prefs.getInstance().setSaveLineEndings(retVal);
200 		return retVal;
201 	}
202 
203 	/**
204 	 * Note: Saves the value as a preference
205 	 */
206 	public boolean isSelectedSaveStripComments() {
207 		boolean retVal = mySaveCommentsCheckbox.isSelected();
208 		Prefs.getInstance().setSaveStripComments(retVal);
209 		return retVal;
210 	}
211 
212 }