1 19 20 package org.netbeans.modules.javacore.internalapi; 21 22 import java.awt.Container ; 23 import java.awt.Dialog ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.text.MessageFormat ; 27 import javax.swing.JComponent ; 28 import javax.swing.SwingConstants ; 29 import javax.swing.border.EmptyBorder ; 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 ; 41 import java.util.List ; 42 import javax.swing.JLabel ; 43 import javax.swing.JOptionPane ; 44 import javax.swing.SwingUtilities ; 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 54 public abstract class JavaMetamodel extends MetamodelManager { 55 public static final String NAME_REPOSITORY = "org.netbeans.java"; 57 private static JavaMetamodel instance = null; 58 private static MDRepository defaultRepository = null; 59 private static UndoManager undoManager = null; 60 61 protected static final ArrayList listeners = new ArrayList (); 62 63 protected JavaMetamodel(MDRepository repository, Class cls) { 64 super(repository, cls); 65 } 66 67 public static synchronized JavaMetamodel getManager() { 68 if (instance == null) { 69 instance = new JMManager(getDefaultRepository()); 70 } 72 return instance; 73 } 74 75 76 public abstract ClassPath getClassPath(); 77 78 public abstract void setClassPath(ClassPath cp); 79 public abstract void setClassPath(List 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 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 99 public abstract JavaModelPackage resolveJavaExtent(FileObject cpRoot); 100 101 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 124 public abstract boolean isModified(FileObject fo); 125 126 public abstract PositionBounds getElementPartPosition(Element element, ElementPartKind part, int position); 127 131 public abstract boolean isScanInProgress(); 132 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 defaultRepository = MDRManager.getDefault().getRepository(NAME_REPOSITORY); 152 if (defaultRepository == null) { 153 throw new IllegalStateException ("FATAL: Repository " + NAME_REPOSITORY + " not found."); } 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 194 public boolean invokeAfterScanFinished(final Runnable runnable , final String actionName) { 195 assert SwingUtilities.isEventDispatchThread(); 196 if (isScanInProgress()) { 197 final ActionPerformer ap = new ActionPerformer(runnable); 198 ActionListener listener = new ActionListener () { 199 public void actionPerformed(ActionEvent e) { 200 ap.cancel(); 201 waitTask.cancel(); 202 } 203 }; 204 JLabel label = new JLabel (getString("MSG_WaitScan"), javax.swing.UIManager.getIcon("OptionPane.informationIcon"), SwingConstants.LEFT); 205 label.setBorder(new EmptyBorder (12,12,11,11)); 206 DialogDescriptor dd = new DialogDescriptor(label, actionName, true, new Object []{getString("LBL_CancelAction", new Object []{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 waitDialog = null; 220 private RequestProcessor.Task waitTask = null; 221 222 private static String getString(String key) { 223 return NbBundle.getMessage(JavaCoreModule.class, key); 224 } 225 226 private static String getString(String key, Object values) { 227 return new MessageFormat (getString(key)).format(values); 228 } 229 230 private class ActionPerformer implements Runnable { 231 private Runnable action; 232 private boolean cancel = false; 233 234 ActionPerformer(Runnable 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 () { 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 if (waitDialog != null) { 260 cancel = true; 261 waitDialog.setVisible(false); 262 } 263 } 264 } 265 } 266 | Popular Tags |