KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > ConfigurableOption


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler;
12
13 /**
14  * Generic option description, which can be modified independently from the
15  * component it belongs to.
16  *
17  * @deprecated backport 1.0 internal functionality
18  */

19
20 import java.util.Locale JavaDoc;
21 import java.util.MissingResourceException JavaDoc;
22 import java.util.NoSuchElementException JavaDoc;
23 import java.util.ResourceBundle JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import org.eclipse.jdt.core.compiler.CharOperation;
27
28 public class ConfigurableOption {
29     private String JavaDoc componentName;
30     private String JavaDoc optionName;
31     private int id;
32
33     private String JavaDoc category;
34     private String JavaDoc name;
35     private String JavaDoc description;
36     private int currentValueIndex;
37     private String JavaDoc[] possibleValues;
38
39     // special value for <possibleValues> indicating that
40
// the <currentValueIndex> is the actual value
41
public final static String JavaDoc[] NoDiscreteValue = {};
42 /**
43  * INTERNAL USE ONLY
44  *
45  * Initialize an instance of this class according to a specific locale
46  *
47  * @param loc java.util.Locale
48  */

49 public ConfigurableOption(
50     String JavaDoc componentName,
51     String JavaDoc optionName,
52     Locale JavaDoc loc,
53     int currentValueIndex) {
54
55     this.componentName = componentName;
56     this.optionName = optionName;
57     this.currentValueIndex = currentValueIndex;
58         
59     ResourceBundle JavaDoc resource = null;
60     try {
61         String JavaDoc location = componentName.substring(0, componentName.lastIndexOf('.'));
62         resource = ResourceBundle.getBundle(location + ".options", loc); //$NON-NLS-1$
63
} catch (MissingResourceException JavaDoc e) {
64         category = "Missing ressources entries for" + componentName + " options"; //$NON-NLS-1$ //$NON-NLS-2$
65
name = "Missing ressources entries for"+ componentName + " options"; //$NON-NLS-1$ //$NON-NLS-2$
66
description = "Missing ressources entries for" + componentName + " options"; //$NON-NLS-1$ //$NON-NLS-2$
67
possibleValues = CharOperation.NO_STRINGS;
68         id = -1;
69     }
70     if (resource == null) return;
71     try {
72         id = Integer.parseInt(resource.getString(optionName + ".number")); //$NON-NLS-1$
73
} catch (MissingResourceException JavaDoc e) {
74         id = -1;
75     } catch (NumberFormatException JavaDoc e) {
76         id = -1;
77     }
78     try {
79         category = resource.getString(optionName + ".category"); //$NON-NLS-1$
80
} catch (MissingResourceException JavaDoc e) {
81         category = "Missing ressources entries for" + componentName + " options"; //$NON-NLS-1$ //$NON-NLS-2$
82
}
83     try {
84         name = resource.getString(optionName + ".name"); //$NON-NLS-1$
85
} catch (MissingResourceException JavaDoc e) {
86         name = "Missing ressources entries for"+ componentName + " options"; //$NON-NLS-1$ //$NON-NLS-2$
87
}
88     try {
89         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(resource.getString(optionName + ".possibleValues"), "|"); //$NON-NLS-1$ //$NON-NLS-2$
90
int numberOfValues = Integer.parseInt(tokenizer.nextToken());
91         if(numberOfValues == -1){
92             possibleValues = NoDiscreteValue;
93         } else {
94             possibleValues = new String JavaDoc[numberOfValues];
95             int index = 0;
96             while (tokenizer.hasMoreTokens()) {
97                 possibleValues[index] = tokenizer.nextToken();
98                 index++;
99             }
100         }
101     } catch (MissingResourceException JavaDoc e) {
102         possibleValues = CharOperation.NO_STRINGS;
103     } catch (NoSuchElementException JavaDoc e) {
104         possibleValues = CharOperation.NO_STRINGS;
105     } catch (NumberFormatException JavaDoc e) {
106         possibleValues = CharOperation.NO_STRINGS;
107     }
108     try {
109         description = resource.getString(optionName + ".description"); //$NON-NLS-1$
110
} catch (MissingResourceException JavaDoc e) {
111         description = "Missing ressources entries for"+ componentName + " options"; //$NON-NLS-1$ //$NON-NLS-2$
112
}
113 }
114 /**
115  * Return a String that represents the localized category of the receiver.
116  * @return java.lang.String
117  */

118 public String JavaDoc getCategory() {
119     return category;
120 }
121 /**
122  * Return a String that identifies the component owner (typically the qualified
123  * type name of the class which it corresponds to).
124  *
125  * e.g. "org.eclipse.jdt.internal.compiler.api.Compiler"
126  *
127  * @return java.lang.String
128  */

129 public String JavaDoc getComponentName() {
130     return componentName;
131 }
132 /**
133  * Answer the index (in possibleValues array) of the current setting for this
134  * particular option.
135  *
136  * In case the set of possibleValues is NoDiscreteValue, then this index is the
137  * actual value (e.g. max line lenght set to 80).
138  *
139  * @return int
140  */

141 public int getCurrentValueIndex() {
142     return currentValueIndex;
143 }
144 /**
145  * Return an String that represents the localized description of the receiver.
146  *
147  * @return java.lang.String
148  */

149 public String JavaDoc getDescription() {
150     return description;
151 }
152 /**
153  * Internal ID which allows the configurable component to identify this particular option.
154  *
155  * @return int
156  */

157 public int getID() {
158     return id;
159 }
160 /**
161  * Return a String that represents the localized name of the receiver.
162  * @return java.lang.String
163  */

164 public String JavaDoc getName() {
165     return name;
166 }
167 /**
168  * Return an array of String that represents the localized possible values of the receiver.
169  * @return java.lang.String[]
170  */

171 public String JavaDoc[] getPossibleValues() {
172     return possibleValues;
173 }
174 /**
175  * Change the index (in possibleValues array) of the current setting for this
176  * particular option.
177  *
178  * In case the set of possibleValues is NoDiscreteValue, then this index is the
179  * actual value (e.g. max line lenght set to 80).
180  */

181 public void setValueIndex(int newIndex) {
182     currentValueIndex = newIndex;
183 }
184 public String JavaDoc toString() {
185     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
186     buffer.append("Configurable option for "); //$NON-NLS-1$
187
buffer.append(this.componentName).append("\n"); //$NON-NLS-1$
188
buffer.append("- category: ").append(this.category).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
189
buffer.append("- name: ").append(this.name).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
190
/* display current value */
191     buffer.append("- current value: "); //$NON-NLS-1$
192
if (possibleValues == NoDiscreteValue){
193         buffer.append(this.currentValueIndex);
194     } else {
195         buffer.append(this.possibleValues[this.currentValueIndex]);
196     }
197     buffer.append("\n"); //$NON-NLS-1$
198

199     /* display possible values */
200     if (possibleValues != NoDiscreteValue){
201         buffer.append("- possible values: ["); //$NON-NLS-1$
202
for (int i = 0, max = possibleValues.length; i < max; i++) {
203             if (i != 0)
204                 buffer.append(", "); //$NON-NLS-1$
205
buffer.append(possibleValues[i]);
206         }
207         buffer.append("]\n"); //$NON-NLS-1$
208
buffer.append("- curr. val. index: ").append(currentValueIndex).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
209
}
210     buffer.append("- description: ").append(description).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
211
return buffer.toString();
212 }
213     /**
214      * Gets the optionName.
215      * @return Returns a String
216      */

217     public String JavaDoc getOptionName() {
218         return optionName;
219     }
220 }
221
Popular Tags