View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    * 
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   */
19  package org.apache.myfaces.trinidad.change;
20  
21  import javax.faces.component.UIComponent;
22  import javax.faces.context.FacesContext;
23  
24  import org.apache.myfaces.trinidad.logging.TrinidadLogger;
25  
26  import org.w3c.dom.Document;
27  
28  
29  /**
30   * Base ChangeManager implementation that manages the bookkeeping for
31   * supporting both ComponentChanges and DocumentChanges.
32   * subclasses must implement addComponentChangeImpl() to implement
33   * the ComponentChange support.  To support DocumentChanges,
34   * <code>getDocument</code> must be implemented.
35   *
36   * @version $Name:  $ ($Revision: adfrt/faces/adf-faces-impl/src/main/java/oracle/adfinternal/view/faces/change/BaseChangeManager.java#1 $) $Date: 11-nov-2005.14:59:41 $
37   */
38  abstract class BaseChangeManager extends ChangeManager
39  {
40    /**
41     * {@inheritDoc}
42     */
43    @Override
44    public void addComponentChange(
45      FacesContext facesContext,
46      UIComponent uiComponent,
47      ComponentChange change)
48    {
49      if (facesContext == null || uiComponent == null || change == null)
50        throw new IllegalArgumentException(_LOG.getMessage(
51          "CANNOT_ADD_CHANGE_WITH_FACECONTEXT_OR_UICOMPONENT_OR_NULL"));
52  
53      // add the change to the component
54      addComponentChangeImpl(facesContext, uiComponent, change);
55  
56      if (supportsDocumentPersistence(facesContext))
57      {
58        DocumentChange docChange = null;
59  
60        if (change instanceof DocumentChange)
61        {
62          docChange = (DocumentChange)change;
63        }
64        else
65        {
66          // try to get equivalent DocumentChange from ComponentChange
67          docChange = createDocumentChange(change);
68        }
69  
70        if (docChange != null)
71        {
72          addDocumentChange(facesContext, uiComponent, docChange);
73        }
74      }
75    }
76  
77    /**
78     * A no-op implementation of adding a ComponentChange. Sub-classers should
79     * override and provide an implementation if they support component changes.
80     * @param facesContext The FacesContext for this request.
81     * @param targetComponent The target component against which this change needs 
82     * to be registered and applied later on.
83     * @param componentChange The ComponentChange to add
84     */
85     protected void addComponentChangeImpl(
86      FacesContext facesContext,
87      UIComponent targetComponent,
88      ComponentChange componentChange)
89    {
90      //no-op
91    }
92  
93    // =-= bts Testing hack hook
94    protected void persistDocumentChanges(
95      FacesContext facesContext)
96    {
97      // noop
98    }
99  
100   /**
101    * Override to return the Document to modify as part of document-based
102    * persistence.
103    * Subclassers adding Document-based Persistence
104    * must override this method and should override
105    * <code>supportsDocumentPersistence</code>
106    * in order to enable  Document-based Persistence
107    */
108   protected abstract Document getDocument(FacesContext context);
109   
110   /**
111    *  Returns true if we can support Document-based Persistence
112    *  in this ChangeManager.  Subclassers adding Document-based Persistence
113    *  should override this method and must override <code>getDocument</code>
114    *  in order to enable  Document-based Persistence.
115    * @param context
116    * @return true if we can support Document-based Persistence
117    */
118   protected boolean supportsDocumentPersistence(FacesContext context)
119   {
120     // correct, but potentially slow implementation
121     return getDocument(context) != null;
122   }
123   private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(
124     BaseChangeManager.class);
125 }