eclipse plugin: next_scenario/first_scenario completion - part II.

This commit is contained in:
Timotei Dolean 2010-08-03 19:58:51 +00:00
parent 492f9a539b
commit 16ab78a7eb
5 changed files with 127 additions and 29 deletions

View File

@ -8,6 +8,9 @@
*******************************************************************************/
package wesnoth_eclipse_plugin;
import java.util.Map.Entry;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Shell;
@ -16,6 +19,8 @@ import org.osgi.framework.BundleContext;
import wesnoth_eclipse_plugin.preferences.Preferences;
import wesnoth_eclipse_plugin.utils.GUIUtils;
import wesnoth_eclipse_plugin.utils.ProjectCache;
import wesnoth_eclipse_plugin.utils.ProjectUtils;
import wesnoth_eclipse_plugin.utils.WorkspaceUtils;
/**
@ -56,6 +61,11 @@ public class Activator extends AbstractUIPlugin
{
plugin = null;
Logger.getInstance().stopLogger();
for(Entry<IProject, ProjectCache> cache :
ProjectUtils.getProjectCaches().entrySet())
{
cache.getValue().saveCache();
}
super.stop(context);
}

View File

@ -37,9 +37,11 @@ import wesnoth_eclipse_plugin.preferences.Preferences;
import wesnoth_eclipse_plugin.templates.ReplaceableParameter;
import wesnoth_eclipse_plugin.utils.AntUtils;
import wesnoth_eclipse_plugin.utils.PreprocessorUtils;
import wesnoth_eclipse_plugin.utils.ProjectCache;
import wesnoth_eclipse_plugin.utils.ProjectUtils;
import wesnoth_eclipse_plugin.utils.ResourceUtils;
import wesnoth_eclipse_plugin.utils.StringUtils;
import wesnoth_eclipse_plugin.utils.WMLSaxHandler;
import wesnoth_eclipse_plugin.utils.WorkspaceUtils;
public class WesnothProjectBuilder extends IncrementalProjectBuilder
@ -53,7 +55,7 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
{
try
{
getProject().accept(new SampleResourceVisitor(monitor));
getProject().accept(new ResourceVisitor(monitor));
} catch (CoreException e)
{
Logger.getInstance().logException(e);
@ -64,7 +66,7 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
throws CoreException
{
// the visitor does the work.
delta.accept(new SampleDeltaVisitor(monitor));
delta.accept(new ResourceDeltaVisitor(monitor));
}
@SuppressWarnings("rawtypes")
@ -153,7 +155,12 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
protected void handleRemovedResource(IResource resource)
{
if (resource instanceof IFile &&
(resource.getName().toLowerCase(Locale.ENGLISH).endsWith(".cfg")))
{
ProjectUtils.getCacheForProject(getProject()).
getScenarios().remove(resource.getName());
}
}
protected void checkResource(IResource resource, IProgressMonitor monitor,
@ -179,6 +186,28 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
PreprocessorUtils.preprocessFile(file, null);
monitor.worked(5);
monitor.subTask("Gathering file information...");
ProjectCache projCache = ProjectUtils.getCacheForProject(getProject());
if (ProjectUtils.isScenarioFile(file.getLocation().toOSString()))
{
WMLSaxHandler handler = ProjectUtils.
getParsedWMLFromResource(PreprocessorUtils.getPreprocessedFilePath(file, false, false).toString());
if (handler.ScenarioId == null)
{
projCache.getScenarios().remove(file.getName());
Logger.getInstance().logWarn("got a null scenario id" +
"for 'scenario' file:" + file.getName());
}
else
{
Logger.getInstance().log("added scenarioId ["+handler.ScenarioId +
"] for file: " + file.getName());
projCache.getScenarios().put(file.getName(), handler.ScenarioId);
}
}
monitor.worked(10);
// we need to find the correct column start/end based on the current document
// (or get that from the tool)
IDocumentProvider provider = new TextFileDocumentProvider();
@ -288,11 +317,11 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
}
}
class SampleDeltaVisitor implements IResourceDeltaVisitor
class ResourceDeltaVisitor implements IResourceDeltaVisitor
{
private IProgressMonitor monitor_;
public SampleDeltaVisitor(IProgressMonitor monitor) {
public ResourceDeltaVisitor(IProgressMonitor monitor) {
monitor_ = monitor;
}
@ -320,11 +349,11 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
}
}
class SampleResourceVisitor implements IResourceVisitor
class ResourceVisitor implements IResourceVisitor
{
private IProgressMonitor monitor_;
public SampleResourceVisitor(IProgressMonitor monitor) {
public ResourceVisitor(IProgressMonitor monitor) {
monitor_ = monitor;
}

View File

@ -11,6 +11,9 @@ package wesnoth_eclipse_plugin.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import org.eclipse.core.resources.IProject;
@ -29,9 +32,11 @@ public class ProjectCache
private Properties properties_;
private IProject associatedProject_;
private File wesnothFile_;
private Map<String, String> scenarios_;
public ProjectCache(IProject project)
{
scenarios_ = new HashMap<String, String>();
propertiesTimetamp_ = 0;
properties_ = new Properties();
@ -64,6 +69,22 @@ public class ProjectCache
ResourceUtils.createWesnothFile(wesnothFile_.getAbsolutePath());
properties_.loadFromXML(new FileInputStream(wesnothFile_));
propertiesTimetamp_ = wesnothFile_.lastModified();
// parse scenario ids
scenarios_.clear();
String[] fileNames = properties_.getProperty("scen_fns", "").split(",");
String[] scenarioIds = properties_.getProperty("scen_ids", "").split(",");
if (fileNames.length == scenarioIds.length)
{
for(int index = 0; index < fileNames.length;index++)
{
if (scenarioIds[index].isEmpty())
continue;
scenarios_.put(fileNames[index], scenarioIds[index]);
}
}
else
Logger.getInstance().logError("incorrect scenarios data.!!");
}
catch (Exception e)
{
@ -89,10 +110,22 @@ public class ProjectCache
return properties_;
}
/**
* Gets the map with the scenarios
* The key represent the filenames of the files
* and the value the scenarioId from that file
* @return
*/
public Map<String, String> getScenarios()
{
return scenarios_;
}
/**
* Saves the cache to disk.
* Saves:
* - properties
* - existing scenarios
* @return
*/
public boolean saveCache()
@ -101,6 +134,22 @@ public class ProjectCache
ResourceUtils.createWesnothFile(wesnothFile_.getAbsolutePath());
try
{
// store scenario ids
StringBuilder fileNames = new StringBuilder(scenarios_.size());
StringBuilder scenarioIds = new StringBuilder(scenarios_.size());
for(Entry<String, String> scenario : scenarios_.entrySet())
{
if (fileNames.length() > 0)
{
fileNames.append(',');
scenarioIds.append(',');
}
fileNames.append(scenario.getKey());
scenarioIds.append(scenario.getValue());
}
properties_.setProperty("scen_fns", fileNames.toString());
properties_.setProperty("scen_ids", scenarioIds.toString());
// store properties
properties_.storeToXML(new FileOutputStream(wesnothFile_), null);

View File

@ -24,6 +24,7 @@ import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Path;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import wesnoth_eclipse_plugin.Logger;
@ -36,6 +37,11 @@ public class ProjectUtils
private static Map<IProject, ProjectCache> projectCache_ =
new HashMap<IProject, ProjectCache>();
public static Map<IProject, ProjectCache> getProjectCaches()
{
return projectCache_;
}
/**
* Gets the properties store for specified project.
* If the store doesn't exist it will be created.
@ -59,7 +65,8 @@ public class ProjectUtils
if (cache == null)
{
projectCache_.put(project, new ProjectCache(project));
cache = new ProjectCache(project);
projectCache_.put(project,cache);
}
return cache;
}
@ -195,7 +202,7 @@ public class ProjectUtils
*/
public static String getCampaignID(IResource resource)
{
WMLSaxHandler handler = getWMLHandlerFromResource(
WMLSaxHandler handler = getParsedWMLFromResource(
PreprocessorUtils.getPreprocessedFilePath(
getMainConfigLocation(resource), false, true).toString());
if (handler == null)
@ -210,18 +217,22 @@ public class ProjectUtils
*/
public static String getScenarioID(IFile file)
{
WMLSaxHandler handler = getWMLHandlerFromResource(
WMLSaxHandler handler = getParsedWMLFromResource(
PreprocessorUtils.getPreprocessedFilePath(file, false, true).toString());
if (handler == null)
return null;
return handler.ScenarioId;
}
private static WMLSaxHandler getWMLHandlerFromResource(String resourcePath)
/**
* Returns the WMLSaxHandler for the parsed specified resource
* @param resourcePath The resourcepath to parse
* @return
*/
public static WMLSaxHandler getParsedWMLFromResource(String resourcePath)
{
ExternalToolInvoker parser = WMLTools.runWMLParser2(resourcePath);
try{
ExternalToolInvoker parser =
WMLTools.runWMLParser2(resourcePath);
parser.waitForTool();
SAXParser saxparser;
saxparser = SAXParserFactory.newInstance().newSAXParser();
@ -230,6 +241,11 @@ public class ProjectUtils
saxparser.parse(new InputSource(new StringReader(parser.getOutputContent())), handler);
return handler;
}
catch (SAXException e) {
Logger.getInstance().logException(e);
Logger.getInstance().logError("Using output: " + parser.getOutputContent());
return null;
}
catch (Exception e)
{
Logger.getInstance().logException(e);

View File

@ -8,7 +8,7 @@
*******************************************************************************/
package org.wesnoth.ui.contentassist;
import java.util.Properties;
import java.util.Map.Entry;
import org.eclipse.core.resources.IFile;
import org.eclipse.emf.ecore.EObject;
@ -93,8 +93,9 @@ public class WMLProposalProvider extends AbstractWMLProposalProvider
dbg(model);
WMLKey key = (WMLKey)model;
// handle the next_scenario
if (key.getName().equals("next_scenario"))
// handle the next_scenario and first_scenario
if (key.getName().equals("next_scenario") ||
key.getName().equals("first_scenario"))
{
IFile file = (IFile)EditorUtils.getActiveXtextEditor()
.getEditorInput().getAdapter(IFile.class);
@ -103,20 +104,13 @@ public class WMLProposalProvider extends AbstractWMLProposalProvider
Logger.getInstance().logError("FATAL! file is null (and it shouldn't)");
return;
}
Properties props = ProjectUtils.getPropertiesForProject(file.getProject());
//TODO: dummy entry. remove when proper architecture is ready
props.setProperty("scenarios", "01_scen1,02_scen2,");
if (props.getProperty("scenarios") != null)
for(Entry<String, String> scenario : ProjectUtils.
getCacheForProject(file.getProject()).getScenarios().entrySet())
{
String[] scenarios = props.getProperty("scenarios").split(",");
for(String scenarioId : scenarios)
{
if (scenarioId.isEmpty())
continue;
acceptor.accept(createCompletionProposal(scenarioId,
scenarioId, WMLLabelProvider.getImageByName("scenario.png"),
context));
}
acceptor.accept(createCompletionProposal(scenario.getValue(),
scenario.getValue(), WMLLabelProvider.getImageByName("scenario.png"),
context));
}
}
}