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 * Copyright 2003 Jonathan Halliday
008 * Copyright 2002 Elmar Grom
009 * 
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 * 
014 *     http://www.apache.org/licenses/LICENSE-2.0
015 *     
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 */
022
023package com.izforge.izpack.panels;
024
025import java.util.Iterator;
026import java.util.Map;
027import java.util.Vector;
028
029import net.n3.nanoxml.XMLElement;
030
031import com.izforge.izpack.installer.AutomatedInstallData;
032import com.izforge.izpack.installer.PanelAutomation;
033import com.izforge.izpack.util.Debug;
034
035/**
036 * Functions to support automated usage of the UserInputPanel
037 * 
038 * @author Jonathan Halliday
039 * @author Elmar Grom
040 */
041public class UserInputPanelAutomationHelper implements PanelAutomation
042{
043
044    // ------------------------------------------------------
045    // automatic script section keys
046    // ------------------------------------------------------
047    private static final String AUTO_KEY_USER_INPUT = "userInput";
048
049    private static final String AUTO_KEY_ENTRY = "entry";
050
051    // ------------------------------------------------------
052    // automatic script keys attributes
053    // ------------------------------------------------------
054    private static final String AUTO_ATTRIBUTE_KEY = "key";
055
056    private static final String AUTO_ATTRIBUTE_VALUE = "value";
057
058    // ------------------------------------------------------
059    // String-String key-value pairs
060    // ------------------------------------------------------
061    private Map entries;
062
063    public UserInputPanelAutomationHelper()
064    {
065        this.entries = null;
066    }
067
068    /**
069     * 
070     * @param entries String-String key-value pairs representing the state of the Panel
071     */
072    public UserInputPanelAutomationHelper(Map entries)
073    {
074        this.entries = entries;
075    }
076
077    /**
078     * Serialize state to XML and insert under panelRoot.
079     * 
080     * @param idata The installation data.
081     * @param panelRoot The XML root element of the panels blackbox tree.
082     */
083    public void makeXMLData(AutomatedInstallData idata, XMLElement panelRoot)
084    {
085        XMLElement userInput;
086        XMLElement dataElement;
087
088        // ----------------------------------------------------
089        // add the item that combines all entries
090        // ----------------------------------------------------
091        userInput = new XMLElement(AUTO_KEY_USER_INPUT);
092        panelRoot.addChild(userInput);
093
094        // ----------------------------------------------------
095        // add all entries
096        // ----------------------------------------------------
097        Iterator keys = entries.keySet().iterator();
098        while (keys.hasNext())
099        {
100            String key = (String) keys.next();
101            String value = (String) entries.get(key);
102
103            dataElement = new XMLElement(AUTO_KEY_ENTRY);
104            dataElement.setAttribute(AUTO_ATTRIBUTE_KEY, key);
105            dataElement.setAttribute(AUTO_ATTRIBUTE_VALUE, value);
106
107            userInput.addChild(dataElement);
108        }
109    }
110
111    /**
112     * Deserialize state from panelRoot and set idata variables accordingly.
113     * 
114     * @param idata The installation data.
115     * @param panelRoot The XML root element of the panels blackbox tree.
116     */
117    public void runAutomated(AutomatedInstallData idata, XMLElement panelRoot)
118    {
119        XMLElement userInput;
120        XMLElement dataElement;
121        String variable;
122        String value;
123
124        // ----------------------------------------------------
125        // get the section containing the user entries
126        // ----------------------------------------------------
127        userInput = panelRoot.getFirstChildNamed(AUTO_KEY_USER_INPUT);
128
129        if (userInput == null) { return; }
130
131        Vector userEntries = userInput.getChildrenNamed(AUTO_KEY_ENTRY);
132
133        if (userEntries == null) { return; }
134
135        // ----------------------------------------------------
136        // retieve each entry and substitute the associated
137        // variable
138        // ----------------------------------------------------
139        for (int i = 0; i < userEntries.size(); i++)
140        {
141            dataElement = (XMLElement) userEntries.elementAt(i);
142            variable = dataElement.getAttribute(AUTO_ATTRIBUTE_KEY);
143            value = dataElement.getAttribute(AUTO_ATTRIBUTE_VALUE);
144
145            Debug.trace("UserInputPanel: setting variable " + variable + " to " + value);
146            idata.setVariable(variable, value);
147        }
148    }
149}