1 package fr.ifremer.tutti.ui.swing.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
38
39
40
41
42
43 public abstract class AbstractTuttiTabContainerUIHandler<M, UI extends TuttiUI<M, ?>> extends AbstractTuttiUIHandler<M, UI> implements TabContainerHandler {
44
45 DelegateTabContainerHandler delegateTabHandler;
46
47
48
49
50
51
52
53
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 }