KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.WeakHashMap JavaDoc;
23 import javax.jmi.reflect.RefObject;
24 import org.netbeans.modules.java.JavaDataObject;
25 import org.netbeans.modules.java.JavaEditor;
26 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
27 import org.openide.cookies.OpenCookie;
28 import org.openide.nodes.CookieSet;
29 import org.openide.nodes.Node;
30 import org.openide.src.*;
31 import org.openide.text.PositionBounds;
32
33 /**
34  * Standard stupid & simple implementation of the language model environment.
35  *
36  * @author sdedic
37  * @version
38  */

39 public class LangEnvImpl implements LangModel.Env {
40     WeakHashMap JavaDoc cookieMap;
41     BindingFactory binding;
42     WrapperFactory wrapper;
43     LangModel model;
44     JavaDataObject jdo;
45     
46     public LangEnvImpl(JavaDataObject jdo) {
47         this.jdo = jdo;
48         this.binding = new MDRBindingFactory ();
49         this.wrapper = DefaultWrapper.getInstance();
50         cookieMap = new WeakHashMap JavaDoc(57);
51     }
52     
53     public void setModel(LangModel model) {
54         this.model = model;
55     }
56
57     public BindingFactory getBindingFactory() {
58         return binding;
59     }
60     
61     public WrapperFactory getWrapperFactory() {
62         return wrapper;
63     }
64
65     /**
66      * Currently no-op implementation.
67      */

68     public void complete(Element scope, int informationKind) {
69     }
70
71     /** The environment is called to resolve type name according to the context it
72      * is used in. The current implementation will return all primitives and
73      * array of primitives unchanged, but will delegate resolution of class names
74      * to the {@link #resolveTypeIdent}. If the resolved type identifier is different
75      * than the original, it reconstructs the type and returns it.
76     */

77     public Type resolveType(Element context, Type original) {
78         if (original.isPrimitive())
79             return original;
80         Type t = original;
81         int depth = 0;
82         while (t.isArray()) {
83             t = t.getElementType();
84             depth++;
85         }
86         if (t.isPrimitive())
87             return original;
88
89         Identifier id = t.getTypeIdentifier();
90         Identifier resolved = resolveTypeIdent(context, id);
91         if (resolved == id)
92             return original;
93         
94         // build up the type again.
95
t = Type.createClass(resolved);
96         while (depth > 0) {
97             t = Type.createArray(t);
98             depth--;
99         }
100         return t;
101     }
102     
103     /**
104      * This is called to resolve/transform a name of a type for this context.
105      * Assume the context will only be ClassElement or SourceElement. This implementation
106      * does not attempt to resolve identifiers, it only marks them as
107      * {@link org.openide.src.Identifier#NOT_YET_RESOLVED} to distinguish them from
108      * resolved ones.
109     */

110     public Identifier resolveTypeIdent(Element context, Identifier original) {
111         /*
112         if (model.isSameContext(context, original))
113             return original;
114         return model.createLocalIdentifier(context, original.getFullName(),
115             original.getSourceName(), Identifier.NOT_YET_RESOLVED);
116          */

117         // [PENDING]
118
return original;
119     }
120     
121     public Node.Cookie findCookie(Element el, Class JavaDoc requested) {
122         Node.Cookie lookup = null;
123
124         if (el instanceof SourceElement)
125             // handle it specially for SourceElement - it has much more common with DataObject than
126
// the rest of Elements.
127
return jdo.getCookie(requested);
128         if (requested == OpenCookie.class) {
129             lookup = lookupCookie(el, requested);
130             if (lookup != null)
131                 return lookup;
132             lookup = createOpenCookie(el);
133         } else {
134             return jdo.getCookie(requested);
135         }
136         return lookup;
137     }
138     
139     protected OpenCookie createOpenCookie(Element el) {
140         ElementImpl impl = (ElementImpl)el.getCookie(ElementImpl.class);
141         OpenCookie ck = new OpenCookieImpl(impl);
142         return ck;
143     }
144     
145     private Node.Cookie lookupCookie(Element el, Class JavaDoc clazz) {
146         synchronized (cookieMap) {
147             Object JavaDoc o = cookieMap.get(el);
148             if (o == null)
149                 return null;
150             
151             if (o instanceof CookieSet)
152                 return ((CookieSet)o).getCookie(clazz);
153             
154             if (o.getClass().isAssignableFrom(clazz))
155                 return (Node.Cookie)o;
156             return null;
157         }
158     }
159     
160     // ..........................................................................
161

162     private class OpenCookieImpl implements OpenCookie, Runnable JavaDoc {
163         
164         RefObject refObject;
165         
166         OpenCookieImpl(ElementImpl impl) {
167             refObject = impl.getJavaElement ();
168         }
169         
170         public void open() {
171             // Fix #20551: if the thread is not the event one, replan
172
// the open action into the AWT thread.
173
org.openide.util.Mutex.EVENT.postReadRequest(this);
174         }
175         
176         public void run() {
177             try {
178                 PositionBounds bounds = JavaMetamodel.getManager().getElementPosition((org.netbeans.jmi.javamodel.Element) refObject);
179                 if (bounds == null)
180                     return;
181                 ((JavaEditor) jdo.getCookie(JavaEditor.class)).openAt(bounds.getBegin());
182             } catch (javax.jmi.reflect.InvalidObjectException e) {
183             }
184         }
185     }
186     
187 }
188
Popular Tags