Author: tchemit Date: 2010-06-21 15:28:01 +0200 (Mon, 21 Jun 2010) New Revision: 1978 Url: http://nuiton.org/repositories/revision/jaxx/1978 Log: add ChildLoador factory in TreeHelper Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/tree/JaxxTreeHelper.java Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/tree/JaxxTreeHelper.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/tree/JaxxTreeHelper.java 2010-06-20 22:06:49 UTC (rev 1977) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/tree/JaxxTreeHelper.java 2010-06-21 13:28:01 UTC (rev 1978) @@ -43,7 +43,9 @@ import java.util.Arrays; import java.util.Collections; import java.util.Enumeration; +import java.util.HashSet; import java.util.List; +import java.util.Set; /** * Tree helper to deal with the build of trees and other usefull operations. @@ -83,6 +85,39 @@ */ protected TreeModelListener treeModelListener; + /** + * Cache of child loadors. + */ + protected static Set<? super JaxxNodeChildLoador<?, ?>> childLoadors; + + protected static Set<? super JaxxNodeChildLoador<?, ?>> getChildLoadors() { + if (childLoadors == null) { + childLoadors = new HashSet<JaxxNodeChildLoador<?, ?>>(); + } + return childLoadors; + } + + public static <L extends JaxxNodeChildLoador<?, ?>> L getChildLoador(Class<L> loadorType) { + Set<? super JaxxNodeChildLoador<?, ?>> cache = getChildLoadors(); + L result = null; + for (Object loador : cache) { + if (loadorType.equals(loador.getClass())) { + result = (L) loador; + break; + } + } + if (result == null) { + // add it in cache + try { + result = loadorType.newInstance(); + cache.add(result); + } catch (Exception e) { + throw new IllegalArgumentException("Could not instanciate loador [" + loadorType.getName() + "]", e); + } + } + return result; + } + public JaxxTreeHelper() { selectionListener = new TreeSelectionListener() {