View Javadoc

1   /*
2    * $Id: ModelUpdater.java,v 1.2 2005/05/31 16:26:41 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.IResourceChangeEvent;
13  import org.eclipse.core.resources.IResourceChangeListener;
14  import org.eclipse.core.resources.IResourceDelta;
15  import org.eclipse.core.runtime.CoreException;
16  
17  import com.bbn.swede.core.IOWLElement;
18  import com.bbn.swede.core.IOWLElementVisitor;
19  import com.bbn.swede.core.IOWLFolder;
20  import com.bbn.swede.core.OWLCore;
21  
22  /***
23   * Keeps the OWL model in synch with changes to the Eclipse workspace.
24   * @author tself
25   */
26  public class ModelUpdater implements IResourceChangeListener
27  {
28     private OWLModelDeltaVisitor _deltaVisitor;
29  
30     /***
31      * Creates an updater to propagate resource changes into the OWL model.
32      */
33     public ModelUpdater()
34     {
35        _deltaVisitor = new OWLModelDeltaVisitor();
36     }
37  
38     /***
39      * An OWL Element visitor that prunes empty folders from the OWL model.
40      * @author jlerner
41      */
42     private class EmptyFolderVisitor implements IOWLElementVisitor
43     {
44  
45        /*
46         *  (non-Javadoc)
47         * @see com.bbn.swede.core.IOWLElementVisitor#visit(com.bbn.swede.core.IOWLElement)
48         */
49        public boolean visit(IOWLElement element)
50        {
51           if (element instanceof IOWLFolder)
52           {
53              IOWLFolder fold = (IOWLFolder)element;
54              if (fold.getElementInfo().getChildren().length == 0)
55              {
56                 IOWLElement parent = fold.getParent();
57                 parent.getElementInfo().removeChild(fold);
58                 return false;
59              }
60           }
61           return true;
62        }
63        
64     }
65     /*
66      *  (non-Javadoc)
67      * @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(
68      *       org.eclipse.core.resources.IResourceChangeEvent)
69      */
70     public void resourceChanged(IResourceChangeEvent event)
71     {
72        IResourceDelta delta = event.getDelta();
73        if(delta != null)
74        {
75           try
76           {
77              delta.accept(_deltaVisitor);
78              SWResourceManager.getModel().accept(new EmptyFolderVisitor());
79           }
80           catch (CoreException ce)
81           {
82              OWLCore.logWarning(OWLCore.getID(), "Unable to process resource delta.", ce);
83           }
84        }
85     }
86  }