Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* Contributor(s): |
28 |
* |
29 |
* The Original Software is NetBeans. The Initial Developer of the Original |
30 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
31 |
* Microsystems, Inc. All Rights Reserved. |
32 |
* |
33 |
* If you wish your version of this file to be governed by only the CDDL |
34 |
* or only the GPL Version 2, indicate your decision by adding |
35 |
* "[Contributor] elects to include this software in this distribution |
36 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
37 |
* single choice of license, a recipient has the option to distribute |
38 |
* your version of this file under either the CDDL, the GPL Version 2 or |
39 |
* to extend the choice of license to its licensees as provided above. |
40 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
41 |
* Version 2 license, then the option applies only if the new code is |
42 |
* made subject to such option by the copyright holder. |
43 |
*/ |
44 |
package org.netbeans.modules.java.ui; |
45 |
|
46 |
import java.awt.Rectangle; |
47 |
import java.util.ArrayList; |
48 |
import java.util.Collections; |
49 |
import java.util.EnumSet; |
50 |
import java.util.List; |
51 |
import java.util.prefs.Preferences; |
52 |
|
53 |
import javax.lang.model.element.ElementKind; |
54 |
import javax.lang.model.element.Modifier; |
55 |
import javax.swing.DefaultListModel; |
56 |
import javax.swing.JEditorPane; |
57 |
import javax.swing.JList; |
58 |
import javax.swing.JPanel; |
59 |
import javax.swing.text.BadLocationException; |
60 |
import javax.swing.text.Document; |
61 |
|
62 |
import com.sun.source.tree.AssignmentTree; |
63 |
import com.sun.source.tree.BlockTree; |
64 |
import com.sun.source.tree.ClassTree; |
65 |
import com.sun.source.tree.CompilationUnitTree; |
66 |
import com.sun.source.tree.ExpressionTree; |
67 |
import com.sun.source.tree.IdentifierTree; |
68 |
import com.sun.source.tree.ModifiersTree; |
69 |
import com.sun.source.tree.NewClassTree; |
70 |
import com.sun.source.tree.Tree; |
71 |
import com.sun.source.tree.TypeParameterTree; |
72 |
import com.sun.source.tree.VariableTree; |
73 |
import javax.lang.model.type.TypeKind; |
74 |
import javax.swing.event.DocumentEvent; |
75 |
import javax.swing.event.DocumentListener; |
76 |
|
77 |
import org.netbeans.api.java.source.CodeStyle; |
78 |
import org.netbeans.api.java.source.CodeStyleUtils; |
79 |
import org.netbeans.api.java.source.GeneratorUtilities; |
80 |
import org.netbeans.api.java.source.JavaSource.Phase; |
81 |
import org.netbeans.api.java.source.ModificationResult; |
82 |
import org.netbeans.api.java.source.TreeMaker; |
83 |
import org.netbeans.api.java.source.WorkingCopy; |
84 |
import org.netbeans.editor.BaseDocument; |
85 |
import org.netbeans.modules.editor.indent.api.Reformat; |
86 |
import static org.netbeans.modules.java.ui.FmtOptions.*; |
87 |
import org.netbeans.modules.java.ui.FmtOptions.CategorySupport; |
88 |
import static org.netbeans.modules.java.ui.FmtOptions.CategorySupport.OPTION_ID; |
89 |
import org.netbeans.modules.options.editor.spi.PreferencesCustomizer; |
90 |
import org.netbeans.modules.parsing.api.ResultIterator; |
91 |
import org.netbeans.modules.parsing.api.Source; |
92 |
import org.netbeans.modules.parsing.api.UserTask; |
93 |
import org.openide.cookies.SaveCookie; |
94 |
import org.openide.filesystems.FileObject; |
95 |
import org.openide.filesystems.FileUtil; |
96 |
import org.openide.loaders.DataObject; |
97 |
import org.openide.util.NbBundle; |
98 |
|
99 |
/** |
100 |
* |
101 |
* @author Ralph Benjamin Ruijs <ralphbenjamin@netbeans.org> |
102 |
*/ |
103 |
public class FmtNaming extends javax.swing.JPanel implements Runnable { |
104 |
|
105 |
/** |
106 |
* Creates new form FmtCodeGeneration |
107 |
*/ |
108 |
public FmtNaming() { |
109 |
initComponents(); |
110 |
preferLongerNamesCheckBox.putClientProperty(OPTION_ID, preferLongerNames); |
111 |
isForBooleanGettersCheckBox.putClientProperty(OPTION_ID, useIsForBooleanGetters); |
112 |
fieldPrefixField.putClientProperty(OPTION_ID, fieldNamePrefix); |
113 |
fieldSuffixField.putClientProperty(OPTION_ID, fieldNameSuffix); |
114 |
staticFieldPrefixField.putClientProperty(OPTION_ID, staticFieldNamePrefix); |
115 |
staticFieldSuffixField.putClientProperty(OPTION_ID, staticFieldNameSuffix); |
116 |
parameterPrefixField.putClientProperty(OPTION_ID, parameterNamePrefix); |
117 |
parameterSuffixField.putClientProperty(OPTION_ID, parameterNameSuffix); |
118 |
localVarPrefixField.putClientProperty(OPTION_ID, localVarNamePrefix); |
119 |
localVarSuffixField.putClientProperty(OPTION_ID, localVarNameSuffix); |
120 |
} |
121 |
|
122 |
public static PreferencesCustomizer.Factory getController() { |
123 |
return new PreferencesCustomizer.Factory() { |
124 |
public PreferencesCustomizer create(Preferences preferences) { |
125 |
NamingCategorySupport support = new NamingCategorySupport(preferences, new FmtNaming()); |
126 |
((Runnable) support.panel).run(); |
127 |
return support; |
128 |
} |
129 |
}; |
130 |
} |
131 |
|
132 |
/** |
133 |
* This method is called from within the constructor to initialize the form. |
134 |
* WARNING: Do NOT modify this code. The content of this method is always |
135 |
* regenerated by the Form Editor. |
136 |
*/ |
137 |
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
138 |
private void initComponents() { |
139 |
java.awt.GridBagConstraints gridBagConstraints; |
140 |
|
141 |
namingConventionsLabel = new javax.swing.JLabel(); |
142 |
preferLongerNamesCheckBox = new javax.swing.JCheckBox(); |
143 |
isForBooleanGettersCheckBox = new javax.swing.JCheckBox(); |
144 |
jPanel1 = new javax.swing.JPanel(); |
145 |
prefixLabel = new javax.swing.JLabel(); |
146 |
suffixLabel = new javax.swing.JLabel(); |
147 |
fieldLabel = new javax.swing.JLabel(); |
148 |
fieldPrefixField = new javax.swing.JTextField(); |
149 |
fieldSuffixField = new javax.swing.JTextField(); |
150 |
staticFieldLabel = new javax.swing.JLabel(); |
151 |
staticFieldPrefixField = new javax.swing.JTextField(); |
152 |
staticFieldSuffixField = new javax.swing.JTextField(); |
153 |
parameterLabel = new javax.swing.JLabel(); |
154 |
parameterPrefixField = new javax.swing.JTextField(); |
155 |
parameterSuffixField = new javax.swing.JTextField(); |
156 |
localVarLabel = new javax.swing.JLabel(); |
157 |
localVarSuffixField = new javax.swing.JTextField(); |
158 |
localVarPrefixField = new javax.swing.JTextField(); |
159 |
|
160 |
setName(org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_Naming")); // NOI18N |
161 |
setOpaque(false); |
162 |
|
163 |
org.openide.awt.Mnemonics.setLocalizedText(namingConventionsLabel, org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_gen_Naming")); // NOI18N |
164 |
|
165 |
org.openide.awt.Mnemonics.setLocalizedText(preferLongerNamesCheckBox, org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_gen_PreferLongerNames")); // NOI18N |
166 |
preferLongerNamesCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); |
167 |
preferLongerNamesCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0)); |
168 |
preferLongerNamesCheckBox.setOpaque(false); |
169 |
|
170 |
org.openide.awt.Mnemonics.setLocalizedText(isForBooleanGettersCheckBox, org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_gen_UseIsForBooleanGetters")); // NOI18N |
171 |
isForBooleanGettersCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); |
172 |
isForBooleanGettersCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0)); |
173 |
isForBooleanGettersCheckBox.setOpaque(false); |
174 |
|
175 |
jPanel1.setLayout(new java.awt.GridBagLayout()); |
176 |
|
177 |
org.openide.awt.Mnemonics.setLocalizedText(prefixLabel, org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_gen_Prefix")); // NOI18N |
178 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
179 |
gridBagConstraints.gridx = 1; |
180 |
gridBagConstraints.gridy = 0; |
181 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
182 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; |
183 |
gridBagConstraints.weightx = 0.5; |
184 |
gridBagConstraints.insets = new java.awt.Insets(0, 8, 4, 0); |
185 |
jPanel1.add(prefixLabel, gridBagConstraints); |
186 |
|
187 |
org.openide.awt.Mnemonics.setLocalizedText(suffixLabel, org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_gen_Suffix")); // NOI18N |
188 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
189 |
gridBagConstraints.gridx = 2; |
190 |
gridBagConstraints.gridy = 0; |
191 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
192 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; |
193 |
gridBagConstraints.weightx = 0.5; |
194 |
gridBagConstraints.insets = new java.awt.Insets(0, 8, 4, 0); |
195 |
jPanel1.add(suffixLabel, gridBagConstraints); |
196 |
|
197 |
org.openide.awt.Mnemonics.setLocalizedText(fieldLabel, org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_gen_Field")); // NOI18N |
198 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
199 |
gridBagConstraints.gridx = 0; |
200 |
gridBagConstraints.gridy = 1; |
201 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; |
202 |
jPanel1.add(fieldLabel, gridBagConstraints); |
203 |
|
204 |
fieldPrefixField.setColumns(5); |
205 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
206 |
gridBagConstraints.gridx = 1; |
207 |
gridBagConstraints.gridy = 1; |
208 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
209 |
gridBagConstraints.weightx = 0.5; |
210 |
gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0); |
211 |
jPanel1.add(fieldPrefixField, gridBagConstraints); |
212 |
|
213 |
fieldSuffixField.setColumns(5); |
214 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
215 |
gridBagConstraints.gridx = 2; |
216 |
gridBagConstraints.gridy = 1; |
217 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
218 |
gridBagConstraints.weightx = 0.5; |
219 |
gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0); |
220 |
jPanel1.add(fieldSuffixField, gridBagConstraints); |
221 |
|
222 |
org.openide.awt.Mnemonics.setLocalizedText(staticFieldLabel, org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_gen_StaticField")); // NOI18N |
223 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
224 |
gridBagConstraints.gridx = 0; |
225 |
gridBagConstraints.gridy = 2; |
226 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; |
227 |
jPanel1.add(staticFieldLabel, gridBagConstraints); |
228 |
|
229 |
staticFieldPrefixField.setColumns(5); |
230 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
231 |
gridBagConstraints.gridx = 1; |
232 |
gridBagConstraints.gridy = 2; |
233 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
234 |
gridBagConstraints.weightx = 0.5; |
235 |
gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0); |
236 |
jPanel1.add(staticFieldPrefixField, gridBagConstraints); |
237 |
|
238 |
staticFieldSuffixField.setColumns(5); |
239 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
240 |
gridBagConstraints.gridx = 2; |
241 |
gridBagConstraints.gridy = 2; |
242 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
243 |
gridBagConstraints.weightx = 0.5; |
244 |
gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0); |
245 |
jPanel1.add(staticFieldSuffixField, gridBagConstraints); |
246 |
|
247 |
org.openide.awt.Mnemonics.setLocalizedText(parameterLabel, org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_gen_Parameter")); // NOI18N |
248 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
249 |
gridBagConstraints.gridx = 0; |
250 |
gridBagConstraints.gridy = 3; |
251 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; |
252 |
jPanel1.add(parameterLabel, gridBagConstraints); |
253 |
|
254 |
parameterPrefixField.setColumns(5); |
255 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
256 |
gridBagConstraints.gridx = 1; |
257 |
gridBagConstraints.gridy = 3; |
258 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
259 |
gridBagConstraints.weightx = 0.5; |
260 |
gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0); |
261 |
jPanel1.add(parameterPrefixField, gridBagConstraints); |
262 |
|
263 |
parameterSuffixField.setColumns(5); |
264 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
265 |
gridBagConstraints.gridx = 2; |
266 |
gridBagConstraints.gridy = 3; |
267 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
268 |
gridBagConstraints.weightx = 0.5; |
269 |
gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0); |
270 |
jPanel1.add(parameterSuffixField, gridBagConstraints); |
271 |
|
272 |
org.openide.awt.Mnemonics.setLocalizedText(localVarLabel, org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_gen_LocalVariable")); // NOI18N |
273 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
274 |
gridBagConstraints.gridx = 0; |
275 |
gridBagConstraints.gridy = 4; |
276 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; |
277 |
gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0); |
278 |
jPanel1.add(localVarLabel, gridBagConstraints); |
279 |
|
280 |
localVarSuffixField.setColumns(5); |
281 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
282 |
gridBagConstraints.gridx = 2; |
283 |
gridBagConstraints.gridy = 4; |
284 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
285 |
gridBagConstraints.weightx = 0.5; |
286 |
gridBagConstraints.insets = new java.awt.Insets(4, 8, 0, 0); |
287 |
jPanel1.add(localVarSuffixField, gridBagConstraints); |
288 |
|
289 |
localVarPrefixField.setColumns(5); |
290 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
291 |
gridBagConstraints.gridx = 1; |
292 |
gridBagConstraints.gridy = 4; |
293 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
294 |
gridBagConstraints.weightx = 0.5; |
295 |
gridBagConstraints.insets = new java.awt.Insets(4, 8, 0, 0); |
296 |
jPanel1.add(localVarPrefixField, gridBagConstraints); |
297 |
|
298 |
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); |
299 |
this.setLayout(layout); |
300 |
layout.setHorizontalGroup( |
301 |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
302 |
.addGroup(layout.createSequentialGroup() |
303 |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
304 |
.addComponent(namingConventionsLabel) |
305 |
.addGroup(layout.createSequentialGroup() |
306 |
.addContainerGap() |
307 |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
308 |
.addComponent(isForBooleanGettersCheckBox) |
309 |
.addComponent(preferLongerNamesCheckBox) |
310 |
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)))) |
311 |
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) |
312 |
); |
313 |
layout.setVerticalGroup( |
314 |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
315 |
.addGroup(layout.createSequentialGroup() |
316 |
.addComponent(namingConventionsLabel) |
317 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
318 |
.addComponent(preferLongerNamesCheckBox) |
319 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
320 |
.addComponent(isForBooleanGettersCheckBox) |
321 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
322 |
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) |
323 |
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) |
324 |
); |
325 |
}// </editor-fold>//GEN-END:initComponents |
326 |
// Variables declaration - do not modify//GEN-BEGIN:variables |
327 |
private javax.swing.JLabel fieldLabel; |
328 |
private javax.swing.JTextField fieldPrefixField; |
329 |
private javax.swing.JTextField fieldSuffixField; |
330 |
private javax.swing.JCheckBox isForBooleanGettersCheckBox; |
331 |
private javax.swing.JPanel jPanel1; |
332 |
private javax.swing.JLabel localVarLabel; |
333 |
private javax.swing.JTextField localVarPrefixField; |
334 |
private javax.swing.JTextField localVarSuffixField; |
335 |
private javax.swing.JLabel namingConventionsLabel; |
336 |
private javax.swing.JLabel parameterLabel; |
337 |
private javax.swing.JTextField parameterPrefixField; |
338 |
private javax.swing.JTextField parameterSuffixField; |
339 |
private javax.swing.JCheckBox preferLongerNamesCheckBox; |
340 |
private javax.swing.JLabel prefixLabel; |
341 |
private javax.swing.JLabel staticFieldLabel; |
342 |
private javax.swing.JTextField staticFieldPrefixField; |
343 |
private javax.swing.JTextField staticFieldSuffixField; |
344 |
private javax.swing.JLabel suffixLabel; |
345 |
// End of variables declaration//GEN-END:variables |
346 |
|
347 |
@Override |
348 |
public void run() { |
349 |
|
350 |
} |
351 |
|
352 |
private static final class NamingCategorySupport extends CategorySupport { |
353 |
|
354 |
private Source source = null; |
355 |
|
356 |
private NamingCategorySupport(Preferences preferences, JPanel panel) { |
357 |
super(preferences, "naming", panel, NbBundle.getMessage(FmtNaming.class, "SAMPLE_Naming"), //NOI18N |
358 |
new String[]{FmtOptions.blankLinesBeforeFields, "1"}); //NOI18N |
359 |
} |
360 |
|
361 |
@Override |
362 |
public void refreshPreview() { |
363 |
final JEditorPane jep = (JEditorPane) getPreviewComponent(); |
364 |
try { |
365 |
Class.forName(CodeStyle.class.getName(), true, CodeStyle.class.getClassLoader()); |
366 |
} catch (ClassNotFoundException cnfe) { |
367 |
// ignore |
368 |
} |
369 |
|
370 |
final CodeStyle codeStyle = codeStyleProducer.create(previewPrefs); |
371 |
jep.setIgnoreRepaint(true); |
372 |
try { |
373 |
if (source == null) { |
374 |
FileObject fo = FileUtil.createMemoryFileSystem().getRoot().createData("org.netbeans.samples.ClassA", "java"); //NOI18N |
375 |
source = Source.create(fo); |
376 |
} |
377 |
final Document doc = source.getDocument(true); |
378 |
if (doc.getLength() > 0) { |
379 |
doc.remove(0, doc.getLength()); |
380 |
} |
381 |
doc.insertString(0, previewText, null); |
382 |
doc.putProperty(CodeStyle.class, codeStyle); |
383 |
jep.setDocument(doc); |
384 |
ModificationResult result = ModificationResult.runModificationTask(Collections.singleton(source), new UserTask() { |
385 |
@Override |
386 |
public void run(ResultIterator resultIterator) throws Exception { |
387 |
WorkingCopy copy = WorkingCopy.get(resultIterator.getParserResult()); |
388 |
copy.toPhase(Phase.RESOLVED); |
389 |
TreeMaker tm = copy.getTreeMaker(); |
390 |
GeneratorUtilities gu = GeneratorUtilities.get(copy); |
391 |
CompilationUnitTree cut = copy.getCompilationUnit(); |
392 |
ClassTree ct = (ClassTree) cut.getTypeDecls().get(0); |
393 |
List<Tree> members = new ArrayList<Tree>(); |
394 |
String name = CodeStyleUtils.addPrefixSuffix("name", |
395 |
codeStyle.getFieldNamePrefix(), |
396 |
codeStyle.getFieldNameSuffix()); |
397 |
VariableTree field = tm.Variable(tm.Modifiers(Collections.singleton(Modifier.PRIVATE)), name, tm.Type("String"), null); |
398 |
members.add(field); |
399 |
String cond = CodeStyleUtils.addPrefixSuffix("cond", |
400 |
codeStyle.getFieldNamePrefix(), |
401 |
codeStyle.getFieldNameSuffix()); |
402 |
VariableTree booleanField = tm.Variable(tm.Modifiers(Collections.singleton(Modifier.PRIVATE)), cond, tm.PrimitiveType(TypeKind.BOOLEAN), null); |
403 |
members.add(booleanField); |
404 |
members.add(gu.createConstructor(ct, Collections.singletonList(field))); |
405 |
members.add(gu.createGetter(field)); |
406 |
members.add(gu.createSetter(ct, field)); |
407 |
members.add(gu.createGetter(booleanField)); |
408 |
members.add(gu.createSetter(ct, booleanField)); |
409 |
ModifiersTree mods = tm.Modifiers(EnumSet.of(Modifier.PRIVATE, Modifier.STATIC)); |
410 |
ClassTree nested = tm.Class(mods, "Nested", Collections.<TypeParameterTree>emptyList(), null, Collections.<Tree>emptyList(), Collections.<Tree>emptyList()); //NOI18N |
411 |
members.add(nested); |
412 |
IdentifierTree nestedId = tm.Identifier("Nested"); //NOI18N |
413 |
String instance = CodeStyleUtils.addPrefixSuffix("instance", |
414 |
codeStyle.getStaticFieldNamePrefix(), |
415 |
codeStyle.getStaticFieldNameSuffix()); |
416 |
VariableTree staticField = tm.Variable(mods, instance, nestedId, null); //NOI18N |
417 |
members.add(staticField); |
418 |
members.add(gu.createGetter(staticField)); |
419 |
members.add(gu.createSetter(ct, staticField)); |
420 |
ClassTree newCT = gu.insertClassMembers(ct, members); |
421 |
copy.rewrite(ct, newCT); |
422 |
} |
423 |
}); |
424 |
result.commit(); |
425 |
final Reformat reformat = Reformat.get(doc); |
426 |
reformat.lock(); |
427 |
try { |
428 |
if (doc instanceof BaseDocument) { |
429 |
((BaseDocument) doc).runAtomicAsUser(new Runnable() { |
430 |
@Override |
431 |
public void run() { |
432 |
try { |
433 |
reformat.reformat(0, doc.getLength()); |
434 |
} catch (BadLocationException ble) { |
435 |
} |
436 |
} |
437 |
}); |
438 |
} else { |
439 |
reformat.reformat(0, doc.getLength()); |
440 |
} |
441 |
} finally { |
442 |
reformat.unlock(); |
443 |
} |
444 |
DataObject dataObject = DataObject.find(source.getFileObject()); |
445 |
SaveCookie sc = dataObject.getLookup().lookup(SaveCookie.class); |
446 |
if (sc != null) { |
447 |
sc.save(); |
448 |
} |
449 |
} catch (Exception ex) { |
450 |
} |
451 |
jep.setIgnoreRepaint(false); |
452 |
jep.scrollRectToVisible(new Rectangle(0, 0, 10, 10)); |
453 |
jep.repaint(100); |
454 |
} |
455 |
|
456 |
private static class Element { |
457 |
|
458 |
private boolean isStatic; |
459 |
private ElementKind kind; |
460 |
|
461 |
@Override |
462 |
public String toString() { |
463 |
return (isStatic ? NbBundle.getMessage(FmtNaming.class, "VAL_gen_STATIC") + " " : "") //NOI18N |
464 |
+ NbBundle.getMessage(FmtNaming.class, "VAL_gen_" + kind.name()); //NOI18N |
465 |
} |
466 |
} |
467 |
|
468 |
private static class Visibility { |
469 |
|
470 |
private String kind; |
471 |
|
472 |
@Override |
473 |
public String toString() { |
474 |
return NbBundle.getMessage(FmtNaming.class, "VAL_gen_" + kind); //NOI18N |
475 |
} |
476 |
} |
477 |
} |
478 |
} |