001: /*******************************************************************************
002: * Copyright (c) 2003 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jsp;
011:
012: import org.eclipse.core.indexsearch.IIndexQuery;
013: import org.eclipse.core.indexsearch.ISearchResultCollector;
014: import org.eclipse.core.indexsearch.SearchEngine;
015: import org.eclipse.core.resources.IFile;
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.core.resources.IResourceChangeEvent;
018: import org.eclipse.core.resources.IResourceChangeListener;
019: import org.eclipse.core.resources.IResourceDelta;
020: import org.eclipse.core.resources.IResourceDeltaVisitor;
021: import org.eclipse.core.resources.IResourceProxy;
022: import org.eclipse.core.resources.IResourceProxyVisitor;
023: import org.eclipse.core.resources.IWorkspace;
024: import org.eclipse.core.resources.ResourcesPlugin;
025: import org.eclipse.core.runtime.CoreException;
026: import org.eclipse.core.runtime.IPluginDescriptor;
027: import org.eclipse.core.runtime.IProgressMonitor;
028: import org.eclipse.core.runtime.IStatus;
029: import org.eclipse.core.runtime.Status;
030:
031: import org.eclipse.jface.preference.IPreferenceStore;
032: import org.eclipse.jface.resource.ImageRegistry;
033:
034: import org.eclipse.swt.widgets.Display;
035: import org.eclipse.ui.editors.text.TextEditorPreferenceConstants;
036: import org.eclipse.ui.plugin.AbstractUIPlugin;
037:
038: /**
039: */
040: public class JspUIPlugin extends AbstractUIPlugin implements
041: IResourceChangeListener {
042:
043: /**
044: * The id of the JavaFamilyExample plugin (value <code>"org.eclipse.jdt.ui.examples.javafamily"</code>).
045: */
046: public static final String ID_PLUGIN = "org.eclipse.jdt.ui.examples.javafamily"; //$NON-NLS-1$
047:
048: public static final String JSP_TYPE = "jsp"; //$NON-NLS-1$
049:
050: private static final boolean DEBUG = false;
051: private static JspUIPlugin fgDefault;
052: private static boolean fgJSPIndexingIsEnabled = false;
053:
054: private SearchEngine fSearchEngine;
055:
056: /**
057: * @param descriptor
058: */
059: public JspUIPlugin(IPluginDescriptor descriptor) {
060: super (descriptor);
061: fgDefault = this ;
062: fSearchEngine = SearchEngine.getSearchEngine();
063: }
064:
065: public static JspUIPlugin getDefault() {
066: return fgDefault;
067: }
068:
069: void controlJSPIndexing(boolean enable) {
070: if (fgJSPIndexingIsEnabled != enable) {
071: fgJSPIndexingIsEnabled = enable;
072: IWorkspace workspace = ResourcesPlugin.getWorkspace();
073: if (enable) {
074:
075: IResourceProxyVisitor visitor = new IResourceProxyVisitor() {
076: public boolean visit(IResourceProxy proxy)
077: throws CoreException {
078: String name = proxy.getName();
079: int pos = name.lastIndexOf('.');
080: if (pos >= 0) {
081: String extension = name.substring(pos + 1);
082: if (JSP_TYPE.equalsIgnoreCase(extension)) {
083: IResource r = proxy.requestResource();
084: if (r instanceof IFile)
085: jspAdded((IFile) r);
086: }
087: }
088: return true;
089: }
090: };
091: try {
092: workspace.getRoot().accept(visitor, 0);
093: } catch (CoreException e) {
094: log("visiting jsp files", e); //$NON-NLS-1$
095: }
096:
097: workspace.addResourceChangeListener(this ,
098: // IResourceChangeEvent.PRE_AUTO_BUILD |
099: // IResourceChangeEvent.POST_AUTO_BUILD |
100: IResourceChangeEvent.POST_CHANGE
101: | IResourceChangeEvent.PRE_DELETE
102: | IResourceChangeEvent.PRE_CLOSE);
103: } else {
104: workspace.removeResourceChangeListener(this );
105: }
106: }
107: }
108:
109: boolean isJSPIndexingOn() {
110: return fgJSPIndexingIsEnabled;
111: }
112:
113: public void resourceChanged(IResourceChangeEvent event) {
114: if (!fgJSPIndexingIsEnabled || event == null)
115: return;
116: IResourceDelta d = event.getDelta();
117: if (d == null)
118: return;
119: try {
120: d.accept(new IResourceDeltaVisitor() {
121: public boolean visit(IResourceDelta delta) {
122: if (delta != null) {
123: IResource r = delta.getResource();
124: if (r instanceof IFile) {
125: IFile file = (IFile) r;
126: String type = file.getFileExtension();
127: if (JSP_TYPE.equalsIgnoreCase(type)) {
128: switch (delta.getKind()) {
129: case IResourceDelta.ADDED:
130: jspAdded(file);
131: break;
132: case IResourceDelta.REMOVED:
133: jspRemoved(file);
134: break;
135: case IResourceDelta.CHANGED:
136: // no need to index if the content has not changed
137: if ((delta.getFlags() & IResourceDelta.CONTENT) != 0)
138: jspAdded(file);
139: break;
140: }
141: }
142: }
143: }
144: return true;
145: }
146: });
147: } catch (CoreException e) {
148: log("processing resource delta", e); //$NON-NLS-1$
149: }
150: }
151:
152: public static void log(String message, Throwable e) {
153: getDefault().getLog().log(
154: new Status(IStatus.ERROR, ID_PLUGIN, IStatus.ERROR,
155: message, e));
156: }
157:
158: void jspAdded(IFile jspFile) {
159: if (DEBUG)
160: System.out.println("Added: " + jspFile); //$NON-NLS-1$
161: JspIndexParser indexer = new JspIndexParser(jspFile);
162: fSearchEngine.add(jspFile.getProject().getFullPath(), indexer);
163: }
164:
165: void jspRemoved(IFile jspFile) {
166: if (DEBUG)
167: System.out.println("Removed: " + jspFile); //$NON-NLS-1$
168: fSearchEngine.remove(jspFile.getFullPath().toString(), jspFile
169: .getProject().getFullPath());
170: }
171:
172: public void search(IIndexQuery query,
173: ISearchResultCollector resultCollector, IProgressMonitor pm) {
174: fSearchEngine.search(query, resultCollector, pm,
175: SearchEngine.WAIT_UNTIL_READY_TO_SEARCH);
176: }
177:
178: /**
179: * Shutdown the JspCore plug-in.
180: * <p>
181: * De-registers the resource changed listener.
182: * <p>
183: * @see org.eclipse.core.runtime.Plugin#shutdown()
184: */
185: public void shutdown() {
186: controlJSPIndexing(false);
187: }
188:
189: protected void initializeDefaultPreferences(IPreferenceStore prefs) {
190: TextEditorPreferenceConstants.initializeDefaultValues(prefs);
191: }
192:
193: /**
194: * Returns the standard display to be used. The method first checks, if
195: * the thread calling this method has an associated display. If so, this
196: * display is returned. Otherwise the method returns the default display.
197: */
198: public static Display getStandardDisplay() {
199: Display display = Display.getCurrent();
200: if (display == null) {
201: display = Display.getDefault();
202: }
203: return display;
204: }
205:
206: protected ImageRegistry createImageRegistry() {
207: return JspPluginImages.initializeImageRegistry();
208: }
209:
210: }
|