Ignore:
Timestamp:
02/08/06 11:46:34 (19 years ago)
Author:
boris
Message:
  • when the package.mo file is changed or added/removed the parent node may need to morph to folder node or package. the core plugin detects such situations now and changes the resource tree accordinantly
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/org.modelica.mdt.test/src/org/modelica/mdt/test/TestModelicaRoot.java

    r291 r299  
    4646import java.util.Vector;
    4747
     48import org.eclipse.core.resources.IFile;
     49import org.eclipse.core.resources.IFolder;
    4850import org.eclipse.core.resources.IProject;
    4951import org.eclipse.core.resources.ResourcesPlugin;
    5052import org.eclipse.core.runtime.CoreException;
     53import org.eclipse.core.runtime.Path;
    5154import org.eclipse.core.resources.IWorkspaceRoot;
    5255
     56import org.modelica.mdt.core.IModelicaClass;
     57import org.modelica.mdt.core.IModelicaElement;
    5358import org.modelica.mdt.core.IModelicaElementChange;
    5459import org.modelica.mdt.core.IModelicaElementChangeListener;
     60import org.modelica.mdt.core.IModelicaFolder;
    5561import org.modelica.mdt.core.IModelicaProject;
    5662import org.modelica.mdt.core.IModelicaRoot;
    5763import org.modelica.mdt.core.ModelicaCore;
     64import org.modelica.mdt.core.compiler.CompilerInstantiationException;
     65import org.modelica.mdt.core.compiler.ConnectException;
     66import org.modelica.mdt.core.compiler.InvocationError;
     67import org.modelica.mdt.core.compiler.UnexpectedReplyException;
    5868import org.modelica.mdt.internal.core.CorePlugin;
    5969import org.modelica.mdt.test.util.Area51Projects;
     
    6777public class TestModelicaRoot extends TestCase
    6878{
    69     /* this flags are set from the modelica element change listener */
     79
     80    /* this flags are set from the workspace change listener */
    7081    private boolean simpleProjectAdded     = false;
    7182    private boolean modelicaProjectAdded   = false;
    7283    private boolean simpleProjectRemoved   = false;
    7384    private boolean modelicaProjectRemoved = false;
    74    
    75     public class ElementListener implements IModelicaElementChangeListener
     85
     86   
     87    public class WorkspaceChangesListener
     88            implements IModelicaElementChangeListener
    7689    {
    7790
     
    115128    }
    116129
     130    /* this flag is set from folder to package morph detector */
     131    private boolean morphedToPackage = false;
     132   
     133    /**
     134     * This class detect when the MORPH_FOLDER folder have morped into
     135     * a package by listening to modelica change events.
     136     *
     137     * To engage this class register it as a listener with modelica root. When
     138     * the morph is detected the morphedToPackage flag is set.
     139     */
     140    public class FolderToPackageMorphDetector
     141        implements IModelicaElementChangeListener
     142    {
     143        private boolean folderRemoved = false;
     144        private boolean packageAdded = false;
     145
     146        public void elementsChanged(Collection<IModelicaElementChange> changes)
     147        {
     148            for (IModelicaElementChange ch : changes)
     149            {
     150                IModelicaElement element = ch.getElement();
     151               
     152                if (element.getElementName().equals(MORPH_FOLDER))
     153                {
     154                    /*
     155                     * we are waiting for a removed event for the
     156                     * folder event and added event for the package object
     157                     */
     158                    switch(ch.getChangeType())
     159                    {
     160                    case REMOVED:
     161                        folderRemoved = true;
     162                        break;
     163                    case ADDED:
     164                        assertTrue("element morphed into something wierd",
     165                                element instanceof IModelicaClass);
     166                        packageAdded = true;
     167                        break;
     168                    }
     169                   
     170                    /*
     171                     * when both removed and added events recieved we know
     172                     * that the folder have morphed
     173                     */
     174                    morphedToPackage = (folderRemoved && packageAdded);
     175                }
     176            }
     177        }
     178    }
     179
     180    /* this flag is set from folder to package morph detector */
     181    private boolean morphedToFolder = false;
     182   
     183    /**
     184     * This class detect when the MORPH_FOLDER package have morped back into
     185     * a folder by listening to modelica change events.
     186     *
     187     * To engage this class register it as a listener with modelica root. When
     188     * the morph is detected the morphedToFolder flag is set.
     189     */
     190    public class PackageToFolderMorphDetector
     191        implements IModelicaElementChangeListener
     192    {
     193        private boolean packageRemoved = false;
     194        private boolean folderAdded = false;
     195
     196        public void elementsChanged(Collection<IModelicaElementChange> changes)
     197        {
     198            for (IModelicaElementChange ch : changes)
     199            {
     200                IModelicaElement element = ch.getElement();
     201               
     202                if (element.getElementName().equals(MORPH_FOLDER))
     203                {
     204                    /*
     205                     * we are waiting for a removed event for the
     206                     * folder event and added event for the package object
     207                     */
     208                    switch(ch.getChangeType())
     209                    {
     210                    case REMOVED:
     211                        packageRemoved = true;
     212                        break;
     213                    case ADDED:
     214                        assertTrue("element morphed into something wierd",
     215                                element instanceof IModelicaFolder);
     216                        folderAdded = true;
     217                        break;
     218                    }
     219                   
     220                    /*
     221                     * when both removed and added events recieved we know
     222                     * that the folder have morphed
     223                     */
     224                    morphedToFolder = (packageRemoved && folderAdded);
     225                }
     226            }
     227        }
     228    }
     229       
     230   
    117231    /* names of modelica projects */
    118232    private Vector<String> modelicaProjects = new Vector<String>(2);
     
    128242        TestModelicaRoot.class.getName() + "3";
    129243   
     244    private static final String MORPH_FOLDER = "morph_folder";
     245   
    130246    /* the SIMple project which is added and removed */
    131247    private static final String PROJECT_NAME_SIM_EXTRA =
     
    145261    private IModelicaRoot modelicaRoot =
    146262            ModelicaCore.getModelicaRoot();
     263   
     264    private IFolder morphFolder = null;
     265
    147266   
    148267    @Override
     
    159278            IModelicaProject moProj =
    160279                ModelicaCore.getModelicaRoot().createProject(PROJECT_NAME_1);
    161             assertNotNull("failed to create project", moProj);     
    162         }
     280            assertNotNull("failed to create project", moProj);
     281            project = moProj.getProject();
     282            /*
     283             * create the folder that will be morphed to a package and then
     284             * back to folder (se testMorphing())
     285             */
     286            IFolder folder = project.getFolder(new Path(MORPH_FOLDER));
     287            folder.create(false, true, null);
     288           
     289        }
     290        morphFolder = project.getFolder(new Path(MORPH_FOLDER));
    163291       
    164292        /*
     
    251379    {
    252380        ModelicaCore.getModelicaRoot().
    253             addModelicaElementChangeListener(new ElementListener());
     381            addModelicaElementChangeListener(new WorkspaceChangesListener());
    254382       
    255383        /*
     
    327455        }
    328456    }
     457   
     458    /**
     459     * test that a IModelicaFolder changes to an IModelicaPackage
     460     * when a package.mo with correct contents is added to it and
     461     * that IModelicaPackage turns back to IModelicaFolder when
     462     * the package.mo is removed
     463     */
     464    public void testMorphing()
     465        throws CoreException, ConnectException, UnexpectedReplyException,
     466            InvocationError, CompilerInstantiationException
     467    {
     468        /* we need to make sure that the folder we are going to morph is loaded */
     469        Utility.getProject(PROJECT_NAME_1).getRootFolder().getChildren();
     470       
     471        /*
     472         * check if folder will morph into a package when the package.mo
     473         * file is added
     474         */
     475        IModelicaElementChangeListener listener =
     476            new FolderToPackageMorphDetector();
     477           
     478        ModelicaCore.getModelicaRoot().addModelicaElementChangeListener
     479            (listener);
     480        IFile file = morphFolder.getFile("package.mo");
     481       
     482        String contents =
     483            "package " + MORPH_FOLDER + "\n" +
     484            "\n" +
     485            "end " + MORPH_FOLDER + ";";
     486       
     487        file.create(Utility.getByteStream(contents), true, null);
     488       
     489       
     490        /* wait tops 5 seconds for the morph to kick in */
     491        long waitUntil = System.currentTimeMillis() + 5000;
     492        while ((!morphedToPackage) && waitUntil > System.currentTimeMillis())
     493        {
     494            Utility.sleep(this, 100);
     495        }
     496       
     497        ModelicaCore.getModelicaRoot().
     498            removeModelicaElementChangeListener(listener);
     499        assertTrue("the folder didn't morph into package, waited to the"+
     500                " change around 5 seconds", morphedToPackage);
     501
     502        /*
     503         * check if package will morph into a folder when the package.mo
     504         * file is removed
     505         */
     506
     507        listener =  new PackageToFolderMorphDetector();
     508           
     509        ModelicaCore.getModelicaRoot().addModelicaElementChangeListener
     510            (listener);
     511        file.delete(true, false, null);
     512       
     513        /* wait tops 5 seconds for the morph to kick in */
     514        waitUntil = System.currentTimeMillis() + 5000;
     515        while ((!morphedToFolder) && waitUntil > System.currentTimeMillis())
     516        {
     517            Utility.sleep(this, 100);
     518        }
     519       
     520        ModelicaCore.getModelicaRoot().
     521            removeModelicaElementChangeListener(listener);
     522        assertTrue("the package didn't morph into folder, waited to the"+
     523                " change around 5 seconds", morphedToFolder);
     524
     525    }
    329526}
Note: See TracChangeset for help on using the changeset viewer.