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 | */ |
---|
17 | package eu.clarin.cmdi.vlo.wicket.panels; |
---|
18 | |
---|
19 | import org.apache.wicket.Component; |
---|
20 | import org.apache.wicket.ajax.AjaxRequestTarget; |
---|
21 | import org.apache.wicket.behavior.AttributeAppender; |
---|
22 | import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxFallbackLink; |
---|
23 | import org.apache.wicket.markup.html.basic.Label; |
---|
24 | import org.apache.wicket.markup.html.link.Link; |
---|
25 | import org.apache.wicket.markup.html.panel.Panel; |
---|
26 | import org.apache.wicket.model.AbstractReadOnlyModel; |
---|
27 | import org.apache.wicket.model.IModel; |
---|
28 | import 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 | */ |
---|
37 | public 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 | } |
---|