View Javadoc

1   /*
2    * $Id: OWLModelDeltaVisitor.java,v 1.1 2005/05/05 19:25:13 jlerner Exp $
3    * Created on Apr 30, 2004
4    *
5    * Copyright (c) 1999-2004, BBN Technologies, LLC.
6    * All rights reserved.
7    * http://www.daml.org/legal/opensource/bbn_license.html
8    */
9   
10  package com.bbn.swede.core.resources;
11  
12  import org.eclipse.core.resources.IFile;
13  import org.eclipse.core.resources.IProject;
14  import org.eclipse.core.resources.IResource;
15  import org.eclipse.core.resources.IResourceDelta;
16  import org.eclipse.core.resources.IResourceDeltaVisitor;
17  import org.eclipse.core.runtime.CoreException;
18  
19  import com.bbn.swede.core.IOWLDocument;
20  import com.bbn.swede.core.IOWLElement;
21  import com.bbn.swede.core.IOWLModel;
22  import com.bbn.swede.core.IOWLProject;
23  import com.bbn.swede.core.OWLCore;
24  
25  /***
26   * Processes resource deltas and propagates the changes into the OWL model.
27   * @author tself
28   */
29  public class OWLModelDeltaVisitor implements IResourceDeltaVisitor
30  {
31     /***
32      * Visits the given delta and applies necessary changes to OWLModel and below.
33      * 
34      * @param delta IResourceDelta that contains the change information
35      * @return boolean true if this visitor should navigate to the next level of
36      *         the delta
37      * @throws CoreException if something horrible happens
38      */
39     public boolean visit(IResourceDelta delta) throws CoreException
40     {
41        IResource res = delta.getResource();
42        switch(delta.getKind())
43        {
44           case IResourceDelta.ADDED:
45              trace("Resource added: " + printResource(res));
46              addResource(res);
47              break;
48           case IResourceDelta.CHANGED:
49              trace("Resource changed: " + printResource(res));
50              changeResource(res, delta);
51              break;
52           case IResourceDelta.REMOVED:
53              trace("Resource removed: " + printResource(res));
54              removeResource(res);
55              break;
56           default:
57              trace("Resource something something: " + printResource(res));
58        }
59        return true;
60     }
61  
62     private void printDetailFlag(IResourceDelta delta)
63     {
64        if((delta.getFlags() & IResourceDelta.CONTENT) != 0)
65        {
66           trace("CONTENT");
67        }
68        if((delta.getFlags() & IResourceDelta.MOVED_FROM) != 0)
69        {
70           trace("MOVED_FROM");
71        }
72        if((delta.getFlags() & IResourceDelta.MOVED_TO) != 0)
73        {
74           trace("MOVED_TO");
75        }
76        if((delta.getFlags() & IResourceDelta.OPEN) != 0)
77        {
78           trace("OPEN");
79        }
80        if((delta.getFlags() & IResourceDelta.TYPE) != 0)
81        {
82           trace("TYPE");
83        }
84        if((delta.getFlags() & IResourceDelta.MARKERS) != 0)
85        {
86           trace("MARKERS");
87        }
88        if((delta.getFlags() & IResourceDelta.REPLACED) != 0)
89        {
90           trace("REPLACED");
91        }
92        if((delta.getFlags() & IResourceDelta.DESCRIPTION) != 0)
93        {
94           trace("DESCRIPTION");
95        }
96        if((delta.getFlags() & IResourceDelta.SYNC) != 0)
97        {
98           trace("SYNC");
99        }
100       trace("getFlags() = " + delta.getFlags());
101    }
102 
103    private String printResource(IResource r)
104    {
105       if(r == null)
106       {
107          return "NULL RESOURCE";
108       }
109       return r.getFullPath().toString();
110    }
111 
112    private void addResource(IResource r)
113    {
114       // if new project
115       try
116       {
117          if(r instanceof IProject
118             && ((IProject)r).hasNature(OWLCore.OWL_NATURE))
119          {
120             trace("NEW OWL PROJECT!", false);
121             IOWLProject oProj = SWResourceManager.create((IProject)r);
122             SWResourceManager.getModel().getElementInfo().addChild((IOWLProject)oProj);
123             return;
124          }
125       }
126       catch (CoreException ce)
127       {
128          trace("Unable to investigate new Project for OWL Nature", true);
129          return;
130       }
131       // handling brand new files in the Wizard itself rather than here
132       // had to re-enable this to handle opening existing projects
133       if(r instanceof IFile)
134       {
135          trace("Added file!");
136          IOWLElement e = SWResourceManager.getModel().getCorrespondingElement(r);
137          if(e != null)
138          {
139             trace("...already in the model: " + e.getElementName());
140          }
141          else
142          {
143             trace("...not yet in the model");
144          }
145          if(SWResourceManager.getModel().getCorrespondingElement(r) == null)
146          {
147             IOWLDocument doc = SWResourceManager.create((IFile)r);
148             // add to the model
149             IOWLModel model = SWResourceManager.getModel();
150             model.add(doc);
151             return;
152          }
153       }
154    }
155 
156    private void removeResource(IResource r)
157    {
158       IOWLElement elem = SWResourceManager.getModel().getCorrespondingElement(r);
159       if(elem == null)
160       {
161          return;
162       }
163       IOWLElement parent = elem.getParent();
164       parent.getElementInfo().removeChild(elem);
165    }
166 
167    private void changeResource(IResource r, IResourceDelta delta)
168    {
169       if(SWResourceManager.getModel().getCorrespondingElement(r) == null
170          && (!(r instanceof IProject)))
171       {
172          return;
173       }
174       // anything below is either already in the OWLModel or a newly opened
175       // project
176       
177       try
178       {
179          if(r instanceof IProject && ((IProject)r).isOpen()
180             && ((IProject)r).hasNature(OWLCore.OWL_NATURE))
181          {
182             if(SWResourceManager.getModel().contains(r))
183             {
184                trace("OWL Project ALREADY IN MODEL");
185             }
186             else
187             {
188                trace("OWL Project OUTSIDE OF MODEL", false);
189                IOWLProject oProj = SWResourceManager.create((IProject)r);
190                SWResourceManager.getModel().getElementInfo().addChild((IOWLProject)oProj);
191                return;
192             }
193          }
194          else if(r instanceof IProject && !((IProject)r).isOpen())
195          {
196             IOWLModel model = SWResourceManager.getModel();
197             IOWLElement element = model.getCorrespondingElement(r);
198             if(element != null)
199             {
200                element.getParent().getElementInfo().removeChild(element);
201             }
202          }
203          if(r instanceof IFile)
204          {
205             IOWLDocument doc = (IOWLDocument)(SWResourceManager.getModel()
206                .getCorrespondingElement(r));
207             doc.getDocumentInfo().buildChildren();
208          }
209       }
210       catch (CoreException ce)
211       {
212          trace("Unable to investigate new Project for OWL Nature", true);       
213          return;
214       }
215    }
216 
217    /* ***************************************************************************
218     * DEBUG METHODS
219     * **************************************************************************/
220    private void trace(String s)
221    {
222       trace(s, false);
223    }
224 
225    private void trace(String s, boolean error)
226    {
227       if(true)
228       {
229          return;
230       }
231       OWLCore.trace("OWLModelDeltaVisitor", s, error);
232    }
233 }