1 19 20 package org.netbeans.modules.java.bridge; 21 22 import org.openide.src.*; 23 24 32 class DefaultInsertStrategy implements Positioner { 33 public Element[] findInsertPositions(Element container, Element[] els, Acceptor posAcceptor) { 34 Element[] siblings; 35 36 if (container instanceof SourceElement) { 37 siblings = ((SourceElement)container).getClasses(); 38 } else { 39 siblings = findElements((ClassElement)container, els[0]); 40 } 41 Element[] refs = new Element[els.length]; 42 Element ref; 43 if (siblings == null || siblings.length == 0) { 44 ref = Positioner.FIRST; 45 } else { 46 ref = siblings[siblings.length - 1]; 47 } 48 ref = findSuitablePos(container, ref, posAcceptor); 49 refs[0] = ref; 50 for (int i = 1; i < refs.length; i++) { 51 refs[i] = els[i - 1]; 52 } 53 return refs; 54 } 55 56 private Element findSuitablePos(Element container, Element ref, Acceptor acc) { 57 if (acc.canInsertAfter(ref)) 58 return ref; 59 ElementOrder o = (ElementOrder)container.getCookie(ElementOrder.class); 60 Element[] children = o.getElements(); 61 int prefPos = 0; 62 int i; 63 64 for (i = 0; i < children.length; i++) { 65 if (ref == children[i]) { 66 prefPos = i; 67 break; 68 } 69 } 70 int after = prefPos + 1; 71 int before = prefPos - 1; 72 73 while (before >= -1 || after < children.length) { 74 if (after < children.length) { 75 if (acc.canInsertAfter(children[after])) 76 return children[after]; 77 after++; 78 } 79 if (before >= 0) { 80 if (acc.canInsertAfter(children[before])) { 81 return children[before]; 82 } 83 before--; 84 } else if (before == -1) { 85 if (acc.canInsertAfter(FIRST)) 86 return FIRST; 87 before--; 88 } 89 } 90 return null; 91 } 92 93 private Element[] findElements(ClassElement container, Element selector) { 94 if (selector instanceof FieldElement) { 95 return getFirstNonEmpty(container, 0); 96 } else if (selector instanceof ClassElement) { 97 return getFirstNonEmpty(container, 4); 98 } else if (selector instanceof MethodElement) { 99 return getFirstNonEmpty(container, 3); 100 } else if (selector instanceof ConstructorElement) { 101 return getFirstNonEmpty(container, 2); 102 } else if (selector instanceof InitializerElement) { 103 return getFirstNonEmpty(container, 1); 104 } 105 return null; 106 } 107 108 private Element[] getFirstNonEmpty(ClassElement container, int startPos) { 109 Element[] items; 110 111 if (startPos > 3) { 112 items = container.getClasses(); 113 if (items != null && items.length > 0) 114 return items; 115 } 116 if (startPos > 2) { 117 items = container.getMethods(); 118 if (items != null && items.length > 0) 119 return items; 120 } 121 if (startPos > 1) { 122 items = container.getConstructors(); 123 if (items != null && items.length > 0) 124 return items; 125 } 126 if (startPos > 0) { 127 items = container.getInitializers(); 128 if (items != null && items.length > 0) 129 return items; 130 } 131 items = container.getFields(); 132 if (items != null && items.length > 0) 133 return items; 134 return null; 135 } 136 } 137 | Popular Tags |