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  
24  import javax.faces.component.UIOutput;
25  import javax.faces.component.behavior.ClientBehaviorHolder;
26  import javax.faces.event.AbortProcessingException;
27  import javax.faces.event.ComponentSystemEvent;
28  import javax.faces.event.ComponentSystemEventListener;
29  import javax.faces.event.ListenerFor;
30  import javax.faces.event.PreRenderComponentEvent;
31  import javax.swing.BoundedRangeModel;
32  
33  /**
34   * {@link org.apache.myfaces.tobago.internal.taglib.component.ProgressTagDeclaration}
35   */
36  @ListenerFor(systemEventClass = PreRenderComponentEvent.class)
37  public abstract class AbstractUIProgress extends UIOutput
38      implements Visual, ComponentSystemEventListener, ClientBehaviorHolder {
39  
40    private double rangeValue;
41    private double rangeMax;
42  
43    public double getRangeValue() {
44      return rangeValue;
45    }
46  
47    public double getRangeMax() {
48      return rangeMax;
49    }
50  
51    @Override
52    public void processEvent(final ComponentSystemEvent event) throws AbortProcessingException {
53  
54      super.processEvent(event);
55  
56      if (event instanceof PreRenderComponentEvent) {
57        final Object model = getValue();
58        if (model instanceof BoundedRangeModel) {
59          final BoundedRangeModel m = (BoundedRangeModel) model;
60          rangeValue = (double) m.getValue();
61          rangeMax = (double) m.getMaximum();
62          final int min = m.getMinimum();
63          if (min != 0) {
64            rangeValue -= min;
65            rangeMax -= min;
66          }
67        } else {
68          if (model instanceof Number) {
69            rangeValue = ((Number) model).doubleValue();
70          } else if (model != null) {
71            rangeValue = Double.parseDouble("" + model);
72          }
73          if (getMax() != null) {
74            rangeMax = getMax();
75          } else {
76            rangeMax = 1.0;
77          }
78          if (rangeValue > rangeMax) {
79            rangeValue = rangeMax;
80          }
81        }
82      }
83    }
84  
85    public abstract Double getMax();
86  }