|
@@ -1,22 +1,22 @@
|
|
|
/*
|
|
|
* Copyright (c) 2003-2012 jMonkeyEngine
|
|
|
* All rights reserved.
|
|
|
- *
|
|
|
+ *
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
* met:
|
|
|
- *
|
|
|
+ *
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
- *
|
|
|
+ *
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
- *
|
|
|
+ *
|
|
|
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
* without specific prior written permission.
|
|
|
- *
|
|
|
+ *
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
@@ -34,12 +34,23 @@ package com.jme3.gde.core.assets;
|
|
|
import com.jme3.export.Savable;
|
|
|
import com.jme3.gde.core.scene.ApplicationLogHandler;
|
|
|
import com.jme3.gde.core.scene.SceneApplication;
|
|
|
+import com.jme3.gde.core.sceneexplorer.SceneExplorerTopComponent;
|
|
|
+import com.jme3.gde.core.sceneexplorer.nodes.JmeNode;
|
|
|
+import com.jme3.gde.core.sceneexplorer.nodes.JmeSpatial;
|
|
|
import com.jme3.gde.core.util.SpatialUtil;
|
|
|
+import com.jme3.gde.core.util.TaggedSpatialFinder;
|
|
|
+import com.jme3.gde.core.util.datatransfer.CopyAnimationDataFromOriginal;
|
|
|
+import com.jme3.gde.core.util.datatransfer.CopyMaterialDataFromOriginal;
|
|
|
+import com.jme3.gde.core.util.datatransfer.CopyMeshDataFromOriginal;
|
|
|
+import com.jme3.gde.core.util.datatransfer.CopyTransformDataFromOriginal;
|
|
|
import com.jme3.scene.Spatial;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
import java.util.concurrent.Callable;
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
import java.util.logging.Level;
|
|
|
import java.util.logging.Logger;
|
|
|
+
|
|
|
import org.netbeans.api.progress.ProgressHandle;
|
|
|
import org.openide.DialogDisplayer;
|
|
|
import org.openide.NotifyDescriptor;
|
|
@@ -51,6 +62,7 @@ import org.openide.filesystems.FileObject;
|
|
|
import org.openide.filesystems.FileRenameEvent;
|
|
|
import org.openide.loaders.DataObject;
|
|
|
import org.openide.loaders.DataObjectNotFoundException;
|
|
|
+import org.openide.nodes.Node;
|
|
|
import org.openide.util.Exceptions;
|
|
|
|
|
|
/**
|
|
@@ -59,16 +71,19 @@ import org.openide.util.Exceptions;
|
|
|
*
|
|
|
* @author normenhansen
|
|
|
*/
|
|
|
-public class ExternalChangeScanner implements AssetDataPropertyChangeListener, FileChangeListener {
|
|
|
+public class ExternalChangeScanner implements AssetDataPropertyChangeListener,
|
|
|
+ FileChangeListener {
|
|
|
|
|
|
- private static final Logger logger = Logger.getLogger(ExternalChangeScanner.class.getName());
|
|
|
+ private static final Logger LOGGER =
|
|
|
+ Logger.getLogger(ExternalChangeScanner.class.getName());
|
|
|
private static final AtomicBoolean userNotified = new AtomicBoolean(false);
|
|
|
- protected final AssetDataObject assetDataObject;
|
|
|
- protected final AssetData assetData;
|
|
|
- protected FileObject originalObject;
|
|
|
+ private final AssetDataObject assetDataObject;
|
|
|
+ private final AssetData assetData;
|
|
|
+ private FileObject originalObject;
|
|
|
|
|
|
public ExternalChangeScanner(AssetDataObject assetDataObject) {
|
|
|
this.assetDataObject = assetDataObject;
|
|
|
+
|
|
|
assetData = assetDataObject.getLookup().lookup(AssetData.class);
|
|
|
if (assetData != null) {
|
|
|
String path = assetData.getProperty("ORIGINAL_PATH");
|
|
@@ -80,88 +95,151 @@ public class ExternalChangeScanner implements AssetDataPropertyChangeListener, F
|
|
|
assetDataObject.getPrimaryFile().addFileChangeListener(new FileChangeAdapter() {
|
|
|
@Override
|
|
|
public void fileDeleted(FileEvent fe) {
|
|
|
- logger.log(Level.INFO, "File {0} deleted, remove!", new Object[]{fe.getFile()});
|
|
|
+ LOGGER.log(Level.INFO, "File {0} deleted, remove!",
|
|
|
+ new Object[]{fe.getFile()});
|
|
|
assetData.removePropertyChangeListener(main);
|
|
|
fe.getFile().removeFileChangeListener(this);
|
|
|
if (originalObject != null) {
|
|
|
- logger.log(Level.INFO, "Remove file change listener for {0}", originalObject);
|
|
|
+ LOGGER.log(Level.INFO, "Remove file change listener "
|
|
|
+ + "for {0}", originalObject);
|
|
|
originalObject.removeFileChangeListener(main);
|
|
|
originalObject = null;
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
|
- logger.log(Level.WARNING, "Trying to observer changes for asset {0} which has no AssetData in Lookup.", assetDataObject.getName());
|
|
|
+ LOGGER.log(Level.WARNING, "Trying to observer changes for asset "
|
|
|
+ + "{0} which has no AssetData in Lookup.",
|
|
|
+ assetDataObject.getName());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void notifyUser() {
|
|
|
if (!userNotified.getAndSet(true)) {
|
|
|
//TODO: execute on separate thread?
|
|
|
- java.awt.EventQueue.invokeLater(new Runnable() {
|
|
|
- public void run() {
|
|
|
- NotifyDescriptor.Confirmation mesg = new NotifyDescriptor.Confirmation("Original file for " + assetDataObject.getName() + " changed\nTry and reapply mesh data to j3o file?",
|
|
|
- "Original file changed",
|
|
|
- NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.QUESTION_MESSAGE);
|
|
|
- DialogDisplayer.getDefault().notify(mesg);
|
|
|
- if (mesg.getValue() != NotifyDescriptor.Confirmation.YES_OPTION) {
|
|
|
- userNotified.set(false);
|
|
|
- return;
|
|
|
- }
|
|
|
- SceneApplication.getApplication().enqueue(new Callable<Void>() {
|
|
|
- public Void call() throws Exception {
|
|
|
- applyExternalData();
|
|
|
- return null;
|
|
|
- }
|
|
|
- });
|
|
|
+ java.awt.EventQueue.invokeLater(() -> {
|
|
|
+ final String noOption = "No";
|
|
|
+ final String allOption = "All";
|
|
|
+ final String meshOption = "Only mesh data";
|
|
|
+ final String animOption = "Only animation data";
|
|
|
+ final NotifyDescriptor.Confirmation message =
|
|
|
+ new NotifyDescriptor.Confirmation("Original file for "
|
|
|
+ + assetDataObject.getName() + " changed\nTry "
|
|
|
+ + "and reapply data to j3o file?",
|
|
|
+ "Original file changed",
|
|
|
+ NotifyDescriptor.YES_NO_CANCEL_OPTION,
|
|
|
+ NotifyDescriptor.QUESTION_MESSAGE);
|
|
|
+ message.setOptions(new Object[]{allOption, meshOption, animOption,
|
|
|
+ noOption});
|
|
|
+ DialogDisplayer.getDefault().notify(message);
|
|
|
+ if (message.getValue().equals(noOption)) {
|
|
|
userNotified.set(false);
|
|
|
+ return;
|
|
|
}
|
|
|
+ SceneApplication.getApplication().enqueue((Callable<Void>) () -> {
|
|
|
+ applyExternalData(message.getValue().equals(meshOption),
|
|
|
+ message.getValue().equals(animOption));
|
|
|
+ return null;
|
|
|
+ });
|
|
|
+ userNotified.set(false);
|
|
|
});
|
|
|
} else {
|
|
|
- logger.log(Level.INFO, "User already notified about change in {0}", assetDataObject.getName());
|
|
|
+ LOGGER.log(Level.INFO, "User already notified about change in "
|
|
|
+ + "{0}", assetDataObject.getName());
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- private void applyExternalData() {
|
|
|
- ProgressHandle handle = ProgressHandle.createHandle("Updating file data");
|
|
|
+
|
|
|
+ private void applyExternalData(final boolean onlyMeshData,
|
|
|
+ final boolean onlyAnimData) {
|
|
|
+ final ProgressHandle handle = ProgressHandle.createHandle("Updating "
|
|
|
+ + "file "
|
|
|
+ + "data");
|
|
|
handle.start();
|
|
|
try {
|
|
|
- Spatial original = loadOriginalSpatial();
|
|
|
- Spatial spat = (Spatial) assetDataObject.loadAsset();
|
|
|
-
|
|
|
- SpatialUtil.updateMeshDataFromOriginal(spat, original);
|
|
|
- SpatialUtil.updateMaterialDataFromOriginal(spat, original);
|
|
|
- if (SpatialUtil.hasAnimations(original)) {
|
|
|
- NotifyDescriptor.Confirmation mesg = new NotifyDescriptor.Confirmation("Model appears to have animations, try to import as well?\nCurrently this will unlink attachment Nodes and clear\nadded effects tracks.",
|
|
|
- "Animations Available",
|
|
|
- NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.QUESTION_MESSAGE);
|
|
|
- DialogDisplayer.getDefault().notify(mesg);
|
|
|
- if (mesg.getValue() == NotifyDescriptor.Confirmation.YES_OPTION) {
|
|
|
- SpatialUtil.updateAnimControlDataFromOriginal(spat, original);
|
|
|
- }
|
|
|
+ final Spatial original = loadOriginalSpatial();
|
|
|
+ final Spatial spat = (Spatial) assetDataObject.loadAsset();
|
|
|
+ final TaggedSpatialFinder finder = new TaggedSpatialFinder();
|
|
|
+
|
|
|
+ if(!onlyMeshData && SpatialUtil.hasAnimations(original)) {
|
|
|
+ new CopyAnimationDataFromOriginal(finder).update(spat,
|
|
|
+ original);
|
|
|
+
|
|
|
}
|
|
|
+ if(!onlyAnimData){
|
|
|
+ new CopyMeshDataFromOriginal(finder).update(spat, original);
|
|
|
+ }
|
|
|
+ if (!onlyMeshData && !onlyAnimData) {
|
|
|
+ new CopyTransformDataFromOriginal(finder).update(spat, original);
|
|
|
+ new CopyMaterialDataFromOriginal(finder).update(spat, original);
|
|
|
+ }
|
|
|
+ // Do a complicated recurse refresh since AbstractSceneExplorerNode:refresh() isn't working
|
|
|
+ SceneApplication.getApplication().enqueue((Runnable) () -> {
|
|
|
+ Node rootNode = SceneExplorerTopComponent.findInstance().getExplorerManager().getRootContext();
|
|
|
+ if (rootNode instanceof JmeNode) {
|
|
|
+ refreshNamedSpatial((JmeNode) rootNode, spat.getName());
|
|
|
+ }
|
|
|
+ });
|
|
|
closeOriginalSpatial();
|
|
|
assetDataObject.saveAsset();
|
|
|
- } catch (Exception e) {
|
|
|
- logger.log(Level.SEVERE, "Exception when trying to update external data.", e);
|
|
|
+ } catch (IOException e) {
|
|
|
+ LOGGER.log(Level.SEVERE, "Exception when trying to update "
|
|
|
+ + "external data.", e);
|
|
|
} finally {
|
|
|
handle.finish();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Look for the spatial to update using the name of the asset
|
|
|
+ * @param spatial
|
|
|
+ * @param name
|
|
|
+ */
|
|
|
+ private void refreshNamedSpatial(JmeSpatial spatial, String name){
|
|
|
+ if(spatial.getName().equals(name)){
|
|
|
+ recurseRefresh(spatial);
|
|
|
+ } else {
|
|
|
+ for(Node s: spatial.getChildren().getNodes()){
|
|
|
+ if(s instanceof JmeSpatial){
|
|
|
+ refreshNamedSpatial((JmeSpatial) s, name);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Refreshes the spatial and all children
|
|
|
+ * @param spatial
|
|
|
+ */
|
|
|
+ private void recurseRefresh(JmeSpatial spatial){
|
|
|
+ spatial.refresh(false);
|
|
|
+ for(Node s: spatial.getChildren().getNodes()){
|
|
|
+ if(s instanceof JmeSpatial){
|
|
|
+ recurseRefresh((JmeSpatial) s);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
private Spatial loadOriginalSpatial() {
|
|
|
try {
|
|
|
- DataObject dobj = DataObject.find(originalObject);
|
|
|
- AssetData originalAssetData = dobj.getLookup().lookup(AssetData.class);
|
|
|
+ final DataObject dobj = DataObject.find(originalObject);
|
|
|
+ final AssetData originalAssetData =
|
|
|
+ dobj.getLookup().lookup(AssetData.class);
|
|
|
if (originalAssetData != null) {
|
|
|
- Savable sav = originalAssetData.loadAsset();
|
|
|
+ final Savable sav = originalAssetData.loadAsset();
|
|
|
if (sav instanceof Spatial) {
|
|
|
return (Spatial) sav;
|
|
|
} else {
|
|
|
- logger.log(Level.SEVERE, "Trying to load original for {0} but it is not a Spatial: {1}", new Object[]{assetDataObject.getName(), originalObject});
|
|
|
+ LOGGER.log(Level.SEVERE, "Trying to load original for {0}"
|
|
|
+ + " but it is not a Spatial: {1}",
|
|
|
+ new Object[]{assetDataObject.getName(),
|
|
|
+ originalObject});
|
|
|
}
|
|
|
} else {
|
|
|
- logger.log(Level.WARNING, "Could not get AssetData for {0}, original file {1}", new Object[]{assetDataObject.getName(), originalObject});
|
|
|
+ LOGGER.log(Level.WARNING, "Could not get AssetData for {0}, "
|
|
|
+ + "original file {1}",
|
|
|
+ new Object[]{assetDataObject.getName(),
|
|
|
+ originalObject});
|
|
|
}
|
|
|
} catch (DataObjectNotFoundException ex) {
|
|
|
Exceptions.printStackTrace(ex);
|
|
@@ -171,12 +249,16 @@ public class ExternalChangeScanner implements AssetDataPropertyChangeListener, F
|
|
|
|
|
|
private Spatial closeOriginalSpatial() {
|
|
|
try {
|
|
|
- DataObject dobj = DataObject.find(originalObject);
|
|
|
- AssetData originalAssetData = dobj.getLookup().lookup(AssetData.class);
|
|
|
+ final DataObject dobj = DataObject.find(originalObject);
|
|
|
+ final AssetData originalAssetData =
|
|
|
+ dobj.getLookup().lookup(AssetData.class);
|
|
|
if (originalAssetData != null) {
|
|
|
originalAssetData.closeAsset();
|
|
|
} else {
|
|
|
- logger.log(Level.WARNING, "Could not get AssetData for {0}, original file {1}", new Object[]{assetDataObject.getName(), originalObject});
|
|
|
+ LOGGER.log(Level.WARNING, "Could not get AssetData for {0}, "
|
|
|
+ + "original file {1}",
|
|
|
+ new Object[]{assetDataObject.getName(),
|
|
|
+ originalObject});
|
|
|
}
|
|
|
} catch (DataObjectNotFoundException ex) {
|
|
|
Exceptions.printStackTrace(ex);
|
|
@@ -184,36 +266,49 @@ public class ExternalChangeScanner implements AssetDataPropertyChangeListener, F
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- private void setObservedFilePath(String assetName) {
|
|
|
- ProjectAssetManager mgr = assetDataObject.getLookup().lookup(ProjectAssetManager.class);
|
|
|
+ private void setObservedFilePath(final String assetName) {
|
|
|
+ final ProjectAssetManager mgr =
|
|
|
+ assetDataObject.getLookup().lookup(ProjectAssetManager.class);
|
|
|
if (mgr == null) {
|
|
|
- logger.log(Level.WARNING, "File is not part of a jME project but tries to find original model...");
|
|
|
+ LOGGER.log(Level.WARNING, "File is not part of a jME project but "
|
|
|
+ + "tries to find original model...");
|
|
|
return;
|
|
|
}
|
|
|
- FileObject fileObject = mgr.getAssetFileObject(assetName);
|
|
|
+ final FileObject fileObject = mgr.getAssetFileObject(assetName);
|
|
|
//ignoring same file -> old properties files
|
|
|
if (fileObject != null) {
|
|
|
if (!fileObject.equals(assetDataObject.getPrimaryFile())) {
|
|
|
if (originalObject != null) {
|
|
|
originalObject.removeFileChangeListener(this);
|
|
|
- logger.log(Level.INFO, "{0} stops listening for external changes on {1}", new Object[]{assetDataObject.getName(), originalObject});
|
|
|
+ LOGGER.log(Level.INFO, "{0} stops listening for external "
|
|
|
+ + "changes on {1}",
|
|
|
+ new Object[]{assetDataObject.getName(),
|
|
|
+ originalObject});
|
|
|
}
|
|
|
fileObject.addFileChangeListener(this);
|
|
|
- logger.log(Level.INFO, "{0} listening for external changes on {1}", new Object[]{assetDataObject.getName(), fileObject});
|
|
|
+ LOGGER.log(Level.INFO, "{0} listening for external changes on"
|
|
|
+ + " {1}", new Object[]{assetDataObject.getName(),
|
|
|
+ fileObject});
|
|
|
originalObject = fileObject;
|
|
|
} else {
|
|
|
- logger.log(Level.FINE, "Ignoring old reference to self for {0}", assetDataObject.getName());
|
|
|
+ LOGGER.log(Level.FINE, "Ignoring old reference to self for "
|
|
|
+ + "{0}", assetDataObject.getName());
|
|
|
}
|
|
|
} else {
|
|
|
- logger.log(Level.INFO, "Could not get FileObject for {0} when trying to update original data for {1}. Possibly deleted.", new Object[]{assetName, assetDataObject.getName()});
|
|
|
+ LOGGER.log(Level.INFO, "Could not get FileObject for {0} when "
|
|
|
+ + "trying to update original data for {1}. Possibly deleted"
|
|
|
+ + ".", new Object[]{assetName, assetDataObject.getName()});
|
|
|
//TODO: add folder listener for when recreated
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void assetDataPropertyChanged(String property, String before, String after) {
|
|
|
- if ("ORIGINAL_PATH".equals(property)) {
|
|
|
- logger.log(Level.INFO, "Notified about change in AssetData properties for {0}", assetDataObject.getName());
|
|
|
+ public void assetDataPropertyChanged(final String property,
|
|
|
+ final String before,
|
|
|
+ final String after) {
|
|
|
+ if (SpatialUtil.ORIGINAL_PATH.equals(property)) {
|
|
|
+ LOGGER.log(Level.INFO, "Notified about change in AssetData "
|
|
|
+ + "properties for {0}", assetDataObject.getName());
|
|
|
setObservedFilePath(after);
|
|
|
}
|
|
|
}
|
|
@@ -225,14 +320,18 @@ public class ExternalChangeScanner implements AssetDataPropertyChangeListener, F
|
|
|
}
|
|
|
|
|
|
public void fileChanged(FileEvent fe) {
|
|
|
- logger.log(Level.INFO, "External file {0} for {1} changed!", new Object[]{fe.getFile(), assetDataObject.getName()});
|
|
|
+ LOGGER.log(Level.INFO, "External file {0} for {1} changed!",
|
|
|
+ new Object[]{fe.getFile(), assetDataObject.getName()});
|
|
|
notifyUser();
|
|
|
}
|
|
|
|
|
|
public void fileDeleted(FileEvent fe) {
|
|
|
- logger.log(Level.INFO, "External file {0} for {1} deleted!", new Object[]{fe.getFile(), assetDataObject.getName()});
|
|
|
+ LOGGER.log(Level.INFO, "External file {0} for {1} deleted!",
|
|
|
+ new Object[]{fe.getFile(), assetDataObject.getName()});
|
|
|
if (originalObject != null) {
|
|
|
- logger.log(ApplicationLogHandler.LogLevel.INFO, "Remove file change listener for deleted object on {0}", assetDataObject.getName());
|
|
|
+ LOGGER.log(ApplicationLogHandler.LogLevel.INFO, "Remove file "
|
|
|
+ + "change listener for deleted object on {0}",
|
|
|
+ assetDataObject.getName());
|
|
|
originalObject.removeFileChangeListener(this);
|
|
|
originalObject = null;
|
|
|
}
|
|
@@ -240,9 +339,11 @@ public class ExternalChangeScanner implements AssetDataPropertyChangeListener, F
|
|
|
}
|
|
|
|
|
|
public void fileRenamed(FileRenameEvent fe) {
|
|
|
- logger.log(Level.INFO, "External file {0} for {1} renamed!", new Object[]{fe.getFile(), assetDataObject.getName()});
|
|
|
+ LOGGER.log(Level.INFO, "External file {0} for {1} renamed!",
|
|
|
+ new Object[]{fe.getFile(), assetDataObject.getName()});
|
|
|
if (originalObject != null) {
|
|
|
- logger.log(Level.INFO, "Remove file change listener for renamed object on {0}", assetDataObject.getName());
|
|
|
+ LOGGER.log(Level.INFO, "Remove file change listener for renamed "
|
|
|
+ + "object on {0}", assetDataObject.getName());
|
|
|
originalObject.removeFileChangeListener(this);
|
|
|
originalObject = null;
|
|
|
}
|