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.Visual;
23  import org.apache.myfaces.tobago.model.ExpandedState;
24  import org.apache.myfaces.tobago.model.Selectable;
25  import org.apache.myfaces.tobago.model.SelectedState;
26  import org.apache.myfaces.tobago.model.TreeDataModel;
27  import org.apache.myfaces.tobago.model.TreeNodeDataModel;
28  import org.apache.myfaces.tobago.model.TreePath;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import javax.faces.FacesException;
33  import javax.faces.component.ContextCallback;
34  import javax.faces.component.UIComponent;
35  import javax.faces.component.visit.VisitCallback;
36  import javax.faces.component.visit.VisitContext;
37  import javax.faces.context.FacesContext;
38  import javax.faces.model.DataModel;
39  import javax.swing.tree.TreeNode;
40  import java.io.IOException;
41  import java.util.List;
42  
43  public abstract class AbstractUIData extends javax.faces.component.UIData implements Visual {
44  
45    private static final Logger LOG = LoggerFactory.getLogger(AbstractUIData.class);
46  
47    /**
48     * @deprecated Since 2.0.0. The marked concept has been replaced by "selected".
49     */
50    @Deprecated
51    public static final String SUFFIX_MARKED = "marked";
52    public static final String SUFFIX_SELECTED = "selected";
53    public static final String SUFFIX_EXPANDED = "expanded";
54  
55    /**
56     * Only for tree model.
57     */
58    private boolean initialized;
59  
60    /**
61     * Only for tree model, other models come from the parent UIData.
62     */
63    private TreeDataModel dataModel;
64  
65    public boolean isTreeModel() {
66      init();
67      return dataModel != null;
68    }
69  
70    public TreeDataModel getTreeDataModel() {
71      if (isTreeModel()) {
72        return dataModel;
73      } else {
74        LOG.warn("Not a tree model");
75        return null;
76      }
77    }
78  
79    @Override
80    protected DataModel getDataModel() {
81      init();
82  
83      if (dataModel != null) {
84        return dataModel;
85      } else {
86        return super.getDataModel();
87      }
88    }
89  
90    private void init() {
91      if (!initialized) {
92        final Object value = getValue();
93        final boolean showRoot = isShowRoot();
94        createTreeDataModel(value, showRoot);
95  
96        initialized = true;
97      }
98    }
99  
100   /**
101    * @deprecated since Tobago 3.0.0, please use {@link #getSelectable}
102    */
103   public Selectable getSelectableAsEnum() {
104     return getSelectable();
105   }
106 
107   public abstract Selectable getSelectable();
108 
109   /**
110    * Creates the TreeDataModel which should be used.
111    * Override this method to use a custom model for an unsupported tree model.
112    * (Currently Tobago supports {@link TreeNode} out of the box.
113    *
114    * @param value    The reference to the data model
115    *                 (comes from the value attribute of the {@link javax.faces.component.UIData})
116    * @param showRoot comes from the showRoot attribute.
117    */
118   protected void createTreeDataModel(final Object value, final boolean showRoot) {
119     // TODO: use a factory
120     if (value instanceof TreeNode) {
121       dataModel = new TreeNodeDataModel((TreeNode) value, showRoot, getExpandedState());
122     }
123   }
124 
125   @Override
126   public void encodeBegin(final FacesContext context) throws IOException {
127     initialized = false;
128     init();
129     if (dataModel != null) {
130       dataModel.reset();
131     }
132     super.encodeBegin(context);
133   }
134 
135   public abstract ExpandedState getExpandedState();
136 
137   public abstract SelectedState getSelectedState();
138 
139   /**
140    * @deprecated The name of this method is ambiguous.
141    * You may use the inverse of {@link #isRowsUnlimited()}. Deprecated since 1.5.5.
142    */
143   @Deprecated
144   public boolean hasRows() {
145     return getRows() != 0;
146   }
147 
148   public boolean isRowVisible() {
149     init();
150     if (dataModel != null) {
151       return dataModel.isRowVisible();
152     } else {
153       return super.getDataModel().isRowAvailable();
154     }
155   }
156 
157   public String getRowClientId() {
158     init();
159     return dataModel != null ? dataModel.getRowClientId() : null;
160   }
161 
162   public String getRowParentClientId() {
163     init();
164     return dataModel != null ? dataModel.getRowParentClientId() : null;
165   }
166 
167   public abstract boolean isShowRoot();
168 
169   public boolean isShowRootJunction() {
170     return false;
171   }
172 
173   /**
174    * @return Is the (maximum) number of rows to display set to zero?
175    */
176   public boolean isRowsUnlimited() {
177     return getRows() == 0;
178   }
179 
180   /**
181    * The value describes, if the UIData renderer creates container elements to hold the row information.
182    * This information is important for the TreeNodeRenderer to set the visible state in the output or not.
183    * Typically the Sheet returns true and a Tree returns false, because the sheet renders the HTML TR tags,
184    * the the sheet also is responsible for the visible state.
185    */
186   public boolean isRendersRowContainer() {
187     return false;
188   }
189 
190   @Override
191   public boolean invokeOnComponent(
192       final FacesContext facesContext, final String clientId, final ContextCallback callback)
193       throws FacesException {
194     // we may need setRowIndex on UISheet
195     final int oldRowIndex = getRowIndex();
196     try {
197       final String sheetId = getClientId(facesContext);
198       if (clientId.startsWith(sheetId)) {
199         String idRemainder = clientId.substring(sheetId.length());
200         if (LOG.isDebugEnabled()) {
201           LOG.debug("idRemainder = '" + idRemainder + "'");
202         }
203         if (idRemainder.matches("^:\\d+:.*")) {
204           idRemainder = idRemainder.substring(1);
205           final int idx = idRemainder.indexOf(":");
206           try {
207             final int rowIndex = Integer.parseInt(idRemainder.substring(0, idx));
208             if (LOG.isDebugEnabled()) {
209               LOG.debug("set rowIndex = '" + rowIndex + "'");
210             }
211             setRowIndex(rowIndex);
212           } catch (final NumberFormatException e) {
213             LOG.warn("idRemainder = '" + idRemainder + "'", e);
214           }
215         } else {
216           if (LOG.isDebugEnabled()) {
217             LOG.debug("no match for '^:\\d+:.*'");
218           }
219         }
220       }
221 
222       return super.invokeOnComponent(facesContext, clientId, callback);
223 
224     } finally {
225       // we should reset rowIndex on UISheet
226       setRowIndex(oldRowIndex);
227     }
228   }
229 
230   /**
231    * @return The TreePath of the current row index.
232    */
233   public TreePath getPath() {
234     if (isTreeModel()) {
235       return ((TreeDataModel) getDataModel()).getPath();
236     } else {
237       LOG.warn("Not a tree model");
238       return null;
239     }
240   }
241 
242   /**
243    * @return Is the current row index representing a folder.
244    */
245   public boolean isFolder() {
246     if (isTreeModel()) {
247       return ((TreeDataModel) getDataModel()).isFolder();
248     } else {
249       LOG.warn("Not a tree model");
250       return false;
251     }
252   }
253 
254   public List<Integer> getRowIndicesOfChildren() {
255     if (isTreeModel()) {
256       return dataModel.getRowIndicesOfChildren();
257     } else {
258       LOG.warn("Not a tree model");
259       return null;
260     }
261   }
262 
263   /**
264    * This is, because we need to visit the UIRow for each row, which is not done in the base implementation.
265    */
266   @Override
267   public boolean visitTree(VisitContext context, VisitCallback callback) {
268 
269     if (super.visitTree(context, callback)) {
270       return true;
271     }
272 
273     // save the current row index
274     int oldRowIndex = getRowIndex();
275     // set row index to -1 to process the facets and to get the rowless clientId
276     setRowIndex(-1);
277     // push the Component to EL
278     pushComponentToEL(context.getFacesContext(), this);
279 
280     try {
281       // iterate over the rows
282       int rowsToProcess = getRows();
283       // if getRows() returns 0, all rows have to be processed
284       if (rowsToProcess == 0) {
285         rowsToProcess = getRowCount();
286       }
287       int rowIndex = getFirst();
288       for (int rowsProcessed = 0; rowsProcessed < rowsToProcess; rowsProcessed++, rowIndex++) {
289         setRowIndex(rowIndex);
290         if (!isRowAvailable()) {
291           return false;
292         }
293         // visit the children of every child of the UIData that is an instance of UIColumn
294         for (int i = 0, childCount = getChildCount(); i < childCount; i++) {
295           UIComponent child = getChildren().get(i);
296           if (child instanceof AbstractUIRow) {
297             if (child.visitTree(context, callback)) {
298               return true;
299             }
300 
301           }
302         }
303       }
304     } finally {
305       // pop the component from EL and restore the old row index
306       popComponentFromEL(context.getFacesContext());
307       setRowIndex(oldRowIndex);
308     }
309 
310     // Return false to allow the visiting to continue
311     return false;
312   }
313 
314 }