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 java.util.List;
22
23 import javax.faces.component.UIComponent;
24
25 import org.w3c.dom.NamedNodeMap;
26 import org.w3c.dom.Node;
27 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
28
29 /**
30 * Change specialization for removal of a child.
31 * While applying this Change, if there were to be a child with the specified
32 * identifier, it will be removed.
33 * @version $Name: $ ($Revision: adfrt/faces/adf-faces-api/src/main/java/oracle/adf/view/faces/change/RemoveChildComponentChange.java#0 $) $Date: 10-nov-2005.19:10:00 $
34 */
35 public class RemoveChildComponentChange extends ComponentChange
36 implements DocumentChange
37 {
38 /**
39 * Constructs a RemoveChildChange with the specified identifier of the child.
40 * @param childId The identifier of the child component that needs to be
41 * removed. If no identifier is specified, the type will be treated
42 * as of 'id' type.
43 * @throws IllegalArgumentException if specified childId were to be null.
44 */
45 public RemoveChildComponentChange(String childId)
46 {
47 this(childId, "id");
48 }
49
50 /**
51 * Constructs a RemoveChildChange with the specified identifier of the child.
52 * @param childId The identifier of the child component that needs to be
53 * removed.
54 * @param identifier Determines the type of identifier which is passed as the
55 * first argument.
56 * @throws IllegalArgumentException if specified childId were to be null.
57 */
58 public RemoveChildComponentChange(String childId, String identifier)
59 {
60 if ((childId == null) || (childId.length() == 0))
61 throw new IllegalArgumentException(_LOG.getMessage(
62 "CANNOT_CONSTRUCT_REMOVECHILDCHANGE_WITH_NULL_ID"));
63
64 if (identifier == null || "".equals(identifier))
65 throw new IllegalArgumentException(_LOG.getMessage(
66 "IDENTIFIER_TYPE_CANNOT_BE_NULL"));
67
68 _childId = childId;
69 _identifier = identifier;
70 }
71
72 /**
73 * Returns the identifier of child component that needs to be removed.
74 */
75 public String getChildId()
76 {
77 return _childId;
78 }
79
80 /**
81 * Returns the identifier type.
82 */
83 public final String getIdentifier()
84 {
85 return _identifier;
86 }
87
88 /**
89 * {@inheritDoc}
90 */
91 @SuppressWarnings("unchecked")
92 @Override
93 public void changeComponent(UIComponent uiComponent)
94 {
95 if (uiComponent.getChildCount() == 0)
96 return;
97
98 List<UIComponent> children = uiComponent.getChildren();
99 children.remove(ChangeUtils.getChildForId(uiComponent, _childId, _identifier));
100 }
101
102 /**
103 * {@inheritDoc}
104 */
105 public void changeDocument(Node componentNode)
106 {
107 Node currChild = componentNode.getFirstChild();
108
109 while (currChild != null)
110 {
111 NamedNodeMap attributes = currChild.getAttributes();
112
113 if (attributes != null)
114 {
115 Node idAttr = attributes.getNamedItem(_identifier);
116
117 if (idAttr != null)
118 {
119 if (_childId.equals(idAttr.getNodeValue()))
120 {
121 currChild.getParentNode().removeChild(currChild);
122 break;
123 }
124 }
125 }
126
127 currChild = currChild.getNextSibling();
128 }
129 }
130
131 /**
132 * Returns true if adding the DocumentChange should force the JSP Document
133 * to reload
134 */
135 public boolean getForcesDocumentReload()
136 {
137 return false;
138 }
139
140 private final String _childId;
141 private final String _identifier;
142 private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(
143 RemoveChildComponentChange.class);
144 private static final long serialVersionUID = 1L;
145 }