# Accordion (Javadocs: controlP5)
Date: 2014-04-27

controlP5

## Class Accordion

- java.lang.Object
- - controlP5.ControllerGroup<T>
  - - controlP5.ControlGroup<Accordion>
    - - controlP5.Accordion

All Implemented Interfaces:
  ControllerInterface<Accordion>, ControlListener, ControlP5Constants

    public class Accordion
    extends ControlGroup<Accordion>

The Accordion is a list of ControlGroups which can be expanded and collapsed. Only one item can be open at a time.

See Also:
  `ControllerGroup`, `ControlGroup`

+Example
  `

      /**
       * ControlP5 Accordion
       * arrange controller groups in an accordion like style.
       *
       * find a list of public methods available for the Accordion Controller
       * at the bottom of this sketch. In the example below 3 groups with controllers
       * are created and added to an accordion controller. Furthermore several key
       * combinations are mapped to control individual settings of the accordion.
       * An accordion comes in 2 modes, Accordion.SINGLE and Accordion.MULTI where the
       * latter allows to open multiple groups of an accordion and the SINGLE mode only
       * allows 1 group to be opened at a time.
       *
       * by Andreas Schlegel, 2012
       * www.sojamo.de/libraries/controlp5
       *
       */

      import controlP5.*;

      ControlP5 cp5;

      Accordion accordion;

      color c = color(0, 160, 100);

      void setup() {
        size(400, 600);
        noStroke();
        smooth();
        gui();
      }

      void gui() {

        cp5 = new ControlP5(this);

        // group number 1, contains 2 bangs
        Group g1 = cp5.addGroup("myGroup1")
                      .setBackgroundColor(color(0, 64))
                      .setBackgroundHeight(150)
                      ;

        cp5.addBang("bang")
           .setPosition(10,20)
           .setSize(100,100)
           .moveTo(g1)
           .plugTo(this,"shuffle");
           ;

        // group number 2, contains a radiobutton
        Group g2 = cp5.addGroup("myGroup2")
                      .setBackgroundColor(color(0, 64))
                      .setBackgroundHeight(150)
                      ;

        cp5.addRadioButton("radio")
           .setPosition(10,20)
           .setItemWidth(20)
           .setItemHeight(20)
           .addItem("black", 0)
           .addItem("red", 1)
           .addItem("green", 2)
           .addItem("blue", 3)
           .addItem("grey", 4)
           .setColorLabel(color(255))
           .activate(2)
           .moveTo(g2)
           ;

        // group number 3, contains a bang and a slider
        Group g3 = cp5.addGroup("myGroup3")
                      .setBackgroundColor(color(0, 64))
                      .setBackgroundHeight(150)
                      ;

        cp5.addBang("shuffle")
           .setPosition(10,20)
           .setSize(40,50)
           .moveTo(g3)
           ;

        cp5.addSlider("hello")
           .setPosition(60,20)
           .setSize(100,20)
           .setRange(100,500)
           .setValue(100)
           .moveTo(g3)
           ;

        cp5.addSlider("world")
           .setPosition(60,50)
           .setSize(100,20)
           .setRange(100,500)
           .setValue(200)
           .moveTo(g3)
           ;

        // create a new accordion
        // add g1, g2, and g3 to the accordion.
        accordion = cp5.addAccordion("acc")
                       .setPosition(40,40)
                       .setWidth(200)
                       .addItem(g1)
                       .addItem(g2)
                       .addItem(g3)
                       ;

        cp5.mapKeyFor(new ControlKey() {public void keyEvent() {accordion.open(0,1,2);}}, 'o');
        cp5.mapKeyFor(new ControlKey() {public void keyEvent() {accordion.close(0,1,2);}}, 'c');
        cp5.mapKeyFor(new ControlKey() {public void keyEvent() {accordion.setWidth(300);}}, '1');
        cp5.mapKeyFor(new ControlKey() {public void keyEvent() {accordion.setPosition(0,0);accordion.setItemHeight(190);}}, '2');
        cp5.mapKeyFor(new ControlKey() {public void keyEvent() {accordion.setCollapseMode(ControlP5.ALL);}}, '3');
        cp5.mapKeyFor(new ControlKey() {public void keyEvent() {accordion.setCollapseMode(ControlP5.SINGLE);}}, '4');
        cp5.mapKeyFor(new ControlKey() {public void keyEvent() {cp5.remove("myGroup1");}}, '0');

        accordion.open(0,1,2);

        // use Accordion.MULTI to allow multiple group
        // to be open at a time.
        accordion.setCollapseMode(Accordion.MULTI);

        // when in SINGLE mode, only 1 accordion
        // group can be open at a time.
        // accordion.setCollapseMode(Accordion.SINGLE);
      }

      void radio(int theC) {
        switch(theC) {
          case(0):c=color(0,200);break;
          case(1):c=color(255,0,0,200);break;
          case(2):c=color(0, 200, 140,200);break;
          case(3):c=color(0, 128, 255,200);break;
          case(4):c=color(50,128);break;
        }
      }

      void shuffle() {
        c = color(random(255),random(255),random(255),random(128,255));
      }

      void draw() {
        background(220);

        fill(c);

        float s1 = cp5.getController("hello").getValue();
        ellipse(200,400,s1,s1);

        float s2 = cp5.getController("world").getValue();
        ellipse(300,100,s2,s2);
      }

      /*
      a list of all methods available for the Accordion Controller
      use ControlP5.printPublicMethodsFor(Accordion.class);
      to print the following list into the console.

      You can find further details about class Accordion in the javadoc.

      Format:
      ClassName : returnType methodName(parameter type)

      controlP5.Accordion : Accordion addItem(ControlGroup)
      controlP5.Accordion : Accordion remove(ControllerInterface)
      controlP5.Accordion : Accordion removeItem(ControlGroup)
      controlP5.Accordion : Accordion setItemHeight(int)
      controlP5.Accordion : Accordion setMinItemHeight(int)
      controlP5.Accordion : Accordion setWidth(int)
      controlP5.Accordion : Accordion updateItems()
      controlP5.Accordion : int getItemHeight()
      controlP5.Accordion : int getMinItemHeight()
      controlP5.ControlGroup : Accordion activateEvent(boolean)
      controlP5.ControlGroup : Accordion addListener(ControlListener)
      controlP5.ControlGroup : Accordion hideBar()
      controlP5.ControlGroup : Accordion removeListener(ControlListener)
      controlP5.ControlGroup : Accordion setBackgroundColor(int)
      controlP5.ControlGroup : Accordion setBackgroundHeight(int)
      controlP5.ControlGroup : Accordion setBarHeight(int)
      controlP5.ControlGroup : Accordion showBar()
      controlP5.ControlGroup : Accordion updateInternalEvents(PApplet)
      controlP5.ControlGroup : String getInfo()
      controlP5.ControlGroup : String toString()
      controlP5.ControlGroup : boolean isBarVisible()
      controlP5.ControlGroup : int getBackgroundHeight()
      controlP5.ControlGroup : int getBarHeight()
      controlP5.ControlGroup : int listenerSize()
      controlP5.ControllerGroup : Accordion add(ControllerInterface)
      controlP5.ControllerGroup : Accordion bringToFront()
      controlP5.ControllerGroup : Accordion bringToFront(ControllerInterface)
      controlP5.ControllerGroup : Accordion close()
      controlP5.ControllerGroup : Accordion disableCollapse()
      controlP5.ControllerGroup : Accordion enableCollapse()
      controlP5.ControllerGroup : Accordion hide()
      controlP5.ControllerGroup : Accordion moveTo(ControlWindow)
      controlP5.ControllerGroup : Accordion moveTo(PApplet)
      controlP5.ControllerGroup : Accordion open()
      controlP5.ControllerGroup : Accordion registerProperty(String)
      controlP5.ControllerGroup : Accordion registerProperty(String, String)
      controlP5.ControllerGroup : Accordion remove(CDrawable)
      controlP5.ControllerGroup : Accordion remove(ControllerInterface)
      controlP5.ControllerGroup : Accordion removeCanvas(ControlWindowCanvas)
      controlP5.ControllerGroup : Accordion removeProperty(String)
      controlP5.ControllerGroup : Accordion removeProperty(String, String)
      controlP5.ControllerGroup : Accordion setAddress(String)
      controlP5.ControllerGroup : Accordion setArrayValue(float[])
      controlP5.ControllerGroup : Accordion setColor(CColor)
      controlP5.ControllerGroup : Accordion setColorActive(int)
      controlP5.ControllerGroup : Accordion setColorBackground(int)
      controlP5.ControllerGroup : Accordion setColorForeground(int)
      controlP5.ControllerGroup : Accordion setColorLabel(int)
      controlP5.ControllerGroup : Accordion setColorValue(int)
      controlP5.ControllerGroup : Accordion setHeight(int)
      controlP5.ControllerGroup : Accordion setId(int)
      controlP5.ControllerGroup : Accordion setLabel(String)
      controlP5.ControllerGroup : Accordion setMouseOver(boolean)
      controlP5.ControllerGroup : Accordion setMoveable(boolean)
      controlP5.ControllerGroup : Accordion setOpen(boolean)
      controlP5.ControllerGroup : Accordion setPosition(PVector)
      controlP5.ControllerGroup : Accordion setPosition(float, float)
      controlP5.ControllerGroup : Accordion setStringValue(String)
      controlP5.ControllerGroup : Accordion setUpdate(boolean)
      controlP5.ControllerGroup : Accordion setValue(float)
      controlP5.ControllerGroup : Accordion setVisible(boolean)
      controlP5.ControllerGroup : Accordion setWidth(int)
      controlP5.ControllerGroup : Accordion show()
      controlP5.ControllerGroup : Accordion update()
      controlP5.ControllerGroup : Accordion updateAbsolutePosition()
      controlP5.ControllerGroup : CColor getColor()
      controlP5.ControllerGroup : ControlWindow getWindow()
      controlP5.ControllerGroup : ControlWindowCanvas addCanvas(ControlWindowCanvas)
      controlP5.ControllerGroup : Controller getController(String)
      controlP5.ControllerGroup : ControllerProperty getProperty(String)
      controlP5.ControllerGroup : ControllerProperty getProperty(String, String)
      controlP5.ControllerGroup : Label getCaptionLabel()
      controlP5.ControllerGroup : Label getValueLabel()
      controlP5.ControllerGroup : PVector getPosition()
      controlP5.ControllerGroup : String getAddress()
      controlP5.ControllerGroup : String getInfo()
      controlP5.ControllerGroup : String getName()
      controlP5.ControllerGroup : String getStringValue()
      controlP5.ControllerGroup : String toString()
      controlP5.ControllerGroup : Tab getTab()
      controlP5.ControllerGroup : boolean isCollapse()
      controlP5.ControllerGroup : boolean isMouseOver()
      controlP5.ControllerGroup : boolean isMoveable()
      controlP5.ControllerGroup : boolean isOpen()
      controlP5.ControllerGroup : boolean isUpdate()
      controlP5.ControllerGroup : boolean isVisible()
      controlP5.ControllerGroup : boolean setMousePressed(boolean)
      controlP5.ControllerGroup : float getValue()
      controlP5.ControllerGroup : float[] getArrayValue()
      controlP5.ControllerGroup : int getHeight()
      controlP5.ControllerGroup : int getId()
      controlP5.ControllerGroup : int getWidth()
      controlP5.ControllerGroup : void remove()
      java.lang.Object : String toString()
      java.lang.Object : boolean equals(Object)

      */

  `

### Field Summary

### Fields inherited from interface controlP5.ControlP5Constants

`acceptClassList, ACTION_BROADCAST, ACTION_CLICK, ACTION_DRAG, ACTION_ENTER, ACTION_EXIT, ACTION_LEAVE, ACTION_MOVE, ACTION_PRESS, ACTION_PRESSED, ACTION_RELEASE, ACTION_RELEASED, ACTION_RELEASEDOUTSIDE, ACTION_WHEEL, ACTIVE, ALL, ALT, AQUA, ARC, ARRAY, BACKSPACE, BASELINE, BITFONT, BLACK, BLUE, BOOLEAN, BOTTOM, BOTTOM_OUTSIDE, CAPTIONLABEL, CENTER, CHECKBOX, COMMANDKEY, CONTROL, controlEventClass, CUSTOM, DECREASE, DEFAULT, DELETE, DONE, DOWN, DROPDOWN, ELLIPSE, ENTER, ESCAPE, EVENT, eventMethod, FADEIN, FADEOUT, FIELD, FLOAT, FUCHSIA, GRAY, GREEN, grixel, HALF_PI, HIDE, HIGHLIGHT, HORIZONTAL, IDLE, IMAGE, INACTIVE, INCREASE, INTEGER, INVALID, J2D, KEYCONTROL, LEFT, LEFT_OUTSIDE, LIME, LINE, LIST, LOAD, MAROON, MENU, METHOD, MOVE, MULTI, MULTIPLES, NAVY, OLIVE, ORANGE, OVER, P2D, P3D, PI, PRESSED, PRINT, PURPLE, RED, RELEASE, RESET, RIGHT, RIGHT_OUTSIDE, SAVE, SHIFT, SILVER, SINGLE, SINGLE_COLUMN, SINGLE_ROW, SPRITE, standard56, standard58, STRING, SWITCH, SWITCH_BACK, SWITCH_FORE, synt24, TAB, TEAL, THEME_A, THEME_CP52014, THEME_CP5BLUE, THEME_RED, THEME_RETRO, THEME_WHITE, TOP, TOP_OUTSIDE, TRANSITION_WAIT_FADEIN, TREE, TWO_PI, UP, VALUELABEL, VERBOSE, VERTICAL, WAIT, WHITE, YELLOW`

### Constructor Summary

Constructors
Constructor and Description
`Accordion(ControlP5 theControlP5, java.lang.String theName)` Convenience constructor to extend Accordion.

### Method Summary

Methods
`Accordion` `addItem(ControlGroup<?> theGroup)` Adds items of type ControlGroup to the Accordion, only ControlGroups can be added.
`Accordion` `close()`
`Accordion` `close(int... theId)`
`void` `controlEvent(ControlEvent theEvent)` controlEvent is called by controlP5's ControlBroadcaster to inform available listeners about value changes.
`int` `getItemHeight()`
`int` `getMinItemHeight()`
`Accordion` `open()`
`Accordion` `open(int... theId)`
`Accordion` `remove(ControllerInterface<?> theGroup)` Removes a ControlGroup from the accordion AND from controlP5 remove(ControllerInterface theGroup) overwrites it's super method.
`Accordion` `removeItem(ControlGroup<?> theGroup)` Removes a ControlGroup from the accordion and puts it back into the default tab of controlP5.
`Accordion` `setCollapseMode(int theMode)`
`Accordion` `setItemHeight(int theHeight)`
`Accordion` `setMinItemHeight(int theHeight)` Sets the minimum height of a collapsed item, default value is 100.
`Accordion` `setWidth(int theWidth)`
`Accordion` `updateItems()` UpdateItems is called when changes such as remove, change of height is performed on an accordion.

### Methods inherited from class controlP5.ControlGroup

`activateEvent, addListener, getBackgroundHeight, getBarHeight, getInfo, listenerSize, mousePressed, removeListener, setBackgroundColor, setBackgroundHeight, setBarHeight, setSize, stringValue, toString, updateInternalEvents`

### Methods inherited from class controlP5.ControllerGroup

`absolutePosition, add, addCanvas, addCloseButton, addDrawable, arrayValue, bringToFront, bringToFront, color, continuousUpdateEvents, controller, disableCollapse, draw, enableCollapse, getAbsolutePosition, getAddress, getArrayValue, getArrayValue, getCaptionLabel, getColor, getController, getHeight, getId, getName, getParent, getPickingColor, getPosition, getProperty, getProperty, getStringValue, getTab, getValue, getValueLabel, getWidth, getWindow, hide, hideArrow, hideBar, id, init, isBarVisible, isCollapse, isMouseOver, isMoveable, isOpen, isUpdate, isVisible, keyEvent, moveTo, moveTo, moveTo, moveTo, moveTo, moveTo, moveTo, moveTo, moveTo, name, parent, position, registerProperty, registerProperty, remove, remove, removeCanvas, removeCloseButton, removeProperty, removeProperty, setAbsolutePosition, setAddress, setArrayValue, setArrayValue, setCaptionLabel, setColor, setColorActive, setColorBackground, setColorForeground, setColorLabel, setColorValue, setGroup, setGroup, setHeight, setId, setLabel, setMouseOver, setMousePressed, setMoveable, setOpen, setPosition, setPosition, setStringValue, setTab, setTab, setTab, setTitle, setUpdate, setValue, setVisible, show, showArrow, showBar, update, updateAbsolutePosition, updateEvents, value`

### Methods inherited from class java.lang.Object

`equals, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Constructor Detail

#### Accordion

    public Accordion(ControlP5 theControlP5,
             java.lang.String theName)

Convenience constructor to extend Accordion.

Parameters:
  `theControlP5` -
  `theName` -

+Example
  `

      /**
       * ControlP5 extending Controllers
       *
       * the following example shows how to extend the Controller class to
       * create customizable Controllers. You can either extend the Controller class itself,
       * or any class that extends Controller itself like the Slider, Button, DropdownList, etc.
       *
       * How to:
       *
       * 1) do a super call to the convenience constructor requiring
       * 2 parameter (ControlP5 instance, name)
       *
       * 2) the Controller class has a set of empty methods that allow you to capture
       * inputs from the mouse including
       * onEnter(), onLeave(), onPress(), onRelease(), onClick(), onScroll(int), onDrag()
       * These you can override and include functionality as needed.
       *
       * 3) use method getPointer() to return the local (relative)
       * xy-coordinates of the controller
       *
       * 4) after instantiation custom controllers are treated the same
       * as default controlP5 controllers.
       *
       * by Andreas Schlegel, 2012
       * www.sojamo.de/libraries/controlp5
       *
       */

      import controlP5.*;

      ControlP5 cp5;
      PApplet p;

      void setup() {
        size(400, 400);
        cp5 = new ControlP5(this);

        // create 2 groups to show nesting of custom controllers and
        //
        Group g1 = cp5.addGroup("a").setPosition(0,100).setWidth(180);
        Group g2 = cp5.addGroup("b").setPosition(0,10).setWidth(180);
        g2.moveTo(g1);

        // create 2 custom Controllers from class MyButton
        // MyButton extends Controller and inherits all methods accordingly.
        new MyButton(cp5, "b1").setPosition(0, 0).setSize(180, 200).moveTo(g2);
        new MyButton(cp5, "b2").setPosition(205, 15).setSize(180, 200);

      }

      void draw() {
        background(0);
      }

      // b1 will be called from Controller b1
      public void b1(float theValue) {
        println("yay button "+theValue);
      }

      public void controlEvent(ControlEvent theEvent) {
        println("controlEvent : "+theEvent);
      }

      // Create a custom Controller, please not that
      // MyButton extends Controller<MyButton>, <MyButton>
      // is an indicator for the super class about the type of
      // custom controller to be created.

      class MyButton extends Controller<MyButton> {

        int current = 0xffff0000;

        float a = 128;

        float na;

        int y;

        // use the convenience constructor of super class Controller
        // MyButton will automatically registered and move to the
        // default controlP5 tab.

        MyButton(ControlP5 cp5, String theName) {
          super(cp5, theName);

          // replace the default view with a custom view.
          setView(new ControllerView() {
            public void display(PApplet p, Object b) {
              // draw button background
              na += (a-na) * 0.1;
              p.fill(current,na);
              p.rect(0, 0, getWidth(), getHeight());

              // draw horizontal line which can be moved on the x-axis
              // using the scroll wheel.
              p.fill(0,255,0);
              p.rect(0,y,width,10);

              // draw the custom label
              p.fill(128);
              translate(0,getHeight()+14);
              p.text(getName(),0,0);
              p.text(getName(),0,0);

            }
          }
          );
        }

        // override various input methods for mouse input control
        void onEnter() {
          cursor(HAND);
          println("enter");
          a = 255;
        }

        void onScroll(int n) {
          println("scrolling");
          y -= n;
          y = constrain(y,0,getHeight()-10);
        }

        void onPress() {
          println("press");
          current = 0xffffff00;
        }

        void onClick() {
          Pointer p1 = getPointer();
          println("clicked at "+p1.x()+", "+p1.y());
          current = 0xffffff00;
          setValue(y);
        }

        void onRelease() {
          println("release");
          current = 0xffffffff;
        }

        void onMove() {
          println("moving "+this+" "+_myControlWindow.getMouseOverList());
        }

        void onDrag() {
          current = 0xff0000ff;
          Pointer p1 = getPointer();
          float dif = dist(p1.px(),p1.py(),p1.x(),p1.y());
          println("dragging at "+p1.x()+", "+p1.y()+" "+dif);
        }

        void onReleaseOutside() {
          onLeave();
        }

        void onLeave() {
          println("leave");
          cursor(ARROW);
          a = 128;
        }
      }

  `

### Method Detail

#### addItem

    public Accordion addItem(ControlGroup<?> theGroup)

Adds items of type ControlGroup to the Accordion, only ControlGroups can be added.

Parameters:
  `theGroup` -
Returns:
  Accordion

#### close

    public Accordion close()

Overrides:
  `close` in class `ControllerGroup<Accordion>`

#### close

    public Accordion close(int... theId)

#### controlEvent

    public void controlEvent(ControlEvent theEvent)

Description copied from interface: `ControlListener`
controlEvent is called by controlP5's ControlBroadcaster to inform available listeners about value changes. Use the CallbackListener to get informed when actions such as pressed, release, drag, etc are performed.

Specified by:
  `controlEvent` in interface `ControlListener`
Overrides:
  `controlEvent` in class `ControlGroup<Accordion>`
Parameters:
  `theEvent` - ControlEvent
See Also:
  `CallbackListener`, `CallbackEvent`

#### getItemHeight

    public int getItemHeight()

#### getMinItemHeight

    public int getMinItemHeight()

#### open

    public Accordion open()

Overrides:
  `open` in class `ControllerGroup<Accordion>`

#### open

    public Accordion open(int... theId)

#### remove

    public Accordion remove(ControllerInterface<?> theGroup)

Removes a ControlGroup from the accordion AND from controlP5 remove(ControllerInterface theGroup) overwrites it's super method. if you want to remove a ControlGroup only from the accordion, use removeItem(ControlGroup).

Specified by:
  `remove` in interface `ControllerInterface<Accordion>`
Overrides:
  `remove` in class `ControllerGroup<Accordion>`
Parameters:
  `theGroup` - ControllerInterface
Returns:
  ControllerInterface
See Also:
  `removeItem(ControlGroup)`

#### removeItem

    public Accordion removeItem(ControlGroup<?> theGroup)

Removes a ControlGroup from the accordion and puts it back into the default tab of controlP5. if you dont have access to a ControlGroup via a variable, use controlP5.group("theNameOfTheGroup") which will return a

Returns:
  Accordion

#### setCollapseMode

    public Accordion setCollapseMode(int theMode)

#### setItemHeight

    public Accordion setItemHeight(int theHeight)

#### setMinItemHeight

    public Accordion setMinItemHeight(int theHeight)

Sets the minimum height of a collapsed item, default value is 100.

Parameters:
  `theHeight` -
Returns:
  Accordion

#### setWidth

    public Accordion setWidth(int theWidth)

Overrides:
  `setWidth` in class `ControllerGroup<Accordion>`
Returns:
  ControllerGroup

#### updateItems

    public Accordion updateItems()

UpdateItems is called when changes such as remove, change of height is performed on an accordion. updateItems() is called automatically for such cases, but by calling updateItems manually an update will be forced.

Returns:
  Accordion