1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.gene.gui.widgets;
13 import java.nio.charset.Charset;
14 import java.util.SortedMap;
15 import java.util.TreeMap;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.DisposeEvent;
18 import org.eclipse.swt.events.DisposeListener;
19 import org.eclipse.swt.events.ModifyEvent;
20 import org.eclipse.swt.events.ModifyListener;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.layout.RowLayout;
24 import org.eclipse.swt.widgets.Combo;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Label;
27 import sk.uniba.euromath.editor.widgets.AbstractUserInputWidget;
28 import sk.uniba.euromath.editor.widgets.ValidityMessages;
29 import sk.uniba.euromath.gene.lang.Messages;
30 /***
31 * Allows user to set the encoding of the output document. Allows user to select
32 * encoding returned by the <code>Charset.availableCharsets()</code> function
33 * (the IANA Charset Registry).
34 * @author Martin Vysny
35 */
36 public class EncodingWidget extends AbstractUserInputWidget {
37 private final Composite c;
38 /***
39 * Creates an instance of the object.
40 * @param parent the parent composite
41 */
42 public EncodingWidget(Composite parent) {
43 this(parent, null);
44 }
45 /***
46 * Creates the query and applies the filter to displayed encodings.
47 * @param parent the parent composite.
48 * @param filter filters out the encodings. If <code>null</code> then no
49 * filter is applied.
50 */
51 public EncodingWidget(Composite parent, IEncodingFilter filter) {
52 super();
53 c = new Composite(parent, SWT.NONE);
54
55
56
57 encodings = new TreeMap<String, String>();
58 for (String charName : Charset.availableCharsets().keySet()) {
59 if ((filter == null) || (filter.accepts(charName)))
60 encodings.put(charName, null);
61 Charset c = Charset.availableCharsets().get(charName);
62 for (String alias : c.aliases()) {
63 if ((filter == null) || (filter.accepts(alias)))
64 encodings.put(alias, charName);
65 }
66 }
67
68 RowLayout shellLayout = new RowLayout();
69 shellLayout.type = SWT.HORIZONTAL;
70 shellLayout.fill = true;
71 c.setLayout(shellLayout);
72 new Label(c, SWT.NONE).setText(Messages.getString("ENCODING_LABEL"));
73 encoding = new Combo(c, SWT.READ_ONLY);
74 String[] items = encodings.keySet().toArray(new String[0]);
75 encoding.setItems(items);
76 encoding.addSelectionListener(new SelectionAdapter() {
77
78
79
80
81 @Override
82 public void widgetSelected(SelectionEvent e) {
83 refreshControls();
84 }
85 });
86 encoding.addDisposeListener(new DisposeListener() {
87 public void widgetDisposed(DisposeEvent e) {
88 getSelected();
89 }
90 });
91 label = new Label(c, SWT.NONE);
92 select("UTF-8");
93 encoding.addModifyListener(new ModifyListener() {
94 public void modifyText(ModifyEvent e) {
95 fireDataModified();
96 }
97 });
98 }
99 /***
100 * Contains all encodings allowed by the filter. If the value for given key
101 * is null then key is primary name of the encoding. If the value is
102 * non-null then key is alias of the charset name stored in the value.
103 */
104 private final SortedMap<String, String> encodings;
105 /***
106 * Encoding selector.
107 */
108 private final Combo encoding;
109 /***
110 * Hint label.
111 */
112 private final Label label;
113 /***
114 * Refresh the control values.
115 */
116 void refreshControls() {
117 int item = encoding.getSelectionIndex();
118 if (item < 0) {
119 label.setText("");
120 return;
121 }
122 String enc = encoding.getItem(item);
123 String canonical = encodings.get(enc);
124 if (canonical == null) {
125 label.setText(Messages.getString("CANONICAL_NAME"));
126 return;
127 }
128
129
130 label.setText(Messages.getString("CANONICAL_ALIAS"));
131 }
132 /***
133 * Contains selected encoding.
134 */
135 private String selected = null;
136 /***
137 * Returns currently selected encoding.
138 * @return currently selected encoding name or <code>null</code> if none
139 * is selected.
140 */
141 public final String getSelected() {
142 if (!c.isDisposed()) {
143 int item = encoding.getSelectionIndex();
144 if (item < 0) {
145 selected = null;
146 return null;
147 }
148 selected = encoding.getItem(item);
149 }
150 return selected;
151 }
152 /***
153 * Returns currently selected encoding canonical name. If an alias is
154 * selected then the returned value is the canonical name.
155 * @return the canonical name or <code>null</code> if none is selected.
156 */
157 public final String getSelectedCanonicalName() {
158 String result = getSelected();
159 if (result == null)
160 return null;
161 String canonical = encodings.get(result);
162 if (canonical == null)
163 return result;
164 return canonical;
165 }
166 /***
167 * Selects given encoding. If no encoding with such name is available then
168 * selects an empty item (item with index -1).
169 * @param encoding the encoding to select.
170 */
171 public final void select(String encoding) {
172 int index = this.encoding.indexOf(encoding);
173 this.encoding.select(index);
174 }
175 /***
176 * Returns currently selected charset.
177 * @return the charset instance or <code>null</code> if none is selected.
178 */
179 public Charset getSelectedCharset() {
180 String cname = getSelectedCanonicalName();
181 if (cname == null)
182 return null;
183 return Charset.forName(cname);
184 }
185
186
187
188
189 public Composite getComposite() {
190 return c;
191 }
192
193
194
195
196 public ValidityMessages getMessages() {
197 String sel = getSelected();
198 if (sel == null) {
199 return new ValidityMessages(Messages.getString("SELECT_ENCODING"));
200 }
201 return null;
202 }
203
204
205
206
207 public Object getState() {
208 return getSelected();
209 }
210
211
212
213
214 public Class< ? > getStateClass() {
215 return String.class;
216 }
217
218
219
220
221 public void setState(Object model) {
222 select((String) model);
223 }
224 }