KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > bridge > DefaultInsertStrategy


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.java.bridge;
21
22 import org.openide.src.*;
23
24 /**
25  * Dump insertion strategy, that inserts after all elemnets with the given type.
26  * Element types are ordered -- a future extension would be to allow specify the order
27  * of element types.
28  *
29  * @author sdedic
30  * @version
31  */

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