1 package fr.ifremer.quadrige3.ui.swing.tab;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
41
42
43
44
45
46
47 public abstract class AbstractTabContainerUIHandler<M, UI extends ApplicationUI<M, ?>> extends AbstractUIHandler<M, UI>
48 implements TabContainerHandler {
49
50
51 private CustomTabLayerUI tabLayerUI;
52
53
54 @Override
55 protected void initUI(UI ui) {
56 super.initUI(ui);
57 tabLayerUI = new CustomTabLayerUI();
58 init();
59 }
60
61
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
82 getContext().getSwingSession().addUnsavedComponent(getTabPanel());
83 }
84
85
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
105
106
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
126 @Override
127 public void setCustomTab(int index, TabContentModel model) {
128
129 JXLayer<CustomTab> tabLayer = new JXLayer<>(new CustomTab(model, this), tabLayerUI);
130 getTabPanel().setTabComponentAt(index, tabLayer);
131 }
132
133
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 }