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