KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > internalapi > JavaMetamodel


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.javacore.internalapi;
21
22 import java.awt.Container JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import javax.swing.JComponent JavaDoc;
28 import javax.swing.SwingConstants JavaDoc;
29 import javax.swing.border.EmptyBorder JavaDoc;
30 import org.netbeans.api.java.classpath.ClassPath;
31 import org.netbeans.api.mdr.MDRManager;
32 import org.netbeans.api.mdr.MDRepository;
33 import org.netbeans.jmi.javamodel.*;
34 import org.netbeans.lib.jmi.util.MetamodelManager;
35 import org.netbeans.modules.javacore.JMManager;
36 import org.netbeans.modules.javacore.JavaCoreModule;
37 import org.openide.filesystems.FileObject;
38 import org.openide.loaders.DataObject;
39 import org.openide.text.PositionBounds;
40 import java.util.ArrayList JavaDoc;
41 import java.util.List JavaDoc;
42 import javax.swing.JLabel JavaDoc;
43 import javax.swing.JOptionPane JavaDoc;
44 import javax.swing.SwingUtilities JavaDoc;
45 import org.netbeans.modules.javacore.UndoProgressListener;
46 import org.openide.DialogDescriptor;
47 import org.openide.DialogDisplayer;
48 import org.openide.util.NbBundle;
49 import org.openide.util.RequestProcessor;
50
51 /**
52  * @author Martin Matula
53  */

54 public abstract class JavaMetamodel extends MetamodelManager {
55     public static final String JavaDoc NAME_REPOSITORY = "org.netbeans.java"; // NOI18N
56

57     private static JavaMetamodel instance = null;
58     private static MDRepository defaultRepository = null;
59     private static UndoManager undoManager = null;
60
61     protected static final ArrayList JavaDoc listeners = new ArrayList JavaDoc();
62
63     protected JavaMetamodel(MDRepository repository, Class JavaDoc cls) {
64         super(repository, cls);
65     }
66
67     public static synchronized JavaMetamodel getManager() {
68         if (instance == null) {
69             instance = new JMManager(getDefaultRepository());
70 // instance.resolveCodebases();
71
}
72         return instance;
73     }
74
75     /** WARNING: This method returns null if no classpath is set during the transaction. */
76     public abstract ClassPath getClassPath();
77     
78     public abstract void setClassPath(ClassPath cp);
79     public abstract void setClassPath(List JavaDoc/*<ClassPath>*/ cps);
80     public abstract void setClassPath(Resource rsc);
81     public abstract void setClassPath(FileObject fo);
82     public abstract void setClassPath(FileObject fo, boolean preferSources);
83     public abstract Resource getResource(FileObject fo);
84     public abstract Resource getResource(FileObject cpRoot, String JavaDoc name);
85     public abstract JavaPackage getPackage(FileObject fo);
86     public abstract FileObject getFileObject(Resource r);
87
88     public abstract DataObject getDataObject(Resource r);
89     public abstract void registerUndoElement(ExternalChange ch);
90     public abstract void registerExtChange(ExternalChange ch);
91
92     public abstract boolean isElementGuarded(Element elem);
93     
94     /** Returns JavaModelPackage for a given cpRoot. If the extent does not exist, it will be created.
95      *
96      * @param cpRoot
97      * @return
98      */

99     public abstract JavaModelPackage resolveJavaExtent(FileObject cpRoot);
100
101     /** Tries to find an extent for a given classpath root. If the extent does not exist, returns null.
102      *
103      * @param cpRoot
104      * @return
105      */

106     public abstract JavaModelPackage getJavaExtent(FileObject cpRoot);
107
108     public abstract JavaModelPackage getDefaultExtent();
109     public abstract PositionBounds getElementPosition(Element element);
110     public abstract PositionBounds getElementPosition(Element element, boolean inclDoc);
111     public abstract Element getElementByOffset(FileObject fo, int offset);
112     public abstract ProgressSupport getProgressSupport();
113     public abstract void addModified(FileObject fobj);
114     
115     /**
116      * Tests if the file is modified and has to be re-parsed. This means, that
117      * content of the file (document) does not correspond to data available
118      * in the storage.
119      *
120      * @param fo file representing the document
121      * @return true, if the file does not correspond to data in the storage,
122      * false, if file content corresponds to data in the storage
123      */

124     public abstract boolean isModified(FileObject fo);
125     
126     public abstract PositionBounds getElementPartPosition(Element element, ElementPartKind part, int position);
127     /**
128      * @return true if classpath scanning is progress <br>
129      * false otherwise
130      */

131     public abstract boolean isScanInProgress();
132     /**
133      * Waits for scanning finish. Goes through all registered path roots and
134      * looks for its extent. When there is extent for all path roots,
135      * method exits. The method blocks the caller.
136      * Currently there isn't any timeout.
137      * @return false if no waiting was needed (scanning was not in progress)
138      * <br>true if scanning was in progress and thread waited
139      */

140     public abstract boolean waitScanFinished();
141
142     public JavaModelPackage getJavaExtent(Element element) {
143         return (JavaModelPackage) element.refImmediatePackage();
144     }
145
146     protected abstract void resolveCodebases();
147
148     public static synchronized MDRepository getDefaultRepository() {
149         if (JavaMetamodel.defaultRepository == null) {
150             // find repository
151
defaultRepository = MDRManager.getDefault().getRepository(NAME_REPOSITORY);
152             if (defaultRepository == null) {
153                 throw new IllegalStateException JavaDoc("FATAL: Repository " + NAME_REPOSITORY + " not found."); // NOI18N
154
}
155         }
156         return defaultRepository;
157     }
158
159     public synchronized static UndoManager getUndoManager() {
160         if (undoManager == null) {
161             undoManager = new UndoManager(new UndoProgressListener());
162         }
163         return undoManager;
164     }
165
166     public static void addParsingListener(ParsingListener listener) {
167         synchronized (listeners) {
168             listeners.add(listener);
169         }
170     }
171
172     public static void removeParsingListener(ParsingListener listener) {
173         synchronized (listeners) {
174             listeners.remove(listener);
175         }
176     }
177     
178     /**
179      * This is a helper method to provide support for delaying invocations of actions
180      * depending on java model. See <a HREF="http://java.netbeans.org/ui/waitscanfinished.html">UI Specification</a>.
181      * <br>Behavior of this method is following:<br>
182      * If classpath scanning is not in progress, runnable's run() is called. <br>
183      * If classpath scanning is in progress, modal cancellable notification dialog with specified
184      * tile is opened.
185      * </ul>
186      * As soon as classpath scanning finishes, this dialog is closed and runnable's run() is called.
187      * This method must be called in AWT EventQueue. Runnable is performed in AWT thread.
188      *
189      * @param runnable Runnable instance which will be called.
190      * @param actionName Title of wait dialog.
191      * @return true action was cancelled <br>
192      * false action was performed
193      */

194     public boolean invokeAfterScanFinished(final Runnable JavaDoc runnable , final String JavaDoc actionName) {
195         assert SwingUtilities.isEventDispatchThread();
196         if (isScanInProgress()) {
197             final ActionPerformer ap = new ActionPerformer(runnable);
198             ActionListener JavaDoc listener = new ActionListener JavaDoc() {
199                 public void actionPerformed(ActionEvent JavaDoc e) {
200                     ap.cancel();
201                     waitTask.cancel();
202                 }
203             };
204             JLabel JavaDoc label = new JLabel JavaDoc(getString("MSG_WaitScan"), javax.swing.UIManager.getIcon("OptionPane.informationIcon"), SwingConstants.LEFT);
205             label.setBorder(new EmptyBorder JavaDoc(12,12,11,11));
206             DialogDescriptor dd = new DialogDescriptor(label, actionName, true, new Object JavaDoc[]{getString("LBL_CancelAction", new Object JavaDoc[]{actionName})}, null, 0, null, listener);
207             waitDialog = DialogDisplayer.getDefault().createDialog(dd);
208             waitDialog.pack();
209             waitTask = RequestProcessor.getDefault().post(ap);
210             waitDialog.setVisible(true);
211             waitTask = null;
212             waitDialog = null;
213             return ap.hasBeenCancelled();
214         } else {
215             runnable.run();
216             return false;
217         }
218     }
219     private Dialog JavaDoc waitDialog = null;
220     private RequestProcessor.Task waitTask = null;
221     
222     private static String JavaDoc getString(String JavaDoc key) {
223         return NbBundle.getMessage(JavaCoreModule.class, key);
224     }
225     
226     private static String JavaDoc getString(String JavaDoc key, Object JavaDoc values) {
227         return new MessageFormat JavaDoc(getString(key)).format(values);
228     }
229
230     private class ActionPerformer implements Runnable JavaDoc {
231         private Runnable JavaDoc action;
232         private boolean cancel = false;
233
234         ActionPerformer(Runnable JavaDoc a) {
235             this.action = a;
236         }
237         
238         public boolean hasBeenCancelled() {
239             return cancel;
240         }
241         
242         public void run() {
243             waitScanFinished();
244             SwingUtilities.invokeLater(new Runnable JavaDoc() {
245                 public void run() {
246                     if (!cancel) {
247                         waitDialog.setVisible(false);
248                         action.run();
249                     }
250                 }
251             });
252         }
253         
254         public void cancel() {
255             assert SwingUtilities.isEventDispatchThread();
256             // check if the scanning did not finish during cancel
257
// invocation - in such case do not set cancel to true
258
// and do not try to hide waitDialog window
259
if (waitDialog != null) {
260                 cancel = true;
261                 waitDialog.setVisible(false);
262             }
263         }
264     }
265 }
266
Popular Tags