View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.tab;
2   
3   /*
4    * #%L
5    * Reef DB :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import fr.ifremer.quadrige3.ui.swing.AbstractUIHandler;
27  import fr.ifremer.quadrige3.ui.swing.ApplicationUI;
28  import org.jdesktop.jxlayer.JXLayer;
29  import org.nuiton.jaxx.application.swing.AbstractApplicationUIHandler;
30  import org.nuiton.jaxx.application.swing.tab.CustomTab;
31  import org.nuiton.jaxx.application.swing.tab.TabContainerHandler;
32  import org.nuiton.jaxx.application.swing.tab.TabContentModel;
33  import org.nuiton.jaxx.application.swing.tab.TabHandler;
34  
35  import javax.swing.DefaultSingleSelectionModel;
36  import javax.swing.JTabbedPane;
37  import java.awt.Component;
38  
39  /**
40   * UI containing a tab panel.
41   * Use DaliTab as tab component
42   *
43   * @param <M>  type of the ui model
44   * @param <UI> type of the screen ui
45   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
46   */
47  public abstract class AbstractTabContainerUIHandler<M, UI extends ApplicationUI<M, ?>> extends AbstractUIHandler<M, UI>
48          implements TabContainerHandler {
49  
50      // the tab layer UI
51      private CustomTabLayerUI tabLayerUI;
52  
53      /** {@inheritDoc} */
54      @Override
55      protected void initUI(UI ui) {
56          super.initUI(ui);
57          tabLayerUI = new CustomTabLayerUI();
58          init();
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public void init() {
64  
65          getTabPanel().setModel(new DefaultSingleSelectionModel() {
66  
67              private static final long serialVersionUID = 1L;
68  
69              @Override
70              public void setSelectedIndex(int index) {
71                  int currentIndex = getTabPanel().getSelectedIndex();
72                  boolean mustChangeTab = onTabChanged(currentIndex, index);
73  
74                  if (mustChangeTab) {
75                      super.setSelectedIndex(index);
76                  }
77              }
78  
79          });
80  
81          // prevent session from saving tab position
82          getContext().getSwingSession().addUnsavedComponent(getTabPanel());
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public boolean onTabChanged(int currentIndex, int newIndex) {
88          boolean result = true;
89          if (currentIndex != newIndex) {
90              TabHandler handler = getTabHandler(currentIndex);
91              if (handler != null) {
92                  result = handler.onHideTab(currentIndex, newIndex);
93              }
94  
95              handler = getTabHandler(newIndex);
96              if (handler != null) {
97                  handler.onShowTab(currentIndex, newIndex);
98              }
99          }
100         return result;
101     }
102 
103     /**
104      * {@inheritDoc}
105      *
106      * Returns the tab handler of the tab i.
107      */
108     @Override
109     public TabHandler getTabHandler(int index) {
110         TabHandler tabHandler = null;
111         JTabbedPane tabPanel = getTabPanel();
112         if (index >= 0 && index < tabPanel.getTabCount()) {
113             Component tab = tabPanel.getComponentAt(index);
114             if (ApplicationUI.class.isInstance(tab)) {
115                 ApplicationUI tabUI = (ApplicationUI) tabPanel.getComponentAt(index);
116                 AbstractApplicationUIHandler handler = tabUI.getHandler();
117                 if (TabHandler.class.isInstance(handler)) {
118                     tabHandler = (TabHandler) handler;
119                 }
120             }
121         }
122         return tabHandler;
123     }
124 
125     /** {@inheritDoc} */
126     @Override
127     public void setCustomTab(int index, TabContentModel model) {
128         // create a CustomTab inside a JXLayer
129         JXLayer<CustomTab> tabLayer = new JXLayer<>(new CustomTab(model, this), tabLayerUI);
130         getTabPanel().setTabComponentAt(index, tabLayer);
131     }
132 
133     /** {@inheritDoc} */
134     @Override
135     public boolean removeTab(int i) {
136         TabHandler tabHandler = getTabHandler(i);
137         boolean remove = tabHandler.onRemoveTab();
138         if (remove) {
139             getTabPanel().removeTabAt(i);
140         }
141         return remove;
142     }
143 }