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 net.n3.nanoxml.XMLElement;
025
026import com.izforge.izpack.installer.AutomatedInstallData;
027import com.izforge.izpack.installer.PanelAutomation;
028import com.izforge.izpack.installer.PanelAutomationHelper;
029import com.izforge.izpack.installer.Unpacker;
030import com.izforge.izpack.util.AbstractUIProgressHandler;
031
032/**
033 * Functions to support automated usage of the InstallPanel
034 * 
035 * @author Jonathan Halliday
036 */
037public class InstallPanelAutomationHelper extends PanelAutomationHelper implements PanelAutomation,
038        AbstractUIProgressHandler
039{
040
041    // state var for thread sync.
042    private boolean done = false;
043
044    private int noOfPacks = 0;
045
046    /**
047     * Null op - this panel type has no state to serialize.
048     * 
049     * @param installData unused.
050     * @param panelRoot unused.
051     */
052    public void makeXMLData(AutomatedInstallData installData, XMLElement panelRoot)
053    {
054        // do nothing.
055    }
056
057    /**
058     * Perform the installation actions.
059     * 
060     * @param panelRoot The panel XML tree root.
061     */
062    public void runAutomated(AutomatedInstallData idata, XMLElement panelRoot)
063    {
064        Unpacker unpacker = new Unpacker(idata, this);
065        unpacker.start();
066        done = false;
067        while (!done && unpacker.isAlive())
068        {
069            try
070            {
071                Thread.sleep(100);
072            }
073            catch (InterruptedException e)
074            {
075                // ignore it, we're waiting for the unpacker to finish...
076            }
077        }
078    }
079
080    /**
081     * Reports progress on System.out
082     * 
083     * @see AbstractUIProgressHandler#startAction(String, int)
084     */
085    public void startAction(String name, int no_of_steps)
086    {
087        System.out.println("[ Starting to unpack ]");
088        this.noOfPacks = no_of_steps;
089    }
090
091    /**
092     * Sets state variable for thread sync.
093     * 
094     * @see com.izforge.izpack.util.AbstractUIProgressHandler#stopAction()
095     */
096    public void stopAction()
097    {
098        System.out.println("[ Unpacking finished. ]");
099        done = true;
100    }
101
102    /**
103     * Null op.
104     * 
105     * @param val
106     * @param msg
107     * @see com.izforge.izpack.util.AbstractUIProgressHandler#progress(int, String)
108     */
109    public void progress(int val, String msg)
110    {
111        // silent for now. should log individual files here, if we had a verbose
112        // mode?
113    }
114
115    /**
116     * Reports progress to System.out
117     * 
118     * @param packName The currently installing pack.
119     * @param stepno The number of the pack
120     * @param stepsize unused
121     * @see com.izforge.izpack.util.AbstractUIProgressHandler#nextStep(String, int, int)
122     */
123    public void nextStep(String packName, int stepno, int stepsize)
124    {
125        System.out.print("[ Processing package: " + packName + " (");
126        System.out.print(stepno);
127        System.out.print('/');
128        System.out.print(this.noOfPacks);
129        System.out.println(") ]");
130    }
131
132}