Add style to Notebook that matches current theme.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / ui / details / Notebook.java
1 /*
2  * Copyright (C) 2011 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
6  * GWT-Glom is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with GWT-Glom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package org.glom.web.client.ui.details;
21
22 import org.glom.web.client.Utils;
23 import org.glom.web.shared.layout.LayoutGroup;
24 import org.glom.web.shared.layout.LayoutItem;
25 import org.glom.web.shared.layout.LayoutItemNotebook;
26
27 import com.google.gwt.dom.client.Style.Unit;
28 import com.google.gwt.user.client.ui.FlowPanel;
29 import com.google.gwt.user.client.ui.TabLayoutPanel;
30 import com.google.gwt.user.client.ui.Widget;
31
32 /**
33  * @author Ben Konrath <ben@bagu.org>
34  * 
35  */
36 public class Notebook extends Group {
37
38         @SuppressWarnings("unused")
39         private Notebook() {
40                 // disable default constructor
41         }
42
43         /**
44          * Create a new Notebook widget based on the specified LayoutItemNotebook DTO.
45          * 
46          * @param layoutItemNotebook
47          */
48         public Notebook(LayoutItemNotebook layoutItemNotebook) {
49                 TabLayoutPanel tabPanel = new TabLayoutPanel(1.6, Unit.EM);
50
51                 int maxChildHeight = 0;
52                 for (LayoutItem layoutItem : layoutItemNotebook.getItems()) {
53                         if (!(layoutItem instanceof LayoutGroup))
54                                 // Ignore non-LayoutGroup items. This is what Glom 1.18 does.
55                                 continue;
56
57                         Widget child = createChildWidget(layoutItem, false, false);
58
59                         // update the maximum value of the child height if required
60                         int childHeight = Utils.getWidgetHeight(child);
61                         if (childHeight > maxChildHeight)
62                                 maxChildHeight = childHeight;
63
64                         tabPanel.add(child, layoutItem.getTitle());
65                 }
66
67                 // Set the first tab as the default tab.
68                 tabPanel.selectTab(0);
69
70                 // The height needs to be set of the TabLayoutPanel to work.
71                 // Use the max child height plus a few extra pixels for padding.
72                 tabPanel.setHeight((maxChildHeight + 6) + "px");
73
74                 FlowPanel mainPanel = getMainPanel();
75                 mainPanel.add(tabPanel);
76                 initWidget(mainPanel);
77         }
78 }