Makefile fixup
[org.ibex.tool.git] / repo / org.ibex.tool / src / org / eclipse / jdt / internal / compiler / ConfigurableOption.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-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.*;
21
22 public class ConfigurableOption {
23         private String componentName;
24         private String optionName;
25         private int id;
26
27         private String category;
28         private String name;
29         private String description;
30         private int currentValueIndex;
31         private int defaultValueIndex;
32         private String[] possibleValues;
33
34         // special value for <possibleValues> indicating that 
35         // the <currentValueIndex> is the actual value
36         public final static String[] NoDiscreteValue = {}; 
37 /**
38  * INTERNAL USE ONLY
39  *
40  * Initialize an instance of this class according to a specific locale
41  *
42  * @param loc java.util.Locale
43  */
44 public ConfigurableOption(
45         String componentName, 
46         String optionName, 
47         Locale loc, 
48         int currentValueIndex) {
49
50         this.componentName = componentName;
51         this.optionName = optionName;
52         this.currentValueIndex = currentValueIndex;
53                 
54         ResourceBundle resource = null;
55         try {
56                 String location = componentName.substring(0, componentName.lastIndexOf('.'));
57                 resource = ResourceBundle.getBundle(location + ".options", loc); //$NON-NLS-1$
58         } catch (MissingResourceException e) {
59                 category = "Missing ressources entries for" + componentName + " options"; //$NON-NLS-1$ //$NON-NLS-2$
60                 name = "Missing ressources entries for"+ componentName + " options"; //$NON-NLS-1$ //$NON-NLS-2$
61                 description = "Missing ressources entries for" + componentName + " options"; //$NON-NLS-1$ //$NON-NLS-2$
62                 possibleValues = new String[0];
63                 id = -1;
64         }
65         if (resource == null) return;
66         try {
67                 id = Integer.parseInt(resource.getString(optionName + ".number")); //$NON-NLS-1$
68         } catch (MissingResourceException e) {
69                 id = -1;
70         } catch (NumberFormatException e) {
71                 id = -1;
72         }
73         try {
74                 category = resource.getString(optionName + ".category"); //$NON-NLS-1$
75         } catch (MissingResourceException e) {
76                 category = "Missing ressources entries for" + componentName + " options"; //$NON-NLS-1$ //$NON-NLS-2$
77         }
78         try {
79                 name = resource.getString(optionName + ".name"); //$NON-NLS-1$
80         } catch (MissingResourceException e) {
81                 name = "Missing ressources entries for"+ componentName + " options"; //$NON-NLS-1$ //$NON-NLS-2$
82         }
83         try {
84                 StringTokenizer tokenizer = new StringTokenizer(resource.getString(optionName + ".possibleValues"), "|"); //$NON-NLS-1$ //$NON-NLS-2$
85                 int numberOfValues = Integer.parseInt(tokenizer.nextToken());
86                 if(numberOfValues == -1){
87                         possibleValues = NoDiscreteValue;
88                 } else {
89                         possibleValues = new String[numberOfValues];
90                         int index = 0;
91                         while (tokenizer.hasMoreTokens()) {
92                                 possibleValues[index] = tokenizer.nextToken();
93                                 index++;
94                         }
95                 }
96         } catch (MissingResourceException e) {
97                 possibleValues = new String[0];
98         } catch (NoSuchElementException e) {
99                 possibleValues = new String[0];
100         } catch (NumberFormatException e) {
101                 possibleValues = new String[0];
102         }
103         try {
104                 description = resource.getString(optionName + ".description");  //$NON-NLS-1$
105         } catch (MissingResourceException e) {
106                 description = "Missing ressources entries for"+ componentName + " options"; //$NON-NLS-1$ //$NON-NLS-2$
107         }
108 }
109 /**
110  * Return a String that represents the localized category of the receiver.
111  * @return java.lang.String
112  */
113 public String getCategory() {
114         return category;
115 }
116 /**
117  * Return a String that identifies the component owner (typically the qualified
118  *      type name of the class which it corresponds to).
119  *
120  * e.g. "org.eclipse.jdt.internal.compiler.api.Compiler"
121  *
122  * @return java.lang.String
123  */
124 public String getComponentName() {
125         return componentName;
126 }
127 /**
128  * Answer the index (in possibleValues array) of the current setting for this
129  * particular option.
130  *
131  * In case the set of possibleValues is NoDiscreteValue, then this index is the
132  * actual value (e.g. max line lenght set to 80).
133  *
134  * @return int
135  */
136 public int getCurrentValueIndex() {
137         return currentValueIndex;
138 }
139 /**
140  * Answer the index (in possibleValues array) of the default setting for this
141  * particular option.
142  *
143  * In case the set of possibleValues is NoDiscreteValue, then this index is the
144  * actual value (e.g. max line lenght set to 80).
145  *
146  * @return int
147  */
148 public int getDefaultValueIndex() {
149         return defaultValueIndex;
150 }
151 /**
152  * Return an String that represents the localized description of the receiver.
153  *
154  * @return java.lang.String
155  */
156 public String getDescription() {
157         return description;
158 }
159 /**
160  * Internal ID which allows the configurable component to identify this particular option.
161  *
162  * @return int
163  */
164 public int getID() {
165         return id;
166 }
167 /**
168  * Return a String that represents the localized name of the receiver.
169  * @return java.lang.String
170  */
171 public String getName() {
172         return name;
173 }
174 /**
175  * Return an array of String that represents the localized possible values of the receiver.
176  * @return java.lang.String[]
177  */
178 public String[] getPossibleValues() {
179         return possibleValues;
180 }
181 /**
182  * Change the index (in possibleValues array) of the current setting for this
183  * particular option.
184  *
185  * In case the set of possibleValues is NoDiscreteValue, then this index is the
186  * actual value (e.g. max line lenght set to 80).
187  */
188 public void setValueIndex(int newIndex) {
189         currentValueIndex = newIndex;
190 }
191 public String toString() {
192         StringBuffer buffer = new StringBuffer();
193         buffer.append("Configurable option for "); //$NON-NLS-1$ 
194         buffer.append(this.componentName).append("\n"); //$NON-NLS-1$ 
195         buffer.append("- category:                      ").append(this.category).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
196         buffer.append("- name:                          ").append(this.name).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
197         /* display current value */
198         buffer.append("- current value: "); //$NON-NLS-1$ 
199         if (possibleValues == NoDiscreteValue){
200                 buffer.append(this.currentValueIndex);
201         } else {
202                 buffer.append(this.possibleValues[this.currentValueIndex]);
203         }
204         buffer.append("\n"); //$NON-NLS-1$ 
205         
206         /* display possible values */
207         if (possibleValues != NoDiscreteValue){
208                 buffer.append("- possible values:       ["); //$NON-NLS-1$ 
209                 for (int i = 0, max = possibleValues.length; i < max; i++) {
210                         if (i != 0)
211                                 buffer.append(", "); //$NON-NLS-1$ 
212                         buffer.append(possibleValues[i]);
213                 }
214                 buffer.append("]\n"); //$NON-NLS-1$ 
215                 buffer.append("- curr. val. index:      ").append(currentValueIndex).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
216         }
217         buffer.append("- description:           ").append(description).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
218         return buffer.toString();
219 }
220         /**
221          * Gets the optionName.
222          * @return Returns a String
223          */
224         public String getOptionName() {
225                 return optionName;
226         }
227 }