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.JTabbedPane;
19  import javax.swing.event.ChangeEvent;
20  import javax.swing.event.ChangeListener;
21  
22  /**
23   * <p>
24   * A specific component handler implementation that deals with a tabbed pane
25   * object.
26   * </p>
27   * <p>
28   * The data of a tabbed pane is defined as its selected index. This is an
29   * integer value. Clients can register itself as change listeners; they are then
30   * notified whenever the select index of the tabbed pane changes. Focus
31   * listeners are also supported.
32   * </p>
33   *
34   * @author Oliver Heger
35   * @version $Id: SwingTabbedPaneHandler.java 205 2012-01-29 18:29:57Z oheger $
36   */
37  class SwingTabbedPaneHandler extends SwingComponentHandler<Integer> implements
38          ChangeListener
39  {
40      /**
41       * Creates a new instance of <code>SwingTabbedPaneHandler</code> and
42       * initializes it with the component to manage.
43       *
44       * @param pane the managed tab pane
45       */
46      public SwingTabbedPaneHandler(JTabbedPane pane)
47      {
48          super(pane);
49      }
50  
51      /**
52       * Returns the tabbed pane component that is wrapped by this handler.
53       *
54       * @return the internal tabbed pane component
55       */
56      public JTabbedPane getTabbedPane()
57      {
58          return (JTabbedPane) getJComponent();
59      }
60  
61      /**
62       * This method gets called when the index of the selected tab changes. This
63       * event is sent to all registered change listeners.
64       *
65       * @param event the source event
66       */
67      public void stateChanged(ChangeEvent event)
68      {
69          fireChangeEvent(event);
70      }
71  
72      /**
73       * Returns the data of the managed component. This is an integer
74       * representing the index of the selected tab.
75       *
76       * @return the data of the managed component
77       */
78      public Integer getData()
79      {
80          return Integer.valueOf(getTabbedPane().getSelectedIndex());
81      }
82  
83      /**
84       * Returns the data type of this handler. In this case this is a single
85       * <code>Integer</code> object representing the selected index of the tab.
86       *
87       * @return the data type of this handler
88       */
89      public Class<?> getType()
90      {
91          return Integer.class;
92      }
93  
94      /**
95       * Sets the data of the managed component. This is an integer representing
96       * the index of the selected tab. So this method can be used to switch to a
97       * certain tab.
98       *
99       * @param data the data of the managed component
100      */
101     public void setData(Integer data)
102     {
103         if (data != null)
104         {
105             getTabbedPane().setSelectedIndex(data.intValue());
106         }
107     }
108 
109     /**
110      * Registers this handler as change listener at the managed component.
111      */
112     @Override
113     protected void registerChangeListener()
114     {
115         getTabbedPane().addChangeListener(this);
116     }
117 
118     /**
119      * Unregisteres this handler as change listener from the managed component.
120      */
121     @Override
122     protected void unregisterChangeListener()
123     {
124         getTabbedPane().removeChangeListener(this);
125     }
126 }