package org.opensim.view.nodes;

import java.awt.Image;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.Action;
import javax.swing.ImageIcon;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.nodes.Sheet;
import org.openide.util.NbBundle;
import org.opensim.modeling.Controller;
import org.opensim.modeling.OpenSimContext;
import org.opensim.modeling.OpenSimObject;
import org.opensim.view.ObjectDisplayMenuAction;
import org.opensim.view.pub.OpenSimDB;

/** Node class to wrap Controller objects */
public class OneControllerNode extends OpenSimObjectNode {
   private static ResourceBundle bundle = NbBundle.getBundle(OneControllerNode.class);
   private boolean disabled=false;
   
   public OneControllerNode(OpenSimObject b) {
      super(b);
      setShortDescription(bundle.getString("HINT_ControllerNode"));
      setChildren(Children.LEAF);      
      //updateDisabledFlag();
   }

    public Node cloneNode() {
        return new OneControllerNode(getOpenSimObject());
    }
    /**
     * Icon for the Controller node 
     **/
    public Image getIcon(int i) {
        URL imageURL;
        if (disabled)
            imageURL = this.getClass().getResource("icons/disabledNode.png");
        else
            imageURL = this.getClass().getResource("icons/constraintNode.png");
        if (imageURL != null) { 
            return new ImageIcon(imageURL, "Controller").getImage();
        } else {
            return null;
        }
    }

    public Action[] getActions(boolean b) {
        Action[] superActions = (Action[]) super.getActions(b);        
        // Arrays are fixed size, onvert to a List
        List<Action> actions = java.util.Arrays.asList(superActions);
        // Create new Array of proper size
        Action[] retActions = new Action[actions.size()+1];
        actions.toArray(retActions);
        if (disabled){  // take out display menu ObjectDisplayMenuAction
            for (int i=0; i< retActions.length; i++){
                if (retActions[i] instanceof ObjectDisplayMenuAction){
                    retActions[i] = null; 
                    break;
                }
            }
        }
        try {
            ToggleEnabledStateAction act =(ToggleEnabledStateAction) ToggleEnabledStateAction.findObject(
                    (Class)Class.forName("org.opensim.view.nodes.ToggleEnabledStateAction"), true);
            retActions[actions.size()]=act;
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        return retActions;
    }
}

@@ -263,41 +263,8 @@
 e;%0D%0A
-import org.openide.nodes.Sheet;%0D%0A
 impo
@@ -322,26 +322,32 @@
 odeling.
-Controller
+AbstractProperty
 ;%0D%0Aimpor
@@ -380,14 +380,13 @@
 nSim
-Contex
+Objec
 t;%0D%0A
@@ -409,37 +409,38 @@
 im.modeling.
-OpenSimObject
+PropertyHelper
 ;%0D%0Aimport or
@@ -483,48 +483,8 @@
 on;%0D
-%0Aimport org.opensim.view.pub.OpenSimDB;%0D
 %0A%0D%0A/
@@ -585,16 +585,45 @@
 ectNode 
+implements DisableableObject 
 %7B%0D%0A   pr
@@ -927,18 +927,16 @@
 %0D%0A      
-//
 updateDi
@@ -1527,32 +1527,742 @@
     %7D%0D%0A    %7D%0D%0A%0D%0A
+    public boolean isDisabled() %7B%0D%0A        return disabled;%0D%0A    %7D%0D%0A    %0D%0A    public void setDisabled(boolean disabled) %7B%0D%0A        //OpenSimDB.getInstance().disableConstraint(getOpenSimObject(), disabled);%0D%0A        this.disabled = disabled;%0D%0A        if (disabled)%0D%0A            setIconBaseWithExtension(%22/org/opensim/view/nodes/icons/disabledNode.png%22);%0D%0A        else%0D%0A            setIconBaseWithExtension(%22/org/opensim/view/nodes/icons/constraintNode.png%22);%0D%0A        //refreshNode();%0D%0A    %7D%0D%0A%0D%0A    private void updateDisabledFlag() %7B%0D%0A        OpenSimObject c = getOpenSimObject();%0D%0A        AbstractProperty ap = c.getPropertyByName(%22isDisabled%22);%0D%0A        disabled = PropertyHelper.getValueBool(ap);%0D%0A    %7D%0D%0A%0D%0A
     public Actio
@@ -3342,12 +3342,162 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    @Override%0D%0A    public void refreshNode() %7B%0D%0A        super.refreshNode();%0D%0A        updateDisabledFlag();%0D%0A        setDisabled(disabled);%0D%0A    %7D%0D
 %0A%7D%0D%0A

Fix handling of disabled flag for Controller nodes.