From c310b933c9baf73495a5ae54ba4accb75683d799 Mon Sep 17 00:00:00 2001 From: Nehon Date: Wed, 4 Feb 2015 22:18:11 +0100 Subject: [PATCH] Added proper undo/redo support in the shader editor --- .../materialdefinition/MatDefDataObject.java | 7 ++----- .../editor/MatDefEditorlElement.java | 5 +++-- .../editor/ShaderEditPanel.java | 19 +++++++++++++------ 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/MatDefDataObject.java b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/MatDefDataObject.java index 74a32a2f0..5e7fb5083 100644 --- a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/MatDefDataObject.java +++ b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/MatDefDataObject.java @@ -37,7 +37,6 @@ import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.IOException; import javax.swing.JEditorPane; -import javax.swing.text.EditorKit; import org.netbeans.api.project.Project; import org.netbeans.api.project.ProjectManager; import org.netbeans.core.spi.multiview.MultiViewElement; @@ -54,7 +53,6 @@ import org.openide.loaders.DataObject; import org.openide.loaders.DataObjectExistsException; import org.openide.loaders.MultiDataObject; import org.openide.loaders.MultiFileLoader; -import org.openide.text.CloneableEditorSupport; import org.openide.util.Lookup; import org.openide.util.NbBundle.Messages; import org.openide.util.lookup.AbstractLookup; @@ -225,7 +223,7 @@ public class MatDefDataObject extends MultiDataObject { @Override public void componentActivated() { super.componentActivated(); - getEditorPane().addKeyListener(listener); + getEditorPane().addKeyListener(listener); } @Override @@ -243,8 +241,7 @@ public class MatDefDataObject extends MultiDataObject { obj.unload(); } }; - - + obj.getLookupContents().add(ed); return ed; } diff --git a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/MatDefEditorlElement.java b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/MatDefEditorlElement.java index fe2f9b605..af9406a56 100644 --- a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/MatDefEditorlElement.java +++ b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/MatDefEditorlElement.java @@ -69,6 +69,7 @@ import javax.swing.JToolBar; import org.netbeans.core.spi.multiview.CloseOperationState; import org.netbeans.core.spi.multiview.MultiViewElement; import org.netbeans.core.spi.multiview.MultiViewElementCallback; +import org.netbeans.core.spi.multiview.text.MultiViewEditorElement; import org.openide.awt.UndoRedo; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; @@ -99,7 +100,7 @@ public final class MatDefEditorlElement extends JPanel implements MultiViewEleme InstanceContent content; Selectable prevNode; MatDefMetaData metaData; - + public MatDefEditorlElement(final Lookup lkp) { initComponents(); obj = lkp.lookup(MatDefDataObject.class); @@ -433,7 +434,7 @@ public final class MatDefEditorlElement extends JPanel implements MultiViewEleme @Override public UndoRedo getUndoRedo() { - return UndoRedo.NONE; + return obj.getLookup().lookup(MultiViewEditorElement.class).getUndoRedo(); } @Override diff --git a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/ShaderEditPanel.java b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/ShaderEditPanel.java index e27ce726b..c7073cbff 100644 --- a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/ShaderEditPanel.java +++ b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/ShaderEditPanel.java @@ -16,6 +16,7 @@ import java.util.List; import javax.swing.JPanel; import javax.swing.JToggleButton; import javax.swing.text.EditorKit; +import org.openide.awt.UndoRedo; import org.openide.cookies.EditorCookie; import org.openide.filesystems.FileLock; import org.openide.filesystems.FileObject; @@ -32,6 +33,7 @@ public class ShaderEditPanel extends JPanel { private DataObject currentDataObject = null; private MatDefEditorlElement parent = null; + private UndoRedo.Manager undoRedoManager; /** * Creates new form ShaderEditPanel @@ -48,9 +50,9 @@ public class ShaderEditPanel extends JPanel { public void keyTyped(KeyEvent e) { } - public void keyPressed(KeyEvent e) { + public void keyPressed(KeyEvent e) { if ((e.getKeyCode() == KeyEvent.VK_S) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) { - saveCurrent(); + saveCurrent(); } } @@ -61,6 +63,7 @@ public class ShaderEditPanel extends JPanel { public void setParent(MatDefEditorlElement parent) { this.parent = parent; + undoRedoManager = (UndoRedo.Manager)parent.getUndoRedo(); } public void setFiles(String title, NodePanel.NodeType type, List fos) { @@ -72,7 +75,7 @@ public class ShaderEditPanel extends JPanel { buttonGroup1.remove((JToggleButton) component); } buttonPanel.removeAll(); - buttonPanel.repaint(); + buttonPanel.repaint(); for (FileObject fo : fos) { final Tab b = new Tab(); @@ -83,8 +86,10 @@ public class ShaderEditPanel extends JPanel { b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { saveCurrent(); - try { - shaderEditorPane.setDocument(b.dataObject.getLookup().lookup(EditorCookie.class).openDocument()); + try { + shaderEditorPane.setDocument(b.dataObject.getLookup().lookup(EditorCookie.class).openDocument()); + undoRedoManager.discardAllEdits(); + shaderEditorPane.getDocument().addUndoableEditListener(undoRedoManager); } catch (IOException ex) { Exceptions.printStackTrace(ex); } @@ -94,6 +99,8 @@ public class ShaderEditPanel extends JPanel { }); if (firstItem) { shaderEditorPane.setDocument(b.dataObject.getLookup().lookup(EditorCookie.class).openDocument()); + undoRedoManager.discardAllEdits(); + shaderEditorPane.getDocument().addUndoableEditListener(undoRedoManager); currentDataObject = b.dataObject; b.setSelected(true); firstItem = false; @@ -224,7 +231,7 @@ public class ShaderEditPanel extends JPanel { private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed setVisible(false); - + shaderEditorPane.getDocument().removeUndoableEditListener(undoRedoManager); saveCurrent();