Add better support for subgroups in the 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.LayoutGroup;
25 import org.glom.web.shared.layout.LayoutItem;
26 import org.glom.web.shared.layout.LayoutItemField;
27 import org.glom.web.shared.layout.LayoutItemPortal;
28
29 import com.google.gwt.user.client.ui.FlowPanel;
30 import com.google.gwt.user.client.ui.InlineLabel;
31 import com.google.gwt.user.client.ui.Label;
32
33 /**
34  * @author Ben Konrath <ben@bagu.org>
35  * 
36  */
37 class FlowTable extends FlowPanel {
38         FlowPanel groupContents;
39         private final ArrayList<InlineLabel> dataLabels = new ArrayList<InlineLabel>();
40
41         @SuppressWarnings("unused")
42         private FlowTable() {
43         }
44
45         public FlowTable(LayoutGroup layoutGroup, boolean subGroup, boolean mainTitleSet) {
46                 super();
47                 this.setStyleName(subGroup ? "subgroup" : "group");
48
49                 String groupTitle = layoutGroup.getTitle();
50                 if (!groupTitle.isEmpty()) {
51                         Label label = new Label(groupTitle);
52                         this.add(label);
53
54                         // the "group-title" class could could be used for the subgroup title if the group title is empty
55                         label.setStyleName(mainTitleSet ? "subgroup-title" : "group-title");
56                         mainTitleSet = true;
57
58                         groupContents = new FlowPanel();
59                         groupContents.setStyleName("group-contents");
60                         this.add(groupContents);
61                 } else {
62                         // don't make a separate contents panel when the group title has not been set
63                         groupContents = this;
64                 }
65
66                 // create the appropriate UI element for each child item
67                 for (LayoutItem layoutItem : layoutGroup.getItems()) {
68
69                         if (layoutItem == null)
70                                 continue;
71
72                         String title = layoutItem.getTitle();
73                         if (layoutItem instanceof LayoutItemField) {
74                                 addDetailsCell(title);
75                         } else if (layoutItem instanceof LayoutItemPortal) {
76                                 // TODO implement support for portals
77                                 continue;
78                         } else if (layoutItem instanceof LayoutGroup) {
79                                 // create a FlowTable for the child group
80                                 FlowTable flowTable = new FlowTable((LayoutGroup) layoutItem, true, mainTitleSet);
81                                 groupContents.add(flowTable);
82                                 dataLabels.addAll(flowTable.getDataLabels());
83                         }
84                 }
85
86         }
87
88         private void addDetailsCell(String title) {
89
90                 InlineLabel detailsLabel = new InlineLabel(title + ":");
91                 detailsLabel.setStyleName("details-label");
92
93                 InlineLabel detailsData = new InlineLabel();
94                 detailsData.setStyleName("details-data");
95
96                 FlowPanel fieldPanel = new FlowPanel();
97                 fieldPanel.setStyleName("details-cell");
98
99                 fieldPanel.add(detailsLabel);
100                 fieldPanel.add(detailsData);
101
102                 groupContents.add(fieldPanel);
103                 dataLabels.add(detailsData);
104         }
105
106         public ArrayList<InlineLabel> getDataLabels() {
107                 return dataLabels;
108         }
109 }