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.Facets;
23  import org.apache.myfaces.tobago.component.OnComponentPopulated;
24  import org.apache.myfaces.tobago.component.SupportsAccessKey;
25  import org.apache.myfaces.tobago.component.SupportsRenderedPartially;
26  import org.apache.myfaces.tobago.component.Visual;
27  import org.apache.myfaces.tobago.event.PopupFacetActionListener;
28  import org.apache.myfaces.tobago.internal.util.AuthorizationHelper;
29  import org.apache.myfaces.tobago.util.ComponentUtils;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  import javax.el.MethodExpression;
34  import javax.faces.component.UICommand;
35  import javax.faces.component.UIComponent;
36  import javax.faces.component.behavior.ClientBehaviorHolder;
37  import javax.faces.context.FacesContext;
38  import javax.faces.event.FacesEvent;
39  import javax.faces.event.PhaseId;
40  import java.util.Arrays;
41  import java.util.Collection;
42  import java.util.Iterator;
43  
44  public abstract class AbstractUICommand
45      extends UICommand
46      implements SupportsRenderedPartially, SupportsAccessKey, OnComponentPopulated, Visual, ClientBehaviorHolder {
47  
48    private static final Logger LOG = LoggerFactory.getLogger(AbstractUICommand.class);
49  
50    // todo generate
51    private static final Collection<String> EVENT_NAMES = Arrays.asList("click");
52  
53    enum PropertyKeys {
54      disabled,
55    }
56  
57    // todo: transient
58    private Boolean parentOfCommands;
59  
60    @Override
61    public void onComponentPopulated(final FacesContext facesContext, final UIComponent parent) {
62      final AbstractUIPopup popup = (AbstractUIPopup) ComponentUtils.getFacet(this, Facets.popup);
63      if (popup != null) {
64        if (!ComponentUtils.containsPopupActionListener(this)) {
65          addActionListener(new PopupFacetActionListener());
66        }
67      }
68    }
69  
70    @Override
71    public void processDecodes(final FacesContext context) {
72      if (context == null) {
73        throw new NullPointerException();
74      }
75  
76      // Skip processing if our rendered flag is false
77      if (!isRendered()) {
78        return;
79      }
80  
81      // Process this component itself
82      try {
83        decode(context);
84      } catch (final RuntimeException e) {
85        context.renderResponse();
86        throw e;
87      }
88  
89      final Iterator kids = getFacetsAndChildren();
90      while (kids.hasNext()) {
91        final UIComponent kid = (UIComponent) kids.next();
92        kid.processDecodes(context);
93      }
94    }
95  
96    @Override
97    public void queueEvent(final FacesEvent facesEvent) {
98      // fix for TOBAGO-262
99      super.queueEvent(facesEvent);
100     if (this == facesEvent.getSource()) {
101       if (isImmediate()) {
102         facesEvent.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
103       } else {
104         facesEvent.setPhaseId(PhaseId.INVOKE_APPLICATION);
105       }
106     }
107   }
108 
109   public boolean isParentOfCommands() {
110     if (parentOfCommands == null) {
111       parentOfCommands = false;
112       for (UIComponent child : getChildren()) {
113         if (child instanceof UICommand) {
114           parentOfCommands = true;
115           break;
116         }
117       }
118     }
119     return parentOfCommands;
120   }
121 
122   /**
123    Flag indicating that this element is disabled.
124    <br>Default: <code>false</code>
125    */
126   public boolean isDisabled() {
127 
128     final FacesContext facesContext = getFacesContext();
129     // todo: get from configuration tobago-config.xml
130     if (true) {
131       final AuthorizationHelper authorizationHelper = AuthorizationHelper.getInstance(facesContext);
132       final MethodExpression actionExpression = getActionExpression();
133       if (actionExpression != null) {
134         final boolean authorized =
135             authorizationHelper.isAuthorized(facesContext, actionExpression.getExpressionString());
136         if (!authorized) {
137           return true;
138         }
139       }
140     }
141 
142     Boolean bool = (Boolean) getStateHelper().eval(PropertyKeys.disabled);
143     if (bool != null) {
144       return bool;
145     }
146     return false;
147   }
148 
149   public void setDisabled(boolean disabled) {
150     getStateHelper().put(PropertyKeys.disabled, disabled);
151   }
152 
153   // todo generate
154   @Override
155   public String getDefaultEventName() {
156     return "click";
157   }
158 
159   // todo generate
160   @Override
161   public Collection<String> getEventNames() {
162     return EVENT_NAMES;
163   }
164 
165   @Override
166   public abstract String getLabel();
167 
168   public abstract boolean isJsfResource();
169 
170   public abstract String getResource();
171 
172   public abstract String getLink();
173 
174   public abstract String getTarget();
175 
176   public abstract boolean isTransition();
177 
178   @Override
179   public abstract String[] getRenderedPartially();
180 
181   public abstract boolean isOmit();
182 
183   public abstract String getTip();
184 
185 //  public abstract Integer getTabIndex();
186 }