Переглянути джерело

Fixed a Crucial Bug in Control Start/Stop functionality. We don't remove them anymore, we rely on AbstractControl's setEnabled method. This also means you have to extend AbstractControl which is recommended anyway

MeFisto94 9 роки тому
батько
коміт
0be8d211f4

+ 23 - 32
jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/JmeControl.java

@@ -34,6 +34,7 @@ package com.jme3.gde.core.sceneexplorer.nodes;
 import com.jme3.gde.core.scene.SceneApplication;
 import com.jme3.gde.core.sceneexplorer.nodes.actions.ControlsPopup;
 import com.jme3.scene.Spatial;
+import com.jme3.scene.control.AbstractControl;
 import com.jme3.scene.control.Control;
 import java.io.IOException;
 import java.util.concurrent.Callable;
@@ -55,7 +56,6 @@ import org.openide.util.actions.SystemAction;
 public abstract class JmeControl extends AbstractSceneExplorerNode {
 
     protected Control control;
-    protected boolean enabled;
     
     public JmeControl() {
         super();
@@ -126,44 +126,28 @@ public abstract class JmeControl extends AbstractSceneExplorerNode {
     }
     
     /**
-     * Enable/Disable the Control. This means essentially it will get detached from our Scene Graph.
+     * Enable/Disable the Control.
+     * This only works for extended AbstractControls!!
      * Also see: {@link #isEnabled() }
      * @param enabled Whether the Control should be enabled or disabled
-     * @return If we had success (false when an Exception occured or no {@link Control} assigned.
+     * @return If we had success (false when an Exception occured or no {@link Control} assigned or not of type {@link AbstractControl} )
      */
-    public boolean setEnabled(boolean enabled) {
-        if (control == null)
+    public boolean setEnabled(final boolean enabled) {
+        if (!isEnableable())
             return false;
-        
-        this.enabled = enabled;
-        final Spatial spat = getParentNode().getLookup().lookup(Spatial.class);
-        
         try {
-            if (enabled) {
-                SceneApplication.getApplication().enqueue(new Callable<Void>() {
-                    public Void call() throws Exception {
-                        if (spat.getControl(control.getClass()) == null)
-                            spat.addControl(control);
-                        return null;
-                    }
-                }).get();
-        
-            } else {
-                SceneApplication.getApplication().enqueue(new Callable<Void>() {
-                        public Void call() throws Exception {
-                            if (spat.getControl(control.getClass()) != null)
-                                spat.removeControl(control);
-                            return null;
-                        }
-                    }).get();
-            }
+            SceneApplication.getApplication().enqueue(new Callable<Void>() {
+                public Void call() throws Exception {
+                    ((AbstractControl)control).setEnabled(enabled);
+                    return null;
+                }
+            }).get();
+           
         } catch (InterruptedException ex) {
             Exceptions.printStackTrace(ex);
-            this.enabled = false;
             return false;
         } catch (ExecutionException ex) {
             Exceptions.printStackTrace(ex);
-            this.enabled = false;
             return false;
         }
         
@@ -171,12 +155,19 @@ public abstract class JmeControl extends AbstractSceneExplorerNode {
     }
     
     /**
-     * Returns whether this Control is enabled or disabled (i.e. attached to the SceneGraph and has it's controlUpdate rendered).
-     * <b>Note:</b> When an Exception occurs during {@link #setEnabled(boolean) }, it's status is considered disabled.
+     * Returns whether this Control is enabled or disabled.
+     * <b>Note:</b> When the Control doesn't extend AbstractControl, FALSE is returned.
      * @return -
      */
     public boolean isEnabled()
     {
-        return enabled;
+        if (isEnableable()) {
+            return ((AbstractControl)control).isEnabled();
+        } else
+            return false;
+    }
+    
+    public boolean isEnableable() {
+        return control instanceof AbstractControl;
     }
 }

+ 2 - 2
jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/JmeSpatial.java

@@ -369,10 +369,10 @@ public class JmeSpatial extends AbstractSceneExplorerNode {
      * Note: There is no isEnabled because it's only passed on to the Controls and they could have several states.
      * @param enable 
      */
-    public void setEnabled(boolean enable) {
+    public void setControlsEnabled(boolean enable) {
         for (Node n : getChildren().getNodes()) {
             if (n instanceof JmeSpatial) {
-                ((JmeSpatial)n).setEnabled(enable);
+                ((JmeSpatial)n).setControlsEnabled(enable);
             } else if (n instanceof JmeControl) {
                 ((JmeControl)n).setEnabled(enable);
             }

+ 11 - 6
jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/actions/ControlsPopup.java

@@ -33,6 +33,7 @@ package com.jme3.gde.core.sceneexplorer.nodes.actions;
 
 import com.jme3.gde.core.sceneexplorer.nodes.JmeControl;
 import com.jme3.gde.core.sceneexplorer.nodes.JmeSpatial;
+import com.jme3.gde.core.util.notify.MessageUtil;
 import com.jme3.scene.Node;
 import java.awt.event.ActionEvent;
 import javax.swing.AbstractAction;
@@ -99,7 +100,7 @@ public class ControlsPopup extends AbstractAction implements Presenter.Popup {
             @Override
             public void actionPerformed(ActionEvent e) {
                 if (Spatial != null) {
-                    Spatial.setEnabled(true);
+                    Spatial.setControlsEnabled(true);
                 }
             }
         };
@@ -116,7 +117,10 @@ public class ControlsPopup extends AbstractAction implements Presenter.Popup {
             @Override
             public void actionPerformed(ActionEvent e) {
                 if (Control != null) {
-                    Control.setEnabled(true);
+                    if (Control.isEnableable())
+                        Control.setEnabled(true);
+                    else
+                        MessageUtil.warn("Cannot Start this Control!\nStart/Stop only works for Controls which extend AbstractControl.\nImplementing Control isn't enough.");
                 }
             }
         };
@@ -133,7 +137,7 @@ public class ControlsPopup extends AbstractAction implements Presenter.Popup {
             @Override
             public void actionPerformed(ActionEvent e) {
                 if (Spatial != null) {
-                    Spatial.setEnabled(false);
+                    Spatial.setControlsEnabled(false);
                 }
             }
         };
@@ -149,9 +153,10 @@ public class ControlsPopup extends AbstractAction implements Presenter.Popup {
         return new AbstractAction("Stop") {
             @Override
             public void actionPerformed(ActionEvent e) {
-                if (Control != null) {
-                    Control.setEnabled(false);
-                }
+                if (Control.isEnableable())
+                        Control.setEnabled(false);
+                    else
+                        MessageUtil.warn("Cannot Stop this Control!\nStart/Stop only works for Controls which extend AbstractControl.\nImplementing Control isn't enough.");
             }
         };
     }