View Javadoc
1   package fr.ifremer.tutti.ui.swing.util;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import org.nuiton.jaxx.application.swing.AbstractApplicationUIHandler;
26  import org.nuiton.jaxx.application.swing.ApplicationUI;
27  import org.nuiton.jaxx.application.swing.tab.CustomTab;
28  import org.nuiton.jaxx.application.swing.tab.DelegateTabContainerHandler;
29  import org.nuiton.jaxx.application.swing.tab.TabContainerHandler;
30  import org.nuiton.jaxx.application.swing.tab.TabContentModel;
31  import org.nuiton.jaxx.application.swing.tab.TabHandler;
32  
33  import javax.swing.*;
34  import java.awt.*;
35  
36  /**
37   * UI containing a tab panel.
38   *
39   * @param <M> type of the ui model
40   * @author Kevin Morin - kmorin@codelutin.com
41   * @since 0.3
42   */
43  public abstract class AbstractTuttiTabContainerUIHandler<M, UI extends TuttiUI<M, ?>> extends AbstractTuttiUIHandler<M, UI> implements TabContainerHandler {
44  
45      DelegateTabContainerHandler delegateTabHandler;
46  
47      /**
48       * Returns the tab handler of the tab i.
49       *
50       * @param index the index of the tab
51       * @return the tab handler of the index i if the handler implements
52       * the {@link TabHandler} interface,
53       * <code>null</code> otherwise
54       */
55      @Override
56      public TabHandler getTabHandler(int index) {
57          TabHandler tabHandler = null;
58          JTabbedPane tabPanel = getTabPanel();
59          if (index >= 0 && index < tabPanel.getTabCount()) {
60              Component tab = tabPanel.getComponentAt(index);
61              if (ApplicationUI.class.isInstance(tab)) {
62                  ApplicationUI tuttiTab = (ApplicationUI) tabPanel.getComponentAt(index);
63                  AbstractApplicationUIHandler handler = tuttiTab.getHandler();
64                  if (TabHandler.class.isInstance(handler)) {
65                      tabHandler = (TabHandler) handler;
66                  }
67              }
68          }
69          return tabHandler;
70      }
71  
72      @Override
73      public void setCustomTab(int index, TabContentModel model) {
74          JTabbedPane tabPanel = getTabPanel();
75          tabPanel.setTabComponentAt(index, new CustomTab(model, this));
76      }
77  
78      @Override
79      protected void initUI(UI ui) {
80          super.initUI(ui);
81          delegateTabHandler = new DelegateTabContainerHandler(getTabPanel());
82          init();
83      }
84  
85      @Override
86      public void init() {
87  
88          getTabPanel().setModel(new DefaultSingleSelectionModel() {
89  
90              private static final long serialVersionUID = 1L;
91  
92              @Override
93              public void setSelectedIndex(int index) {
94                  int currentIndex = getTabPanel().getSelectedIndex();
95                  boolean mustChangeTab = onTabChanged(currentIndex, index);
96  
97                  if (mustChangeTab) {
98                      super.setSelectedIndex(index);
99                  }
100             }
101 
102         });
103     }
104 
105     @Override
106     public boolean onTabChanged(int currentIndex, int newIndex) {
107         boolean result = true;
108         if (currentIndex != newIndex) {
109             TabHandler handler = getTabHandler(currentIndex);
110             if (handler != null) {
111                 result = handler.onHideTab(currentIndex, newIndex);
112             }
113 
114             handler = getTabHandler(newIndex);
115             if (handler != null) {
116                 handler.onShowTab(currentIndex, newIndex);
117             }
118         }
119         return result;
120     }
121 }