source: vlo/trunk/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/panels/TogglePanel.java @ 5136

Last change on this file since 5136 was 5136, checked in by Twan Goosen, 10 years ago

added busy indicators to the toggle link of the toggle panel and the all facet values options form

File size: 4.2 KB
Line 
1/*
2 * Copyright (C) 2014 CLARIN
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17package eu.clarin.cmdi.vlo.wicket.panels;
18
19import org.apache.wicket.Component;
20import org.apache.wicket.ajax.AjaxRequestTarget;
21import org.apache.wicket.behavior.AttributeAppender;
22import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxFallbackLink;
23import org.apache.wicket.markup.html.basic.Label;
24import org.apache.wicket.markup.html.link.Link;
25import org.apache.wicket.markup.html.panel.Panel;
26import org.apache.wicket.model.AbstractReadOnlyModel;
27import org.apache.wicket.model.IModel;
28import org.apache.wicket.model.Model;
29
30/**
31 * A panel that wraps a content component and adds a link to toggle its
32 * visibility. The toggle link is an Ajax fallback link with a dynamic label,
33 * showing a custom text depending on the toggle state.
34 *
35 * @author twagoo
36 */
37public abstract class TogglePanel extends Panel {
38
39    private final IModel<Boolean> visibilityModel = Model.of(false);
40    private final Component content;
41
42    /**
43     *
44     * @param id component id
45     * @param showTextModel model for text to show when collapsed ('show
46     * content')
47     * @param hideTextModel model for text to show when expanded ('hide
48     * content')
49     */
50    public TogglePanel(String id, final IModel<String> showTextModel, final IModel<String> hideTextModel) {
51        super(id);
52
53        // add the actual toggle link
54        final Link toggler = new IndicatingAjaxFallbackLink("toggler") {
55
56            @Override
57            public void onClick(AjaxRequestTarget target) {
58                visibilityModel.setObject(!visibilityModel.getObject());
59                if (target != null) {
60                    target.add(TogglePanel.this);
61                }
62            }
63        };
64        add(toggler);
65
66        // add the text, with a dynamic model that gets the value depending on the toggle state
67        toggler.add(new Label("toggleText", new AbstractReadOnlyModel<String>() {
68
69            @Override
70            public String getObject() {
71                if (visibilityModel.getObject()) {
72                    return hideTextModel.getObject();
73                } else {
74                    return showTextModel.getObject();
75                }
76            }
77        }));
78
79        // make 'class' attribute depend on toggle state
80        add(new AttributeAppender("class", new AbstractReadOnlyModel<String>() {
81
82            @Override
83            public String getObject() {
84                if (visibilityModel.getObject()) {
85                    return getExpandedClass();
86                } else {
87                    return getCollapsedClass();
88                }
89            }
90        }));
91
92        // add the toggled content
93        content = createContent("toggleContent");
94        add(content);
95
96        setOutputMarkupId(true);
97    }
98
99    @Override
100    protected void onConfigure() {
101        super.onConfigure();
102        content.setVisible(visibilityModel.getObject());
103    }
104
105    /**
106     *
107     * @return class set on the panel markup element when toggle state is
108     * collapsed (this implementation returns "collapsed")
109     */
110    protected static String getCollapsedClass() {
111        return "collapsed";
112    }
113
114    /**
115     *
116     * @return class set on the panel markup element when toggle state is
117     * expanded (this implementation returns "expanded")
118     */
119    protected static String getExpandedClass() {
120        return "expanded";
121    }
122
123    /**
124     * Implementer should return content component in this method!
125     *
126     * @param id component id to use for content
127     * @return component that will be rendered as toggled content
128     */
129    protected abstract Component createContent(String id);
130
131}
Note: See TracBrowser for help on using the repository browser.