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