1 /*
2  * Licensed under the Apache License, Version 2.0 (the "License");
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14package org.tp23.antinstaller.renderer.swing;
15
16import java.util.ArrayList;
17
18import javax.swing.JPanel;
19
20import org.tp23.antinstaller.ResourceBundleHelper;
21import org.tp23.antinstaller.input.ConditionalField;
22import org.tp23.antinstaller.input.InputField;
23import org.tp23.antinstaller.renderer.RendererFactory;
24import org.tp23.antinstaller.runtime.ConfigurationException;
25import org.tp23.antinstaller.runtime.logic.Expression;
26import org.tp23.gui.GBCF;
27
28/**
29 * Conditionally render a collection of fields.
30 * For now, will ONLY handle <code>&lt;hiddeb&gt;</code> fields
31 *
32 * @author mwilson
33 * @version $Id
34 * @since 0.7.4 patch 7
35 */
36public class ConditionalFieldRenderer extends SwingOutputFieldRenderer {
37    
38    private ResourceBundleHelper resHelper = new ResourceBundleHelper( "org.tp23.antinstaller.renderer.Res" );
39    private ConditionalField condField;
40    private ArrayList renderers = new ArrayList( );
41
42    public ConditionalFieldRenderer() {
43    }
44
45    public void initComponent( JPanel parent ) {
46
47        condField = (ConditionalField) outputField;
48        InputField[] fields = condField.getFields();
49
50        for (int i = 0; i < fields.length; i++) {
51            try {
52                SwingOutputFieldRenderer renderer = RendererFactory.getSwingRenderer(fields[i]);
53                renderer.setOutputField(fields[i]);
54                renderer.setInstallerContext(ctx);
55                renderers.add( renderer );
56            }
57            catch( ClassNotFoundException clsNotFndExc ) {
58
59            }
60        }
61
62    }
63
64    public void updateInputField() {
65
66        if( expressionIsTrue() ) {
67            int listSize  = renderers.size();
68            for( int i = 0; i < listSize; i++ ) {
69                SwingOutputFieldRenderer renderer = (SwingOutputFieldRenderer) renderers.get( i );
70                renderer.updateInputField();
71            }
72        }
73
74    }
75
76    public void updateDefaultValue() {
77        if( expressionIsTrue() ) {
78            int listSize  = renderers.size();
79            for( int i = 0; i < listSize; i++ ) {
80                SwingOutputFieldRenderer renderer = (SwingOutputFieldRenderer) renderers.get( i );
81                renderer.updateDefaultValue();
82            }
83        }
84    }
85
86    public void renderError() {
87        int listSize  = renderers.size();
88        for( int i = 0; i < listSize; i++ ) {
89            SwingOutputFieldRenderer renderer = (SwingOutputFieldRenderer) renderers.get( i );
90            renderer.renderError();
91        }
92    }
93
94    public int addSelf( JPanel content, GBCF cf, int row, boolean overflow ) {
95        return row;
96    }
97
98    private boolean expressionIsTrue() {
99        Expression expr = null;
00
01        try {
02            expr = condField.getExpression();
03        }
04        catch( ConfigurationException configExc ) {
05            ctx.log( resHelper.getMessage( "invalid.conditional.expression", configExc ) );
06            return false;
07        }
08
09         return expr.evaluate();
10    }
11}
12