eclipse plugin: Make the 'Tree' be 'List'

This commit is contained in:
Timotei Dolean 2011-07-10 20:47:37 +00:00
parent c67dd24831
commit 266d32cf58
4 changed files with 91 additions and 178 deletions

View file

@ -38,12 +38,12 @@ import org.wesnoth.utils.ResourceUtils.WMLFilesComparator;
import org.wesnoth.wml.WMLMacroCall;
import org.wesnoth.wml.WMLRoot;
public class DependencyTreeBuilder implements Serializable
public class DependencyListBuilder implements Serializable
{
private static final long serialVersionUID = 6007509520015856611L;
/**
* The key by which the rood node of the tree is memorized
* in the tree.
* The key by which the root node of the list is memorized
* in the list.
*/
public static final String ROOT_NODE_KEY = "_ROOT_";
@ -51,18 +51,17 @@ public class DependencyTreeBuilder implements Serializable
protected boolean isCreated_;
protected int currentIndex_;
private ProjectDependencyNode parent_;
private ProjectDependencyNode previous_;
private DependencyListNode previous_;
protected Map< String, ProjectDependencyNode > tree_;
protected Map< String, DependencyListNode > list_;
protected List< String > directories_;
public DependencyTreeBuilder( IProject project )
public DependencyListBuilder( IProject project )
{
tree_ = new HashMap<String, ProjectDependencyNode>();
list_ = new HashMap<String, DependencyListNode>();
directories_ = new ArrayList<String>();
parent_ = previous_ = null;
previous_ = null;
project_ = project;
isCreated_ = false;
@ -70,37 +69,34 @@ public class DependencyTreeBuilder implements Serializable
}
/**
* Create the whole dependency tree from scratch.
* @param force True for force re-creating the tree even if it
* Create the whole dependency list from scratch.
* @param force True for force re-creating the list even if it
* was previously created
*/
public void createDependencyTree( boolean force )
public void createDependencyList( boolean force )
{
if ( isCreated_ && !force ) {
Logger.getInstance( ).log( " Skipping depedency tree for project " +
Logger.getInstance( ).log( " Skipping depedency list for project " +
project_.getName( ) );
return;
}
isCreated_ = true;
currentIndex_ = 0;
parent_ = previous_ = null;
tree_.clear( );
previous_ = null;
list_.clear( );
// start creating the PDT (project dependency tree)
// start creating the PDL (project dependency List)
Queue<IContainer> containers = new LinkedBlockingDeque<IContainer>( );
containers.add( project_ );
while( containers.isEmpty( ) == false ) {
// each container should start a new "branch"
previous_ = null;
IContainer container = containers.poll( );
IResource main_cfg = container.findMember( "_main.cfg" ); //$NON-NLS-1$
if ( main_cfg != null ) {
// add main.cfg to tree
// add main.cfg to list
internal_addNode( (IFile) main_cfg );
Set<String> containersToAdd = getContainers( (IFile) main_cfg );
@ -203,18 +199,17 @@ public class DependencyTreeBuilder implements Serializable
return containersToAdd;
}
public ProjectDependencyNode addNode( IFile file )
public DependencyListNode addNode( IFile file )
{
// save current nodes
ProjectDependencyNode parentBak = parent_, previousBak = previous_, newNode = null ;
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( );
ProjectDependencyNode root = getNode( ROOT_NODE_KEY );
parent_ = null;
DependencyListNode root = getNode( ROOT_NODE_KEY );
while ( root != null ) {
@ -223,7 +218,7 @@ public class DependencyTreeBuilder implements Serializable
equals( parentPath.toString( ) ) ) {
// found the directory. Now find the place
ProjectDependencyNode leaf = root;
DependencyListNode leaf = root;
while ( leaf != null ) {
// we found the place?
@ -238,21 +233,6 @@ public class DependencyTreeBuilder implements Serializable
// update links
newNode.setNext( leaf );
leaf.setPrevious( newNode );
if ( leaf.getSon( ) != null ){
newNode.setSon( leaf.getSon( ) );
leaf.getSon( ).setParent( newNode );
leaf.setSon( null );
}
if ( leaf.getParent( ) != null ) {
newNode.setParent( leaf.getParent( ) );
leaf.getParent( ).setSon( newNode );
leaf.setParent( null );
}
break;
}
@ -262,8 +242,7 @@ public class DependencyTreeBuilder implements Serializable
break;
}
parent_ = root;
root = root.getSon( );
root = root.getNext( );
}
// didn't found any place to put it. where shall we?
@ -274,54 +253,45 @@ public class DependencyTreeBuilder implements Serializable
}
// restore nodes
parent_ = parentBak;
previous_ = previousBak;
// print the new tree
// print the new list
System.out.println( toString( ) );
return newNode;
}
/**
* Adds a new node to this tree
* Adds a new node to this list
* @param file The file to add
*/
private ProjectDependencyNode internal_addNode( IFile file )
private DependencyListNode internal_addNode( IFile file )
{
ProjectDependencyNode newNode = new ProjectDependencyNode( file, currentIndex_ );
currentIndex_ += ProjectDependencyNode.INDEX_STEP;
DependencyListNode newNode = new DependencyListNode( file, currentIndex_ );
currentIndex_ += DependencyListNode.INDEX_STEP;
if ( previous_ != null ){
previous_.setNext( newNode );
newNode.setPrevious( previous_ );
} else {
// first node in the current directory -> make it son of parent
if ( parent_ != null ) {
parent_.setSon( newNode );
newNode.setParent( parent_ );
} else {
// no parent yet (== null)
// so we're making this the root node for this tree
tree_.put( ROOT_NODE_KEY, newNode ); //$NON-NLS-1$
}
parent_ = newNode;
// no previous yet (== null)
// so we're making this the root node for this list
list_.put( ROOT_NODE_KEY, newNode ); //$NON-NLS-1$
}
tree_.put( file.getProjectRelativePath( ).toString( ), newNode );
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 tree
* @param file The file to remove from the list
*/
public void removeNode( IFile file )
{
ProjectDependencyNode node = getNode( file );
DependencyListNode node = getNode( file );
// the node didn't even exist in the tree!?
// the node didn't even exist in the list!?
if ( node == null )
return;
@ -330,17 +300,17 @@ public class DependencyTreeBuilder implements Serializable
if ( node.getNext( ) != null )
node.getNext( ).setPrevious( node.getPrevious( ) );
tree_.remove( file.getProjectRelativePath( ).toString( ) );
list_.remove( file.getProjectRelativePath( ).toString( ) );
}
/**
* Returns the node specified by the file
* @param file The file to get the depedency node for
* @return An instance of {@link ProjectDependencyNode}
* @return An instance of {@link DependencyListNode}
*/
public ProjectDependencyNode getNode( IFile file )
public DependencyListNode getNode( IFile file )
{
return tree_.get( file.getProjectRelativePath( ).toString( ) );
return list_.get( file.getProjectRelativePath( ).toString( ) );
}
/**
@ -348,15 +318,15 @@ public class DependencyTreeBuilder implements Serializable
* usually project-relative paths for project's files, or
* the {@link #ROOT_NODE_KEY}
* @param key The key to get the node by
* @return An instance of {@link ProjectDependencyNode}
* @return An instance of {@link DependencyListNode}
*/
public ProjectDependencyNode getNode ( String key )
public DependencyListNode getNode ( String key )
{
return tree_.get( key );
return list_.get( key );
}
/**
* Returns true if the tree was already created, false otherwise
* Returns true if the list was already created, false otherwise
* @return A boolean value
*/
public boolean getIsCreated()
@ -372,18 +342,17 @@ public class DependencyTreeBuilder implements Serializable
*/
public void deserialize( ObjectInputStream input ) throws IOException, ClassNotFoundException
{
DependencyTreeBuilder tmp = (DependencyTreeBuilder) input.readObject( );
DependencyListBuilder tmp = (DependencyListBuilder) input.readObject( );
if ( tmp == null )
return;
this.currentIndex_ = tmp.currentIndex_;
this.isCreated_ = tmp.isCreated_;
this.tree_ = tmp.tree_;
this.list_ = tmp.list_;
this.previous_ = tmp.previous_;
this.parent_ = tmp.parent_;
// now, refill the dependency nodes
for ( ProjectDependencyNode node : tree_.values( ) ) {
for ( DependencyListNode node : list_.values( ) ) {
node.file_ = project_.getFile( node.fileName_ );
}
}
@ -392,20 +361,14 @@ public class DependencyTreeBuilder implements Serializable
public String toString()
{
StringBuilder str = new StringBuilder( );
str.append( "tree: \n" ); //$NON-NLS-1$
if ( !tree_.isEmpty( ) ) {
ProjectDependencyNode node = tree_.get( ROOT_NODE_KEY );
str.append( "list: \n" ); //$NON-NLS-1$
if ( !list_.isEmpty( ) ) {
DependencyListNode node = list_.get( ROOT_NODE_KEY );
do {
str.append( "> " ); //$NON-NLS-1$
ProjectDependencyNode leaf = node;
str.append( node + "; " ); //$NON-NLS-1$
do {
str.append( leaf + "; " ); //$NON-NLS-1$
leaf = leaf.getNext( );
} while ( leaf != null );
node = node.getSon( );
node = node.getNext( );
str.append( "\n" ); //$NON-NLS-1$
}while ( node != null );
}

View file

@ -19,14 +19,13 @@ import org.wesnoth.Constants;
import org.wesnoth.Logger;
/**
* This class represents a node
* in the Project's Depedency tree,
* This class represents a node in the Project's Depedency list,
* which is constructed on a full build of the project.
*
* Alternatively, a tree node is created
* Alternatively, a list node is created
* when a new resource is added.
*/
public class ProjectDependencyNode implements Serializable
public class DependencyListNode implements Serializable
{
private static final long serialVersionUID = -9140173740211465384L;
@ -39,13 +38,10 @@ public class ProjectDependencyNode implements Serializable
*/
public static final int INDEX_STEP = 10000;
protected static final QualifiedName PDT_INDEX = new QualifiedName( Constants.PLUGIN_ID, "pdt_index" ); //$NON-NLS-1$
protected static final QualifiedName PDL_INDEX = new QualifiedName( Constants.PLUGIN_ID, "pdl_index" ); //$NON-NLS-1$
private ProjectDependencyNode previous_;
private ProjectDependencyNode next_;
private ProjectDependencyNode parent_;
private ProjectDependencyNode son_;
private DependencyListNode previous_;
private DependencyListNode next_;
protected transient IFile file_;
protected String fileName_;
@ -53,9 +49,9 @@ public class ProjectDependencyNode implements Serializable
private int index_;
public ProjectDependencyNode( IFile file, int index )
public DependencyListNode( IFile file, int index )
{
previous_ = next_ = parent_ = son_ = null;
previous_ = next_ = null;
includes_ = new ArrayList<String>();
@ -74,7 +70,7 @@ public class ProjectDependencyNode implements Serializable
}
/**
* Returns the index of this node in the whole dependency tree node
* Returns the index of this node in the whole dependency list
* @return
*/
public int getIndex()
@ -91,54 +87,18 @@ public class ProjectDependencyNode implements Serializable
index_ = index;
try {
file_.setPersistentProperty( PDT_INDEX, Integer.toString( index ) );
file_.setPersistentProperty( PDL_INDEX, Integer.toString( index ) );
}
catch ( CoreException e ) {
Logger.getInstance( ).logException( e );
}
}
/**
* Gets the parent of this node
* @return A node or null if there is no parent
*/
public ProjectDependencyNode getParent()
{
return parent_;
}
/**
* Sets a new parent for this node
* @param parent The parent to set
*/
public void setParent( ProjectDependencyNode parent )
{
parent_ = parent;
}
/**
* Gets the son of this node
* @return A node or null if there is no parent
*/
public ProjectDependencyNode getSon()
{
return son_;
}
/**
* Sets a new son node for this node
* @param son The new son node to set
*/
public void setSon( ProjectDependencyNode son )
{
son_ = son;
}
/**
* Gets the node before the current node
* @return A node or null if there is no parent
*/
public ProjectDependencyNode getPrevious()
public DependencyListNode getPrevious()
{
return previous_;
}
@ -147,7 +107,7 @@ public class ProjectDependencyNode implements Serializable
* Sets a new previous node for this node
* @param previous The new previous node to set
*/
public void setPrevious( ProjectDependencyNode previous )
public void setPrevious( DependencyListNode previous )
{
previous_ = previous;
}
@ -156,7 +116,7 @@ public class ProjectDependencyNode implements Serializable
* Gets the node after the current node
* @return A node or null if there is no parent
*/
public ProjectDependencyNode getNext()
public DependencyListNode getNext()
{
return next_;
}
@ -165,7 +125,7 @@ public class ProjectDependencyNode implements Serializable
* Sets a new next node for this node
* @param next The new next node to set
*/
public void setNext( ProjectDependencyNode next )
public void setNext( DependencyListNode next )
{
next_ = next;
}

View file

@ -46,14 +46,15 @@ import org.wesnoth.wml.core.ConfigFile;
/**
* The builder does the following steps in order to create and ensure
* a correct PDT (Project Dependency Tree)
* a correct PDL (Project Dependency Lits)
*
* 1) remove REMOVED files from the PDT
* 1) remove REMOVED files from the PDL
* 2) parse ADDED or CHANGED files, to check if new directory/file includes
* happened
* 3) add new dependencies to the tree
*/
* 3) add new dependencies to the list
*
//TODO: what happens if a file changes the order of the includes?
*/
public class WesnothProjectBuilder extends IncrementalProjectBuilder
{
private ProjectCache projectCache_;
@ -80,8 +81,8 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
return null;
monitor.worked(5);
monitor.subTask( "Creating the project tree ..." );
projectCache_.getDependencyTree( ).createDependencyTree( false );
monitor.subTask( "Creating the project list ..." );
projectCache_.getDependencyList( ).createDependencyList( false );
monitor.worked( 10 );
// create the temporary directory used by the plugin if not created
@ -128,33 +129,22 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
*/
protected boolean fullBuild(final IProgressMonitor monitor) throws CoreException
{
projectCache_.getDependencyTree( ).createDependencyTree( true );
projectCache_.getDependencyList( ).createDependencyList( true );
boolean foundCfg = false;
ProjectDependencyNode node = null, leaf = null;
DependencyListNode node = null;
node = projectCache_.getDependencyTree( ).getNode(
DependencyTreeBuilder.ROOT_NODE_KEY );
node = projectCache_.getDependencyList( ).getNode( DependencyListBuilder.ROOT_NODE_KEY );
if ( node != null ) {
// going downwards the tree
do {
leaf = node;
foundCfg = true;
// going right the current branch
do {
foundCfg = true;
// process the leaf
checkResource( leaf.getFile( ), monitor );
leaf = leaf.getNext( );
}while ( leaf != null );
node = node.getSon( );
// process the node
checkResource( node.getFile( ), monitor );
node = node.getNext( );
} while ( node != null );
}
@ -173,9 +163,9 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
{
boolean foundCfg = false;
DependencyTreeBuilder tree = projectCache_.getDependencyTree( );
DependencyListBuilder list = projectCache_.getDependencyList( );
Queue<IResourceDelta> deltasQueue = new LinkedBlockingDeque<IResourceDelta>();
List< ProjectDependencyNode > nodesToProcess = new ArrayList<ProjectDependencyNode>();
List< DependencyListNode > nodesToProcess = new ArrayList<DependencyListNode>();
// gather the list of configs modified
deltasQueue.add( delta );
@ -190,22 +180,22 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
int deltaKind = deltaItem.getKind( );
if ( deltaKind == IResourceDelta.REMOVED ) {
projectCache_.getDependencyTree( ).removeNode( file );
projectCache_.getDependencyList( ).removeNode( file );
projectCache_.getConfigs().remove( file.getName() );
} else if ( deltaKind == IResourceDelta.ADDED ){
ProjectDependencyNode newNode = tree.addNode( file );
DependencyListNode newNode = list.addNode( file );
if ( newNode == null )
Logger.getInstance( ).logError( "Couldn't create a new" +
"PDT node for file: " + file.getFullPath( ).toString( ) );
"PDL node for file: " + file.getFullPath( ).toString( ) );
else
nodesToProcess.add( newNode );
} else if ( deltaKind == IResourceDelta.CHANGED ) {
//TODO: check if the included directories have changed their
// order
ProjectDependencyNode node = tree.getNode( file );
DependencyListNode node = list.getNode( file );
if ( node == null )
Logger.getInstance( ).logError( "Couldn't find file "
+ file.getFullPath( ).toString( ) + " in PDT!." );
+ file.getFullPath( ).toString( ) + " in PDL!." );
else
nodesToProcess.add( node );
} else {
@ -217,10 +207,10 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
}
// sort the list by index (ascending)
Collections.sort( nodesToProcess, new Comparator<ProjectDependencyNode> () {
Collections.sort( nodesToProcess, new Comparator<DependencyListNode> () {
@Override
public int compare( ProjectDependencyNode o1, ProjectDependencyNode o2 )
public int compare( DependencyListNode o1, DependencyListNode o2 )
{
if ( o1.getIndex( ) < o2.getIndex( ) )
return -1;
@ -233,7 +223,7 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
foundCfg = ( !nodesToProcess.isEmpty( ) );
// process nodes
for ( ProjectDependencyNode node : nodesToProcess ) {
for ( DependencyListNode node : nodesToProcess ) {
checkResource( node.getFile( ), monitor );
}

View file

@ -21,7 +21,7 @@ import org.eclipse.jface.dialogs.DialogSettings;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.wesnoth.Constants;
import org.wesnoth.Logger;
import org.wesnoth.builder.DependencyTreeBuilder;
import org.wesnoth.builder.DependencyListBuilder;
import org.wesnoth.preferences.Preferences;
import org.wesnoth.preprocessor.Define;
import org.wesnoth.preprocessor.PreprocessorUtils;
@ -49,7 +49,7 @@ public class ProjectCache
private Map< String, ConfigFile > configFiles_;
private Map< String, Define > defines_;
private DependencyTreeBuilder dependTree_;
private DependencyListBuilder dependTree_;
private IProject project_;
@ -60,7 +60,7 @@ public class ProjectCache
configFiles_ = new HashMap<String, ConfigFile>();
defines_ = new HashMap<String, Define>(0);
dependTree_ = new DependencyTreeBuilder( project_ );
dependTree_ = new DependencyListBuilder( project_ );
propertiesTimetamp_ = 0;
properties_ = new DialogSettings("project"); //$NON-NLS-1$
@ -292,7 +292,7 @@ public class ProjectCache
* Returns the current dependency tree builder for this project
* @return A dependency tree
*/
public DependencyTreeBuilder getDependencyTree()
public DependencyListBuilder getDependencyList()
{
return dependTree_;
}