1 package org.objectstyle.cayenne.modeler.dialog.pref; 2 3 import java.awt.Component ; 4 import java.beans.PropertyChangeListener ; 5 import java.io.ByteArrayOutputStream ; 6 import java.io.OutputStreamWriter ; 7 import java.util.Arrays ; 8 import java.util.Collections ; 9 import java.util.Vector ; 10 11 import javax.swing.DefaultComboBoxModel ; 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 23 public class EncodingSelector extends CayenneController { 24 25 public static final String ENCODING_PROPERTY_BINDING = "encoding"; 26 27 protected PropertyChangeListener encodingChangeListener; 28 protected ObjectBinding defaultEncodingBinding; 29 protected ObjectBinding otherEncodingBinding; 30 protected ObjectBinding selectedEncodingBinding; 31 32 protected EncodingSelectorView view; 33 protected String systemEncoding; 34 protected String encoding; 35 protected boolean defaultEncoding; 36 37 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 getView() { 52 return view; 53 } 54 55 protected void initBindings() { 56 this.systemEncoding = detectPlatformEncoding(); 58 59 Vector allEncodings = supportedEncodings(systemEncoding); 60 view.getEncodingChoices().setModel(new DefaultComboBoxModel (allEncodings)); 61 view.getDefaultEncodingLabel().setText("Default (" + systemEncoding + ")"); 62 view.getDefaultEncoding().setSelected(true); 63 64 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 82 protected String detectPlatformEncoding() { 83 return new OutputStreamWriter (new ByteArrayOutputStream ()).getEncoding(); 85 } 86 87 92 protected Vector supportedEncodings(String platformEncoding) { 93 String [] defaultCharsets = new String [] { 94 "US-ASCII", "ISO-8859-1", "UTF-8", "UTF-16BE", "UTF-16LE", "UTF-16" 95 }; 96 97 Vector charsets = new Vector (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 expression, Object 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 129 public void setEncoding(String encoding) { 130 if (!Util.nullSafeEquals(this.encoding, encoding)) { 131 Object old = this.encoding; 132 133 this.encoding = encoding; 134 firePropertyChange("encoding", old, encoding); 135 } 136 } 137 138 public String 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 |