1 19 20 package org.netbeans.modules.form; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 25 31 32 class CustomCodeData { 33 34 enum CodeCategory { CREATE_AND_INIT, DECLARATION } 35 private CodeCategory defaultCategory = CodeCategory.CREATE_AND_INIT; 36 37 private List <EditableBlock> initEditableBlocks = new ArrayList (); 39 private List <GuardedBlock> initGuardedBlocks = new ArrayList (); 40 41 private VariableDeclaration declaration; 43 private List <EditableBlock> declarationEditableBlocks = new ArrayList (); 44 private List <GuardedBlock> declarationGuardedBlocks = new ArrayList (); 45 46 private int highestPreference; 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 73 void addEditableBlock(String code, 74 FormProperty targetProperty, int preferenceIndex, String displayName, String hint) 76 { 77 addEditableBlock(null, code, targetProperty, preferenceIndex, displayName, hint, false, false); 78 } 79 80 void addEditableBlock(String code, 81 FormProperty targetProperty, int preferenceIndex, String displayName, String 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 code, 89 FormProperty targetProperty, int preferenceIndex, String displayName, String hint, 91 boolean pre, boolean post) 92 { 93 if (category == null) 94 category = defaultCategory; 95 96 List <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 code) { 109 addGuardedBlock(null, code, null, null, false, null, null, null); 110 } 111 112 void addGuardedBlock(String defaultCode, String customCode, String customCodeMark, boolean customized, 113 FormProperty targetProperty, String displayName, String hint) { 115 addGuardedBlock(null, defaultCode, customCode, customCodeMark, customized, 116 targetProperty, displayName, hint); 117 } 118 119 void addGuardedBlock(CodeCategory category, 120 String defaultCode, String customCode, String customCodeMark, boolean customized, 121 FormProperty targetProperty, String displayName, String hint) { 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) { int markLen = customCodeMark.length(); 137 String 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; } 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 166 private List <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 <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 184 void check() { 185 checkEditableGuardedPairs(initEditableBlocks, initGuardedBlocks); 186 checkEditableGuardedPairs(declarationEditableBlocks, declarationGuardedBlocks); 187 } 188 189 private void checkEditableGuardedPairs(List <EditableBlock> eList, List <GuardedBlock> gList) { 190 assert (eList.size() == 0 && gList.size() == 0) 191 || eList.size() == gList.size() + 1; 192 } 193 194 196 197 static class CodeEntry { 198 private String code; 199 200 private FormProperty targetProperty; 201 private String displayName; 202 private String hint; 203 private boolean pre; private boolean post; 206 private CodeEntry(String code, 207 FormProperty prop, String displayName, String 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 getCode() { 221 return code; 222 } 223 224 void setCode(String code) { 225 this.code = code; 226 } 227 228 String getName() { 229 return targetProperty.getName(); 230 } 231 232 String getDisplayName() { 233 return displayName; 234 } 235 236 String 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 toString() { 253 return displayName; 254 } 255 } 256 257 static class EditableBlock { 258 private List <CodeEntry> entries = new ArrayList (); 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 defaultCode; 278 private String customCode; 279 private int headerLength; private int footerLength; private boolean customCodeSet; 282 private CodeEntry customEntry; 284 private GuardedBlock(String defaultCode, String 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 getDefaultCode() { 297 return defaultCode; 298 } 299 300 String getCustomCode() { 301 return customCode; 302 } 303 304 void setCustomizedCode(String 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; private VariableDeclaration(boolean local, int modifiers) { 342 this.local = local; 343 this.modifiers = modifiers; 344 } 345 } 346 } 347 | Popular Tags |