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 * 
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *     http://www.apache.org/licenses/LICENSE-2.0
014 *     
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 */
021
022package com.izforge.izpack.panels;
023
024import java.util.Iterator;
025import java.util.Vector;
026
027import net.n3.nanoxml.XMLElement;
028
029import com.izforge.izpack.Pack;
030import com.izforge.izpack.installer.AutomatedInstallData;
031import com.izforge.izpack.installer.PanelAutomation;
032
033/**
034 * Functions to support automated usage of the PacksPanel
035 * 
036 * @author Jonathan Halliday
037 * @author Julien Ponge
038 */
039public class PacksPanelAutomationHelper implements PanelAutomation
040{
041
042    /**
043     * Asks to make the XML panel data.
044     * 
045     * @param idata The installation data.
046     * @param panelRoot The XML tree to write the data in.
047     */
048    public void makeXMLData(AutomatedInstallData idata, XMLElement panelRoot)
049    {
050        // We add each pack to the panelRoot element
051        for (int i = 0; i < idata.availablePacks.size(); i++)
052        {
053            Pack pack = (Pack) idata.availablePacks.get(i);
054            XMLElement el = new XMLElement("pack");
055            el.setAttribute("index", new Integer(i).toString());
056            el.setAttribute("name", pack.name);
057            Boolean selected = Boolean.valueOf(idata.selectedPacks.contains(pack));
058            el.setAttribute("selected", selected.toString());
059
060            panelRoot.addChild(el);
061        }
062    }
063
064    /**
065     * Asks to run in the automated mode.
066     * 
067     * @param idata The installation data.
068     * @param panelRoot The root of the panel data.
069     */
070    public void runAutomated(AutomatedInstallData idata, XMLElement panelRoot)
071    {
072        // We first get the <selected> child (new from version 3.7.0).
073        XMLElement selectedPacks = panelRoot.getFirstChildNamed("selected");
074        // We get the packs markups
075        Vector pm = selectedPacks.getChildrenNamed("pack");
076
077        // We figure out the selected ones
078        int size = pm.size();
079        idata.selectedPacks.clear();
080        for (int i = 0; i < size; i++)
081        {
082            XMLElement el = (XMLElement) pm.get(i);
083            Boolean selected = new Boolean(true); // No longer needed.
084
085            if (selected.booleanValue())
086            {
087                String index_str = el.getAttribute("index");
088
089                // be liberal in what we accept
090                // (For example, this allows auto-installer files to be fitted
091                // to automatically
092                // generated installers, yes I need this! tisc.)
093                if (index_str != null)
094                {
095                    try
096                    {
097                        int index = Integer.parseInt(index_str);
098                        if ((index >= 0) && (index < idata.availablePacks.size()))
099                        {
100                            idata.selectedPacks.add(idata.availablePacks.get(index));
101                        }
102                        else
103                        {
104                            System.err.println("Invalid pack index \"" + index_str + "\" in line "
105                                    + el.getLineNr());
106                        }
107                    }
108                    catch (NumberFormatException e)
109                    {
110                        System.err.println("Invalid pack index \"" + index_str + "\" in line "
111                                + el.getLineNr());
112                    }
113                }
114                else
115                {
116                    String name = el.getAttribute("name");
117
118                    if (name != null)
119                    {
120                        // search for pack with that name
121                        Iterator pack_it = idata.availablePacks.iterator();
122
123                        boolean found = false;
124
125                        while ((!found) && pack_it.hasNext())
126                        {
127                            Pack pack = (Pack) pack_it.next();
128
129                            if (pack.name.equals(name))
130                            {
131                                idata.selectedPacks.add(pack);
132                                found = true;
133                            }
134
135                        }
136
137                        if (!found)
138                        {
139                            System.err.println("Could not find selected pack named \"" + name
140                                    + "\" in line " + el.getLineNr());
141                        }
142
143                    }
144
145                }
146
147            }
148
149        }
150
151    }
152
153}