001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.extensions.markup.html.repeater.data.table;
018
019import org.apache.wicket.behavior.Behavior;
020import org.apache.wicket.markup.html.WebMarkupContainer;
021import org.apache.wicket.markup.html.panel.Panel;
022import org.apache.wicket.markup.repeater.RepeatingView;
023
024/**
025 * A panel that renders <colgroup> with <col> elements
026 * inside it.
027 *
028 * The columns can be used to style the whole table column.
029 *
030 * <p>
031 * <strong>Important</strong>: this component requires
032 * {@link org.apache.wicket.settings.MarkupSettings#getStripWicketTags()}
033 * to return {@code true}, otherwise the browsers break the rendering of
034 * the HTML elements. For example Google Chrome renders two &lt;colgroup&gt;
035 * elements.
036 * </p>
037 *
038 * @see DataTable#getColGroup()
039 */
040public class ColGroup extends Panel
041{
042        private static final long serialVersionUID = 1L;
043
044        private final RepeatingView colgroupCols;
045
046        public ColGroup(String id)
047        {
048                super(id);
049
050                this.colgroupCols = new RepeatingView("col");
051                add(colgroupCols);
052        }
053
054        /**
055         * Adds a column to the group.
056         *
057         * <p>Usage:
058         * <pre>
059         *     <code>
060         *         colgroup.addCol(colgroup.new Col(AttributeModifier.append("span", "2"),
061         *             AttributeModifier.append("style", "background-color: #CC6633")))
062         *     </code>
063         * </pre>
064         *
065         * </p>
066         *
067         * @param column
068         *          The column with the styling behaviors
069         * @return {@code this}, for method chaining
070         */
071        public ColGroup addCol(Col column)
072        {
073                colgroupCols.add(column);
074                return this;
075        }
076
077        /**
078         * Hides the ColGroup if there are no &lt;col&gt;s to render
079         */
080        @Override
081        protected void onConfigure()
082        {
083                super.onConfigure();
084
085                setVisible(colgroupCols.size() > 0);
086        }
087
088        public class Col extends WebMarkupContainer
089        {
090                private static final long serialVersionUID = 1L;
091
092                public Col(Behavior... behaviors)
093                {
094                        super(colgroupCols.newChildId());
095
096                        if (behaviors != null)
097                        {
098                                add(behaviors);
099                        }
100                }
101        }
102}