From ff0b35b5f0a0f6e33f8d12da4470efa1b9412539 Mon Sep 17 00:00:00 2001 From: "nor..67" Date: Sun, 27 Jan 2013 03:46:11 +0000 Subject: [PATCH] SDK: - extend UberAssetLocator git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10197 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../gde/modelimporter/UberAssetLocator.java | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/sdk/jme3-model-importer/src/com/jme3/gde/modelimporter/UberAssetLocator.java b/sdk/jme3-model-importer/src/com/jme3/gde/modelimporter/UberAssetLocator.java index c520c6c51..d1ebba8a1 100644 --- a/sdk/jme3-model-importer/src/com/jme3/gde/modelimporter/UberAssetLocator.java +++ b/sdk/jme3-model-importer/src/com/jme3/gde/modelimporter/UberAssetLocator.java @@ -38,6 +38,8 @@ import com.jme3.asset.AssetManager; import java.io.File; import java.io.FileNotFoundException; import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; import javax.swing.filechooser.FileFilter; import org.openide.filesystems.FileChooserBuilder; import org.openide.filesystems.FileObject; @@ -45,15 +47,74 @@ import org.openide.filesystems.FileUtil; import org.openide.util.Exceptions; /** + * Asset locator that tries to find a file across the filesystem or get user + * input on where the file is. * * @author normenhansen */ public class UberAssetLocator implements AssetLocator { + //ugly static due to Locator instantiation + + private static final List locatedAssets = new ArrayList(); + private String rootPath; + + public static void resetLocatedList() { + locatedAssets.clear(); + } + + public static List getLocatedList() { + return new ArrayList(locatedAssets); + } + + private static UberAssetInfo getInfo(AssetKey key) { + for (UberAssetInfo uberAssetInfo : locatedAssets) { + if (uberAssetInfo.getKey().getName().equals(key.getName())) { + return uberAssetInfo; + } + } + return null; + } + + public UberAssetLocator() { + } public void setRootPath(String rootPath) { + this.rootPath = rootPath; } public AssetInfo locate(AssetManager manager, AssetKey key) { + AssetInfo existing = getInfo(key); + if (existing != null) { + return existing; + } + FileObject file = findFile(key); + if (file == null) { + return null; + } + UberAssetInfo info = new UberAssetInfo(file, manager, key); + locatedAssets.add(info); + return info; + } + + private FileObject findFile(AssetKey key) { + //TODO: better attempt to actually find file.. :) + String rootPath = this.rootPath != null ? this.rootPath.replace("\\", "/") : null; + if (rootPath != null) { + File file = new File(rootPath + "/" + key.getName()); + FileObject fileObject = FileUtil.toFileObject(file); + if (fileObject != null) { + return fileObject; + } + } + File file = new File(key.getName()); + FileObject fileObject = FileUtil.toFileObject(file); + if (fileObject != null) { + return fileObject; + } + return getUserPath(key); + } + + private FileObject getUserPath(AssetKey key) { final String ext = key.getExtension(); FileChooserBuilder fcb = new FileChooserBuilder(this.getClass()); fcb.setTitle("Locate " + key.getName()); @@ -77,7 +138,7 @@ public class UberAssetLocator implements AssetLocator { if (file == null) { return null; } - return new UberAssetInfo(FileUtil.toFileObject(file), manager, key); + return FileUtil.toFileObject(file); } public static class UberAssetInfo extends AssetInfo { @@ -89,6 +150,10 @@ public class UberAssetLocator implements AssetLocator { this.file = file; } + public FileObject getFileObject() { + return file; + } + @Override public InputStream openStream() { try {