KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > CustomCodeData


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.form;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 /**
26  * This class holds code-related data of one component in a representation
27  * suitable for editing in the customizer dialog.
28  *
29  * @author Tomas Pavek
30  */

31
32 class CustomCodeData {
33
34     enum CodeCategory { CREATE_AND_INIT, DECLARATION }
35     private CodeCategory defaultCategory = CodeCategory.CREATE_AND_INIT;
36
37     // creation and initialization code
38
private List JavaDoc<EditableBlock> initEditableBlocks = new ArrayList JavaDoc();
39     private List JavaDoc<GuardedBlock> initGuardedBlocks = new ArrayList JavaDoc();
40
41     // declaration code
42
private VariableDeclaration declaration;
43     private List JavaDoc<EditableBlock> declarationEditableBlocks = new ArrayList JavaDoc();
44     private List JavaDoc<GuardedBlock> declarationGuardedBlocks = new ArrayList JavaDoc();
45
46     private int highestPreference; // used temporarily when filling in editable blocks
47

48     CustomCodeData() {
49     }
50
51     int getEditableBlockCount(CodeCategory category) {
52         return getEditableList(category).size();
53     }
54
55     EditableBlock getEditableBlock(CodeCategory category, int index) {
56         return getEditableList(category).get(index);
57     }
58
59     int getGuardedBlockCount(CodeCategory category) {
60         return getGuardedList(category).size();
61     }
62
63     GuardedBlock getGuardedBlock(CodeCategory category, int index) {
64         return getGuardedList(category).get(index);
65     }
66
67     VariableDeclaration getDeclarationData() {
68         return declaration;
69     }
70
71     // -----
72

73     void addEditableBlock(String JavaDoc code,
74                           FormProperty targetProperty, int preferenceIndex, // String propName
75
String JavaDoc displayName, String JavaDoc hint)
76     {
77         addEditableBlock(null, code, targetProperty, preferenceIndex, displayName, hint, false, false);
78     }
79
80     void addEditableBlock(String JavaDoc code,
81                           FormProperty targetProperty, int preferenceIndex, // String propName
82
String JavaDoc displayName, String JavaDoc hint,
83                           boolean pre, boolean post)
84     {
85         addEditableBlock(null, code, targetProperty, preferenceIndex, displayName, hint, pre, post);
86     }
87
88     void addEditableBlock(CodeCategory category, String JavaDoc code,
89                           FormProperty targetProperty, int preferenceIndex, // String propName
90
String JavaDoc displayName, String JavaDoc hint,
91                           boolean pre, boolean post)
92     {
93         if (category == null)
94             category = defaultCategory;
95
96         List JavaDoc<EditableBlock> editList = getEditableList(category);
97         if (editList.size() <= getGuardedList(category).size()) {
98             highestPreference = 0;
99             editList.add(new EditableBlock());
100         }
101         EditableBlock eBlock = editList.get(editList.size()-1);
102         eBlock.addEntry(new CodeEntry(code, targetProperty, displayName, hint, pre, post),
103                         preferenceIndex > highestPreference);
104         if (preferenceIndex > highestPreference)
105             highestPreference = preferenceIndex;
106     }
107
108     void addGuardedBlock(String JavaDoc code) {
109         addGuardedBlock(null, code, null, null, false, null, null, null);
110     }
111
112     void addGuardedBlock(String JavaDoc defaultCode, String JavaDoc customCode, String JavaDoc customCodeMark, boolean customized,
113                          FormProperty targetProperty, String JavaDoc displayName, String JavaDoc hint) // String propName
114
{
115         addGuardedBlock(null, defaultCode, customCode, customCodeMark, customized,
116                         targetProperty, displayName, hint);
117     }
118
119     void addGuardedBlock(CodeCategory category,
120                          String JavaDoc defaultCode, String JavaDoc customCode, String JavaDoc customCodeMark, boolean customized,
121                          FormProperty targetProperty, String JavaDoc displayName, String JavaDoc hint) // String propName
122
{
123         if (category == null)
124             category = defaultCategory;
125
126         assert getGuardedList(category).size()+1 == getEditableList(category).size();
127
128         CodeEntry customEntry = null;
129         int guardHeading = 0;
130         int guardEnding = 0;
131         if (customCode != null) {
132             int codeLen = customCode.length();
133             int first = customCode.indexOf(customCodeMark);
134             int last = customCode.lastIndexOf(customCodeMark);
135             if (first > 0 && last < codeLen-1) { // not a first or last char
136
int markLen = customCodeMark.length();
137                 String JavaDoc customizablePart = customCode.substring(first + markLen, last);
138                 customCode = customCode.substring(0, first)
139                              + customizablePart
140                              + customCode.substring(last + markLen);
141                 guardHeading = first;
142                 guardEnding = codeLen - markLen - last;
143                 customEntry = new CodeEntry(customized ? customizablePart : null,
144                                             targetProperty, displayName, hint,
145                                             false, false);
146             }
147             else customCode = null; // no customizable section
148
}
149
150         GuardedBlock gBlock = new GuardedBlock(defaultCode, customCode,
151                                                guardHeading, guardEnding, customized,
152                                                customEntry);
153         getGuardedList(category).add(gBlock);
154     }
155
156     void setDefaultCategory(CodeCategory category) {
157         defaultCategory = category;
158     }
159
160     void setDeclarationData(boolean local, int modifiers) {
161         declaration = new VariableDeclaration(local, modifiers);
162     }
163
164     // -----
165

166     private List JavaDoc<EditableBlock> getEditableList(CodeCategory category) {
167         switch (category) {
168             case CREATE_AND_INIT: return initEditableBlocks;
169             case DECLARATION: return declarationEditableBlocks;
170         }
171         return null;
172     }
173
174     private List JavaDoc<GuardedBlock> getGuardedList(CodeCategory category) {
175         switch (category) {
176             case CREATE_AND_INIT: return initGuardedBlocks;
177             case DECLARATION: return declarationGuardedBlocks;
178         }
179         return null;
180     }
181
182     // -----
183

184     void check() {
185         checkEditableGuardedPairs(initEditableBlocks, initGuardedBlocks);
186         checkEditableGuardedPairs(declarationEditableBlocks, declarationGuardedBlocks);
187     }
188
189     private void checkEditableGuardedPairs(List JavaDoc<EditableBlock> eList, List JavaDoc<GuardedBlock> gList) {
190         assert (eList.size() == 0 && gList.size() == 0)
191                || eList.size() == gList.size() + 1;
192     }
193
194     // -----
195

196     /** Holds custom code and its origin (property where it is stored).*/
197     static class CodeEntry {
198         private String JavaDoc code;
199
200         private FormProperty targetProperty;
201         private String JavaDoc displayName;
202         private String JavaDoc hint;
203         private boolean pre; // whether stored as pre-code of the property
204
private boolean post; // whether stored as post-code of the property
205

206         private CodeEntry(String JavaDoc code,
207                           FormProperty prop, // String name
208
String JavaDoc displayName, String JavaDoc hint,
209                           boolean pre, boolean post)
210         {
211             assert (!pre && !post) || pre != post;
212             this.code = code;
213             this.targetProperty = prop;
214             this.displayName = displayName;
215             this.hint = hint;
216             this.pre = pre;
217             this.post = post;
218         }
219
220         String JavaDoc getCode() {
221             return code;
222         }
223
224         void setCode(String JavaDoc code) {
225             this.code = code;
226         }
227
228         String JavaDoc getName() {
229             return targetProperty.getName();
230         }
231
232         String JavaDoc getDisplayName() {
233             return displayName;
234         }
235
236         String JavaDoc getToolTipText() {
237             return hint;
238         }
239
240         FormProperty getTargetProperty() {
241             return targetProperty;
242         }
243
244         boolean isPropertyPreInit() {
245             return pre;
246         }
247
248         boolean isPropertyPostInit() {
249             return post;
250         }
251
252         public String JavaDoc toString() {
253             return displayName;
254         }
255     }
256
257     static class EditableBlock {
258         private List JavaDoc<CodeEntry> entries = new ArrayList JavaDoc();
259         private int prefEntryIndex;
260
261         private void addEntry(CodeEntry e, boolean preferred) {
262             entries.add(e);
263             if (preferred)
264                 prefEntryIndex = entries.size() - 1;
265         }
266
267         int getPreferredEntryIndex() {
268             return prefEntryIndex;
269         }
270
271         CodeEntry[] getEntries() {
272             return entries.toArray(new CodeEntry[entries.size()]);
273         }
274     }
275
276     static class GuardedBlock {
277         private String JavaDoc defaultCode;
278         private String JavaDoc customCode;
279         private int headerLength; // number of guarded chars before customizable area
280
private int footerLength; // number of guarded chars following the customizable area
281
private boolean customCodeSet;
282         private CodeEntry customEntry; // describes where the custom code is stored
283

284         private GuardedBlock(String JavaDoc defaultCode, String JavaDoc customCode,
285                              int header, int footer, boolean customSet,
286                              CodeEntry customEntry)
287         {
288             this.defaultCode = defaultCode;
289             this.customCode = customCode;
290             this.headerLength = header;
291             this.footerLength = footer;
292             this.customCodeSet = customSet;
293             this.customEntry = customEntry;
294         }
295
296         String JavaDoc getDefaultCode() {
297             return defaultCode;
298         }
299
300         String JavaDoc getCustomCode() {
301             return customCode;
302         }
303
304         void setCustomizedCode(String JavaDoc code) {
305             customEntry.setCode(code);
306             if (code != null) {
307                 customCode = customCode.substring(0, headerLength)
308                         + code
309                         + customCode.substring(customCode.length() - footerLength);
310                 customCodeSet = true;
311             }
312             else {
313                 customCodeSet = false;
314             }
315         }
316
317         boolean isCustomizable() {
318             return customCode != null;
319         }
320
321         boolean isCustomized() {
322             return customCodeSet;
323         }
324
325         CodeEntry getCustomEntry() {
326             return customEntry;
327         }
328
329         int getHeaderLength() {
330             return headerLength;
331         }
332
333         int getFooterLength() {
334             return footerLength;
335         }
336     }
337
338     static class VariableDeclaration {
339         boolean local;
340         int modifiers; // combination of java.lang.reflect.Modifier constants
341
private VariableDeclaration(boolean local, int modifiers) {
342             this.local = local;
343             this.modifiers = modifiers;
344         }
345     }
346 }
347
Popular Tags