KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeEvent JavaDoc;
23
24 import org.openide.src.*;
25
26 import org.netbeans.api.mdr.events.*;
27 import org.netbeans.jmi.javamodel.JavaPackage;
28
29 public class ImportImpl extends ElementImpl implements ImportElement.Impl {
30
31     SourceElementImpl sourceImpl;
32
33     private ElementImpl.ElementListener importListener;
34
35     private static final long serialVersionUID = -3319955190266278185L;
36
37     ImportImpl(DefaultLangModel model, org.netbeans.jmi.javamodel.Import imp) {
38         super(model, imp);
39     }
40     
41     public void connectListener () {
42         importListener = new ImportListener (this);
43         importListener.connect ();
44     }
45     
46     protected void createFromModel(Element model) throws SourceException {
47         ImportElement i = (ImportElement)model;
48         setImport(i.getImport());
49     }
50
51     public void setParent(ElementImpl impl) {
52         sourceImpl = (SourceElementImpl)impl;
53     }
54     
55     public Import getImport() {
56         repository.beginTrans(false);
57         try {
58             if (javaElement.isValid()) {
59                 setClassPath();
60                 org.netbeans.jmi.javamodel.Import importStatement = (org.netbeans.jmi.javamodel.Import) javaElement;
61                 String JavaDoc impName = importStatement.getName ();
62                 if (impName == null) // [PENDING]
63
impName = "";
64                 Identifier id = Identifier.create (impName);
65                 return new Import (id, importStatement.getImportedNamespace() instanceof JavaPackage ? Import.PACKAGE : Import.CLASS); // NOI18N
66
} else {
67                 return null;
68             }
69         } finally {
70             repository.endTrans(false);
71         }
72     }
73     
74     public SourceElementImpl findSource() {
75         return sourceImpl;
76     }
77     
78     public void setImport(Import imp) throws SourceException {
79         checkWritable(false);
80         checkDocument();
81         org.netbeans.jmi.javamodel.Import importStatement = (org.netbeans.jmi.javamodel.Import) javaElement;
82         boolean failed = true;
83         repository.beginTrans (true);
84         try {
85             if (javaElement.isValid()) {
86                 setClassPath();
87                 boolean isNamed = !importStatement.getName ().endsWith(".*"); // NOI18N
88
String JavaDoc impName = importStatement.getName ();
89                 if ((isNamed == imp.isClass ()) &&
90                     (imp.getIdentifier().getSourceName().equals(impName))) {
91                     failed = false;
92                     return;
93                 }
94                 Import old = new Import (Identifier.create (impName),
95                     isNamed ? Import.CLASS : Import.PACKAGE);
96                 PropertyChangeEvent JavaDoc evt = new PropertyChangeEvent JavaDoc(
97                     getElement(), "import", old, imp); // NOI18N
98
checkVetoablePropertyChange(evt);
99
100                 // sourceImpl.imports.importsChanged(); // [PENDING] should this be called ??
101
failed = false;
102             } else {
103                 failed = false;
104                 throwIsInvalid ();
105             }
106         } finally {
107             repository.endTrans (failed);
108         }
109     }
110
111     protected Element cloneSelf() {
112         ImportElement.MemoryImpl impl = new ImportElement.MemoryImpl();
113         try {
114             impl.setImport(getImport());
115         } catch (SourceException e) {
116         }
117         return new ImportElement(impl, null);
118     }
119     
120     public void fireImportChange (Import oldValue, Import newValue) {
121         if (oldValue.equals (newValue))
122             return;
123         PropertyChangeEvent JavaDoc evt = new PropertyChangeEvent JavaDoc(
124                 getElement(), "import", oldValue, newValue); // NOI18N
125
fireOwnPropertyChange(evt);
126         
127         ImportElement old = (ImportElement) cloneSelf ();
128         try {
129             old.setImport (oldValue);
130         } catch (SourceException e) {
131             e.printStackTrace ();
132         }
133         notifyConnectionChange (old);
134     }
135     
136     public Object JavaDoc readResolve() {
137         return null;
138     }
139     
140     public Object JavaDoc writeReplace() {
141         return null;
142     }
143     
144     protected boolean parentValid() {
145         return sourceImpl != null && sourceImpl.isValid();
146     }
147     
148     protected void checkWritable(boolean unsafeOp) throws SourceException {
149         if (sourceImpl != null) {
150             sourceImpl.checkWritable(unsafeOp);
151         }
152     }
153     
154     // ..........................................................................
155

156     static class ImportListener extends ElementImpl.ElementListener {
157         
158         Import value;
159         
160         ImportListener (ImportImpl impl) {
161             super (impl);
162             value = impl.getImport ();
163         }
164     
165         public void doChange (MDRChangeEvent event) {
166             super.doChange (event);
167             if ((source == javaElement) && (event instanceof AttributeEvent)) {
168                 AttributeEvent attrEvent = (AttributeEvent) event;
169                 String JavaDoc attrName = attrEvent.getAttributeName ();
170                 if (attrName.equals ("name")) { // NOI18N
171
String JavaDoc name = (String JavaDoc) attrEvent.getNewElement ();
172                     if (name == null) {
173                         name = "";
174                     }
175                     boolean isClass = true;
176                     if (name.endsWith(".*")) { // NOI18N
177
isClass = false;
178                         name = name.substring (0, name.length() - 2);
179                     }
180                     Import old = value;
181                     value = new Import (Identifier.create (name), isClass);
182                     ((ImportImpl) impl).fireImportChange (old, value);
183                 }
184             }
185         }
186     }
187     
188 }
189
Popular Tags