View Javadoc

1   /*
2    Wotonomy: OpenStep design patterns for pure Java applications.
3    Copyright (C) 2000 Blacksmith, Inc.
4   
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9   
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14  
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, see http://www.gnu.org
17   */
18  
19  package net.wotonomy.web;
20  
21  import net.wotonomy.foundation.NSDictionary;
22  import net.wotonomy.foundation.NSMutableDictionary;
23  
24  public class WOSwitchComponent extends WODynamicElement {
25  
26      private NSMutableDictionary elements;
27      private String currentName;
28      private WOElement currentElement;
29  
30      protected WOSwitchComponent() 
31      {
32          super();
33          elements = new NSMutableDictionary();
34      }
35  
36      public WOSwitchComponent(String aName, NSDictionary aMap, WOElement template) 
37      {
38          super(aName, aMap, template);
39          elements = new NSMutableDictionary();
40      }
41      
42      private WOElement getCurrentElement( WOContext c )
43      {
44          String name = stringForProperty( "WOComponentName", c.component() );
45          if ( name == null ) return null;
46          
47          if ( currentElement != null && name.equals( currentName ) ) return currentElement;
48          
49          currentName = name;
50          currentElement = (WOElement) elements.objectForKey( name );
51          if ( currentElement == null )
52          {
53              currentElement = WOApplication.application().pageWithName( name, c );
54              if ( currentElement != null )
55              {
56                  currentElement.associations = associations;
57                  elements.setObjectForKey( currentElement, name );
58              }
59          }
60          
61          return currentElement;
62      }
63  
64      void ensureAwakeInContext (WOContext aContext)
65      {
66          if ( aContext != null )
67          {
68              WOElement element = getCurrentElement( aContext );
69              if ( element != null )
70              {
71                  aContext.pushElement( element );
72                  element.ensureAwakeInContext( aContext );
73                  aContext.popElement();
74              }
75          }
76      }
77  
78      public void takeValuesFromRequest(WORequest r, WOContext c) 
79      {
80          WOElement element = getCurrentElement( c );
81          if ( element != null )
82          {
83              c.pushElement( element );
84              element.takeValuesFromRequest( r, c );
85              c.popElement();
86          }
87      }
88  
89      public WOActionResults invokeAction(WORequest r, WOContext c) 
90      {
91          WOActionResults result = null;
92          WOElement element = getCurrentElement( c );
93          if ( element != null )
94          {
95              c.pushElement( element );
96              result = element.invokeAction( r, c );
97              c.popElement();
98          }
99          return result;
100     }
101 
102     public void appendToResponse(WOResponse r, WOContext c) 
103     {
104         WOElement element = getCurrentElement( c );
105         if ( element != null )
106         {
107             c.pushElement( element );
108             element.appendToResponse( r, c );
109             c.popElement();
110         }
111     }
112 }