]> git.argeo.org Git - gpl/argeo-suite.git/blob - core/org.argeo.suite.ui/src/org/argeo/suite/ui/widgets/TabbedArea.java
Support embedding videos.
[gpl/argeo-suite.git] / core / org.argeo.suite.ui / src / org / argeo / suite / ui / widgets / TabbedArea.java
1 package org.argeo.suite.ui.widgets;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.jcr.Node;
7
8 import org.argeo.cms.ui.CmsUiProvider;
9 import org.argeo.cms.ui.util.CmsUiUtils;
10 import org.argeo.cms.ui.viewers.Section;
11 import org.argeo.eclipse.ui.Selected;
12 import org.argeo.jcr.Jcr;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.custom.StackLayout;
15 import org.eclipse.swt.graphics.Image;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.layout.GridLayout;
18 import org.eclipse.swt.widgets.Button;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Label;
22 import org.eclipse.swt.widgets.ToolBar;
23 import org.eclipse.swt.widgets.ToolItem;
24
25 /** Manages {@link Section} in a tab-like structure. */
26 public class TabbedArea extends Composite {
27 private static final long serialVersionUID = 8659669229482033444L;
28
29 private Composite headers;
30 private Composite body;
31
32 private List<Section> sections = new ArrayList<>();
33
34 private Node previousNode;
35 private CmsUiProvider previousUiProvider;
36 private CmsUiProvider currentUiProvider;
37
38 private String tabStyle;
39 private String tabSelectedStyle;
40 private String bodyStyle;
41 private Image closeIcon;
42
43 private StackLayout stackLayout;
44
45 private boolean singleTab = false;
46
47 public TabbedArea(Composite parent, int style) {
48 super(parent, SWT.NONE);
49 CmsUiUtils.style(parent, bodyStyle);
50
51 setLayout(CmsUiUtils.noSpaceGridLayout());
52
53 // TODO manage tabs at bottom or sides
54 headers = new Composite(this, SWT.NONE);
55 headers.setLayoutData(CmsUiUtils.fillWidth());
56 body = new Composite(this, SWT.NONE);
57 body.setLayoutData(CmsUiUtils.fillAll());
58 // body.setLayout(new FormLayout());
59 stackLayout = new StackLayout();
60 body.setLayout(stackLayout);
61 emptyState();
62 }
63
64 protected void refreshTabHeaders() {
65 int tabCount = sections.size() > 0 ? sections.size() : 1;
66 for (Control tab : headers.getChildren())
67 tab.dispose();
68
69 headers.setLayout(CmsUiUtils.noSpaceGridLayout(new GridLayout(tabCount, true)));
70
71 if (sections.size() == 0) {
72 Composite emptyHeader = new Composite(headers, SWT.NONE);
73 emptyHeader.setLayoutData(CmsUiUtils.fillAll());
74 emptyHeader.setLayout(new GridLayout());
75 Label lbl = new Label(emptyHeader, SWT.NONE);
76 lbl.setText("");
77 lbl.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false));
78
79 }
80
81 Section currentSection = getCurrentSection();
82 for (Section section : sections) {
83 boolean selected = section == currentSection;
84 Composite sectionHeader = section.createHeader(headers);
85 CmsUiUtils.style(sectionHeader, selected ? tabSelectedStyle : tabStyle);
86 int headerColumns = singleTab ? 1 : 2;
87 sectionHeader.setLayout(new GridLayout(headerColumns, false));
88 sectionHeader.setLayout(CmsUiUtils.noSpaceGridLayout(headerColumns));
89 Button title = new Button(sectionHeader, SWT.FLAT);
90 CmsUiUtils.style(title, selected ? tabSelectedStyle : tabStyle);
91 title.setLayoutData(CmsUiUtils.fillWidth());
92 title.addSelectionListener((Selected) (e) -> showTab(tabIndex(section.getNode())));
93 Node node = section.getNode();
94 String titleStr = Jcr.getTitle(node);
95 // TODO internationalize
96 title.setText(titleStr);
97 if (!singleTab) {
98 ToolBar toolBar = new ToolBar(sectionHeader, SWT.NONE);
99 ToolItem closeItem = new ToolItem(toolBar, SWT.FLAT);
100 if (closeIcon != null)
101 closeItem.setImage(closeIcon);
102 else
103 closeItem.setText("X");
104 CmsUiUtils.style(closeItem, selected ? tabSelectedStyle : tabStyle);
105 closeItem.addSelectionListener((Selected) (e) -> closeTab(section));
106 }
107 }
108
109 }
110
111 public void view(CmsUiProvider uiProvider, Node context) {
112 if (body.isDisposed())
113 return;
114 int index = tabIndex(context);
115 if (index >= 0) {
116 showTab(index);
117 previousNode = context;
118 previousUiProvider = uiProvider;
119 return;
120 }
121 Section section = (Section) body.getChildren()[0];
122 previousNode = section.getNode();
123 if (previousNode == null) {// empty state
124 previousNode = context;
125 previousUiProvider = uiProvider;
126 } else {
127 previousUiProvider = currentUiProvider;
128 }
129 currentUiProvider = uiProvider;
130 section.setNode(context);
131 // section.setLayoutData(CmsUiUtils.coverAll());
132 build(section, uiProvider, context);
133 if (sections.size() == 0)
134 sections.add(section);
135 refreshTabHeaders();
136 index = tabIndex(context);
137 showTab(index);
138 layout(true, true);
139 }
140
141 public void open(CmsUiProvider uiProvider, Node context) {
142 if (singleTab)
143 throw new UnsupportedOperationException("Open is not supported in single tab mode.");
144
145 if (previousNode != null && Jcr.getIdentifier(previousNode).equals(Jcr.getIdentifier(context))) {
146 // does nothing
147 return;
148 }
149 if (sections.size() == 0)
150 CmsUiUtils.clear(body);
151 Section currentSection = getCurrentSection();
152 int currentIndex = sections.indexOf(currentSection);
153 Section previousSection = new Section(body, SWT.NONE, context);
154 build(previousSection, previousUiProvider, previousNode);
155 // previousSection.setLayoutData(CmsUiUtils.coverAll());
156 int index = currentIndex + 1;
157 sections.add(index, previousSection);
158 showTab(index);
159 refreshTabHeaders();
160 layout(true, true);
161 }
162
163 public void showTab(int index) {
164 Section sectionToShow = sections.get(index);
165 // sectionToShow.moveAbove(null);
166 stackLayout.topControl = sectionToShow;
167 refreshTabHeaders();
168 layout(true, true);
169 }
170
171 protected void build(Section section, CmsUiProvider uiProvider, Node context) {
172 for (Control child : section.getChildren())
173 child.dispose();
174 CmsUiUtils.style(section, bodyStyle);
175 section.setNode(context);
176 uiProvider.createUiPart(section, context);
177
178 }
179
180 private int tabIndex(Node node) {
181 for (int i = 0; i < sections.size(); i++) {
182 Section section = sections.get(i);
183 if (Jcr.getIdentifier(section.getNode()).equals(Jcr.getIdentifier(node)))
184 return i;
185 }
186 return -1;
187 }
188
189 public void closeTab(Section section) {
190 int currentIndex = sections.indexOf(section);
191 int nextIndex = currentIndex == 0 ? 0 : currentIndex - 1;
192 sections.remove(section);
193 section.dispose();
194 if (sections.size() == 0) {
195 emptyState();
196 refreshTabHeaders();
197 layout(true, true);
198 return;
199 }
200 refreshTabHeaders();
201 showTab(nextIndex);
202 }
203
204 public void closeAllTabs() {
205 for(Section section:sections) {
206 section.dispose();
207 }
208 sections.clear();
209 emptyState();
210 refreshTabHeaders();
211 layout(true, true);
212 }
213
214 protected void emptyState() {
215 new Section(body, SWT.NONE, null);
216 refreshTabHeaders();
217 }
218
219 public Composite getCurrent() {
220 return getCurrentSection();
221 }
222
223 protected Section getCurrentSection() {
224 return (Section) stackLayout.topControl;
225 }
226
227 public Node getCurrentContext() {
228 Section section = getCurrentSection();
229 if (section != null) {
230 return section.getNode();
231 } else {
232 return null;
233 }
234 }
235
236 public void setTabStyle(String tabStyle) {
237 this.tabStyle = tabStyle;
238 }
239
240 public void setTabSelectedStyle(String tabSelectedStyle) {
241 this.tabSelectedStyle = tabSelectedStyle;
242 }
243
244 public void setBodyStyle(String bodyStyle) {
245 this.bodyStyle = bodyStyle;
246 }
247
248 public void setCloseIcon(Image closeIcon) {
249 this.closeIcon = closeIcon;
250 }
251
252 public void setSingleTab(boolean singleTab) {
253 this.singleTab = singleTab;
254 }
255
256 }