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.model; |
---|
18 | |
---|
19 | import java.io.Serializable; |
---|
20 | import org.apache.wicket.model.IModel; |
---|
21 | |
---|
22 | /** |
---|
23 | * Wraps a model and allows for toggling between two values as the model object |
---|
24 | * of the wrapped model by calling ({@link #setObject(java.lang.Boolean) |
---|
25 | * }) on <em>this</em>, which will result in a setting of either the 'false |
---|
26 | * value' object or the 'true value'. |
---|
27 | * |
---|
28 | * @author twagoo |
---|
29 | * @param <T> type for values and wrapped model |
---|
30 | */ |
---|
31 | public class ToggleModel<T extends Serializable> implements IModel<Boolean> { |
---|
32 | |
---|
33 | private final IModel<T> wrappedModel; |
---|
34 | private final T falseValue; |
---|
35 | private final T trueValue; |
---|
36 | |
---|
37 | /** |
---|
38 | * |
---|
39 | * @param wrappedModel model to wrap |
---|
40 | * @param falseValue value to set on the wrapped model if <em>this</em> is |
---|
41 | * set to false (can be null) |
---|
42 | * @param trueValue value to set on the wrapped model if <em>this</em> is |
---|
43 | * set to true (should not be null) |
---|
44 | */ |
---|
45 | public ToggleModel(IModel<T> wrappedModel, T falseValue, T trueValue) { |
---|
46 | this.wrappedModel = wrappedModel; |
---|
47 | this.falseValue = falseValue; |
---|
48 | this.trueValue = trueValue; |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * |
---|
53 | * @return whether {@link IModel#getObject() } of the wrapped model is equal |
---|
54 | * to the 'true value' object |
---|
55 | */ |
---|
56 | @Override |
---|
57 | public Boolean getObject() { |
---|
58 | return trueValue.equals(wrappedModel.getObject()); |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * |
---|
63 | * @param object sets the value of the wrapped model to the 'true value' |
---|
64 | * object if object equals {@link Boolean#TRUE}, otherwise to the 'false |
---|
65 | * value' object |
---|
66 | */ |
---|
67 | @Override |
---|
68 | public void setObject(Boolean object) { |
---|
69 | if (object) { |
---|
70 | wrappedModel.setObject(trueValue); |
---|
71 | } else { |
---|
72 | wrappedModel.setObject(falseValue); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * Detaches the wrapped model |
---|
78 | */ |
---|
79 | @Override |
---|
80 | public void detach() { |
---|
81 | wrappedModel.detach(); |
---|
82 | } |
---|
83 | |
---|
84 | } |
---|