1 /*
2 J-Bird net/sourceforge/jbird/swing/EncodingChooser.java
3
4 Copyright 2001, 2002, 2003 Dick Repasky
5 */
6 package net.sourceforge.jbird.swing;
7
8 import java.awt.BorderLayout;
9 import java.awt.Dimension;
10 import java.text.MessageFormat;
11
12 import javax.swing.JPanel;
13 import javax.swing.JLabel;
14 import javax.swing.JList;
15 import javax.swing.JScrollPane;
16 import javax.swing.ListSelectionModel;
17
18 import net.sourceforge.jbird.io.AvailableEncodings;
19
20 // Note that if there is risk of this thing changing size while
21 // it runs inside of a JFileChooser, before doing so do:
22 // Dimension dim = getSize();
23 // setMaximumSize(dim);
24 // setPreferredSize(dim);
25
26 public class EncodingChooser extends JPanel {
27
28 //public constant int INPUT_ENCODINGS = 0;
29 //public constant int OUTPUT_ENCODINGS = 1;
30 //public constant int AVAILABLE_ENCODINGS = 2;
31 private int enctype = 2; // default for starters
32
33 private AvailableEncodings encodings;
34 private Thread tester = null;
35 private JList list;
36 private String[] enccodes;
37 private String[] encdescs;
38
39 private JLabel headerlabel = new JLabel("Text encoding:");
40 private JLabel defaultlabel;
41
42 /* *
43 * Builds a chooser set up for available encodings
44 *
45 */
46
47 public EncodingChooser () {
48 super(new BorderLayout());
49 encodings = new AvailableEncodings(true);
50 tester = new Thread(encodings);
51 tester.start();
52 buildComponents();
53 }
54
55 /* *
56 * Encoding type constant determines wheher input, output
57 * or all encoding types are available
58 *
59 */
60
61 public EncodingChooser (int enctypeconst) {
62 super(new BorderLayout());
63 setEncodingType(enctypeconst);
64 encodings = new AvailableEncodings(true);
65 tester = new Thread(encodings);
66 tester.start();
67 buildComponents();
68 }
69
70 public EncodingChooser(AvailableEncodings ava) {
71 super(new BorderLayout());
72 encodings = ava;
73 buildComponents();
74 }
75
76 public EncodingChooser(int enctypeconst, AvailableEncodings ava) {
77 super(new BorderLayout());
78 encodings = ava;
79 setEncodingType(enctypeconst);
80 buildComponents();
81 }
82
83 public void setHeaderLabel(String to) {
84 headerlabel.setText(to);
85 }
86
87 public void setDefaultEncodingLabel(String fmtstring) {
88 Object[] p = new Object[1];
89 p[0] = System.getProperty("file.encoding");
90 defaultlabel.setText(MessageFormat.format(fmtstring, p));
91 }
92
93 /* *
94 * Build the interface
95 *
96 */
97
98 private void buildComponents() {
99 add(headerlabel, BorderLayout.NORTH);
100 defaultlabel = new JLabel();
101 setDefaultEncodingLabel("Default system encoding: {0}");
102 add(defaultlabel, BorderLayout.SOUTH);
103 if (tester != null) {
104 try {
105 tester.join();
106 }
107 catch(InterruptedException e) {
108 }
109 }
110 fillEncodingData(enctype);
111 list = new JList(encdescs);
112 list.setSelectedIndex(0);
113 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
114 Dimension listdim = list.getPreferredSize();
115 JScrollPane scroller = new JScrollPane(list);
116
117 listdim.width = defaultlabel.getPreferredSize().width;
118 listdim.height *= .5;
119 scroller.setMaximumSize(listdim);
120 scroller.setPreferredSize(listdim);
121 add(scroller, BorderLayout.CENTER);
122 }
123
124 /* *
125 * Build arrays of data that contain availble encodings.
126 *
127 */
128
129 private void fillEncodingData(int type) {
130 if (type == 2) {
131 enccodes = encodings.getAvailableEncodings();
132 } else if (type == 1) {
133 enccodes = encodings.getWritableEncodings();
134 } else {
135 enccodes = encodings.getReadableEncodings();
136 }
137 encdescs = encodings.getDescriptions(enccodes);
138 int iceil = enccodes.length;
139 int i;
140 for (i = 0; i < iceil; i++) {
141 encdescs[i] = new StringBuffer(80)
142 .append(enccodes[i])
143 .append(" - ")
144 .append(encdescs[i])
145 .toString();
146 }
147 }
148
149 /* *
150 * Return the name of the selected encoding.
151 *
152 */
153
154 public String getSelectedEncoding() {
155 return enccodes[list.getSelectedIndex()];
156 }
157
158 /* *
159 * Set selected encoding to that given. Return false
160 * if the encoding does not exist in the list of
161 * available encoding, and make no change to the chooser.
162 * Return true upon success.
163 *
164 */
165
166 public boolean setSelectedEncoding(String enc) {
167 int idx = encodingIdx(enc);
168 if (idx < 0) {
169 return false;
170 }
171 list.setSelectedIndex(idx);
172 list.ensureIndexIsVisible(idx);
173 return true;
174 }
175
176 public void setEncodingType(int typeconstant) {
177 if (typeconstant >= 0 && typeconstant <= 2
178 && typeconstant != enctype) {
179 enctype = typeconstant;
180 if (list != null) {
181 rebuildList();
182 }
183 }
184 }
185
186 /* *
187 * Rebuild the list chooser if say the type of encoding
188 * changes (input vs output).
189 *
190 */
191
192 private void rebuildList() {
193 String selected = getSelectedEncoding();
194 list.setEnabled(false);
195 fillEncodingData(enctype);
196 int idx = encodingIdx(selected);
197 if (idx < 0) {
198 idx = 0;
199 }
200 list.setListData(encdescs);
201 if (! setSelectedEncoding(selected)) {
202 list.setSelectedIndex(0);
203 list.ensureIndexIsVisible(0);
204 }
205 }
206
207 /* *
208 * Return the index that is associated with an encoding.
209 * Returns -1 if argument is not in encoding list.
210 *
211 */
212
213 private int encodingIdx(String enc) {
214 int max = encdescs.length;
215 int i;
216 for (i = 0; i < max; i++) {
217 if (enc.compareTo(encdescs[i]) == 0) {
218 return i;
219 }
220 }
221 return -1;
222 }
223
224
225 }