Update Accordion.java

`synchronized ( controllers.get( ) ) { }`
This commit is contained in:
GoToLoop 2016-07-15 19:24:25 -03:00 committed by GitHub
parent 1f7cb64986
commit d6d722c7ed
1 changed files with 100 additions and 146 deletions

View File

@ -33,17 +33,16 @@ package controlP5;
* @see controlP5.ControlGroup * @see controlP5.ControlGroup
* @example controllers/ControlP5accordion * @example controllers/ControlP5accordion
*/ */
@SuppressWarnings( "rawtypes" ) public class Accordion extends ControlGroup< Accordion > { public class Accordion extends ControlGroup< Accordion > {
protected int spacing = 1; protected int spacing = 1;
protected int minHeight = 100; protected int minHeight = 100;
protected int itemheight; protected int itemHeight;
protected int _myMode = SINGLE; protected int _myMode = SINGLE;
public Accordion( ControlP5 theControlP5 , String theName ) { public Accordion( ControlP5 theControlP5 , String theName ) {
this( theControlP5 , theControlP5.getDefaultTab( ) , theName , 0 , 0 , 200 ); this( theControlP5 , theControlP5.getDefaultTab( ) , theName , 0 , 0 , 200 );
theControlP5.register( theControlP5.papplet , theName , this ); cp5.register( cp5.papplet , theName , this );
} }
Accordion( ControlP5 theControlP5 , Tab theTab , String theName , int theX , int theY , int theW ) { Accordion( ControlP5 theControlP5 , Tab theTab , String theName , int theX , int theY , int theW ) {
@ -58,75 +57,58 @@ package controlP5;
* @param theGroup * @param theGroup
* @return Accordion * @return Accordion
*/ */
public Accordion addItem( ControlGroup< ? > theGroup ) { public Accordion addItem( final ControlGroup< ? extends ControlGroup< ? > > theGroup ) {
theGroup.close( ); theGroup.close( ).moveTo( this ).activateEvent( true ).addListener( this ).setMoveable( false );
theGroup.moveTo( this ); if ( theGroup.getBackgroundHeight( ) < minHeight ) theGroup.setBackgroundHeight( minHeight );
theGroup.activateEvent( true );
theGroup.addListener( this );
theGroup.setMoveable( false );
if ( theGroup.getBackgroundHeight( ) < minHeight ) {
theGroup.setBackgroundHeight( minHeight );
}
controllers.add( theGroup ); controllers.add( theGroup );
updateItems( ); return updateItems( );
return this;
} }
/** /**
* Removes a ControlGroup from the accordion AND from controlP5 remove(ControllerInterface * Removes a ControlGroup from the accordion AND from controlP5.
* theGroup) overwrites it's super method. if you want to remove a ControlGroup only from the * remove(ControllerInterface theGroup) overwrites its super method.
* accordion, use removeItem(ControlGroup). * If you want to remove a ControlGroup only from the Accordion, use removeItem(ControlGroup).
* *
* @see controlP5.Accordion#removeItem(ControlGroup) * @see controlP5.Accordion#removeItem(ControlGroup)
* @return ControllerInterface * @return ControllerInterface
*/ */
@Override public Accordion remove( ControllerInterface< ? > theGroup ) { @Override public Accordion remove( final ControllerInterface< ? extends ControlGroup< ? > > theGroup ) {
if ( theGroup instanceof ControlGroup< ? > ) { theGroup.removeListener( this );
controllers.remove( theGroup ); updateItems( );
( ( ControlGroup< ? > ) theGroup ).removeListener( this ); return super.remove( theGroup );
updateItems( );
}
super.remove( theGroup );
return this;
} }
/** /**
* Removes a ControlGroup from the accordion and puts it back into the default tab of controlP5. * 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 * If you don't have access to a ControlGroup via a variable,
* controlP5.group("theNameOfTheGroup") which will return a * use controlP5.group("theNameOfTheGroup") which will return a
* *
* @return Accordion * @return Accordion
*/ */
public Accordion removeItem( ControlGroup< ? > theGroup ) { public Accordion removeItem( final ControlGroup< ? extends ControlGroup< ? > > theGroup ) {
if ( theGroup == null ) { if ( theGroup == null ) return this;
return this;
}
controllers.remove( theGroup ); controllers.remove( theGroup );
theGroup.removeListener( this ); theGroup.removeListener( this ).moveTo( cp5.controlWindow );
theGroup.moveTo( cp5.controlWindow ); return updateItems( );
updateItems( );
return this;
} }
/** /**
* UpdateItems is called when changes such as remove, change of height is performed on an * updateItems() is called when changes such as remove, change of height are performed on an Accordion.
* accordion. updateItems() is called automatically for such cases, but by calling updateItems * updateItems() is called automatically for such cases, but by calling updateItems()
* manually an update will be forced. * manually an update will be forced.
* *
* @return Accordion * @return Accordion
*/ */
public Accordion updateItems( ) { public Accordion updateItems( ) {
int n = 0;
setWidth( _myWidth ); setWidth( _myWidth );
if ( controllers.size( ) != 0 ) synchronized ( controllers.get( ) ) {
for ( ControllerInterface< ? > cg : controllers.get( ) ) { int n = 0;
if ( cg instanceof ControlGroup ) { for ( final ControllerInterface< ? > ci : controllers.get( ) )
n += ( ( ControlGroup ) cg ).getBarHeight( ) + spacing; if ( ci instanceof ControlGroup< ? > ) {
cg.setPosition( 0 , n ); final ControlGroup< ? > cg = ( ControlGroup< ? > ) ci;
if ( ( ( ControlGroup ) cg ).isOpen( ) ) { cg.setPosition( 0 , n += cg.getBarHeight( ) + spacing );
n += ( ( ControlGroup ) cg ).getBackgroundHeight( ); if ( cg.isOpen( ) ) n += cg.getBackgroundHeight( );
} }
}
} }
return this; return this;
} }
@ -139,15 +121,15 @@ package controlP5;
*/ */
public Accordion setMinItemHeight( int theHeight ) { public Accordion setMinItemHeight( int theHeight ) {
minHeight = theHeight; minHeight = theHeight;
for ( ControllerInterface< ? > cg : controllers.get( ) ) { if ( controllers.size( ) != 0 ) synchronized ( controllers.get( ) ) {
if ( cg instanceof ControlGroup ) { for ( final ControllerInterface< ? > ci : controllers.get( ) )
if ( ( ( ControlGroup ) cg ).getBackgroundHeight( ) < minHeight ) { if ( ci instanceof ControlGroup< ? > ) {
( ( ControlGroup ) cg ).setBackgroundHeight( minHeight ); final ControlGroup< ? > cg = ( ControlGroup< ? > ) ci;
if ( cg.getBackgroundHeight( ) < minHeight )
cg.setBackgroundHeight( minHeight );
} }
}
} }
updateItems( ); return updateItems( );
return this;
} }
public int getMinItemHeight( ) { public int getMinItemHeight( ) {
@ -155,131 +137,103 @@ package controlP5;
} }
public Accordion setItemHeight( int theHeight ) { public Accordion setItemHeight( int theHeight ) {
itemheight = theHeight; itemHeight = theHeight;
for ( ControllerInterface< ? > cg : controllers.get( ) ) { if ( controllers.size( ) != 0 ) synchronized ( controllers.get( ) ) {
if ( cg instanceof ControlGroup ) { for ( final ControllerInterface< ? > ci : controllers.get( ) )
( ( ControlGroup ) cg ).setBackgroundHeight( itemheight ); if ( cg instanceof ControlGroup< ? > )
} ( ( ControlGroup< ? > ) ci ).setBackgroundHeight( itemHeight );
} }
updateItems( ); return updateItems( );
return this;
} }
public int getItemHeight( ) { public int getItemHeight( ) {
return itemheight; return itemHeight;
} }
@Override public Accordion setWidth( int theWidth ) { @Override public T setWidth( final int theWidth ) {
super.setWidth( theWidth ); if ( controllers.size( ) != 0 ) synchronized ( controllers.get( ) ) {
for ( ControllerInterface< ? > cg : controllers.get( ) ) { for ( ControllerInterface< ? > ci : controllers.get( ) )
if ( cg instanceof ControlGroup ) { if ( cg instanceof ControlGroup< ? > )
( ( ControlGroup ) cg ).setWidth( theWidth ); ( ( ControlGroup< ? > ) ci ).setWidth( theWidth );
}
} }
return this; return super.setWidth( theWidth );
} }
/** /**
* @exclude {@inheritDoc} * @exclude {@inheritDoc}
*/ */
@Override @ControlP5.Invisible public void controlEvent( ControlEvent theEvent ) { @ControlP5.Invisible @Override public void controlEvent( final ControlEvent theEvent ) {
if ( theEvent.isGroup( ) ) { if ( theEvent.isGroup( ) && controllers.size( ) != 0 ) synchronized ( controllers.get( ) ) {
int n = 0; int n = 0;
for ( ControllerInterface< ? > cg : controllers.get( ) ) { for ( final ControllerInterface< ? > ci : controllers.get( ) ) {
if ( cg instanceof ControlGroup ) { if ( ci instanceof ControlGroup< ? > ) {
n += ( ( ControlGroup ) cg ).getBarHeight( ) + spacing; final ControlGroup< ? > cg = ( ControlGroup< ? > ) ci;
cg.setPosition( 0 , n ); cg.setPosition( 0 , n += cg.getBarHeight( ) + spacing );
if ( _myMode == SINGLE ) { if ( _myMode != SINGLE ) {
if ( cg == theEvent.getGroup( ) && ( ( ControlGroup ) cg ).isOpen( ) ) { if ( cg.isOpen( ) ) n += cg.getBackgroundHeight( );
n += ( ( ControlGroup ) cg ).getBackgroundHeight( ); } else if ( cg.isOpen( ) && cg == theEvent.getGroup( ) ) {
} else { n += cg.getBackgroundHeight( );
( ( ControlGroup ) cg ).close( ); } else cg.close( );
}
} else {
if ( ( ( ControlGroup ) cg ).isOpen( ) ) {
n += ( ( ControlGroup ) cg ).getBackgroundHeight( );
}
}
} }
} }
} }
} }
public Accordion open( ) { @Override public T open( ) {
int[] n = new int[ controllers.size( ) ]; final int len = controllers.size( ) , n[] = new int[ len ];
for ( int i = 0 ; i < controllers.size( ) ; i++ ) { for ( int i = 0 ; i < len ; n[ i ] = i++ );
n[ i ] = i;
}
return open( n ); return open( n );
} }
public Accordion close( ) { @Override public T close( ) {
int[] n = new int[ controllers.size( ) ]; final int len = controllers.size( ) , n[] = new int[ len ];
for ( int i = 0 ; i < controllers.size( ) ; i++ ) { for ( int i = 0 ; i < len ; n[ i ] = i++ );
n[ i ] = i;
}
return close( n ); return close( n );
} }
public Accordion open( int ... theId ) { @SafeVarargs public final T open( final int... theIds ) {
if ( theId[ 0 ] == -1 ) { if ( theIds == null || theIds[ 0 ] == -1 ) return open( );
return open( ); if ( controllers.size( ) != 0 ) synchronized ( controllers.get( ) ) {
} int n = 0 , i = 0;
int n = 0 , i = 0; for ( final ControllerInterface< ? > ci : controllers.get( ) )
for ( ControllerInterface< ? > cg : controllers.get( ) ) { if ( ci instanceof ControlGroup< ? > ) {
if ( cg instanceof ControlGroup ) { final ControlGroup< ? > cg = ( ControlGroup< ? > ) ci;
boolean a = false; boolean idCheck = false;
for ( int j = 0 ; j < theId.length ; j++ ) { for ( final int id : theIds ) if ( id == i ) {
if ( theId[ j ] == i ) { idCheck = true;
a = true; break;
} }
++i;
cg.setPosition( 0 , n += cg.getBarHeight( ) + spacing );
if ( idCheck || cg.isOpen( ) ) n += cg.open( ).getBackgroundHeight( );
} }
boolean b = ( ( ControlGroup ) cg ).isOpen( ) || a ? true : false;
i++;
n += ( ( ControlGroup ) cg ).getBarHeight( ) + spacing;
cg.setPosition( 0 , n );
if ( b ) {
n += ( ( ControlGroup ) cg ).getBackgroundHeight( );
( ( ControlGroup ) cg ).open( );
}
}
} }
return this; return me;
} }
public Accordion close( int ... theId ) { @SafeVarargs public final T close( final int... theIds ) {
if ( theId[ 0 ] == -1 ) { if ( theIds == null || theIds[ 0 ] == -1 ) return close( );
return close( ); if ( controllers.size( ) != 0 ) synchronized ( controllers.get( ) ) {
} int n = 0 , i = 0;
int n = 0 , i = 0; for ( final ControllerInterface< ? > ci : controllers.get( ) )
for ( ControllerInterface< ? > cg : controllers.get( ) ) { if ( ci instanceof ControlGroup< ? > ) {
if ( cg instanceof ControlGroup ) { final ControlGroup< ? > cg = ( ControlGroup< ? > ) ci;
boolean a = false; boolean idCheck = false;
for ( int j = 0 ; j < theId.length ; j++ ) { for ( final int id : theIds ) if ( id == i ) {
if ( theId[ j ] == i ) { idCheck = true;
a = true; break;
} }
++i;
cg.setPosition( 0 , n += cg.getBarHeight( ) + spacing );
if ( idCheck || !cg.isOpen( ) ) cg.close( );
else n += cg.getBackgroundHeight( );
} }
boolean b = ! ( ( ControlGroup ) cg ).isOpen( ) || a ? true : false;
i++;
n += ( ( ControlGroup ) cg ).getBarHeight( ) + spacing;
( ( ControlGroup ) cg ).setPosition( 0 , n );
if ( b ) {
( ( ControlGroup ) cg ).close( );
} else {
n += ( ( ControlGroup ) cg ).getBackgroundHeight( );
}
}
} }
return this; return me;
} }
public Accordion setCollapseMode( int theMode ) { public Accordion setCollapseMode( final int theMode ) {
if ( theMode == 0 ) { _myMode = theMode == 0 ? SINGLE : MULTI;
_myMode = SINGLE;
} else {
_myMode = MULTI;
}
return this; return this;
} }
} }