Make ListViewTable and RelatedListTable a consistent height.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / ui / cell / BooleanCell.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.cell;
21
22 import org.glom.web.shared.layout.LayoutItemField.GlomFieldType;
23
24 import com.google.gwt.cell.client.CheckboxCell;
25 import com.google.gwt.cell.client.ValueUpdater;
26 import com.google.gwt.dom.client.Element;
27 import com.google.gwt.dom.client.InputElement;
28 import com.google.gwt.dom.client.NativeEvent;
29 import com.google.gwt.event.dom.client.KeyCodes;
30 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
31
32 /**
33  * Cell renderer for {@link GlomFieldType} TYPE_BOOLEAN.
34  * 
35  * @author Ben Konrath <ben@bagu.org>
36  * 
37  */
38 public class BooleanCell extends CheckboxCell {
39
40         public BooleanCell() {
41                 super(false, false);
42         }
43
44         /*
45          * This method is overridden to ensure the user can't toggle the checkbox. This method can be removed when support
46          * for editing is added.
47          * 
48          * @see com.google.gwt.cell.client.CheckboxCell#onBrowserEvent(com.google.gwt.cell.client.Cell.Context,
49          * com.google.gwt.dom.client.Element, java.lang.Boolean, com.google.gwt.dom.client.NativeEvent,
50          * com.google.gwt.cell.client.ValueUpdater)
51          */
52         @Override
53         public void onBrowserEvent(com.google.gwt.cell.client.Cell.Context context, Element parent, Boolean value,
54                         NativeEvent event, ValueUpdater<Boolean> valueUpdater) {
55                 String type = event.getType();
56                 boolean enterPressed = "keydown".equals(type) && event.getKeyCode() == KeyCodes.KEY_ENTER;
57                 if ("change".equals(type) || enterPressed) {
58                         InputElement input = parent.getFirstChild().cast();
59                         input.setChecked(!input.isChecked());
60                 }
61         }
62
63         /*
64          * This method is overridden to handle rendering empty rows.
65          * 
66          * @see com.google.gwt.cell.client.CheckboxCell#render(com.google.gwt.cell.client.Cell.Context, java.lang.Boolean,
67          * com.google.gwt.safehtml.shared.SafeHtmlBuilder)
68          */
69         @Override
70         public void render(com.google.gwt.cell.client.Cell.Context context, Boolean value, SafeHtmlBuilder sb) {
71                 if (value == null)
72                         // The value is from an empty row.
73                         sb.appendHtmlConstant("&nbsp;");
74                 else
75                         super.render(context, value, sb);
76         }
77
78 }