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