View Javadoc

1   /*
2    * Copyright 2006-2016 The JGUIraffe Team.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.jguiraffe.gui.platform.swing.builder.components;
17  
18  import javax.swing.JSlider;
19  import javax.swing.event.ChangeEvent;
20  import javax.swing.event.ChangeListener;
21  
22  /**
23   * <p>
24   * A Swing-specific {@code ComponentHandler} implementation for slider
25   * components.
26   * </p>
27   * <p>
28   * This handler implementation supports change events. They are mapped to change
29   * events of the slider. Action events are not supported.
30   * </p>
31   *
32   * @author Oliver Heger
33   * @version $Id: SwingSliderHandler.java 205 2012-01-29 18:29:57Z oheger $
34   */
35  class SwingSliderHandler extends SwingComponentHandler<Integer> implements
36          ChangeListener
37  {
38      /**
39       * Creates a new instance of {@code SwingSliderHandler} and initializes it
40       * with the managed slider component.
41       *
42       * @param comp the slider component
43       */
44      public SwingSliderHandler(JSlider comp)
45      {
46          super(comp);
47      }
48  
49      /**
50       * Returns the managed slider component.
51       *
52       * @return the slider component
53       */
54      public final JSlider getSlider()
55      {
56          return (JSlider) getJComponent();
57      }
58  
59      /**
60       * Returns the data of this {@code ComponentHandler}. This is the current
61       * value of the managed slider.
62       *
63       * @return the data of this handler
64       */
65      public Integer getData()
66      {
67          return getSlider().getValue();
68      }
69  
70      /**
71       * Returns the data type of this handler. This handler uses the slider's
72       * current value as data. Therefore the data type is integer.
73       *
74       * @return the handler's data type
75       */
76      public Class<?> getType()
77      {
78          return Integer.class;
79      }
80  
81      /**
82       * Sets the data of this {@code ComponentHandler}. If a non-<b>null</b>
83       * value is provided, the slider's current value is set.
84       *
85       * @param data the new data of this handler
86       */
87      public void setData(Integer data)
88      {
89          if (data != null)
90          {
91              getSlider().setValue(data.intValue());
92          }
93      }
94  
95      /**
96       * Notifies this object about a change of the managed slider component. This
97       * method is part of the support for change listeners. It fires a
98       * platform-independent change event with the specified event as source.
99       *
100      * @param e the Swing-specific change event
101      */
102     public void stateChanged(ChangeEvent e)
103     {
104         if (!getSlider().getValueIsAdjusting())
105         {
106             fireChangeEvent(e);
107         }
108     }
109 
110     /**
111      * Registers a change listener at the managed slider component. This
112      * implementation registers this object as change listener at the slider.
113      * The event processing method causes a platform-independent change event to
114      * be fired.
115      */
116     @Override
117     protected void registerChangeListener()
118     {
119         getSlider().addChangeListener(this);
120     }
121 
122     /**
123      * Unregisters the change listener which was registered by
124      * {@link #registerChangeListener()}. This method is called when change
125      * events are no more of interest.
126      */
127     @Override
128     protected void unregisterChangeListener()
129     {
130         getSlider().removeChangeListener(this);
131     }
132 }