[Issue 3152659] [Nifty] Suggestion : Modification of the method Screen::findElementByName(…)

Hello,

In fact it concerns the methods findElementByName(…) and findControl(…) but since the second already got changed in 1.3, I didn’t add it to the title :slight_smile:

( I’m waiting for JME nightly build to add Nifty1.3 since I’m unable to do it myself. xD )



I’m wishing that those methods don’t return element flagged with tag markForRemoval()

Is it possible ?



I will explain why :



I use a panel element that is dynamicly populated.

Each time I populate it, I first destroy the previous element via a Callable PanelCleaner:

[java]

public class PanelCleaner implements Callable<Object> {



private Screen screen;

private Element parent;



public PanelCleaner( Screen screen, Element parent )

{

this.screen = screen;

this.parent = parent;

}





@Override

public Object call() throws Exception

{

if( ! screen.isRunning() )

return null;



for( Iterator<Element> i = parent.getElements().iterator(); i.hasNext(); )

{

Element element = i.next();

element.markForRemoval();

}

return null;

}

[/java]

The important part here is the method markForRemoval()



After that I populate with new element via another Callable.



And then I want to use those elements ( For highlighting some for example : )

[java]

public class MapElementHighlighter implements Callable<Object> {



private Nifty nifty;

private Screen screen;

private Vector<String> elementsToHighlight;

private Vector<String> allElements;



public MapElementHighlighter( Nifty nifty, Screen screen, Vector<String> elementsToHighlight, Vector<String> allElements )

{

this.nifty = nifty;

this.screen = screen;

this.elementsToHighlight= elementsToHighlight;

this.allElements = allElements;

}



@Override

public Object call() throws Exception

{

HistoryBuffer history = DataManager.getData().getHistory();

for( int i = 0; i < allElements.size(); i++ )

{

String name = allElements.get( i );

boolean checked = false;



if( elementsToHighlight.contains( name ) )

{

//Special case : FEATHERID. This one is not a real place

if( name.equals( MapController.CURRENTPLACEID ))

checked = true;

else

{

HistoryMapElt place = history.getAvailablePlaces().get( name );

if( place != null && place.isReacheable() )

checked = true;

}

}



CustomCheckboxControl checkbox = screen.findControl( name, CustomCheckboxControl.class );



if( checkbox != null )

checkbox.setChecked( checked );

}

return null;

}

}

[/java]

The problem reside in line

[java]CustomCheckboxControl checkbox = screen.findControl( name, CustomCheckboxControl.class );[/java]

The element returned is the first one with id ‘name’ → The one that will be destroyed just after.



I haven’t found a work around yet, so if my demand match with your view of using those methods … :slight_smile:

I reckon that calling a method to actually destroy those controls would be more standard than ignoring elements marked for removal, IMHO.

Well, if the element shouldn’t be used anymore ( since it will be deleted in a near future ) why retrieving it with methods like findElementByName() ?



Otherwise I finally found a work around for my problem. ( seems like explaining it to other helped me :slight_smile: )

The answer is in the PanelCleaner.

If I don’t want those elements to be retrievable in the future, I just have to rename them :smiley:



[java]

@Override

public Object call() throws Exception

{

if( ! screen.isRunning() )

return null;



for( Iterator<Element> i = parent.getElements().iterator(); i.hasNext(); )

{

Element element = i.next();

flagElements( element );

element.markForRemoval();

}

return null;

}





private void flagElements( Element element )

{

element.setId( “toBeDeleted” );

for( int i = 0; i < element.getElements().size(); i++ )

{

Element elem = element.getElements().get( i );

if( elem != null )

flagElements( elem );

}

}

[/java]

yes, nice idea! can you add this as a feature request directly at sf.net for the nifty project? :slight_smile:

I will do that :slight_smile: