001: /*
002: * Licensed under the Apache License, Version 2.0 (the "License");
003: * you may not use this file except in compliance with the License.
004: * You may obtain a copy of the License at
005: *
006: * http://www.apache.org/licenses/LICENSE-2.0
007: *
008: * Unless required by applicable law or agreed to in writing, software
009: * distributed under the License is distributed on an "AS IS" BASIS,
010: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: * See the License for the specific language governing permissions and
012: * limitations under the License.
013: */
014: package org.tp23.antinstaller.renderer.swing;
015:
016: import java.util.ArrayList;
017:
018: import javax.swing.JPanel;
019:
020: import org.tp23.antinstaller.AIResourceBundleHelper;
021: import org.tp23.antinstaller.input.ConditionalField;
022: import org.tp23.antinstaller.input.InputField;
023: import org.tp23.antinstaller.renderer.RendererFactory;
024: import org.tp23.antinstaller.runtime.ConfigurationException;
025: import org.tp23.antinstaller.runtime.logic.Expression;
026: import org.tp23.gui.GBCF;
027:
028: /**
029: * Conditionally render a collection of fields.
030: * For now, will ONLY handle <code><hiddeb></code> fields
031: *
032: * @author mwilson
033: * @version $Id
034: * @since 0.7.4 patch 7
035: */
036: public class ConditionalFieldRenderer extends SwingOutputFieldRenderer {
037:
038: private AIResourceBundleHelper resHelper = new AIResourceBundleHelper();
039: private ConditionalField condField;
040: private ArrayList renderers = new ArrayList();
041:
042: public ConditionalFieldRenderer() {
043: }
044:
045: public void initComponent(JPanel parent) {
046:
047: condField = (ConditionalField) outputField;
048: InputField[] fields = condField.getFields();
049:
050: for (int i = 0; i < fields.length; i++) {
051: try {
052: SwingOutputFieldRenderer renderer = RendererFactory
053: .getSwingRenderer(fields[i]);
054: renderer.setOutputField(fields[i]);
055: renderer.setInstallerContext(ctx);
056: renderers.add(renderer);
057: } catch (ClassNotFoundException clsNotFndExc) {
058:
059: }
060: }
061:
062: }
063:
064: public void updateInputField() {
065:
066: if (expressionIsTrue()) {
067: int listSize = renderers.size();
068: for (int i = 0; i < listSize; i++) {
069: SwingOutputFieldRenderer renderer = (SwingOutputFieldRenderer) renderers
070: .get(i);
071: renderer.updateInputField();
072: }
073: }
074:
075: }
076:
077: public void updateDefaultValue() {
078: if (expressionIsTrue()) {
079: int listSize = renderers.size();
080: for (int i = 0; i < listSize; i++) {
081: SwingOutputFieldRenderer renderer = (SwingOutputFieldRenderer) renderers
082: .get(i);
083: renderer.updateDefaultValue();
084: }
085: }
086: }
087:
088: public void renderError() {
089: int listSize = renderers.size();
090: for (int i = 0; i < listSize; i++) {
091: SwingOutputFieldRenderer renderer = (SwingOutputFieldRenderer) renderers
092: .get(i);
093: renderer.renderError();
094: }
095: }
096:
097: public int addSelf(JPanel content, GBCF cf, int row,
098: boolean overflow) {
099: return row;
100: }
101:
102: private boolean expressionIsTrue() {
103: Expression expr = null;
104:
105: try {
106: expr = condField.getExpression();
107: } catch (ConfigurationException configExc) {
108: ctx.log(resHelper.getMessage(
109: "invalid.conditional.expression", configExc));
110: return false;
111: }
112:
113: return expr.evaluate();
114: }
115: }
|