Create container element for LayoutItemPortal in Details View.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / ui / FlowTable.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;
21
22 import java.util.ArrayList;
23
24 import org.glom.web.shared.layout.Formatting;
25 import org.glom.web.shared.layout.LayoutGroup;
26 import org.glom.web.shared.layout.LayoutItem;
27 import org.glom.web.shared.layout.LayoutItemField;
28 import org.glom.web.shared.layout.LayoutItemPortal;
29
30 import com.google.gwt.dom.client.Style.Unit;
31 import com.google.gwt.user.client.ui.FlowPanel;
32 import com.google.gwt.user.client.ui.Label;
33
34 /**
35  * @author Ben Konrath <ben@bagu.org>
36  * 
37  */
38 class FlowTable extends FlowPanel {
39         FlowPanel groupContents;
40         private final ArrayList<Label> dataLabels = new ArrayList<Label>();
41
42         @SuppressWarnings("unused")
43         private FlowTable() {
44         }
45
46         public FlowTable(LayoutGroup layoutGroup, boolean subGroup, boolean mainTitleSet) {
47                 super();
48                 this.setStyleName(subGroup ? "subgroup" : "group");
49
50                 String groupTitle = layoutGroup.getTitle();
51                 if (!groupTitle.isEmpty()) {
52                         Label label = new Label(groupTitle);
53                         this.add(label);
54
55                         // the "group-title" class could could be used for the subgroup title if the group title is empty
56                         label.setStyleName(mainTitleSet ? "subgroup-title" : "group-title");
57                         mainTitleSet = true;
58
59                         groupContents = new FlowPanel();
60                         groupContents.setStyleName("group-contents");
61                         this.add(groupContents);
62                 } else {
63                         // don't make a separate contents panel when the group title has not been set
64                         groupContents = this;
65                 }
66
67                 // create the appropriate UI element for each child item
68                 for (LayoutItem layoutItem : layoutGroup.getItems()) {
69
70                         if (layoutItem == null)
71                                 continue;
72
73                         if (layoutItem instanceof LayoutItemField) {
74                                 addDetailsCell((LayoutItemField) layoutItem);
75                         } else if (layoutItem instanceof LayoutItemPortal) {
76                                 FlowPanel portalCell = new FlowPanel();
77                                 portalCell.setStyleName("subgroup");
78                                 portalCell.setHeight("8em");
79
80                                 Label portalTitle = new Label("Portal Title"); // TODO: replace temporary title
81                                 portalTitle.setStyleName(mainTitleSet ? "subgroup-title" : "group-title");
82                                 portalCell.add(portalTitle);
83
84                                 FlowPanel portalContents = new FlowPanel();
85                                 portalContents.setStyleName("group-contents"); // using the same style as the group-contents element
86
87                                 // TODO add proper CellTable
88                                 Label tempPortalData = new Label("Portal Data");
89                                 tempPortalData.getElement().getStyle().setFontSize(2, Unit.EM);
90                                 portalContents.add(tempPortalData);
91
92                                 portalCell.add(portalContents);
93                                 groupContents.add(portalCell);
94                         } else if (layoutItem instanceof LayoutGroup) {
95                                 // create a FlowTable for the child group
96                                 FlowTable flowTable = new FlowTable((LayoutGroup) layoutItem, true, mainTitleSet);
97                                 groupContents.add(flowTable);
98                                 dataLabels.addAll(flowTable.getDataLabels());
99                         }
100                 }
101
102         }
103
104         private void addDetailsCell(LayoutItemField layoutItemField) {
105
106                 // Labels (text in div a element) are being used so that the height of the details-data element can be set for
107                 // the multiline height of LayoutItemFeilds. This allows the the data element to display the correct height
108                 // if style is applied that shows the height. This has the added benefit of allowing the order of the label and
109                 // data elements to be changed for right-to-left languages.
110
111                 Label detailsLabel = new Label(layoutItemField.getTitle() + ":");
112                 detailsLabel.setStyleName("details-label");
113
114                 Label detailsData = new Label();
115                 detailsData.setStyleName("details-data");
116                 Formatting formatting = layoutItemField.getFormatting();
117                 detailsData.setHeight(formatting.getTextFormatMultilineHeightLines() + "em");
118
119                 FlowPanel detailsCell = new FlowPanel();
120                 detailsCell.setStyleName("details-cell");
121
122                 detailsCell.add(detailsLabel);
123                 detailsCell.add(detailsData);
124
125                 groupContents.add(detailsCell);
126                 dataLabels.add(detailsData);
127         }
128
129         public ArrayList<Label> getDataLabels() {
130                 return dataLabels;
131         }
132 }