001: package org.drools.eclipse.view.rules;
002:
003: import java.util.ArrayList;
004: import java.util.HashMap;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.Map;
008:
009: import org.drools.eclipse.DRLInfo;
010: import org.drools.eclipse.DroolsEclipsePlugin;
011: import org.drools.eclipse.core.DroolsElement;
012: import org.drools.eclipse.core.DroolsModelBuilder;
013: import org.drools.eclipse.core.Function;
014: import org.drools.eclipse.core.Global;
015: import org.drools.eclipse.core.Package;
016: import org.drools.eclipse.core.Query;
017: import org.drools.eclipse.core.Rule;
018: import org.drools.eclipse.core.RuleSet;
019: import org.drools.eclipse.core.Template;
020: import org.drools.eclipse.core.ui.DroolsContentProvider;
021: import org.drools.eclipse.core.ui.DroolsLabelProvider;
022: import org.drools.eclipse.core.ui.DroolsTreeSorter;
023: import org.drools.eclipse.core.ui.FilterActionGroup;
024: import org.drools.lang.descr.FactTemplateDescr;
025: import org.drools.lang.descr.FunctionDescr;
026: import org.drools.lang.descr.GlobalDescr;
027: import org.drools.lang.descr.QueryDescr;
028: import org.drools.lang.descr.RuleDescr;
029: import org.eclipse.core.resources.IFile;
030: import org.eclipse.core.resources.IProject;
031: import org.eclipse.core.resources.IResource;
032: import org.eclipse.core.resources.IResourceChangeEvent;
033: import org.eclipse.core.resources.IResourceChangeListener;
034: import org.eclipse.core.resources.IResourceDelta;
035: import org.eclipse.core.resources.IResourceDeltaVisitor;
036: import org.eclipse.core.resources.IResourceVisitor;
037: import org.eclipse.core.resources.ResourcesPlugin;
038: import org.eclipse.core.runtime.CoreException;
039: import org.eclipse.jdt.core.IJavaProject;
040: import org.eclipse.jdt.core.JavaCore;
041: import org.eclipse.jface.viewers.DoubleClickEvent;
042: import org.eclipse.jface.viewers.IDoubleClickListener;
043: import org.eclipse.jface.viewers.ISelection;
044: import org.eclipse.jface.viewers.IStructuredSelection;
045: import org.eclipse.jface.viewers.StructuredSelection;
046: import org.eclipse.jface.viewers.TreeViewer;
047: import org.eclipse.swt.SWT;
048: import org.eclipse.swt.widgets.Composite;
049: import org.eclipse.ui.IEditorPart;
050: import org.eclipse.ui.IMemento;
051: import org.eclipse.ui.IViewSite;
052: import org.eclipse.ui.PartInitException;
053: import org.eclipse.ui.forms.editor.FormEditor;
054: import org.eclipse.ui.ide.IDE;
055: import org.eclipse.ui.part.ViewPart;
056: import org.eclipse.ui.texteditor.ITextEditor;
057:
058: public class RulesView extends ViewPart implements
059: IDoubleClickListener, IResourceVisitor, IResourceChangeListener {
060:
061: private final RuleSet ruleSet = DroolsModelBuilder.createRuleSet();
062:
063: private Map resourcesMap = new HashMap();
064: private TreeViewer treeViewer;
065:
066: public void createPartControl(Composite parent) {
067: treeViewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL
068: | SWT.V_SCROLL);
069: treeViewer.setContentProvider(new DroolsContentProvider());
070: treeViewer.setLabelProvider(new DroolsLabelProvider());
071: treeViewer.setSorter(new DroolsTreeSorter());
072: treeViewer.addDoubleClickListener(this );
073: treeViewer.setUseHashlookup(true);
074: treeViewer.setInput(ruleSet);
075: FilterActionGroup filterActionGroup = new FilterActionGroup(
076: treeViewer, "org.drools.eclipse.view.rules.RulesView");
077: filterActionGroup.fillActionBars(getViewSite().getActionBars());
078: }
079:
080: public void init(IViewSite site, IMemento memento)
081: throws PartInitException {
082: super .init(site, memento);
083: try {
084: ResourcesPlugin.getWorkspace().getRoot().accept(this );
085: } catch (CoreException e) {
086: DroolsEclipsePlugin.log(e);
087: }
088: ResourcesPlugin.getWorkspace().addResourceChangeListener(this );
089: }
090:
091: public void dispose() {
092: ResourcesPlugin.getWorkspace().removeResourceChangeListener(
093: this );
094: }
095:
096: public void setFocus() {
097: treeViewer.getControl().setFocus();
098: }
099:
100: public boolean visit(IResource resource) throws CoreException {
101: return updateResource(resource);
102: }
103:
104: private boolean updateResource(IResource resource) {
105: IProject project = resource.getProject();
106: if (project != null) {
107: IJavaProject javaProject = JavaCore.create(project);
108: if (!javaProject.exists()) {
109: return false;
110: }
111: if (resource instanceof IFile
112: && "drl".equals(resource.getFileExtension())
113: && javaProject.isOnClasspath(resource)) {
114: try {
115: IFile file = (IFile) resource;
116: DRLInfo drlInfo = DroolsEclipsePlugin.getDefault()
117: .parseResource(resource, false);
118: String packageName = drlInfo.getPackageName();
119: Package pkg = ruleSet.getPackage(packageName);
120: if (pkg == null) {
121: pkg = DroolsModelBuilder.addPackage(ruleSet,
122: packageName, 0, 0);
123: }
124: // add rules
125: List rules = drlInfo.getPackageDescr().getRules();
126: for (Iterator iterator = rules.iterator(); iterator
127: .hasNext();) {
128: RuleDescr ruleDescr = (RuleDescr) iterator
129: .next();
130: boolean isQuery = ruleDescr instanceof QueryDescr;
131: String ruleName = ruleDescr.getName();
132: if (!isQuery) {
133: Rule rule = DroolsModelBuilder
134: .addRule(
135: pkg,
136: ruleName,
137: file,
138: ruleDescr
139: .getStartCharacter(),
140: ruleDescr.getEndCharacter()
141: - ruleDescr
142: .getStartCharacter()
143: + 1, null);
144: // create link between resource and created rule nodes
145: List droolsElements = (List) resourcesMap
146: .get(file);
147: if (droolsElements == null) {
148: droolsElements = new ArrayList();
149: resourcesMap.put(file, droolsElements);
150: }
151: droolsElements.add(rule);
152: } else {
153: Query query = DroolsModelBuilder
154: .addQuery(
155: pkg,
156: ruleName,
157: file,
158: ruleDescr
159: .getStartCharacter(),
160: ruleDescr.getEndCharacter()
161: - ruleDescr
162: .getStartCharacter()
163: + 1);
164: // create link between resource and created rule nodes
165: List droolsElements = (List) resourcesMap
166: .get(file);
167: if (droolsElements == null) {
168: droolsElements = new ArrayList();
169: resourcesMap.put(file, droolsElements);
170: }
171: droolsElements.add(query);
172: }
173: }
174: // add templates
175: List templates = drlInfo.getPackageDescr()
176: .getFactTemplates();
177: for (Iterator iterator = templates.iterator(); iterator
178: .hasNext();) {
179: FactTemplateDescr templateDescr = (FactTemplateDescr) iterator
180: .next();
181: Template template = DroolsModelBuilder
182: .addTemplate(
183: pkg,
184: templateDescr.getName(),
185: file,
186: templateDescr
187: .getStartCharacter(),
188: templateDescr.getEndCharacter()
189: - templateDescr
190: .getStartCharacter()
191: + 1);
192: // create link between resource and created rule nodes
193: List droolsElements = (List) resourcesMap
194: .get(file);
195: if (droolsElements == null) {
196: droolsElements = new ArrayList();
197: resourcesMap.put(file, droolsElements);
198: }
199: droolsElements.add(template);
200: }
201: // add globals
202: List globals = drlInfo.getPackageDescr()
203: .getGlobals();
204: for (Iterator iterator = globals.iterator(); iterator
205: .hasNext();) {
206: GlobalDescr globalDescr = (GlobalDescr) iterator
207: .next();
208: Global global = DroolsModelBuilder.addGlobal(
209: pkg, globalDescr.getIdentifier(), file,
210: globalDescr.getStartCharacter(),
211: globalDescr.getEndCharacter()
212: - globalDescr
213: .getStartCharacter()
214: + 1);
215: // create link between resource and created rule nodes
216: List droolsElements = (List) resourcesMap
217: .get(file);
218: if (droolsElements == null) {
219: droolsElements = new ArrayList();
220: resourcesMap.put(file, droolsElements);
221: }
222: droolsElements.add(global);
223: }
224: // add functions
225: List functions = drlInfo.getPackageDescr()
226: .getFunctions();
227: for (Iterator iterator = functions.iterator(); iterator
228: .hasNext();) {
229: FunctionDescr functionDescr = (FunctionDescr) iterator
230: .next();
231: String functionName = functionDescr.getName();
232: Function function = DroolsModelBuilder
233: .addFunction(
234: pkg,
235: functionName,
236: file,
237: functionDescr
238: .getStartCharacter(),
239: functionDescr.getEndCharacter()
240: - functionDescr
241: .getStartCharacter()
242: + 1);
243: // create link between resource and created rule nodes
244: List droolsElements = (List) resourcesMap
245: .get(file);
246: if (droolsElements == null) {
247: droolsElements = new ArrayList();
248: resourcesMap.put(file, droolsElements);
249: }
250: droolsElements.add(function);
251: }
252: } catch (Throwable t) {
253: DroolsEclipsePlugin.log(t);
254: }
255: return false;
256: }
257: }
258: return true;
259: }
260:
261: public void resourceChanged(final IResourceChangeEvent event) {
262: try {
263: if (event.getType() == IResourceChangeEvent.POST_CHANGE) {
264: IResourceDelta delta = event.getDelta();
265: if (delta != null) {
266: delta.accept(new IResourceDeltaVisitor() {
267: public boolean visit(IResourceDelta delta)
268: throws CoreException {
269: IResource resource = delta.getResource();
270: removeElementsFromResource(resource);
271: boolean result = true;
272: if (delta.getKind() != IResourceDelta.REMOVED) {
273: result = updateResource(resource);
274: }
275: treeViewer.getControl().getDisplay()
276: .asyncExec(new Runnable() {
277: public void run() {
278: treeViewer.refresh();
279: }
280: });
281: return result;
282: }
283: });
284: }
285: } else if (event.getType() == IResourceChangeEvent.PRE_DELETE) {
286: IResource resource = event.getResource();
287: if (resource != null) {
288: resource.accept(new IResourceVisitor() {
289: public boolean visit(IResource resource)
290: throws CoreException {
291: removeElementsFromResource(resource);
292: return true;
293: }
294: });
295: }
296: } else if (event.getType() == IResourceChangeEvent.PRE_CLOSE) {
297: IResource resource = event.getResource();
298: if (resource != null) {
299: resource.accept(new IResourceVisitor() {
300: public boolean visit(IResource resource)
301: throws CoreException {
302: removeElementsFromResource(resource);
303: return true;
304: }
305: });
306: }
307: }
308: } catch (Throwable t) {
309: DroolsEclipsePlugin.log(t);
310: }
311: }
312:
313: private void removeElementsFromResource(IResource resource) {
314: List droolsElements = (List) resourcesMap.get(resource);
315: if (droolsElements != null) {
316: for (Iterator iterator = droolsElements.iterator(); iterator
317: .hasNext();) {
318: DroolsModelBuilder
319: .removeElement((DroolsElement) iterator.next());
320: }
321: resourcesMap.remove(resource);
322: }
323: }
324:
325: public void doubleClick(DoubleClickEvent event) {
326: ISelection selection = event.getSelection();
327: if (selection instanceof IStructuredSelection) {
328: Object selected = ((StructuredSelection) selection)
329: .getFirstElement();
330: if (selected != null && selected instanceof DroolsElement) {
331: DroolsElement droolsSelected = (DroolsElement) selected;
332: IFile file = droolsSelected.getFile();
333: if (file != null) {
334: try {
335: IEditorPart editor = IDE.openEditor(getSite()
336: .getPage(), file);
337: if (editor instanceof FormEditor) {
338: editor = ((FormEditor) editor)
339: .getActiveEditor();
340: }
341: if (editor instanceof ITextEditor) {
342: ((ITextEditor) editor).selectAndReveal(
343: droolsSelected.getOffset(),
344: droolsSelected.getLength());
345: }
346: } catch (Throwable t) {
347: DroolsEclipsePlugin.log(t);
348: }
349: }
350: }
351: }
352: }
353:
354: }
|