Millions of fixes, fixes for me

- Fixed hanging save on a Material definition file.
- Fixed Node editor blow up when changing a shader node definition content.
- Enhanced error reporting.
- and many more.
experimental
Nehon 10 years ago
parent e14c30a3cf
commit 212a2d6e96
  1. 46
      sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/EditableMatDefFile.java
  2. 64
      sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/MatDefEditorlElement.java
  3. 62
      sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/ShaderEditPanel.java
  4. 10
      sdk/jme3-materialeditor/src/com/jme3/gde/materials/MaterialPreviewRenderer.java

@ -30,8 +30,8 @@ import com.jme3.util.blockparser.Statement;
import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -55,7 +55,7 @@ import org.openide.util.WeakListeners;
public class EditableMatDefFile { public class EditableMatDefFile {
private FileObject matDefFile; private FileObject matDefFile;
private MatDefDataObject obj; private final MatDefDataObject obj;
private Material material; private Material material;
private MatDefBlock matDefStructure; private MatDefBlock matDefStructure;
private TechniqueBlock currentTechnique; private TechniqueBlock currentTechnique;
@ -64,7 +64,7 @@ public class EditableMatDefFile {
// MatParamTopComponent matParamComponent; // MatParamTopComponent matParamComponent;
private ShaderGenerator glsl100; private ShaderGenerator glsl100;
private ShaderGenerator glsl150; private ShaderGenerator glsl150;
private String selectedTechnique = "Default"; private final String selectedTechnique = "Default";
private final static String GLSL100 = "GLSL100"; private final static String GLSL100 = "GLSL100";
private final static String GLSL150 = "GLSL150"; private final static String GLSL150 = "GLSL150";
private Lookup lookup; private Lookup lookup;
@ -78,6 +78,7 @@ public class EditableMatDefFile {
} }
public final void load(Lookup lookup) { public final void load(Lookup lookup) {
loaded = false;
this.matDefFile = obj.getPrimaryFile(); this.matDefFile = obj.getPrimaryFile();
this.assetManager = lookup.lookup(ProjectAssetManager.class); this.assetManager = lookup.lookup(ProjectAssetManager.class);
this.glsl100 = new Glsl100ShaderGenerator(assetManager); this.glsl100 = new Glsl100ShaderGenerator(assetManager);
@ -92,20 +93,27 @@ public class EditableMatDefFile {
obj.getLookupContents().remove(materialDef); obj.getLookupContents().remove(materialDef);
materialDef = null; materialDef = null;
} }
if (material != null) {
obj.getLookupContents().remove(material);
matToRemove = material;
material = null;
}
FileLock lock = null; FileLock lock = null;
InputStream in = null;
boolean matParseError = false;
try { try {
lock = matDefFile.lock(); lock = matDefFile.lock();
List<Statement> sta = BlockLanguageParser.parse(obj.getPrimaryFile().getInputStream()); in = obj.getPrimaryFile().getInputStream();
List<Statement> sta = BlockLanguageParser.parse(in);
matDefStructure = new MatDefBlock(sta.get(0)); matDefStructure = new MatDefBlock(sta.get(0));
if (assetManager != null) { if (assetManager != null) {
AssetKey<MaterialDef> matDefKey = new AssetKey<MaterialDef>(assetManager.getRelativeAssetPath(assetManager.getRelativeAssetPath(matDefFile.getPath()))); AssetKey<MaterialDef> matDefKey = new AssetKey<MaterialDef>(assetManager.getRelativeAssetPath(assetManager.getRelativeAssetPath(matDefFile.getPath())));
assetManager.deleteFromCache(matDefKey); assetManager.deleteFromCache(matDefKey);
materialDef = (MaterialDef) assetManager.loadAsset(assetManager.getRelativeAssetPath(matDefFile.getPath())); materialDef = (MaterialDef) assetManager.loadAsset(assetManager.getRelativeAssetPath(matDefFile.getPath()));
} }
lock.releaseLock();
} catch (Exception ex) { } catch (Exception ex) {
Throwable t = ex.getCause(); Throwable t = ex.getCause();
boolean matParseError = false;
while (t != null) { while (t != null) {
if (t instanceof MatParseException) { if (t instanceof MatParseException) {
Logger.getLogger(EditableMatDefFile.class.getName()).log(Level.SEVERE, t.getMessage()); Logger.getLogger(EditableMatDefFile.class.getName()).log(Level.SEVERE, t.getMessage());
@ -120,8 +128,15 @@ public class EditableMatDefFile {
if (lock != null) { if (lock != null) {
lock.releaseLock(); lock.releaseLock();
} }
if (in != null) {
try {
in.close();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
} }
if (materialDef != null) { }
}
if (materialDef != null && !matParseError) {
currentTechnique = matDefStructure.getTechniques().get(0); currentTechnique = matDefStructure.getTechniques().get(0);
registerListener(matDefStructure); registerListener(matDefStructure);
@ -164,8 +179,7 @@ public class EditableMatDefFile {
glsl150.initialize(material.getActiveTechnique()); glsl150.initialize(material.getActiveTechnique());
s = glsl150.generateShader(); s = glsl150.generateShader();
} }
for (Iterator<Shader.ShaderSource> it = s.getSources().iterator(); it.hasNext();) { for (Shader.ShaderSource source : s.getSources()) {
Shader.ShaderSource source = it.next();
if (source.getType() == type) { if (source.getType() == type) {
return source.getSource(); return source.getSource();
} }
@ -177,13 +191,6 @@ public class EditableMatDefFile {
} }
} }
// public MatParamTopComponent getMatParamComponent() {
// return matParamComponent;
// }
//
// public void setMatParamComponent(MatParamTopComponent matParamComponent) {
// this.matParamComponent = matParamComponent;
// }
public TechniqueBlock getCurrentTechnique() { public TechniqueBlock getCurrentTechnique() {
return currentTechnique; return currentTechnique;
} }
@ -191,7 +198,7 @@ public class EditableMatDefFile {
public MatDefBlock getMatDefStructure() { public MatDefBlock getMatDefStructure() {
return matDefStructure; return matDefStructure;
} }
private MatStructChangeListener changeListener = new MatStructChangeListener(); private final MatStructChangeListener changeListener = new MatStructChangeListener();
J3MLoader loader = new J3MLoader(); J3MLoader loader = new J3MLoader();
private void updateLookupWithMaterialData(MatDefDataObject obj) { private void updateLookupWithMaterialData(MatDefDataObject obj) {
@ -202,7 +209,12 @@ public class EditableMatDefFile {
material.selectTechnique("Default", SceneApplication.getApplication().getRenderManager()); material.selectTechnique("Default", SceneApplication.getApplication().getRenderManager());
if (matToRemove != null) { if (matToRemove != null) {
for (MatParam matParam : matToRemove.getParams()) { for (MatParam matParam : matToRemove.getParams()) {
try {
material.setParam(matParam.getName(), matParam.getVarType(), matParam.getValue()); material.setParam(matParam.getName(), matParam.getVarType(), matParam.getValue());
} catch (IllegalArgumentException ie) {
matToRemove.clearParam(matParam.getName());
}
} }
obj.getLookupContents().remove(matToRemove); obj.getLookupContents().remove(matToRemove);
matToRemove = null; matToRemove = null;

@ -52,16 +52,20 @@ import com.jme3.shader.ShaderNodeVariable;
import com.jme3.shader.ShaderUtils; import com.jme3.shader.ShaderUtils;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point; import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException; import java.beans.PropertyVetoException;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import javax.swing.Action; import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
@ -71,10 +75,12 @@ import org.netbeans.core.spi.multiview.MultiViewElement;
import org.netbeans.core.spi.multiview.MultiViewElementCallback; import org.netbeans.core.spi.multiview.MultiViewElementCallback;
import org.netbeans.core.spi.multiview.text.MultiViewEditorElement; import org.netbeans.core.spi.multiview.text.MultiViewEditorElement;
import org.openide.awt.UndoRedo; import org.openide.awt.UndoRedo;
import org.openide.cookies.EditorCookie;
import org.openide.filesystems.FileObject; import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil; import org.openide.filesystems.FileUtil;
import org.openide.nodes.Children; import org.openide.nodes.Children;
import org.openide.nodes.Node; import org.openide.nodes.Node;
import org.openide.text.EditorSupport;
import org.openide.util.Exceptions; import org.openide.util.Exceptions;
import org.openide.util.Lookup; import org.openide.util.Lookup;
import org.openide.util.LookupEvent; import org.openide.util.LookupEvent;
@ -213,7 +219,6 @@ public final class MatDefEditorlElement extends JPanel implements MultiViewEleme
diagram1.revalidate(); diagram1.revalidate();
jScrollPane1.addComponentListener(diagram1); jScrollPane1.addComponentListener(diagram1);
diagram1.refreshPreviews(mat); diagram1.refreshPreviews(mat);
final Lookup.Result<Material> resMat = obj.getLookup().lookupResult(Material.class); final Lookup.Result<Material> resMat = obj.getLookup().lookupResult(Material.class);
resMat.addLookupListener(new LookupListener() { resMat.addLookupListener(new LookupListener() {
@ -226,7 +231,6 @@ public final class MatDefEditorlElement extends JPanel implements MultiViewEleme
} }
}); });
final MatDefNavigatorPanel nav = obj.getLookup().lookup(MatDefNavigatorPanel.class); final MatDefNavigatorPanel nav = obj.getLookup().lookup(MatDefNavigatorPanel.class);
if (nav != null) { if (nav != null) {
@ -290,11 +294,31 @@ public final class MatDefEditorlElement extends JPanel implements MultiViewEleme
public void showShaderEditor(String nodeName, NodePanel.NodeType type, List<String> pathList) { public void showShaderEditor(String nodeName, NodePanel.NodeType type, List<String> pathList) {
List<FileObject> fos = new ArrayList<FileObject>(); List<FileObject> fos = new ArrayList<FileObject>();
Map<String, String> readOnlyFiles = new HashMap<String, String>();
for (String path : pathList) { for (String path : pathList) {
try {
FileObject text = FileUtil.toFileObject(new File(getAssetManager().getAbsoluteAssetPath(path))); FileObject text = FileUtil.toFileObject(new File(getAssetManager().getAbsoluteAssetPath(path)));
fos.add(text); fos.add(text);
} catch (NullPointerException e) {
try {
//cannot load the files because they are probably in a jar
InputStream is = getAssetManager().getResourceAsStream(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out.append(line).append("\n");
}
readOnlyFiles.put(path.substring(path.lastIndexOf("/") + 1), out.toString()); //Prints the string content read from input stream
reader.close();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
} }
shaderEditPanel1.setFiles(nodeName, type, fos);
}
}
shaderEditPanel1.setFiles(nodeName, type, fos, readOnlyFiles);
shaderEditPanel1.revalidate(); shaderEditPanel1.revalidate();
if (!shaderEditPanel1.isVisible() || jSplitPane.getDividerLocation() == jSplitPane.getMinimumDividerLocation()) { if (!shaderEditPanel1.isVisible() || jSplitPane.getDividerLocation() == jSplitPane.getMinimumDividerLocation()) {
shaderEditPanel1.setVisible(true); shaderEditPanel1.setVisible(true);
@ -596,7 +620,6 @@ public final class MatDefEditorlElement extends JPanel implements MultiViewEleme
} }
for (WorldParamBlock worldParamBlock : technique.getWorldParams()) { for (WorldParamBlock worldParamBlock : technique.getWorldParams()) {
ShaderNodeVariable var = new ShaderNodeVariable("", "WorldParam", worldParamBlock.getName()); ShaderNodeVariable var = new ShaderNodeVariable("", "WorldParam", worldParamBlock.getName());
var.setType(MaterialUtils.getWorldParamType(var.getName())); var.setType(MaterialUtils.getWorldParamType(var.getName()));
@ -635,7 +658,13 @@ public final class MatDefEditorlElement extends JPanel implements MultiViewEleme
} }
public void reload() { public void reload() {
try {
obj.getLookup().lookup(EditorCookie.class).saveDocument();
obj.getEditableFile().load(obj.getLookup());
reload(obj.getEditableFile(), obj.getLookup()); reload(obj.getEditableFile(), obj.getLookup());
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
} }
private void reload(final EditableMatDefFile file, final Lookup lkp) throws NumberFormatException { private void reload(final EditableMatDefFile file, final Lookup lkp) throws NumberFormatException {
@ -647,21 +676,14 @@ public final class MatDefEditorlElement extends JPanel implements MultiViewEleme
} }
} else { } else {
diagram1.clear(); diagram1.clear();
JLabel error = new JLabel("Cannot load material definition."); JLabel error = new JLabel("<html><center>Cannot load material definition.<br>Please see the error log and fix it in the text editor</center></html>");
error.setForeground(Color.RED); error.setForeground(Color.ORANGE);
error.setBounds(0, 0, 200, 20); error.setFont(new Font("Arial", Font.BOLD, 24));
error.setBounds(0, 0, 400, 100);
jScrollPane1.getHorizontalScrollBar().setValue(0);
error.setLocation(jScrollPane1.getViewport().getWidth() / 2 - 200, jScrollPane1.getViewport().getHeight() / 2 - 50);
diagram1.add(error); diagram1.add(error);
JButton btn = new JButton("retry"); diagram1.repaint();
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
file.load(lkp);
if (file.isLoaded()) {
initDiagram(lkp);
}
}
});
btn.setBounds(0, 25, 150, 20);
diagram1.add(btn);
} }
} }

@ -13,8 +13,10 @@ import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JToggleButton; import javax.swing.JToggleButton;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.EditorKit; import javax.swing.text.EditorKit;
import org.openide.awt.UndoRedo; import org.openide.awt.UndoRedo;
import org.openide.cookies.EditorCookie; import org.openide.cookies.EditorCookie;
@ -34,16 +36,17 @@ public class ShaderEditPanel extends JPanel {
private DataObject currentDataObject = null; private DataObject currentDataObject = null;
private MatDefEditorlElement parent = null; private MatDefEditorlElement parent = null;
private UndoRedo.Manager undoRedoManager; private UndoRedo.Manager undoRedoManager;
private final String MIME = "text/x-glsl";
/** /**
* Creates new form ShaderEditPanel * Creates new form ShaderEditPanel
*/ */
public ShaderEditPanel() { public ShaderEditPanel() {
initComponents(); initComponents();
String mime = "text/x-glsl";
EditorKit ek = CloneableEditorSupport.getEditorKit(mime); EditorKit ek = CloneableEditorSupport.getEditorKit(MIME);
shaderEditorPane.setEditorKit(ek); shaderEditorPane.setEditorKit(ek);
shaderEditorPane.setContentType(mime); shaderEditorPane.setContentType(MIME);
shaderEditorPane.addKeyListener(new KeyListener() { shaderEditorPane.addKeyListener(new KeyListener() {
@ -66,7 +69,8 @@ public class ShaderEditPanel extends JPanel {
undoRedoManager = (UndoRedo.Manager) parent.getUndoRedo(); undoRedoManager = (UndoRedo.Manager) parent.getUndoRedo();
} }
public void setFiles(String title, NodePanel.NodeType type, List<FileObject> fos) { public void setFiles(String title, NodePanel.NodeType type, List<FileObject> fos, final Map<String, String> readOnlyFiles) {
headerText.setText(title); headerText.setText(title);
headerText.setIcon(Icons.getIconForShaderType(type)); headerText.setIcon(Icons.getIconForShaderType(type));
@ -87,33 +91,65 @@ public class ShaderEditPanel extends JPanel {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
saveCurrent(); saveCurrent();
try { try {
shaderEditorPane.setDocument(b.dataObject.getLookup().lookup(EditorCookie.class).openDocument()); switchEditableDoc(b);
undoRedoManager.discardAllEdits();
shaderEditorPane.getDocument().addUndoableEditListener(undoRedoManager);
} catch (IOException ex) { } catch (IOException ex) {
Exceptions.printStackTrace(ex); Exceptions.printStackTrace(ex);
} }
currentDataObject = b.dataObject;
} }
}); });
if (firstItem) { if (firstItem) {
shaderEditorPane.setDocument(b.dataObject.getLookup().lookup(EditorCookie.class).openDocument()); switchEditableDoc(b);
undoRedoManager.discardAllEdits();
shaderEditorPane.getDocument().addUndoableEditListener(undoRedoManager);
currentDataObject = b.dataObject;
b.setSelected(true); b.setSelected(true);
firstItem = false; firstItem = false;
} }
} catch (IOException ex) { } catch (IOException ex) {
Exceptions.printStackTrace(ex); Exceptions.printStackTrace(ex);
} }
buttonPanel.add(b);
}
for (String key : readOnlyFiles.keySet()) {
final Tab b = new Tab();
b.setText(key);
buttonGroup1.add(b);
final String theKey = key;
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
switchReadOnlyDoc(readOnlyFiles.get(theKey));
}
});
if (firstItem) {
switchReadOnlyDoc(readOnlyFiles.get(key));
b.setSelected(true);
firstItem = false;
}
buttonPanel.add(b); buttonPanel.add(b);
} }
} }
private void switchEditableDoc(Tab b) throws IOException {
if(currentDataObject != null){
currentDataObject.getLookup().lookup(EditorCookie.class).close();
}
shaderEditorPane.setDocument(b.dataObject.getLookup().lookup(EditorCookie.class).openDocument());
undoRedoManager.discardAllEdits();
shaderEditorPane.getDocument().addUndoableEditListener(undoRedoManager);
shaderEditorPane.setEditable(true);
currentDataObject = b.dataObject;
}
private void switchReadOnlyDoc(String text) {
if(currentDataObject != null){
currentDataObject.getLookup().lookup(EditorCookie.class).close();
}
shaderEditorPane.setText(text);
undoRedoManager.discardAllEdits();
shaderEditorPane.setEditable(false);
currentDataObject = null;
}
public void saveCurrent() { public void saveCurrent() {
if (currentDataObject != null && currentDataObject.isModified()) { if (currentDataObject != null && currentDataObject.isModified()) {
FileLock lock = null; FileLock lock = null;

@ -117,7 +117,7 @@ public class MaterialPreviewRenderer implements SceneListener {
label.setIcon(Icons.error); label.setIcon(Icons.error);
} }
}); });
Logger.getLogger(MaterialPreviewRenderer.class.getName()).log(Level.SEVERE, "Error rendering material{0}", e.getMessage()); smartLog("Error rendering material{0}", e.getMessage());
} }
} }
}); });
@ -126,7 +126,15 @@ public class MaterialPreviewRenderer implements SceneListener {
return mat; return mat;
} }
}); });
}
private int lastErrorHash = 0;
private void smartLog(String expText, String message){
int hash = message.hashCode();
if(hash != lastErrorHash){
Logger.getLogger(MaterialPreviewRenderer.class.getName()).log(Level.SEVERE, expText, message);
lastErrorHash = hash;
}
} }
public Material reloadMaterial(Material mat) { public Material reloadMaterial(Material mat) {

Loading…
Cancel
Save