1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.commonclipse;
18
19 import java.util.StringTokenizer;
20 import java.util.regex.Pattern;
21 import java.util.regex.PatternSyntaxException;
22
23 import org.eclipse.jface.util.IPropertyChangeListener;
24 import org.eclipse.jface.util.PropertyChangeEvent;
25
26
27 /***
28 * Provide access to parsed plugin preferences.
29 * @author fgiust
30 * @version $Revision: 1.5 $ ($Author: fgiust $)
31 */
32 public final class CCPluginPreferences
33 {
34
35 /***
36 * the single instance of CCPluginPreferences.
37 */
38 private static CCPluginPreferences instance;
39
40 /***
41 * custom toStringStyle - fully qualified class name.
42 */
43 private String toStringFQCN;
44
45 /***
46 * custom toStringStyle - use.
47 */
48 private boolean toStringUseCustom;
49
50 /***
51 * custom toStringStyle - class and constant only.
52 */
53 private String toStringClassAndConstant;
54
55 /***
56 * Regular expression which will match all the excluded field names.
57 */
58 private Pattern excludePattern;
59
60 /***
61 * Preference listener. This is needed to process changes to ToStringStyle and excluded fields list avoiding to
62 * parse values at each run
63 */
64 private IPropertyChangeListener preferenceListener = new IPropertyChangeListener()
65 {
66
67 /***
68 * @see IPropertyChangeListener.propertyChange()
69 */
70 public void propertyChange(PropertyChangeEvent event)
71 {
72
73 if (event.getProperty().equals(CCPlugin.P_TOSTRING_STYLE))
74 {
75 evaluateToStringStyle();
76 }
77 else if (event.getProperty().equals(CCPlugin.P_EXCLUDE))
78 {
79 evaluateExclusionList();
80 }
81
82 }
83 };
84
85 /***
86 * private contructor. Initialize plugin preferences and set up preference listener
87 */
88 private CCPluginPreferences()
89 {
90 CCPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this.preferenceListener);
91 evaluateToStringStyle();
92 evaluateExclusionList();
93 }
94
95 /***
96 * returns the instance of plugin preferences.
97 * @return single instance of CCPluginPreferences
98 */
99 public static CCPluginPreferences getPreferences()
100 {
101 if (instance == null)
102 {
103 instance = new CCPluginPreferences();
104 }
105 return instance;
106 }
107
108 /***
109 * Gets the CCPlugin.P_TOSTRING_STYLE preference from the preference store and parse it.
110 */
111 protected void evaluateToStringStyle()
112 {
113
114 String toStringfullStyle = CCPlugin.getDefault().getPreferenceStore().getString(CCPlugin.P_TOSTRING_STYLE);
115
116 if (toStringfullStyle == null || toStringfullStyle.equals(""))
117 {
118
119 this.toStringUseCustom = false;
120 this.toStringFQCN = null;
121 this.toStringClassAndConstant = null;
122 }
123 else
124 {
125
126 this.toStringUseCustom = true;
127
128 int constantPos = toStringfullStyle.lastIndexOf(".");
129 int classPos = toStringfullStyle.substring(0, constantPos).lastIndexOf(".") + 1;
130
131 this.toStringFQCN = toStringfullStyle.substring(0, constantPos);
132 this.toStringClassAndConstant = toStringfullStyle.substring(classPos, toStringfullStyle.length());
133 }
134 }
135
136 /***
137 * Append super to hashcode method?
138 * @return <code>true</code> if appendSuper should be used
139 */
140 public boolean appendSuperToHashcode()
141 {
142 return CCPlugin.getDefault().getPreferenceStore().getBoolean(CCPlugin.P_HASHCODE_SUPER);
143 }
144
145 /***
146 * Append super to toString method?
147 * @return <code>true</code> if appendSuper should be used
148 */
149 public boolean appendSuperToToString()
150 {
151 return CCPlugin.getDefault().getPreferenceStore().getBoolean(CCPlugin.P_TOSTRING_SUPER);
152 }
153
154 /***
155 * Append super to equals method?
156 * @return <code>true</code> if appendSuper should be used
157 */
158 public boolean appendSuperToEquals()
159 {
160 return CCPlugin.getDefault().getPreferenceStore().getBoolean(CCPlugin.P_EQUALS_SUPER);
161 }
162
163 /***
164 * append super to compareTo method?
165 * @return <code>true</code> if appendSuper should be used
166 */
167 public boolean appendSuperToCompareTo()
168 {
169 return CCPlugin.getDefault().getPreferenceStore().getBoolean(CCPlugin.P_COMPARETO_SUPER);
170 }
171
172 /***
173 * Add an instance equality check to equals method?
174 * @return <code>true</code> if an instance equality check should be added
175 */
176 public boolean addInstanceCheckToEquals()
177 {
178 return CCPlugin.getDefault().getPreferenceStore().getBoolean(CCPlugin.P_EQUALS_INSTANCECHECK);
179 }
180
181 /***
182 * use javabean properties in toString() instead of fields?
183 * @return <code>true</code> if javabean properties should be used in toString
184 */
185 public boolean useJavabeanToString()
186 {
187 return CCPlugin.TOSTRINGSTYLE_BEAN.equals(CCPlugin.getDefault().getPreferenceStore().getString(
188 CCPlugin.P_TOSTRING_BEAN));
189 }
190
191 /***
192 * Overwrite existing methods without confirmation?
193 * @return <code>true</code> if methods should be overwritten without confirmation
194 */
195 public boolean dontAskOnOverwrite()
196 {
197 return CCPlugin.getDefault().getPreferenceStore().getBoolean(CCPlugin.P_DONTASKONOVERWRITE);
198 }
199
200 /***
201 * Use final parameters in generated methods?
202 * @return <code>true</code> if final parameters should be used.
203 */
204 public boolean useFinalParameters()
205 {
206 return CCPlugin.getDefault().getPreferenceStore().getBoolean(CCPlugin.P_FINALPARAMETERS);
207 }
208
209 /***
210 * Returns the list of excluded fields/properties.
211 * @return list of excluded fields/properties
212 */
213 public Pattern getExcludedFielsPattern()
214 {
215 return this.excludePattern;
216 }
217
218 /***
219 * Use a custom toString style?
220 * @return <code>true</code> if a custom ToStringStyle is selected
221 */
222 public boolean useCustomToStringStyle()
223 {
224 return this.toStringUseCustom;
225 }
226
227 /***
228 * Gets the package.class part of the custom toStringStyle.
229 * @return fully qualified class of the custom toStringStyle
230 */
231 public String getToStringStyleQualifiedClass()
232 {
233 return this.toStringFQCN;
234 }
235
236 /***
237 * Gets the class.CONSTANT part of the custom toStringStyle.
238 * @return class.CONSTANT part from the custom toStringStyle
239 */
240 public String getToStringStyleClassAndConstant()
241 {
242 return this.toStringClassAndConstant;
243 }
244
245 /***
246 * Gets the CCPlugin.P_EXCLUDE preference from the preference store and generates a Pattern from it.
247 */
248 protected void evaluateExclusionList()
249 {
250 String excludedString = CCPlugin.getDefault().getPreferenceStore().getString(CCPlugin.P_EXCLUDE);
251 this.excludePattern = generateRegExp(excludedString);
252 }
253
254 /***
255 * Generate a single regular expression used to match excluded fields.
256 * @param stringList list of fileds separate by ";"
257 * @return regular expression that matches all the given Strings
258 */
259 public static Pattern generateRegExp(String stringList)
260 {
261 if (stringList == null || stringList.length() == 0)
262 {
263
264 return Pattern.compile("^[0]$");
265 }
266 StringTokenizer st = new StringTokenizer(stringList, ";");
267 StringBuffer buffer = new StringBuffer();
268
269 while (st.hasMoreElements())
270 {
271 buffer.append("(^");
272 buffer.append(st.nextToken().replace('?', '.'));
273 buffer.append("$)");
274
275 if (st.hasMoreElements())
276 {
277 buffer.append('|');
278 }
279 }
280 try
281 {
282 return Pattern.compile(buffer.toString());
283 }
284 catch (PatternSyntaxException e)
285 {
286
287 return Pattern.compile("^[0]$");
288 }
289 }
290
291 }