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.JProgressBar;
19  import javax.swing.event.ChangeEvent;
20  import javax.swing.event.ChangeListener;
21  
22  import net.sf.jguiraffe.gui.builder.components.model.ProgressBarHandler;
23  
24  import org.apache.commons.lang.StringUtils;
25  
26  /**
27   * <p>
28   * An internally used handler class that deals with components of type
29   * <code>JProgressBar</code>.
30   * </p>
31   * <p>
32   * The data type of this component handler is an integer representing the
33   * current value of the progress bar. This class also implements the
34   * <code>ProgressBarHandler</code> interface, which allows direct access to
35   * important properties.
36   * </p>
37   *
38   * @author Oliver Heger
39   * @version $Id: SwingProgressBarHandler.java 205 2012-01-29 18:29:57Z oheger $
40   */
41  class SwingProgressBarHandler extends SwingComponentHandler<Integer> implements
42          ProgressBarHandler, ChangeListener
43  {
44      /**
45       * Creates a new instance of <code>SwingProgressBarHandler</code> and
46       * initializes it with the managed component.
47       *
48       * @param bar the progress bar to be wrapped
49       */
50      public SwingProgressBarHandler(JProgressBar bar)
51      {
52          super(bar);
53      }
54  
55      /**
56       * Returns the managed <code>JProgressBar</code> component.
57       *
58       * @return the managed progress bar
59       */
60      public JProgressBar getProgressBar()
61      {
62          return (JProgressBar) getJComponent();
63      }
64  
65      /**
66       * Returns the text to be displayed on the progress bar.
67       *
68       * @return the progress text
69       */
70      public String getProgressText()
71      {
72          return getProgressBar().getString();
73      }
74  
75      /**
76       * Returns the current value of the progress bar.
77       *
78       * @return the current value
79       */
80      public int getValue()
81      {
82          return getProgressBar().getValue();
83      }
84  
85      /**
86       * Sets the text to be displayed on the progress bar. The text will only be
87       * displayed if the <code>allowText</code> property was set when the
88       * progress bar was created.
89       *
90       * @param s the progress text
91       */
92      public void setProgressText(String s)
93      {
94          getProgressBar().setString((s != null) ? s : StringUtils.EMPTY);
95      }
96  
97      /**
98       * Sets the current value of the progress bar.
99       *
100      * @param v the new value
101      */
102     public void setValue(int v)
103     {
104         getProgressBar().setValue(v);
105     }
106 
107     /**
108      * Returns the data of the managed component.
109      *
110      * @return the component's data
111      */
112     public Integer getData()
113     {
114         return Integer.valueOf(getValue());
115     }
116 
117     /**
118      * Returns the data type of the managed component. In this case this is an
119      * integer.
120      *
121      * @return the handler's data type
122      */
123     public Class<?> getType()
124     {
125         return Integer.class;
126     }
127 
128     /**
129      * Sets the data of the managed component. The passed in value must be an
130      * integer. It is interpreted as the new value of the progress bar.
131      *
132      * @param data the new data
133      */
134     public void setData(Integer data)
135     {
136         if (data != null)
137         {
138             setValue(data.intValue());
139         }
140     }
141 
142     /**
143      * Notification method for change events. Notifies the registered change
144      * listeners.
145      *
146      * @param e the event
147      */
148     public void stateChanged(ChangeEvent e)
149     {
150         fireChangeEvent(e);
151     }
152 
153     /**
154      * Registers this object as change listener at the managed component.
155      */
156     @Override
157     protected void registerChangeListener()
158     {
159         getProgressBar().addChangeListener(this);
160     }
161 
162     /**
163      * Unregisters this handler as change listener from the managed component.
164      */
165     @Override
166     protected void unregisterChangeListener()
167     {
168         getProgressBar().removeChangeListener(this);
169     }
170 }