Browse Source

MaterialPreviewRenderer now loads the material in a separate thread to avoid to stall the awt thread.
This and the previous change makes the material editor a lot faster to open when the loaded material has big textures.

Nehon 10 years ago
parent
commit
18c65e8d2f

+ 16 - 7
jme3-materialeditor/src/com/jme3/gde/materials/MaterialPreviewRenderer.java

@@ -24,6 +24,7 @@ import com.jme3.scene.shape.Sphere;
 import com.jme3.util.MaterialDebugAppState;
 import com.jme3.util.MaterialDebugAppState;
 import com.jme3.util.TangentBinormalGenerator;
 import com.jme3.util.TangentBinormalGenerator;
 import java.util.concurrent.Callable;
 import java.util.concurrent.Callable;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
 import java.util.logging.Level;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.logging.Logger;
 import javax.swing.ImageIcon;
 import javax.swing.ImageIcon;
@@ -42,6 +43,7 @@ public class MaterialPreviewRenderer implements SceneListener {
     private Material currentMaterial;
     private Material currentMaterial;
     private boolean init = false;
     private boolean init = false;
     private final JLabel label;
     private final JLabel label;
+    private final ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(5);
 
 
     public enum DisplayType {
     public enum DisplayType {
 
 
@@ -79,16 +81,22 @@ public class MaterialPreviewRenderer implements SceneListener {
     }
     }
 
 
     @SuppressWarnings("unchecked")
     @SuppressWarnings("unchecked")
-    public void showMaterial(ProjectAssetManager assetManager, String materialFileName) {
+    public void showMaterial(final ProjectAssetManager assetManager,final String materialFileName) {
         if (!init) {
         if (!init) {
             init();
             init();
         }
         }
-        MaterialKey key = new MaterialKey(assetManager.getRelativeAssetPath(materialFileName));
-        assetManager.deleteFromCache(key);
-        Material mat = (Material) assetManager.loadAsset(key);
-        if (mat != null) {
-            showMaterial(mat);
-        }
+        exec.execute(new Runnable() {
+
+            public void run() {
+                MaterialKey key = new MaterialKey(assetManager.getRelativeAssetPath(materialFileName));
+                assetManager.deleteFromCache(key);
+                Material mat = (Material) assetManager.loadAsset(key);
+                if (mat != null) {
+                    showMaterial(mat);
+                }
+            }
+        });
+        
 
 
     }
     }
 
 
@@ -229,5 +237,6 @@ public class MaterialPreviewRenderer implements SceneListener {
 
 
     public void cleanUp() {
     public void cleanUp() {
         SceneApplication.getApplication().removeSceneListener(this);
         SceneApplication.getApplication().removeSceneListener(this);
+        exec.shutdownNow();
     }
     }
 }
 }