KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > dialog > pref > EncodingSelector


1 package org.objectstyle.cayenne.modeler.dialog.pref;
2
3 import java.awt.Component JavaDoc;
4 import java.beans.PropertyChangeListener JavaDoc;
5 import java.io.ByteArrayOutputStream JavaDoc;
6 import java.io.OutputStreamWriter JavaDoc;
7 import java.util.Arrays JavaDoc;
8 import java.util.Collections JavaDoc;
9 import java.util.Vector JavaDoc;
10
11 import javax.swing.DefaultComboBoxModel JavaDoc;
12
13 import org.objectstyle.cayenne.modeler.util.CayenneController;
14 import org.objectstyle.cayenne.swing.BindingBuilder;
15 import org.objectstyle.cayenne.swing.ObjectBinding;
16 import org.objectstyle.cayenne.util.Util;
17
18 /**
19  * A controller for stream encoding picker component.
20  *
21  * @author Andrei Adamchik
22  */

23 public class EncodingSelector extends CayenneController {
24
25     public static final String JavaDoc ENCODING_PROPERTY_BINDING = "encoding";
26
27     protected PropertyChangeListener JavaDoc encodingChangeListener;
28     protected ObjectBinding defaultEncodingBinding;
29     protected ObjectBinding otherEncodingBinding;
30     protected ObjectBinding selectedEncodingBinding;
31
32     protected EncodingSelectorView view;
33     protected String JavaDoc systemEncoding;
34     protected String JavaDoc encoding;
35     protected boolean defaultEncoding;
36
37     /**
38      * Creates new EncodingPicker.
39      */

40     public EncodingSelector(CayenneController parent) {
41         this(parent, new EncodingSelectorView());
42     }
43
44     public EncodingSelector(CayenneController parent, EncodingSelectorView view) {
45         super(parent);
46         this.view = view;
47
48         initBindings();
49     }
50
51     public Component JavaDoc getView() {
52         return view;
53     }
54
55     protected void initBindings() {
56         // init static models...
57
this.systemEncoding = detectPlatformEncoding();
58
59         Vector JavaDoc allEncodings = supportedEncodings(systemEncoding);
60         view.getEncodingChoices().setModel(new DefaultComboBoxModel JavaDoc(allEncodings));
61         view.getDefaultEncodingLabel().setText("Default (" + systemEncoding + ")");
62         view.getDefaultEncoding().setSelected(true);
63
64         // create bindings...
65
BindingBuilder builder = new BindingBuilder(
66                 getApplication().getBindingFactory(),
67                 this);
68
69         this.defaultEncodingBinding = builder
70                 .bindToStateChange(view.getDefaultEncoding(), "defaultEncoding");
71
72         this.otherEncodingBinding = builder.bindToStateChange(view.getOtherEncoding(),
73                 "otherEncoding");
74
75         this.selectedEncodingBinding = builder.bindToComboSelection(view
76                 .getEncodingChoices(), "encoding");
77     }
78
79     /**
80      * Returns default encoding on the current platform.
81      */

82     protected String JavaDoc detectPlatformEncoding() {
83         // this info is private until 1.5, so have to hack it...
84
return new OutputStreamWriter JavaDoc(new ByteArrayOutputStream JavaDoc()).getEncoding();
85     }
86
87     /**
88      * Returns an array of charsets that all JVMs must support cross-platform combined
89      * with a default platform charset. See Javadocs for java.nio.charset.Charset for the
90      * list of "standard" charsets.
91      */

92     protected Vector JavaDoc supportedEncodings(String JavaDoc platformEncoding) {
93         String JavaDoc[] defaultCharsets = new String JavaDoc[] {
94                 "US-ASCII", "ISO-8859-1", "UTF-8", "UTF-16BE", "UTF-16LE", "UTF-16"
95         };
96
97         Vector JavaDoc charsets = new Vector JavaDoc(Arrays.asList(defaultCharsets));
98         if (!charsets.contains(platformEncoding)) {
99             charsets.add(platformEncoding);
100         }
101
102         Collections.sort(charsets);
103         return charsets;
104     }
105
106     public void bindingUpdated(String JavaDoc expression, Object JavaDoc newValue) {
107         if (ENCODING_PROPERTY_BINDING.equals(expression)) {
108             this.encoding = (newValue != null) ? newValue.toString() : null;
109             this.defaultEncoding = encoding == null || encoding.equals(systemEncoding);
110
111             selectedEncodingBinding.updateView();
112             if (defaultEncoding) {
113                 defaultEncodingBinding.updateView();
114                 view.getEncodingChoices().setEnabled(false);
115                 view.getDefaultEncodingLabel().setEnabled(true);
116             }
117             else {
118                 otherEncodingBinding.updateView();
119                 view.getEncodingChoices().setEnabled(true);
120                 view.getDefaultEncodingLabel().setEnabled(false);
121             }
122         }
123     }
124
125     // ===============
126
// Properties
127
// ===============
128

129     public void setEncoding(String JavaDoc encoding) {
130         if (!Util.nullSafeEquals(this.encoding, encoding)) {
131             Object JavaDoc old = this.encoding;
132
133             this.encoding = encoding;
134             firePropertyChange("encoding", old, encoding);
135         }
136     }
137
138     public String JavaDoc getEncoding() {
139         return encoding;
140     }
141
142     public boolean isDefaultEncoding() {
143         return defaultEncoding;
144     }
145
146     public void setDefaultEncoding(boolean b) {
147         if (b != defaultEncoding) {
148             this.defaultEncoding = b;
149
150             if (b) {
151                 setEncoding(systemEncoding);
152                 view.getEncodingChoices().setEnabled(false);
153                 view.getDefaultEncodingLabel().setEnabled(true);
154             }
155             else {
156                 view.getEncodingChoices().setEnabled(true);
157                 view.getDefaultEncodingLabel().setEnabled(false);
158             }
159
160         }
161     }
162
163     public boolean isOtherEncoding() {
164         return !isDefaultEncoding();
165     }
166
167     public void setOtherEncoding(boolean b) {
168         setDefaultEncoding(!b);
169     }
170 }
Popular Tags