Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 
 package org.apache.cayenne.modeler.dialog.pref;
 
 
 
 import  org.apache.cayenne.util.Util;

A controller for stream encoding picker component.

Author(s):
Andrei Adamchik
 
 public class EncodingSelector extends CayenneController {
 
     public static final String ENCODING_PROPERTY_BINDING = "encoding";
 
     protected ObjectBinding otherEncodingBinding;
 
     protected EncodingSelectorView view;
     protected String systemEncoding;
     protected String encoding;
     protected boolean defaultEncoding;

    
Creates new EncodingPicker.
 
     public EncodingSelector(CayenneController parent) {
         this(parentnew EncodingSelectorView());
     }
 
     public EncodingSelector(CayenneController parentEncodingSelectorView view) {
         super(parent);
         this. = view;
 
         initBindings();
     }
 
     public Component getView() {
         return ;
     }
 
     protected void initBindings() {
         // init static models...
         this. = detectPlatformEncoding();
 
         Vector allEncodings = supportedEncodings();
         .getEncodingChoices().setModel(new DefaultComboBoxModel(allEncodings));
         .getDefaultEncodingLabel().setText("Default (" +  + ")");
         .getDefaultEncoding().setSelected(true);
 
         // create bindings...
         BindingBuilder builder = new BindingBuilder(
                 getApplication().getBindingFactory(),
                 this);
 
         this. = builder
                 .bindToStateChange(.getDefaultEncoding(), "defaultEncoding");
 
         this. = builder.bindToStateChange(.getOtherEncoding(),
                 "otherEncoding");
 
         this. = builder.bindToComboSelection(
                 .getEncodingChoices(), "encoding");
     }

    
Returns default encoding on the current platform.
    protected String detectPlatformEncoding() {
        // this info is private until 1.5, so have to hack it...
        return new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding();
    }

    
Returns an array of charsets that all JVMs must support cross-platform combined with a default platform charset. See Javadocs for java.nio.charset.Charset for the list of "standard" charsets.
    protected Vector supportedEncodings(String platformEncoding) {
        String[] defaultCharsets = new String[] {
                "US-ASCII""ISO-8859-1""UTF-8""UTF-16BE""UTF-16LE""UTF-16"
        };
        Vector charsets = new Vector(Arrays.asList(defaultCharsets));
        if (!charsets.contains(platformEncoding)) {
            charsets.add(platformEncoding);
        }
        Collections.sort(charsets);
        return charsets;
    }
    public void bindingUpdated(String expressionObject newValue) {
        if (.equals(expression)) {
            this. = (newValue != null) ? newValue.toString() : null;
            this. =  == null || .equals();
            .updateView();
            if () {
                .updateView();
                .getEncodingChoices().setEnabled(false);
                .getDefaultEncodingLabel().setEnabled(true);
            }
            else {
                .updateView();
                .getEncodingChoices().setEnabled(true);
                .getDefaultEncodingLabel().setEnabled(false);
            }
        }
    }
    // ===============
    //    Properties
    // ===============
    public void setEncoding(String encoding) {
        if (!Util.nullSafeEquals(this.encoding)) {
            Object old = this.;
            this. = encoding;
            firePropertyChange("encoding"oldencoding);
        }
    }
    public String getEncoding() {
        return ;
    }
    public boolean isDefaultEncoding() {
        return ;
    }
    public void setDefaultEncoding(boolean b) {
        if (b != ) {
            this. = b;
            if (b) {
                setEncoding();
                .getEncodingChoices().setEnabled(false);
                .getDefaultEncodingLabel().setEnabled(true);
            }
            else {
                .getEncodingChoices().setEnabled(true);
                .getDefaultEncodingLabel().setEnabled(false);
            }
        }
    }
    public boolean isOtherEncoding() {
        return !isDefaultEncoding();
    }
    public void setOtherEncoding(boolean b) {
        setDefaultEncoding(!b);
    }
New to GrepCode? Check out our FAQ X