1
2
3
4
5 package net.sf.wfnm;
6
7 /***
8 * The Navigation Manager factory manage instances of navigation manager.<br>
9 * It allows to get/reset/verify the unique instance of a navigation manager.
10 *
11 * @author <a href="mailto:malbari@users.sourceforge.net">Maurizio Albari</a>
12 * @version 1.0.6
13 */
14 public class NavigationManagerFactory {
15 /***
16 * The navigation mangaer session key.
17 */
18 private static final String NAVIGATION_MANAGER_KEY = ".wfnm";
19
20 /***
21 * Acquire the container unique instance of the navigation manager.
22 *
23 * @param container the attribute container
24 *
25 * @return the navigation manager
26 */
27 public static NavigationManager getInstance(AttributeContainer container) {
28 NavigationManager navigationManager = (NavigationManager) container.getAttribute(NAVIGATION_MANAGER_KEY);
29
30 if (navigationManager == null) {
31 Class managerClass = Config.getInstance().getManager();
32
33 try {
34 navigationManager = (NavigationManager) managerClass.newInstance();
35 } catch (Exception e) {
36 throw new ConfigException("Unable to instantiate the class " + managerClass.getName(), e);
37 }
38
39 Class pageChangedListenerClass = Config.getInstance().getPageChangedListener();
40
41 if (pageChangedListenerClass != null) {
42 try {
43 PageChangedListener pageChangedListener = (PageChangedListener) pageChangedListenerClass.newInstance();
44 navigationManager.setPageChangedListener(pageChangedListener);
45 } catch (Exception e) {
46 throw new ConfigException("Unable to instantiate the class " + pageChangedListenerClass.getName(), e);
47 }
48 }
49
50 Class webflowChangedListenerClass = Config.getInstance().getWebflowChangedListener();
51
52 if (webflowChangedListenerClass != null) {
53 try {
54 WebflowChangedListener webflowChangedListener = (WebflowChangedListener) webflowChangedListenerClass.newInstance();
55 navigationManager.setWebflowChangedListener(webflowChangedListener);
56 } catch (Exception e) {
57 throw new ConfigException("Unable to instantiate the class " + pageChangedListenerClass.getName(), e);
58 }
59 }
60
61 container.setAttributeValue(NAVIGATION_MANAGER_KEY, navigationManager);
62 }
63
64 return navigationManager;
65 }
66
67 /***
68 * Returns true if a key is the key used by the navigation manager.
69 *
70 * @param key the key
71 *
72 * @return true if a key is the key used by the navigation manager
73 */
74 public static boolean isNavigationManagerKey(String key) {
75 return NAVIGATION_MANAGER_KEY.equals(key);
76 }
77
78 /***
79 * Returns true if the instance of the navigation manager exists.
80 *
81 * @param container the attribute container
82 *
83 * @return true if the instance of the navigation manager exists
84 */
85 public static boolean existsInstance(AttributeContainer container) {
86 NavigationManager navigationManager = (NavigationManager) container.getAttribute(NAVIGATION_MANAGER_KEY);
87
88 return (navigationManager != null);
89 }
90
91 /***
92 * Reset the attribute container unique instance of the navigation manager.
93 *
94 * @param container the attribute container
95 */
96 public static void resetInstance(AttributeContainer container) {
97 NavigationManager navigationManager = (NavigationManager) container.getAttribute(NAVIGATION_MANAGER_KEY);
98
99 if (navigationManager != null) {
100 navigationManager.resetFramework(container);
101 container.removeAttributeValue(NAVIGATION_MANAGER_KEY);
102 }
103 }
104 }