001/*
002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved.
003 * 
004 * http://www.izforge.com/izpack/
005 * http://developer.berlios.de/projects/izpack/
006 * 
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 * 
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *     
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package com.izforge.izpack.panels;
021
022import com.izforge.izpack.installer.InstallData;
023import com.izforge.izpack.installer.InstallerFrame;
024
025/**
026 * This panel adds some conditional behavior to the standard UserInputPanel. <br/> <b>Usage:</b><br/>
027 * In the "panels" list, just use ConditionalUserInputPanel like the normal UserInputPanel. The
028 * specification goes also into userInputSpec.xml and userInputLang.xml_XXX. To specify a condition
029 * for a certain ConditionalUserInputPanel, you have to specify the condition in the
030 * "variables"-section by defining the following variables:<br/>
031 * <li><i>compareToVariable."panel-order"</i>: The variable name containing the value to compare
032 * with
033 * <li><i>compareToOperator."panel-order"</i>: The compare operator to use, currently only "=" and
034 * "!=" are allowed
035 * <li><i>compareToValue."panel-order"</i>: The value to compare with<br/> If the compare fails,
036 * the panel will be skipped.
037 * 
038 * @see UserInputPanel
039 * 
040 * @author $author$
041 * @version $Revision: 1.5 $
042 */
043public class ConditionalUserInputPanel extends UserInputPanel
044{
045
046    private static final long serialVersionUID = 3257283617406465844L;
047
048    /**
049     * Creates a new ConditionalUserInputPanel object.
050     * 
051     * @param parent
052     * @param installData
053     */
054    public ConditionalUserInputPanel(InstallerFrame parent, InstallData installData)
055    {
056        super(parent, installData);
057    }
058
059    /**
060     * Panel is only activated, if the configured condition is true
061     */
062    public void panelActivate()
063    {
064        // get configured condition for this panel
065        String compareToValue = idata.getVariable("compareToValue." + instanceNumber);
066        String compareToVariable = idata.getVariable("compareToVariable." + instanceNumber);
067        String compareToOperator = idata.getVariable("compareToOperator." + instanceNumber);
068        String compareValue = null;
069
070        // get value of the compareVariable
071        if (null != compareToVariable)
072        {
073            compareValue = idata.getVariable(compareToVariable);
074        }
075
076        if ("=".equalsIgnoreCase(compareToOperator))
077        {
078            // compare using equal
079            if (((null != compareToValue) && compareToValue.equalsIgnoreCase(compareValue))
080                    || ((null != compareValue) && compareValue.equalsIgnoreCase(compareToValue)))
081            {
082                super.panelActivate();
083            }
084            else
085            {
086                parent.skipPanel();
087            }
088        }
089        else if ("!=".equalsIgnoreCase(compareToOperator))
090        {
091            // compare using un-equal
092            if (((null != compareToValue) && !compareToValue.equalsIgnoreCase(compareValue))
093                    || ((null != compareValue) && !compareValue.equalsIgnoreCase(compareToValue)))
094            {
095                super.panelActivate();
096            }
097            else
098            {
099                parent.skipPanel();
100            }
101        }
102        else
103        {
104            // wrong operator!
105            emitError("Invalid operator specified for compareToOperator." + instanceNumber,
106                    "Only '=' and '!=' are currently allowed!");
107            parent.skipPanel();
108        }
109    }
110}