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 javax.faces.event.AbortProcessingException;
23  import javax.faces.event.ComponentSystemEvent;
24  import javax.faces.event.ListenerFor;
25  import javax.faces.event.PreRenderComponentEvent;
26  import javax.swing.BoundedRangeModel;
27  
28  @ListenerFor(systemEventClass = PreRenderComponentEvent.class)
29  public abstract class AbstractUIStars extends AbstractUIInput {
30  
31    private int rangeValue;
32    private int rangeMax;
33  
34    @Override
35    public void processEvent(ComponentSystemEvent event) throws AbortProcessingException {
36  
37      super.processEvent(event);
38  
39      if (event instanceof PreRenderComponentEvent) {
40        Object model = getValue();
41        if (model instanceof BoundedRangeModel) {
42          BoundedRangeModel boundedRangeModel = (BoundedRangeModel) model;
43          rangeValue = boundedRangeModel.getValue();
44          rangeMax = boundedRangeModel.getMaximum();
45        } else {
46          if (model instanceof Number) {
47            rangeValue = ((Number) model).intValue();
48          } else if (model != null && !model.equals("")) {
49            rangeValue = Integer.valueOf("" + model);
50          } else {
51            rangeValue = 0;
52          }
53          rangeMax = getMax() != null ? getMax() : 5;
54        }
55  
56        if (rangeMax <= 0) {
57          rangeMax = 5;
58        }
59        if (rangeValue > rangeMax) {
60          rangeValue = rangeMax;
61        }
62      }
63    }
64  
65    public abstract Integer getMax();
66  
67    public abstract Double getPlaceholder();
68  
69    public int getRangeValue() {
70      return rangeValue;
71    }
72  
73    public int getRangeMax() {
74      return rangeMax;
75    }
76  }