eclipse plugin: Add the 'isConfigFile' method...

...to eliminante duplicate code
This commit is contained in:
Timotei Dolean 2011-07-06 16:52:55 +00:00
parent 547f8102dc
commit e5f94b1cec
3 changed files with 108 additions and 63 deletions

View file

@ -40,9 +40,16 @@ import org.wesnoth.wml.WMLRoot;
public class DependencyTreeBuilder implements Serializable
{
private static final long serialVersionUID = 6007509520015856611L;
/**
* The key by which the rood node of the tree is memorized
* in the tree.
*/
public static final String ROOT_NODE_KEY = "_ROOT_";
protected transient IProject project_;
protected int currentIndex_ = 0;
protected boolean isCreated_;
protected int currentIndex_;
private ProjectDependencyNode parent_;
private ProjectDependencyNode previous_;
@ -51,14 +58,29 @@ public class DependencyTreeBuilder implements Serializable
public DependencyTreeBuilder( IProject project )
{
tree_ = new HashMap<String, ProjectDependencyNode>();
parent_ = previous_ = null;
project_ = project;
isCreated_ = false;
currentIndex_ = 0;
}
/**
* Create the whole dependency tree from scratch
* Create the whole dependency tree from scratch.
* @param force True for force re-creating the tree even if it
* was previously created
*/
public void createDependencyTree()
public void createDependencyTree( boolean force )
{
if ( isCreated_ && !force ) {
Logger.getInstance( ).log( "Depedency tree for project " +
project_.getName( ) + " already built. Skipping it." );
return;
}
isCreated_ = true;
// start creating the PDT (project dependency tree)
Queue<IContainer> containers = new LinkedBlockingDeque<IContainer>( );
@ -155,11 +177,8 @@ public class DependencyTreeBuilder implements Serializable
if ( resource instanceof IContainer )
containers.add( (IContainer)resource );
else {
String fileName = resource.getName( );
// just config files.
if ( ! fileName.endsWith( ".cfg" ) || //$NON-NLS-1$
! ( resource instanceof IFile ))
if ( !ResourceUtils.isConfigFile( resource ) )
continue;
addNode( ( IFile ) resource );
@ -170,7 +189,7 @@ public class DependencyTreeBuilder implements Serializable
System.out.println("tree:"); //$NON-NLS-1$
if ( !tree_.isEmpty( ) ) {
ProjectDependencyNode node = tree_.get( "_ROOT_" ); // $NON-NLS-1$ //$NON-NLS-1$
ProjectDependencyNode node = tree_.get( ROOT_NODE_KEY );
do {
System.out.print( "> " ); //$NON-NLS-1$
@ -210,7 +229,7 @@ public class DependencyTreeBuilder implements Serializable
} else {
// no parent yet (== null)
// so we're making this the root node for this tree
tree_.put( "_ROOT_", newNode ); //$NON-NLS-1$
tree_.put( ROOT_NODE_KEY, newNode ); //$NON-NLS-1$
}
parent_ = newNode;
@ -240,6 +259,15 @@ public class DependencyTreeBuilder implements Serializable
return tree_.get( file.getProjectRelativePath( ).toString( ) );
}
/**
* Returns true if the tree was already created, false otherwise
* @return A boolean value
*/
public boolean getIsCreated()
{
return isCreated_;
}
/**
* Deserializes this object from the input
* @param input The object input stream
@ -253,6 +281,7 @@ public class DependencyTreeBuilder implements Serializable
return;
this.currentIndex_ = tmp.currentIndex_;
this.isCreated_ = tmp.isCreated_;
this.tree_ = tmp.tree_;
this.previous_ = tmp.previous_;
this.parent_ = tmp.parent_;

View file

@ -13,7 +13,6 @@ import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.eclipse.core.resources.IContainer;
@ -49,21 +48,6 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
super();
}
protected void fullBuild(final IProgressMonitor monitor) throws CoreException
{
ProjectCache cache = ProjectUtils.getCacheForProject( getProject( ) );
cache.getDependencyTree( ).createDependencyTree( );
// getProject().accept(new ResourceVisitor(monitor));
}
protected void incrementalBuild(IResourceDelta delta, IProgressMonitor monitor)
throws CoreException
{
// the visitor does the work.
//delta.accept(new ResourceDeltaVisitor(monitor));
}
@SuppressWarnings("rawtypes")
@Override
protected IProject[] build(int kind, Map args, IProgressMonitor monitor)
@ -72,51 +56,29 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
if ( WesnothInstallsUtils.setupInstallForResource( getProject() ) == false )
return null;
String installName = WesnothInstallsUtils.getInstallNameForResource( getProject() );
Logger.getInstance().log("building..."); //$NON-NLS-1$
monitor.beginTask(String.format(Messages.WesnothProjectBuilder_1, getProject().getName()), 100);
String installName = WesnothInstallsUtils.getInstallNameForResource( getProject() );
Paths paths = Preferences.getPaths( installName );
monitor.subTask(Messages.WesnothProjectBuilder_3);
if ( paths.getUserDir( ).isEmpty( ) )
{
Logger.getInstance().log("no preferences set (project builder)", //$NON-NLS-1$
Messages.WesnothProjectBuilder_7);
if ( WorkspaceUtils.checkPathsAreSet( installName, true ) == false )
return null;
}
monitor.worked(5);
monitor.subTask( "Creating the project tree ..." );
ProjectCache cache = ProjectUtils.getCacheForProject( getProject( ) );
cache.getDependencyTree( ).createDependencyTree( false );
monitor.worked( 10 );
// create the temporary directory used by the plugin if not created
monitor.subTask(Messages.WesnothProjectBuilder_6);
WorkspaceUtils.getTemporaryFolder();
monitor.worked(2);
// check for 'build.xml' existance
if (new File(getProject().getLocation().toOSString() + "/build.xml").exists() == true) //$NON-NLS-1$
{
// run the ant job to copy the whole project
// in the user add-ons directory (incremental)
monitor.subTask(Messages.WesnothProjectBuilder_8);
Map<String, String> properties = new HashMap<String, String>();
properties.put("wesnoth.user.dir", paths.getUserDir( )); //$NON-NLS-1$
Logger.getInstance().log("Ant result:"); //$NON-NLS-1$
String result = AntUtils.runAnt(
getProject().getLocation().toOSString() + "/build.xml", //$NON-NLS-1$
properties, true);
Logger.getInstance().log(result);
monitor.worked(10);
if (result == null)
{
Logger.getInstance().log("error running the ant job", //$NON-NLS-1$
Messages.WesnothProjectBuilder_13);
return null;
}
}
monitor.worked(2);
if ( runAntJob(paths, monitor ) == false )
return null;
boolean readDefines = true;
if (kind == FULL_BUILD)
@ -137,7 +99,7 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
for(IResourceDelta tmp : affected)
{
if (tmp.getResource().getName().toLowerCase(Locale.ENGLISH).endsWith(".cfg")) //$NON-NLS-1$
if ( ResourceUtils.isConfigFile( tmp.getResource( ) ) )
{
readDefines = true;
break;
@ -151,7 +113,6 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
{
// we read the defines at the end of the build
// to speed up things (and only if we had any .cfg files processed)
ProjectCache cache = ProjectUtils.getCacheForProject( getProject() );
cache.readDefines( true );
cache.saveCache( );
@ -161,10 +122,54 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
return null;
}
protected void fullBuild(final IProgressMonitor monitor) throws CoreException
{
ProjectCache cache = ProjectUtils.getCacheForProject( getProject( ) );
cache.getDependencyTree( ).createDependencyTree( true );
// getProject().accept(new ResourceVisitor(monitor));
}
protected void incrementalBuild(IResourceDelta delta, IProgressMonitor monitor)
throws CoreException
{
// the visitor does the work.
//delta.accept(new ResourceDeltaVisitor(monitor));
}
private boolean runAntJob( Paths paths, IProgressMonitor monitor )
{
// check for 'build.xml' existance
if (new File(getProject().getLocation().toOSString() + "/build.xml").exists() == true) //$NON-NLS-1$
{
// run the ant job to copy the whole project
// in the user add-ons directory (incremental)
monitor.subTask(Messages.WesnothProjectBuilder_8);
Map<String, String> properties = new HashMap<String, String>();
properties.put("wesnoth.user.dir", paths.getUserDir( )); //$NON-NLS-1$
Logger.getInstance().log("Ant result:"); //$NON-NLS-1$
String result = AntUtils.runAnt(
getProject().getLocation().toOSString() + "/build.xml", //$NON-NLS-1$
properties, true);
Logger.getInstance().log(result);
monitor.worked(10);
if (result == null)
{
Logger.getInstance().log("error running the ant job", //$NON-NLS-1$
Messages.WesnothProjectBuilder_13);
return false;
}
}
monitor.worked(2);
return true;
}
protected void handleRemovedResource(IResource resource)
{
if (resource instanceof IFile &&
(resource.getName().toLowerCase(Locale.ENGLISH).endsWith(".cfg"))) //$NON-NLS-1$
if ( ResourceUtils.isConfigFile( resource ) )
{
ProjectUtils.getCacheForProject(getProject()).
getConfigs().remove(resource.getName());
@ -181,8 +186,7 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
return false;
// config files
if (resource instanceof IFile &&
(resource.getName().toLowerCase(Locale.ENGLISH).endsWith(".cfg"))) //$NON-NLS-1$
if ( ResourceUtils.isConfigFile( resource ) )
{
boolean isMainCfg = resource.getName().equals("_main.cfg"); //$NON-NLS-1$
if (handleMainCfg == false && isMainCfg == true)
@ -321,7 +325,7 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
for(IResourceDelta tmp : affected)
{
if (tmp.getResource().getName().toLowerCase(Locale.ENGLISH).endsWith(".cfg")) //$NON-NLS-1$
if ( ResourceUtils.isConfigFile( tmp.getResource( ) ) )
{
foundCfg = true;
break;

View file

@ -19,6 +19,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.List;
import java.util.Locale;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
@ -304,6 +305,17 @@ public class ResourceUtils
return valid;
}
/**
* Returns true if the resource is a WML config file
* @param resource The resource to check
* @return True or false
*/
public static boolean isConfigFile ( IResource resource )
{
return resource instanceof IFile &&
resource.getName().toLowerCase(Locale.ENGLISH).endsWith(".cfg"); //$NON-NLS-1$
}
/**
* Returns "_main.cfg" file
* from the specified resource or null if it isn't any