I’ve been looking into the cloner class to figure out what changes do i have to make in lemur.
I came into the conclution that i have to start implementing Cloneable or JmeCloneable into the clases that needs to be clone, and update all the clonefields to add this classes
I started with the Panel class and made a change in the cloneFields method like this
@Override
public void cloneFields( Cloner cloner, Object original ) {
cloner.setCloneFunction(java.util.Map.class, new MapCloneFunction());
super.cloneFields(cloner, original);
this.elementId = cloner.clone(elementId);
this.name = this.name+" clone";
}
I made this class to handle map cloning because theres a map that needs to be cloned
public class MapCloneFunction<T extends Map> implements CloneFunction<T> {
public T cloneObject( Cloner cloner, T object ) {
try {
T clone = cloner.javaClone(object);
return clone;
} catch( CloneNotSupportedException e ) {
throw new IllegalArgumentException("Clone not supported for type:" + object.getClass(), e);
}
}
/**
* Clones the elements of the map.
*/
@SuppressWarnings("unchecked")
public void cloneFields( Cloner cloner, T clone, T object ) {
for(Object mapp : object.entrySet()){
Map.Entry entry = (Map.Entry) mapp;
if(entry.getKey().equals(entry.getValue().getClass()) ||
entry.getKey() instanceof String ||
entry.getKey() instanceof Integer ){
ValueClone( cloner, clone, object ,entry);
}else{
KeyValueClone( cloner, clone, object ,entry);
}
}
}
/**
* It only clones the value of the map if it's part of the jme-clonning system
*/
public void ValueClone(Cloner cloner, T clone, T object ,Map.Entry entry){
CloneFunction f = cloner.getCloneFunction(entry.getValue().getClass());
if(f != null ||
entry.getValue() instanceof Cloneable ||
entry.getValue() instanceof JmeCloneable ||
entry.getValue().getClass().isArray() ){
clone.put(entry.getKey(), cloner.clone(entry.getValue()));
}
}
/**
* It clones the key and value of the map if they are part of the jme-clonning system
*/
public void KeyValueClone(Cloner cloner, T clone, T object ,Map.Entry entry){
CloneFunction f = cloner.getCloneFunction(entry.getKey().getClass());
CloneFunction f2 = cloner.getCloneFunction(entry.getValue().getClass());
if(f != null ||
entry.getKey() instanceof Cloneable ||
entry.getKey() instanceof JmeCloneable ||
entry.getKey().getClass().isArray()
&&
f2 != null ||
entry.getValue() instanceof Cloneable ||
entry.getValue() instanceof JmeCloneable ||
entry.getValue().getClass().isArray()){
clone.put(cloner.clone(entry.getKey()), cloner.clone(entry.getValue()));
}else{
if(f != null ||
entry.getKey() instanceof Cloneable ||
entry.getKey() instanceof JmeCloneable ||
entry.getKey().getClass().isArray() ){
clone.put(cloner.clone(entry.getKey()), entry.getValue());
}else{
if(f2 != null ||
entry.getValue() instanceof Cloneable ||
entry.getValue() instanceof JmeCloneable ||
entry.getValue().getClass().isArray()){
clone.put(entry.getKey(), cloner.clone(entry.getValue()));
}else{
clone.put(entry.getKey(), entry.getValue());
}
}
}
}
}
On the ElementId class i implemented Cloneable and ad this method
@Override
public ElementId clone() {
return new ElementId(this.getId());
}
On the GuiControl class i override the cloneFields method like this
@Override
public void cloneFields( Cloner cloner, Object original ) {
super.cloneFields(cloner, original);
this.listeners = cloner.clone(listeners);
this.focusListeners = cloner.clone(focusListeners);
this.updateListeners = cloner.clone(updateListeners);
this.preferredSizeOverride = cloner.clone(preferredSizeOverride);
this.lastSize = cloner.clone(lastSize);
this.componentStack = cloner.clone(componentStack);
this.componentStack.attach(this);
this.layout = cloner.clone(layout);
this.setLayout(this.layout);
}
Seens im cloning the ComponentStack class i implemented JmeCloneable and add this methods
@Override
public Object jmeClone() {
try {
return super.clone();
} catch( CloneNotSupportedException e ) {
throw new RuntimeException( "Can't clone ComponentStack ", e );
}
}
@Override
public void cloneFields( Cloner cloner, Object original ) {
this.components = cloner.clone(components);
this.topList = cloner.clone(topList);
this.index = cloner.clone(index);
}
And on the GuiComponent interface i extended Cloneable
With this changes i can clone a Container object without the childrens, i hope if i keep doing this type of changes to all the GuiElements i could clone everything, but before i continue i whant to know if im doing it right or is there another way