1 19 20 package org.netbeans.modules.web.core.syntax.completion; 21 22 import java.util.*; 23 import java.beans.*; 24 import java.util.logging.Logger ; 25 import javax.swing.text.JTextComponent ; 26 import javax.swing.ImageIcon ; 27 import org.netbeans.editor.ext.CompletionQuery; 28 import org.netbeans.modules.editor.NbEditorUtilities; 29 import org.netbeans.modules.web.jsps.parserapi.PageInfo; 30 import org.openide.filesystems.FileObject; 31 import org.openide.filesystems.FileStateInvalidException; 32 import org.openide.filesystems.FileSystem; 33 import org.netbeans.modules.web.core.syntax.*; 34 import org.netbeans.modules.web.core.syntax.completion.JavaJSPCompletionProvider.CompletionQueryDelegatedToJava; 35 import org.netbeans.modules.web.core.syntax.completion.JspCompletionQuery.JspCompletionResult; 36 import org.netbeans.spi.editor.completion.CompletionItem; 37 import org.netbeans.spi.editor.completion.CompletionProvider; 38 39 40 44 public class AttrSupports extends Object { 45 private static final Logger logger = Logger.getLogger(AttrSupports.class.getName()); 46 public static class ScopeSupport extends AttributeValueSupport.Default { 47 48 public ScopeSupport(boolean tag, String longName, String attrName) { 49 super(tag, longName, attrName); 50 } 51 52 protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) { 53 ArrayList list = new ArrayList(); 54 list.add("application"); list.add("page"); list.add("request"); list.add("session"); return list; 59 } 60 61 } 62 63 public static class RootVersionSupport extends AttributeValueSupport.Default { 64 65 public RootVersionSupport(boolean tag, String longName, String attrName) { 66 super(tag, longName, attrName); 67 } 68 69 protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) { 70 ArrayList list = new ArrayList(); 71 list.add("1.2"); list.add("2.0"); return list; 74 } 75 76 } 77 78 public static class PluginTypeSupport extends AttributeValueSupport.Default { 79 80 public PluginTypeSupport(boolean tag, String longName, String attrName) { 81 super(tag, longName, attrName); 82 } 83 84 protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) { 85 ArrayList list = new ArrayList(); 86 list.add("bean"); list.add("applet"); return list; 89 } 90 91 } 92 93 public static class VariableScopeSupport extends AttributeValueSupport.Default { 94 95 public VariableScopeSupport(boolean tag, String longName, String attrName) { 96 super(tag, longName, attrName); 97 } 98 99 protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) { 100 ArrayList list = new ArrayList(); 101 list.add("AT_BEGIN"); list.add("AT_END"); list.add("NESTED"); return list; 105 } 106 107 } 108 109 public static class YesNoTrueFalseSupport extends AttributeValueSupport.Default { 110 111 public YesNoTrueFalseSupport(boolean tag, String longName, String attrName) { 112 super(tag, longName, attrName); 113 } 114 115 protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) { 116 ArrayList list = new ArrayList(); 117 list.add("false"); list.add("no"); list.add("true"); list.add("yes"); return list; 122 } 123 124 } 125 126 129 public static class ClassNameSupport extends AttributeValueSupport.Default { 130 131 public ClassNameSupport(boolean tag, String longName, String attrName) { 132 super(tag, longName, attrName); 133 } 134 135 protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) { 136 return new ArrayList(); 137 } 138 139 protected String getFakedClassBody(String prefix){ 140 return "class Foo extends " + prefix; } 142 143 144 public CompletionQuery.Result getResult(JTextComponent component, int offset, 145 JspSyntaxSupport sup, SyntaxElement.TagDirective item, String valuePart) { 146 147 String fakedClassBody = getFakedClassBody(valuePart); 148 int shiftedOffset = fakedClassBody.length(); 149 150 logger.fine("JSP CC: delegating CC query to java file:\n" + fakedClassBody.substring(0, shiftedOffset) 152 + "|" + fakedClassBody.substring(shiftedOffset) + "\n"); 154 CompletionQueryDelegatedToJava delegate = new CompletionQueryDelegatedToJava( 155 offset, shiftedOffset, CompletionProvider.COMPLETION_QUERY_TYPE); 156 157 delegate.create(component.getDocument(), fakedClassBody); 158 List<? extends CompletionItem> items = delegate.getCompletionItems(); 159 160 JspCompletionResult result = new JspCompletionResult(component, null, items, offset, valuePart.length(), -1); 161 return result; 163 } 164 165 168 private List completionResults(int offset, JspSyntaxSupport sup, SyntaxElement.TagDirective item, String valuePart) { 169 return null; 170 } 171 172 } 173 174 177 public static class PackageListSupport extends ClassNameSupport { 178 179 public PackageListSupport(boolean tag, String longName, String attrName) { 180 super(tag, longName, attrName); 181 } 182 183 @Override protected String getFakedClassBody(String prefix){ 184 int commaPos = prefix.lastIndexOf(","); 185 186 if (commaPos > -1){ 187 prefix = prefix.substring(commaPos + 1); 188 } 189 190 return "import " + prefix; } 192 } 193 194 public static class GetSetPropertyName extends AttributeValueSupport.Default { 195 196 public GetSetPropertyName(boolean tag, String longName, String attrName) { 197 super(tag, longName, attrName); 198 } 199 200 protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) { 201 ArrayList list = new ArrayList(); 202 PageInfo.BeanData[] beanData = sup.getBeanData(); 203 if(beanData != null) { 204 for (int i = 0; i < beanData.length; i++) { 205 list.add(beanData[i].getId()); 206 } 207 } 208 return list; 209 } 210 211 } 212 213 214 public static abstract class GetSetPropertyProperty extends AttributeValueSupport.Default { 215 216 public GetSetPropertyProperty(boolean tag, String longName, String attrName) { 217 super(tag, longName, attrName); 218 } 219 220 protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item, boolean setter) { 221 ArrayList list = new ArrayList(); 222 String namePropertyValue = (String )item.getAttributes().get("name"); if (namePropertyValue != null) { 224 String className = null; 225 PageInfo.BeanData[] beanData = sup.getBeanData(); 226 for (int i = 0; i < beanData.length; i++) { 227 if (beanData[i] == null || beanData[i].getId() == null) 228 continue; 229 230 if (beanData[i].getId().equals(namePropertyValue)) { 231 className = beanData[i].getClassName(); 232 break; 233 } 234 } 235 236 if (className != null) { 237 try { 238 FileObject fileObject = NbEditorUtilities.getDataObject( sup.getDocument()).getPrimaryFile(); 239 ClassLoader cld = JspUtils.getModuleClassLoader( sup.getDocument(), fileObject); 240 Class beanClass = Class.forName(className, false, cld); 241 Introspector.flushFromCaches(beanClass); 242 BeanInfo benInfo = Introspector.getBeanInfo(beanClass); 243 PropertyDescriptor[] properties = benInfo.getPropertyDescriptors(); 244 for (int j = 0; j < properties.length; j++) { 245 if (setter && (properties[j].getWriteMethod() != null)) 246 list.add(properties[j].getName()); 247 if (!setter && (properties[j].getReadMethod() != null) && !properties[j].getName().equals("class")) list.add(properties[j].getName()); 249 } 250 } catch (ClassNotFoundException e) { 251 } catch (IntrospectionException e) { 253 } 255 } 256 } 257 return list; 258 } 259 } 260 261 public static class GetPropertyProperty extends GetSetPropertyProperty { 262 263 public GetPropertyProperty() { 264 super(true, "jsp:getProperty", "property"); } 266 267 public List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) { 268 return possibleValues(sup, item, false); 269 } 270 271 } 272 273 public static class SetPropertyProperty extends GetSetPropertyProperty { 274 275 public SetPropertyProperty() { 276 super(true, "jsp:setProperty", "property"); } 278 279 protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) { 280 List list = possibleValues(sup, item, true); 281 list.add(0, "*"); return list; 283 } 284 285 } 286 287 public static class TaglibURI extends AttributeValueSupport.Default { 288 289 public TaglibURI() { 290 super(false, "taglib", "uri"); } 292 293 protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) { 294 List list = new ArrayList(); 295 Map map = sup.getTagLibraryMappings(); 296 if (map != null) { 297 Iterator iterator = map.keySet().iterator(); 298 while(iterator.hasNext()) { 299 String s = (String )iterator.next(); 300 list.add(s); 301 } 302 } 303 Collections.sort(list); 305 return list; 306 } 307 308 } 309 310 public static class TaglibTagdir extends AttributeValueSupport.Default { 311 312 public TaglibTagdir() { 313 super(false, "taglib", "tagdir"); } 315 316 protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) { 317 List l = new ArrayList(); 318 FileObject orig = sup.getFileObject(); 319 FileObject documentBase = JspUtils.guessWebModuleRoot(sup.getDocument(), orig); 320 if (documentBase != null) { 321 FileObject webInfTags = JspUtils.findRelativeFileObject(documentBase, "WEB-INF/tags"); 322 if (webInfTags != null) { 323 if (isValidTagDir(webInfTags)) { 325 l.add(JspUtils.findRelativeContextPath(documentBase, webInfTags)); 326 } 327 Enumeration en = webInfTags.getFolders(true); 329 while (en.hasMoreElements()) { 330 FileObject subF = (FileObject)en.nextElement(); 331 if (isValidTagDir(subF)) { 332 l.add(JspUtils.findRelativeContextPath(documentBase, subF)); 333 } 334 } 335 } 336 } 337 Collections.sort(l); 339 return l; 340 } 341 342 private boolean isValidTagDir(FileObject subF) { 343 return subF.getChildren(false).hasMoreElements(); 345 } 346 347 } 348 349 350 351 public static class FilenameSupport extends AttributeValueSupport.Default { 352 static final ImageIcon PACKAGE_ICON = 353 new ImageIcon (org.openide.util.Utilities.loadImage("org/openide/loaders/defaultFolder.gif")); 355 356 private int itemOffset; 357 358 private int itemLength; 359 360 public FilenameSupport(boolean tag, String longName, String attrName) { 361 super(tag, longName, attrName); 362 } 363 364 protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) { 365 return new ArrayList(); 366 } 367 368 369 public CompletionQuery.Result getResult(JTextComponent component, int offset, 370 JspSyntaxSupport sup, SyntaxElement.TagDirective item, String valuePart) { 371 List res = completionResults(offset, sup, item, valuePart); 372 return new JspCompletionQuery.JspCompletionResult(component, 373 completionTitle(), res, 374 itemOffset, itemLength, -1); 375 } 376 377 380 private List completionResults(int offset, JspSyntaxSupport sup, SyntaxElement.TagDirective item, String valuePart) { 381 List res = new ArrayList(); 382 String path = ""; String fileNamePart = valuePart; 384 int lastSlash = valuePart.lastIndexOf('/'); 385 if (lastSlash == 0) { 386 path = "/"; fileNamePart = valuePart.substring(1); 388 } else if (lastSlash > 0) { path = valuePart.substring(0, lastSlash); 390 fileNamePart = (lastSlash == valuePart.length())? "": valuePart.substring(lastSlash+1); } 392 393 try { 394 FileObject orig = sup.getFileObject(); 395 FileObject documentBase = JspUtils.guessWebModuleRoot(sup.getDocument(), orig); 396 String ctxPath = JspUtils.resolveRelativeURL("/"+orig.getPath(), path); if (path.startsWith("/")) 400 ctxPath = documentBase.getPath() + path; 401 else 402 ctxPath = ctxPath.substring(1); 403 404 405 FileSystem fs = orig.getFileSystem(); 406 407 FileObject folder = fs.findResource(ctxPath); 408 if (folder != null) { 409 res = files(folder, fileNamePart, sup); 410 if (!folder.equals(documentBase) && !path.startsWith("/") && (path.length() == 0 || (path.lastIndexOf("../")+3 == path.length()))){ res.add(0, new JspCompletionItem.FileAttributeValue("../", java.awt.Color.BLUE, PACKAGE_ICON)); } 414 } 415 } catch (FileStateInvalidException ex) { 416 } catch (IllegalArgumentException ex) { 418 } 420 itemOffset = offset - valuePart.length() + lastSlash + 1; itemLength = fileNamePart.length(); 422 423 424 Iterator i = res.iterator(); 426 while(i.hasNext()) { 427 JspCompletionItem.JspResultItem resultItem = (JspCompletionItem.JspResultItem)i.next(); 428 resultItem.setSubstituteOffset(itemOffset); 429 } 430 431 return res; 432 } 433 434 private List files(FileObject folder, String prefix, JspSyntaxSupport sup) { 435 ArrayList res = new ArrayList(); 436 TreeMap resFolders = new TreeMap(); 437 TreeMap resFiles = new TreeMap(); 438 439 Enumeration files = folder.getChildren(false); 440 while (files.hasMoreElements()) { 441 FileObject file = (FileObject)files.nextElement(); 442 String fname = file.getNameExt(); 443 if (fname.startsWith(prefix) && !"cvs".equalsIgnoreCase(fname)) { 444 445 if (file.isFolder()) 446 resFolders.put(file.getNameExt(), new JspCompletionItem.FileAttributeValue(file.getNameExt() + "/", java.awt.Color.BLUE, PACKAGE_ICON)); 447 else{ 448 java.awt.Image icon = JspUtils.getIcon(sup.getDocument(), file); 449 if (icon != null) 450 resFiles.put(file.getNameExt(), new JspCompletionItem.FileAttributeValue(file.getNameExt(), java.awt.Color.BLACK, new javax.swing.ImageIcon (icon))); 451 else 452 resFiles.put(file.getNameExt(), new JspCompletionItem.FileAttributeValue(file.getNameExt(), java.awt.Color.BLACK)); 453 } 454 } 455 } 456 res.addAll(resFolders.values()); 457 res.addAll(resFiles.values()); 458 459 return res; 460 } 461 462 } 463 464 public static class TrueFalseSupport extends AttributeValueSupport.Default { 465 466 public TrueFalseSupport(boolean tag, String longName, String attrName) { 467 super(tag, longName, attrName); 468 } 469 470 protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) { 471 ArrayList list = new ArrayList(); 472 list.add("false"); list.add("true"); return list; 475 } 476 477 } 478 479 public static class PageLanguage extends AttributeValueSupport.Default { 480 481 public PageLanguage() { 482 super(false, "page", "language"); } 484 485 protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) { 486 ArrayList list = new ArrayList(); 487 list.add("java"); return list; 489 } 490 491 } 492 493 public static class EncodingSupport extends AttributeValueSupport.Default { 494 495 public EncodingSupport(boolean tag, String longName, String attrName) { 496 super(tag, longName, attrName); 497 } 498 499 protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) { 500 ArrayList list = new ArrayList(); 501 Iterator iter = java.nio.charset.Charset.availableCharsets().keySet().iterator(); 502 503 while (iter.hasNext()) 504 list.add(iter.next()); 505 506 return list; 507 } 508 509 } 510 511 } 512 | Popular Tags |