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