Refactory: remove unused classes, imports, fields and methods.
[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
29         private String optionName;
30
31         private int id;
32
33         private String category;
34
35         private String name;
36
37         private String description;
38
39         private int currentValueIndex;
40
41         //private int defaultValueIndex;
42
43         private String[] possibleValues;
44
45         // special value for <possibleValues> indicating that
46         // the <currentValueIndex> is the actual value
47         public final static String[] NoDiscreteValue = {};
48
49         /**
50          * INTERNAL USE ONLY
51          * 
52          * Initialize an instance of this class according to a specific locale
53          * 
54          * @param loc
55          *            java.util.Locale
56          */
57         public ConfigurableOption(String componentName, String optionName,
58                         Locale loc, int currentValueIndex) {
59
60                 this.componentName = componentName;
61                 this.optionName = optionName;
62                 this.currentValueIndex = currentValueIndex;
63
64                 ResourceBundle resource = null;
65                 try {
66                         String location = componentName.substring(0, componentName
67                                         .lastIndexOf('.'));
68                         resource = ResourceBundle.getBundle(location + ".Options", loc); //$NON-NLS-1$
69                 } catch (MissingResourceException e) {
70                         category = "Missing ressources entries for" + componentName + " options"; //$NON-NLS-1$ //$NON-NLS-2$
71                         name = "Missing ressources entries for" + componentName + " options"; //$NON-NLS-1$ //$NON-NLS-2$
72                         description = "Missing ressources entries for" + componentName + " options"; //$NON-NLS-1$ //$NON-NLS-2$
73                         possibleValues = new String[0];
74                         id = -1;
75                 }
76                 if (resource == null)
77                         return;
78                 try {
79                         id = Integer.parseInt(resource.getString(optionName + ".number")); //$NON-NLS-1$
80                 } catch (MissingResourceException e) {
81                         id = -1;
82                 } catch (NumberFormatException e) {
83                         id = -1;
84                 }
85                 try {
86                         category = resource.getString(optionName + ".category"); //$NON-NLS-1$
87                 } catch (MissingResourceException e) {
88                         category = "Missing ressources entries for" + componentName + " options"; //$NON-NLS-1$ //$NON-NLS-2$
89                 }
90                 try {
91                         name = resource.getString(optionName + ".name"); //$NON-NLS-1$
92                 } catch (MissingResourceException e) {
93                         name = "Missing ressources entries for" + componentName + " options"; //$NON-NLS-1$ //$NON-NLS-2$
94                 }
95                 try {
96                         StringTokenizer tokenizer = new StringTokenizer(resource
97                                         .getString(optionName + ".possibleValues"), "|"); //$NON-NLS-1$ //$NON-NLS-2$
98                         int numberOfValues = Integer.parseInt(tokenizer.nextToken());
99                         if (numberOfValues == -1) {
100                                 possibleValues = NoDiscreteValue;
101                         } else {
102                                 possibleValues = new String[numberOfValues];
103                                 int index = 0;
104                                 while (tokenizer.hasMoreTokens()) {
105                                         possibleValues[index] = tokenizer.nextToken();
106                                         index++;
107                                 }
108                         }
109                 } catch (MissingResourceException e) {
110                         possibleValues = new String[0];
111                 } catch (NoSuchElementException e) {
112                         possibleValues = new String[0];
113                 } catch (NumberFormatException e) {
114                         possibleValues = new String[0];
115                 }
116                 try {
117                         description = resource.getString(optionName + ".description"); //$NON-NLS-1$
118                 } catch (MissingResourceException e) {
119                         description = "Missing ressources entries for" + componentName + " options"; //$NON-NLS-1$ //$NON-NLS-2$
120                 }
121         }
122
123         /**
124          * Return a String that represents the localized category of the receiver.
125          * 
126          * @return java.lang.String
127          */
128         public String getCategory() {
129                 return category;
130         }
131
132         /**
133          * Return a String that identifies the component owner (typically the
134          * qualified type name of the class which it corresponds to).
135          * 
136          * e.g. "org.phpeclipse.phpdt.internal.compiler.api.Compiler"
137          * 
138          * @return java.lang.String
139          */
140         public String getComponentName() {
141                 return componentName;
142         }
143
144         /**
145          * Answer the index (in possibleValues array) of the current setting for
146          * this particular option.
147          * 
148          * In case the set of possibleValues is NoDiscreteValue, then this index is
149          * the actual value (e.g. max line lenght set to 80).
150          * 
151          * @return int
152          */
153         public int getCurrentValueIndex() {
154                 return currentValueIndex;
155         }
156
157         /**
158          * Answer the index (in possibleValues array) of the default setting for
159          * this particular option.
160          * 
161          * In case the set of possibleValues is NoDiscreteValue, then this index is
162          * the actual value (e.g. max line lenght set to 80).
163          * 
164          * @return int
165          */
166 //      public int getDefaultValueIndex() {
167 //              return defaultValueIndex;
168 //      }
169
170         /**
171          * Return an String that represents the localized description of the
172          * receiver.
173          * 
174          * @return java.lang.String
175          */
176         public String getDescription() {
177                 return description;
178         }
179
180         /**
181          * Internal ID which allows the configurable component to identify this
182          * particular option.
183          * 
184          * @return int
185          */
186         public int getID() {
187                 return id;
188         }
189
190         /**
191          * Return a String that represents the localized name of the receiver.
192          * 
193          * @return java.lang.String
194          */
195         public String getName() {
196                 return name;
197         }
198
199         /**
200          * Return an array of String that represents the localized possible values
201          * of the receiver.
202          * 
203          * @return java.lang.String[]
204          */
205 //      public String[] getPossibleValues() {
206 //              return possibleValues;
207 //      }
208
209         /**
210          * Change the index (in possibleValues array) of the current setting for
211          * this particular option.
212          * 
213          * In case the set of possibleValues is NoDiscreteValue, then this index is
214          * the actual value (e.g. max line lenght set to 80).
215          * 
216          * @return int
217          */
218 //      public void setValueIndex(int newIndex) {
219 //              currentValueIndex = newIndex;
220 //      }
221
222         public String toString() {
223                 StringBuffer buffer = new StringBuffer();
224                 buffer.append("Configurable option for "); //$NON-NLS-1$ 
225                 buffer.append(this.componentName).append("\n"); //$NON-NLS-1$ 
226                 buffer.append("- category:                      ").append(this.category).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
227                 buffer.append("- name:                          ").append(this.name).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
228                 /* display current value */
229                 buffer.append("- current value: "); //$NON-NLS-1$ 
230                 if (possibleValues == NoDiscreteValue) {
231                         buffer.append(this.currentValueIndex);
232                 } else {
233                         buffer.append(this.possibleValues[this.currentValueIndex]);
234                 }
235                 buffer.append("\n"); //$NON-NLS-1$ 
236
237                 /* display possible values */
238                 if (possibleValues != NoDiscreteValue) {
239                         buffer.append("- possible values:       ["); //$NON-NLS-1$ 
240                         for (int i = 0, max = possibleValues.length; i < max; i++) {
241                                 if (i != 0)
242                                         buffer.append(", "); //$NON-NLS-1$ 
243                                 buffer.append(possibleValues[i]);
244                         }
245                         buffer.append("]\n"); //$NON-NLS-1$ 
246                         buffer
247                                         .append("- curr. val. index:    ").append(currentValueIndex).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
248                 }
249                 buffer.append("- description:           ").append(description).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
250                 return buffer.toString();
251         }
252
253         /**
254          * Gets the optionName.
255          * 
256          * @return Returns a String
257          */
258         public String getOptionName() {
259                 return optionName;
260         }
261 }