KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > widgets > FileEncodingInput


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.common.widgets;
22
23
24 import java.nio.charset.Charset JavaDoc;
25
26 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants;
27
28
29 /**
30  * The FileEncodingInput is an OptionInput with fixed options.
31  * It is used to select the file encoding. The default
32  * value is always the platform's default encoding.
33  * The other options are the values return from
34  * {@link Charset#availableCharsets()}. No custom input is allowed.
35  *
36  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
37  * @version $Rev$, $Date$
38  */

39 public class FileEncodingInput extends OptionsInput
40 {
41
42     /**
43      * Creates a new instance of FileEncodingInput.
44      *
45      * @param initialRawValue the initial raw value
46      * @param asGroup the asGroup flag
47      */

48     public FileEncodingInput( String JavaDoc initialRawValue, boolean asGroup )
49     {
50         super( "File Encoding", getDefaultDisplayValue(), getDefaultRawValue(), getOtherDisplayValues(),
51             getOtherRawValues(), initialRawValue, asGroup, false );
52
53     }
54
55
56     /**
57      * Gets the default display value.
58      *
59      * @return the default display value
60      */

61     private static String JavaDoc getDefaultDisplayValue()
62     {
63         return getCharsetDisplayValue( getDefaultRawValue() );
64     }
65
66
67     /**
68      * Gets the default raw value, always the platform's
69      * default encoding.
70      *
71      * @return the default raw value
72      */

73     private static String JavaDoc getDefaultRawValue()
74     {
75         return BrowserCoreConstants.DEFAULT_ENCODING;
76     }
77
78
79     /**
80      * Gets the other display values.
81      *
82      * @return the other display values
83      */

84     private static String JavaDoc[] getOtherDisplayValues()
85     {
86         String JavaDoc[] otherEncodingsRawValues = getOtherRawValues();
87         String JavaDoc[] otherEncodingsDisplayValues = new String JavaDoc[otherEncodingsRawValues.length];
88         for ( int i = 0; i < otherEncodingsDisplayValues.length; i++ )
89         {
90             String JavaDoc rawValue = otherEncodingsRawValues[i];
91             otherEncodingsDisplayValues[i] = getCharsetDisplayValue( rawValue );
92         }
93         return otherEncodingsDisplayValues;
94     }
95
96
97     /**
98      * Gets the other raw values. That are all values
99      * returned from {@link Charset#availableCharsets()}.
100      *
101      * @return the other raw values
102      */

103     private static String JavaDoc[] getOtherRawValues()
104     {
105         String JavaDoc[] otherEncodingsRawValues = ( String JavaDoc[] ) Charset.availableCharsets().keySet().toArray( new String JavaDoc[0] );
106         return otherEncodingsRawValues;
107     }
108
109
110     /**
111      * Gets the charset display value.
112      *
113      * @param charsetRawValue the charset raw value
114      *
115      * @return the charset display value
116      */

117     private static String JavaDoc getCharsetDisplayValue( String JavaDoc charsetRawValue )
118     {
119         try
120         {
121             Charset JavaDoc charset = Charset.forName( charsetRawValue );
122             return charset.displayName();
123         }
124         catch ( Exception JavaDoc e )
125         {
126             return charsetRawValue;
127         }
128     }
129
130 }
131
Popular Tags