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  
20  package org.apache.myfaces.tobago.internal.component;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  import org.apache.myfaces.tobago.component.Visual;
24  import org.apache.myfaces.tobago.model.ExpandedState;
25  import org.apache.myfaces.tobago.model.ScrollPosition;
26  import org.apache.myfaces.tobago.model.SelectedState;
27  import org.apache.myfaces.tobago.model.TreeState;
28  
29  import javax.el.ELContext;
30  import javax.el.ValueExpression;
31  import javax.faces.component.NamingContainer;
32  import javax.faces.component.UIComponent;
33  import javax.faces.context.FacesContext;
34  
35  /**
36   * {@link org.apache.myfaces.tobago.internal.taglib.component.TreeTagDeclaration}
37   */
38  public abstract class AbstractUITree extends AbstractUIData implements NamingContainer, Visual {
39  
40    public static final String MESSAGE_NOT_LEAF = "tobago.tree.MESSAGE_NOT_LEAF";
41  
42    /**
43     * @deprecated since 2.0.0
44     */
45    @Deprecated
46    public static final String SEP = "-";
47  
48    /**
49     * @deprecated since 2.0.0
50     */
51    @Deprecated
52    public static final String SELECT_STATE = SEP + "selectState";
53  
54    /**
55     * @deprecated since 2.0.0
56     */
57    @Deprecated
58    public static final String MARKED = "marked";
59  
60    private TreeState state;
61  
62    @Override
63    public void processValidators(final FacesContext facesContext) {
64      final int last = hasRows() ? getFirst() + getRows() : Integer.MAX_VALUE;
65      for (int rowIndex = getFirst(); rowIndex < last; rowIndex++) {
66        setRowIndex(rowIndex);
67        if (!isRowAvailable()) {
68          break;
69        }
70        for (final UIComponent child : getChildren()) {
71          child.processValidators(facesContext);
72        }
73      }
74      setRowIndex(-1);
75    }
76  
77    @Override
78    public void processUpdates(final FacesContext facesContext) {
79      final int last = hasRows() ? getFirst() + getRows() : Integer.MAX_VALUE;
80      for (int rowIndex = getFirst(); rowIndex < last; rowIndex++) {
81        setRowIndex(rowIndex);
82        if (!isRowAvailable()) {
83          break;
84        }
85        for (final UIComponent child : getChildren()) {
86          child.processUpdates(facesContext);
87        }
88      }
89      setRowIndex(-1);
90    }
91  
92    /**
93     * @deprecated since 2.0.0
94     */
95    @Deprecated
96    public UIComponent getRoot() {
97      // find the UITreeNode in the children.
98      for (final UIComponent child : getChildren()) {
99        if (child instanceof AbstractUITreeNodeBase) {
100         return child;
101       }
102     }
103     return null;
104   }
105 
106   @Override
107   public boolean getRendersChildren() {
108     return true;
109   }
110 
111   @Override
112   public void processDecodes(final FacesContext facesContext) {
113 
114     if (!isRendered()) {
115       return;
116     }
117 
118     final int last = hasRows() ? getFirst() + getRows() : Integer.MAX_VALUE;
119     for (int rowIndex = getFirst(); rowIndex < last; rowIndex++) {
120       setRowIndex(rowIndex);
121       if (!isRowAvailable()) {
122         break;
123       }
124       for (final UIComponent child : getChildren()) {
125         child.processDecodes(facesContext);
126       }
127     }
128     setRowIndex(-1);
129 
130     decode(facesContext);
131   }
132 
133 /* XXX
134   @Override
135   public void validate(FacesContext context) {
136 */
137 
138   // todo: validate must be written new, without TreeState
139 /*
140     if (isRequired() && getState().getSelection().size() == 0) {
141       setValid(false);
142       FacesMessage facesMessage = MessageUtils.createFacesMessage(context,
143           UISelectOne.MESSAGE_VALUE_REQUIRED, FacesMessage.SEVERITY_ERROR);
144       context.addMessage(getClientId(context), facesMessage);
145     }
146 
147     String selectable = ComponentUtils.getStringAttribute(this, selectable);
148     if (selectable != null && selectable.endsWith("LeafOnly")) {
149 
150       Set<DefaultMutableTreeNode> selection = getState().getSelection();
151 
152       for (DefaultMutableTreeNode node : selection) {
153         if (!node.isLeaf()) {
154           FacesMessage facesMessage = MessageUtils.createFacesMessage(
155               context, MESSAGE_NOT_LEAF, FacesMessage.SEVERITY_ERROR);
156           context.addMessage(getClientId(context), facesMessage);
157           break; // don't continue iteration, no dublicate messages needed
158         }
159       }
160     }
161 */
162 //  call all validators
163 /*
164     if (getValidators() != null) {
165       for (Validator validator : getValidators()) {
166         try {
167           validator.validate(context, this, null);
168         } catch (ValidatorException ve) {
169           // If the validator throws an exception, we're
170           // invalid, and we need to add a message
171           setValid(false);
172           FacesMessage message = ve.getFacesMessage();
173           if (message != null) {
174             message.setSeverity(FacesMessage.SEVERITY_ERROR);
175             context.addMessage(getClientId(context), message);
176           }
177         }
178       }
179     }
180   }
181 
182   @Override
183   public void updateModel(FacesContext facesContext) {
184     // nothing to update for tree's
185     // TODO: updating the model here and *NOT* in the decode phase
186   }
187 */
188   public void setState(final TreeState state) {
189     this.state = state;
190   }
191 
192   public TreeState getState() {
193     if (state != null) {
194       return state;
195     }
196 
197     final ELContext elContext = FacesContext.getCurrentInstance().getELContext();
198     final ValueExpression expression = getValueExpression(Attributes.state.getName());
199 
200     if (expression != null) {
201       TreeState state = (TreeState) expression.getValue(elContext);
202       if (state == null) {
203         state = new TreeState(new ExpandedState(2), new SelectedState(), new ScrollPosition());
204         expression.setValue(elContext, state);
205       }
206       return state;
207     }
208 
209     state = new TreeState(new ExpandedState(2), new SelectedState(), new ScrollPosition());
210     return state;
211   }
212 
213   @Override
214   public SelectedState getSelectedState() {
215     return getState().getSelectedState();
216   }
217 
218   @Override
219   public ExpandedState getExpandedState() {
220     return getState().getExpandedState();
221   }
222 
223   @Override
224   public void restoreState(final FacesContext context, final Object componentState) {
225     final Object[] values = (Object[]) componentState;
226     super.restoreState(context, values[0]);
227     state = (TreeState) values[1];
228   }
229 
230   @Override
231   public Object saveState(final FacesContext context) {
232     final Object[] values = new Object[2];
233     values[0] = super.saveState(context);
234     values[1] = state;
235     return values;
236   }
237 }