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.compat.InvokeOnComponent;
23  import org.apache.myfaces.tobago.component.Attributes;
24  import org.apache.myfaces.tobago.model.ExpandedState;
25  import org.apache.myfaces.tobago.model.Selectable;
26  import org.apache.myfaces.tobago.model.SelectedState;
27  import org.apache.myfaces.tobago.model.TreeDataModel;
28  import org.apache.myfaces.tobago.model.TreeNodeDataModel;
29  import org.apache.myfaces.tobago.model.TreePath;
30  import org.apache.myfaces.tobago.util.ComponentUtils;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  import javax.faces.FacesException;
35  import javax.faces.component.ContextCallback;
36  import javax.faces.context.FacesContext;
37  import javax.faces.model.DataModel;
38  import javax.swing.tree.TreeNode;
39  import java.io.IOException;
40  import java.util.List;
41  
42  public abstract class AbstractUIData extends javax.faces.component.UIData implements InvokeOnComponent {
43  
44    private static final Logger LOG = LoggerFactory.getLogger(AbstractUIData.class);
45  
46    /**
47     * @deprecated Since 2.0.0. The marked concept has been replaced by "selected".
48     */
49    @Deprecated
50    public static final String SUFFIX_MARKED = "marked";
51    public static final String SUFFIX_SELECTED = "selected";
52    public static final String SUFFIX_EXPANDED = "expanded";
53  
54    /**
55     * Only for tree model.
56     */
57    private boolean initialized;
58  
59    /**
60     * Only for tree model, other models come from the parent UIData.
61     */
62    private TreeDataModel dataModel;
63  
64    public boolean isTreeModel() {
65      init();
66      return dataModel != null;
67    }
68  
69    public TreeDataModel getTreeDataModel() {
70      if (isTreeModel()) {
71        return dataModel;
72      } else {
73        LOG.warn("Not a tree model");
74        return null;
75      }
76    }
77  
78    @Override
79    protected DataModel getDataModel() {
80      init();
81  
82      if (dataModel != null) {
83        return dataModel;
84      } else {
85        return super.getDataModel();
86      }
87    }
88  
89    private void init() {
90      if (!initialized) {
91        Object value = getValue();
92        boolean showRoot = isShowRoot();
93        createTreeDataModel(value, showRoot);
94  
95        initialized = true;
96      }
97    }
98  
99    /**
100    * Will be obsolete later when selectable has the type TreeSelectable.
101    */
102   public Selectable getSelectableAsEnum() {
103     final Selectable selectable = Selectable.parse(ComponentUtils.getStringAttribute(this, Attributes.SELECTABLE));
104     return selectable != null ? selectable : Selectable.NONE; // should not happen
105   }
106 
107   /**
108    * Creates the TreeDataModel which should be used.
109    * Override this method to use a custom model for an unsupported tree model.
110    * (Currently Tobago supports {@link TreeNode} out of the box.
111    * @param value The reference to the data model
112    *              (comes from the value attribute of the {@link javax.faces.component.UIData})
113    * @param showRoot comes from the showRoot attribute.
114    */
115   protected void createTreeDataModel(Object value, boolean showRoot) {
116     // TODO: use a factory
117     if (value instanceof TreeNode) {
118       dataModel = new TreeNodeDataModel((TreeNode) value, showRoot, getExpandedState());
119     }
120   }
121 
122   @Override
123   public void encodeBegin(FacesContext context) throws IOException {
124     initialized = false;
125     init();
126     if (dataModel != null) {
127       dataModel.reset();
128     }
129     super.encodeBegin(context);
130   }
131 
132   public abstract ExpandedState getExpandedState();
133 
134   public abstract SelectedState getSelectedState();
135 
136   /**
137    * @deprecated The name of this method is ambiguous.
138    * You may use the inverse of {@link #isRowsUnlimited()}. Deprecated since 1.5.5.
139    */
140   @Deprecated
141   public boolean hasRows() {
142     return getRows() != 0;
143   }
144 
145   public boolean isRowVisible() {
146     init();
147     if (dataModel != null) {
148       return dataModel.isRowVisible();
149     } else {
150       return super.getDataModel().isRowAvailable();
151     }
152   }
153 
154   public String getRowClientId() {
155     init();
156     return dataModel != null ? dataModel.getRowClientId() : null;
157   }
158 
159   public String getRowParentClientId() {
160     init();
161     return dataModel != null ? dataModel.getRowParentClientId() : null;
162   }
163 
164   public abstract boolean isShowRoot();
165 
166   public boolean isShowRootJunction() {
167     return false;
168   }
169 
170   /**
171    * @return Is the (maximum) number of rows to display set to zero?
172    */
173   public boolean isRowsUnlimited() {
174     return getRows() == 0;
175   }
176 
177   /**
178    * The value describes, if the UIData renderer creates container elements to hold the row information.
179    * This information is important for the TreeNodeRenderer to set the visible state in the output or not.
180    * Typically the Sheet returns true and a Tree returns false, because the sheet renders the HTML TR tags,
181    * the the sheet also is responsible for the visible state.
182    */
183   public boolean isRendersRowContainer() {
184     return false;
185   }
186 
187     // todo: after removing jsf 1.1: @Override
188   public boolean invokeOnComponent(FacesContext facesContext, String clientId, ContextCallback callback)
189       throws FacesException {
190     // we may need setRowIndex on UISheet
191     int oldRowIndex = getRowIndex();
192     try {
193       String sheetId = getClientId(facesContext);
194       if (clientId.startsWith(sheetId)) {
195         String idRemainder = clientId.substring(sheetId.length());
196         if (LOG.isDebugEnabled()) {
197           LOG.debug("idRemainder = '" + idRemainder + "'");
198         }
199         if (idRemainder.matches("^:\\d+:.*")) {
200           idRemainder = idRemainder.substring(1);
201           int idx = idRemainder.indexOf(":");
202           try {
203             int rowIndex = Integer.parseInt(idRemainder.substring(0, idx));
204             if (LOG.isDebugEnabled()) {
205               LOG.debug("set rowIndex = '" + rowIndex + "'");
206             }
207             setRowIndex(rowIndex);
208           } catch (NumberFormatException e) {
209             LOG.warn("idRemainder = '" + idRemainder + "'", e);
210           }
211         } else {
212           if (LOG.isDebugEnabled()) {
213             LOG.debug("no match for '^:\\d+:.*'");
214           }
215         }
216       }
217 
218       return ComponentUtils.invokeOnComponent(facesContext, this, clientId, callback);
219 
220     } finally {
221       // we should reset rowIndex on UISheet
222       setRowIndex(oldRowIndex);
223     }
224   }
225 
226   /**
227    * @return The TreePath of the current row index.
228    */
229   public TreePath getPath() {
230     if (isTreeModel()) {
231       return ((TreeDataModel) getDataModel()).getPath();
232     } else {
233       LOG.warn("Not a tree model");
234       return null;
235     }
236   }
237 
238   /**
239    * @return Is the current row index representing a folder.
240    */
241   public boolean isFolder() {
242     if (isTreeModel()) {
243       return ((TreeDataModel) getDataModel()).isFolder();
244     } else {
245       LOG.warn("Not a tree model");
246       return false;
247     }
248   }
249 
250   public List<Integer> getRowIndicesOfChildren() {
251     if (isTreeModel()) {
252       return dataModel.getRowIndicesOfChildren();
253     } else {
254       LOG.warn("Not a tree model");
255       return null;
256     }
257   }
258 }