Author: timotei
Date: Sun Jul 10 22:47:56 2011
New Revision: 50267

URL: http://svn.gna.org/viewcvs/wesnoth?rev=50267&view=rev
Log:
eclipse plugin: Refactor and reorganize the whole
list builder

Modified:
    
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/builder/DependencyListBuilder.java
    
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/builder/WesnothProjectBuilder.java

Modified: 
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/builder/DependencyListBuilder.java
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/builder/DependencyListBuilder.java?rev=50267&r1=50266&r2=50267&view=diff
==============================================================================
--- 
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/builder/DependencyListBuilder.java
 (original)
+++ 
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/builder/DependencyListBuilder.java
 Sun Jul 10 22:47:56 2011
@@ -13,14 +13,13 @@
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Queue;
 import java.util.Set;
-import java.util.concurrent.LinkedBlockingDeque;
 
 import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IFile;
@@ -54,12 +53,20 @@
     private DependencyListNode previous_;
 
     protected Map< String, DependencyListNode > list_;
+
+    /**
+     * Holds a list of directories that are parsed in the WML order
+     * (that is, they don't have a _main.cfg in them) and
+     * the value is the first node in that directory existing in the list
+     */
     protected List< String > directories_;
+    protected List< DependencyListNode > directoriesEntries_;
 
     public DependencyListBuilder( IProject project )
     {
         list_ = new HashMap<String, DependencyListNode>();
         directories_ = new ArrayList<String>();
+        directoriesEntries_ = new ArrayList<DependencyListNode>();
 
         previous_ = null;
 
@@ -76,7 +83,7 @@
     public void createDependencyList( boolean force )
     {
         if ( isCreated_ && !force ) {
-            Logger.getInstance( ).log( " Skipping depedency list for project " 
+
+            Logger.getInstance( ).log( "Skipping depedency list for project " +
                     project_.getName( ) );
             return;
         }
@@ -85,63 +92,251 @@
         currentIndex_ = 0;
         previous_ = null;
         list_.clear( );
-
-        // start creating the PDL (project dependency List)
-        Queue<IContainer> containers = new LinkedBlockingDeque<IContainer>( );
-
-        containers.add( project_ );
-
-        while( containers.isEmpty( ) == false ) {
-            IContainer container = containers.poll( );
-
-            IResource main_cfg = container.findMember( "_main.cfg" ); 
//$NON-NLS-1$
-            if ( main_cfg != null ) {
-                // add main.cfg to list
-                internal_addNode( (IFile) main_cfg );
-
-                Set<String> containersToAdd = getContainers( (IFile) main_cfg 
);
-                // push the containers in the queue
-                for ( String containerPath : containersToAdd ) {
-                    containers.offer( project_.getFolder( containerPath ) );
+        directories_.clear( );
+        directoriesEntries_.clear( );
+
+        internal_addContainer( null );
+
+        System.out.println( toString( ) );
+    }
+
+    /**
+     * Adds a new node in the PDL
+     * @param file The file to add
+     * @return The newly created node
+     */
+    public DependencyListNode addNode( IFile file )
+    {
+        DependencyListNode backupPrevious = previous_, newNode = null;
+        String fileProjectPath = file.getProjectRelativePath( ).toString( );
+
+        // we add a file in an existing processed directory.
+        if ( directories_.contains( fileProjectPath ) ) {
+
+            int dirEntryIndex = directories_.indexOf( fileProjectPath );
+
+            /* TODO: check if the file is _main.cfg. If yes,
+             * all current nodes from this directory need to be erased
+             * and processed by the includes from other _main.cfg
+             */
+            DependencyListNode tmpNode = directoriesEntries_.get( 
dirEntryIndex );
+
+            // had any files in dir?
+            if ( tmpNode != null ) {
+                // search for the correct place in current dir
+                String fileName = file.getName( );
+
+                DependencyListNode prevTmpNode = null;
+                while ( tmpNode != null ) {
+
+                    // we found the place?
+                    if ( ResourceUtils.wmlFileNameCompare(
+                            fileName, tmpNode.getFile( ).getName( ) ) > 0 ) {
+
+                        previous_ = tmpNode.getPrevious( );
+
+                        newNode = internal_addNode( file );
+                        break;
+                    }
+
+                    prevTmpNode = tmpNode;
+                    tmpNode = tmpNode.getNext( );
                 }
 
-            }else {
-                // no main.cfg, just follow WML reading rules
-
-                List<IResource> members = null;
-                try {
-                    members = Arrays.asList( container.members( ) );
+                // we arrived at the end
+                if ( newNode == null ) {
+                    previous_ = prevTmpNode;
+                    newNode = internal_addNode( file );
                 }
-                catch ( CoreException e ) {
-                    Logger.getInstance( ).logException( e );
-
-                    continue;
+            } else {
+
+                // previous_ should be the first non-null node in previous
+                // directories.
+
+                while ( dirEntryIndex > 0 && tmpNode == null ) {
+                    -- dirEntryIndex;
+                    tmpNode = directoriesEntries_.get( dirEntryIndex );
                 }
 
-                Collections.sort( members, new WMLFilesComparator() );
-
-                if ( members.isEmpty( ) )
-                    continue;
-
-                previous_ = null;
-
-                for ( IResource resource : members ) {
-                    if ( resource instanceof IContainer )
-                        containers.add( (IContainer)resource );
-                    else {
-                        // just config files.
-                        if ( !ResourceUtils.isConfigFile( resource ) )
-                            continue;
-
+                previous_ = tmpNode;
+                newNode = internal_addNode( file );
+            }
+
+            // now, parse the file to check if we should include other dirs
+            internal_addContainers( getContainers( file ) );
+        } else {
+            // didn't found any place to put it. where shall we?
+
+            //TODO: the place should be dictated by other cfg,
+            // by getting the included directory
+        }
+
+        // restore old previous
+        previous_ = backupPrevious;
+
+        // print the new list
+        System.out.println( toString( ) );
+        return newNode;
+    }
+
+    /**
+     * Adds the containers and their contents to the list
+     * @param containerList The list of container paths
+     */
+    private void internal_addContainers( Collection<String> containerList )
+    {
+        for ( String container : containerList ) {
+            internal_addContainer( container );
+        }
+    }
+
+    /**
+     * Add the container and it's contents to the list
+     * @param containerPath The path of the container
+     */
+    private void internal_addContainer( String containerPath )
+    {
+        IContainer container = null ;
+        if ( containerPath == null )
+            container = project_;
+        else
+            container = project_.getFolder( containerPath );
+
+        IResource main_cfg = container.findMember( "_main.cfg" ); //$NON-NLS-1$
+        if ( main_cfg != null ) {
+            // add main.cfg to list
+            internal_addNode( (IFile) main_cfg );
+
+            // add any included containers
+            internal_addContainers( getContainers( (IFile) main_cfg ) );
+        }else {
+            List<IResource> members = null;
+            try {
+                members = Arrays.asList( container.members( ) );
+            }
+            catch ( CoreException e ) {
+                Logger.getInstance( ).logException( e );
+
+                return;
+            }
+
+            Collections.sort( members, new WMLFilesComparator() );
+
+            boolean toAddDirectoryEntry = false;
+            // no main.cfg, just follow WML reading rules
+            if ( ! directories_.contains( container.getProjectRelativePath( 
).toString( ) ) ) {
+                directories_.add( container.getProjectRelativePath( 
).toString( ) );
+                directoriesEntries_.add( null );
+
+                toAddDirectoryEntry = true;
+            }
+
+            if ( members.isEmpty( ) )
+                return;
+
+            DependencyListNode firstNewNode = null;
+
+            for ( IResource resource : members ) {
+                if ( resource instanceof IContainer )
+                    internal_addContainer( resource.getProjectRelativePath( 
).toString( ) );
+                else {
+                    // just config files.
+                    if ( !ResourceUtils.isConfigFile( resource ) )
+                        continue;
+
+                    if ( firstNewNode != null )
                         internal_addNode( ( IFile ) resource );
-                    }
+                    else
+                        firstNewNode = internal_addNode( (IFile) resource );
                 }
             }
-        }
-
+
+            if ( firstNewNode != null && toAddDirectoryEntry ) {
+                // update the first directory node
+                directoriesEntries_.set( directories_.size( ) - 1, 
firstNewNode );
+            }
+        }
+    }
+
+    /**
+     * Adds a new node to this list
+     * @param file The file to add
+     * @return The newly created node
+     */
+    private DependencyListNode internal_addNode( IFile file )
+    {
+        DependencyListNode newNode = new DependencyListNode( file, -1 );
+
+        if ( previous_ != null ){
+
+            // inserting is done between 2 nodes
+            if ( previous_.getNext( ) != null ){
+                newNode.setIndex(
+                        (previous_.getIndex( ) +
+                         previous_.getNext( ).getIndex( )) / 2 );
+
+                newNode.setNext( previous_.getNext( ) );
+                previous_.getNext( ).setPrevious( newNode );
+            } else {
+                newNode.setIndex( currentIndex_ );
+                currentIndex_ += DependencyListNode.INDEX_STEP;
+            }
+
+            previous_.setNext( newNode );
+            newNode.setPrevious( previous_ );
+        } else {
+            // no previous yet (== null)
+            // so we're making this the root node for this list
+
+            // check if we had a previous root node
+            DependencyListNode root = list_.get( ROOT_NODE_KEY );
+            if ( root != null ) {
+                root.setPrevious( newNode );
+                newNode.setNext( root );
+
+                newNode.setIndex( root.getIndex( ) - 
DependencyListNode.INDEX_STEP );
+            } else {
+                newNode.setIndex( currentIndex_ );
+                currentIndex_ += DependencyListNode.INDEX_STEP;
+            }
+
+            list_.put( ROOT_NODE_KEY, newNode ); //$NON-NLS-1$
+        }
+
+        list_.put( file.getProjectRelativePath( ).toString( ), newNode );
+        previous_ = newNode;
+        return newNode;
+    }
+
+    /**
+     * Removes a node specified by the file
+     * @param file The file to remove from the list
+     */
+    public void removeNode( IFile file )
+    {
+        DependencyListNode node = getNode( file );
+
+        // the node didn't even exist in the list!?
+        if ( node == null )
+            return;
+
+        if ( node.getPrevious( ) != null )
+            node.getPrevious( ).setNext( node.getNext( ) );
+        if ( node.getNext( ) != null )
+            node.getNext( ).setPrevious( node.getPrevious( ) );
+
+        list_.remove( file.getProjectRelativePath( ).toString( ) );
+
+        //debug
         System.out.println( toString( ) );
     }
 
+    /**
+     * Gets the set of included containers in this file
+     * as a macro call
+     * @param file The file to get the containers from
+     * @return A set of containers represented by their Path as string
+     */
     public static Set<String> getContainers( IFile file )
     {
         IProject project = file.getProject( );
@@ -199,136 +394,6 @@
         return containersToAdd;
     }
 
-    public DependencyListNode addNode( IFile file )
-    {
-        // save current nodes
-        DependencyListNode previousBak = previous_, newNode = null ;
-
-        // find the correct previous and parent to place the new node
-        String parentPath = file.getParent( ).getProjectRelativePath( ).
-            removeTrailingSeparator( ).toString( );
-        String fileName = file.getName( );
-
-        DependencyListNode root = getNode( ROOT_NODE_KEY );
-
-        while ( root != null ) {
-
-            if ( root.getFile( ).getParent( ).getProjectRelativePath( ).
-                    removeTrailingSeparator( ).toString( ).
-                    equals( parentPath.toString( ) ) ) {
-
-                // found the directory. Now find the place
-                DependencyListNode leaf = root;
-                while ( leaf != null ) {
-
-                    // we found the place?
-                    if ( ResourceUtils.wmlFileNameCompare(
-                            fileName,
-                            leaf.getFile( ).getName( ) ) < 0 ) {
-
-                        previous_ = leaf.getPrevious( );
-
-                        newNode = internal_addNode( file );
-
-                        // update links
-                        newNode.setNext( leaf );
-                        leaf.setPrevious( newNode );
-                        break;
-                    }
-
-                    leaf = leaf.getNext( );
-                }
-
-                break;
-            }
-
-            root = root.getNext( );
-        }
-
-        // didn't found any place to put it. where shall we?
-        //TODO: the place should be dictated by other cfg,
-        // by getting the included directory
-        if ( newNode == null ) {
-
-        }
-
-        // restore nodes
-        previous_ = previousBak;
-
-        // print the new list
-        System.out.println( toString( ) );
-        return newNode;
-    }
-
-    /**
-     * Adds a new node to this list
-     * @param file The file to add
-     */
-    private DependencyListNode internal_addNode( IFile file )
-    {
-        DependencyListNode newNode = new DependencyListNode( file, -1 );
-
-        if ( previous_ != null ){
-
-            // inserting is done between 2 nodes
-            if ( previous_.getNext( ) != null ){
-                newNode.setIndex(
-                        (previous_.getIndex( ) +
-                         previous_.getNext( ).getIndex( )) / 2 );
-
-                newNode.setNext( previous_.getNext( ) );
-                previous_.getNext( ).setPrevious( newNode );
-            } else {
-                newNode.setIndex( currentIndex_ );
-                currentIndex_ += DependencyListNode.INDEX_STEP;
-            }
-
-            previous_.setNext( newNode );
-            newNode.setPrevious( previous_ );
-        } else {
-            // no previous yet (== null)
-            // so we're making this the root node for this list
-
-            // check if we had a previous root node
-            DependencyListNode root = list_.get( ROOT_NODE_KEY );
-            if ( root != null ) {
-                root.setPrevious( newNode );
-                newNode.setNext( root );
-
-                newNode.setIndex( root.getIndex( ) - 
DependencyListNode.INDEX_STEP );
-            } else {
-                newNode.setIndex( currentIndex_ );
-                currentIndex_ += DependencyListNode.INDEX_STEP;
-            }
-
-            list_.put( ROOT_NODE_KEY, newNode ); //$NON-NLS-1$
-        }
-
-        list_.put( file.getProjectRelativePath( ).toString( ), newNode );
-        previous_ = newNode;
-        return newNode;
-    }
-
-    /**
-     * Removes a node specified by the file
-     * @param file The file to remove from the list
-     */
-    public void removeNode( IFile file )
-    {
-        DependencyListNode node = getNode( file );
-
-        // the node didn't even exist in the list!?
-        if ( node == null )
-            return;
-
-        if ( node.getPrevious( ) != null )
-            node.getPrevious( ).setNext( node.getNext( ) );
-        if ( node.getNext( ) != null )
-            node.getNext( ).setPrevious( node.getPrevious( ) );
-
-        list_.remove( file.getProjectRelativePath( ).toString( ) );
-    }
-
     /**
      * Returns the node specified by the file
      * @param file The file to get the depedency node for

Modified: 
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/builder/WesnothProjectBuilder.java
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/builder/WesnothProjectBuilder.java?rev=50267&r1=50266&r2=50267&view=diff
==============================================================================
--- 
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/builder/WesnothProjectBuilder.java
 (original)
+++ 
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/builder/WesnothProjectBuilder.java
 Sun Jul 10 22:47:56 2011
@@ -190,8 +190,6 @@
                     else
                         nodesToProcess.add( newNode );
                 } else if ( deltaKind == IResourceDelta.CHANGED ) {
-                    //TODO: check if the included directories have changed 
their
-                    // order
                     DependencyListNode node = list.getNode( file );
                     if ( node == null )
                         Logger.getInstance( ).logError( "Couldn't find file "
@@ -268,6 +266,8 @@
 
        protected boolean checkResource( IResource resource, IProgressMonitor 
monitor )
        {
+           if ( true )
+               return true;
                monitor.worked(5);
                if ( resource.exists() == false ||
                         monitor.isCanceled() )


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to