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 2004 Klaus Bartz
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.event;
023
024import java.io.File;
025import java.util.ArrayList;
026
027import com.izforge.izpack.LocaleDatabase;
028import com.izforge.izpack.Pack;
029import com.izforge.izpack.PackFile;
030import com.izforge.izpack.installer.AutomatedInstallData;
031import com.izforge.izpack.installer.ResourceManager;
032import com.izforge.izpack.util.AbstractUIProgressHandler;
033import com.izforge.izpack.util.SpecHelper;
034
035/**
036 * <p>
037 * This class implements all methods of interface InstallerListener, but do not do anything. It can
038 * be used as base class to save implementation of unneeded methods.
039 * </p>
040 * <p>
041 * Additional there are some common helper methods which are used from the base class SpecHelper.
042 * </p>
043 * 
044 * @author Klaus Bartz
045 * 
046 */
047public class SimpleInstallerListener implements InstallerListener
048{
049
050    private static ArrayList progressBarCaller = new ArrayList();
051
052    /** The name of the XML file that specifies the panel langpack */
053    protected static final String LANG_FILE_NAME = "CustomActionsLang.xml";
054
055    /** The packs locale database. */
056    protected static LocaleDatabase langpack = null;
057
058    protected static boolean doInformProgressBar = false;
059
060    private AutomatedInstallData installdata = null;
061
062    private SpecHelper specHelper = null;
063
064    /**
065     * The default constructor.
066     */
067    public SimpleInstallerListener()
068    {
069        this(false);
070    }
071
072    /**
073     * Constructs a simple installer listener. If useSpecHelper is true, a specification helper will
074     * be created.
075     * 
076     * @param useSpecHelper
077     * 
078     */
079    public SimpleInstallerListener(boolean useSpecHelper)
080    {
081        super();
082        if (useSpecHelper) setSpecHelper(new SpecHelper());
083    }
084
085    /*
086     * (non-Javadoc)
087     * 
088     * @see com.izforge.izpack.compiler.InstallerListener#handleFile(java.io.File,
089     * com.izforge.izpack.PackFile)
090     */
091    public void afterFile(File file, PackFile pf) throws Exception
092    {
093        // Do nothing
094        ;
095    }
096
097    /*
098     * (non-Javadoc)
099     * 
100     * @see com.izforge.izpack.compiler.InstallerListener#handleDir(java.io.File,
101     * com.izforge.izpack.PackFile)
102     */
103    public void afterDir(File dir, PackFile pf) throws Exception
104    {
105        // Do nothing
106        ;
107    }
108
109    /*
110     * (non-Javadoc)
111     * 
112     * @see com.izforge.izpack.compiler.InstallerListener#afterPacks(com.izforge.izpack.installer.AutomatedInstallData,
113     * com.izforge.izpack.util.AbstractUIProgressHandler)
114     */
115    public void afterPacks(AutomatedInstallData idata, AbstractUIProgressHandler handler)
116            throws Exception
117    {
118
119        // Do nothing
120        ;
121    }
122
123    /*
124     * (non-Javadoc)
125     * 
126     * @see com.izforge.izpack.compiler.InstallerListener#afterPack(com.izforge.izpack.Pack, int,
127     * com.izforge.izpack.util.AbstractUIProgressHandler)
128     */
129    public void afterPack(Pack pack, Integer i, AbstractUIProgressHandler handler) throws Exception
130    {
131        // TODO Auto-generated method stub
132
133    }
134
135    /*
136     * (non-Javadoc)
137     * 
138     * @see com.izforge.izpack.compiler.InstallerListener#beforePacks(com.izforge.izpack.installer.AutomatedInstallData,
139     * int, com.izforge.izpack.util.AbstractUIProgressHandler)
140     */
141    public void beforePacks(AutomatedInstallData idata, Integer npacks,
142            AbstractUIProgressHandler handler) throws Exception
143    {
144        if (installdata == null) installdata = idata;
145        if (installdata != null && SimpleInstallerListener.langpack == null)
146        {
147            // Load langpack.
148            try
149            {
150                String resource = LANG_FILE_NAME + "_" + installdata.localeISO3;
151                SimpleInstallerListener.langpack = new LocaleDatabase(ResourceManager.getInstance()
152                        .getInputStream(resource));
153            }
154            catch (Throwable exception)
155            {}
156
157        }
158    }
159
160    /*
161     * (non-Javadoc)
162     * 
163     * @see com.izforge.izpack.compiler.InstallerListener#beforePack(com.izforge.izpack.Pack, int,
164     * com.izforge.izpack.util.AbstractUIProgressHandler)
165     */
166    public void beforePack(Pack pack, Integer i, AbstractUIProgressHandler handler)
167            throws Exception
168    {
169        // Do nothing
170        ;
171    }
172
173    /*
174     * (non-Javadoc)
175     * 
176     * @see com.izforge.izpack.installer.InstallerListener#isFileListener()
177     */
178    public boolean isFileListener()
179    {
180        // For default no.
181        return false;
182    }
183
184    /*
185     * (non-Javadoc)
186     * 
187     * @see com.izforge.izpack.installer.InstallerListener#beforeFile(java.io.File,
188     * com.izforge.izpack.PackFile)
189     */
190    public void beforeFile(File file, PackFile pf) throws Exception
191    {
192        // Do nothing
193        ;
194    }
195
196    /*
197     * (non-Javadoc)
198     * 
199     * @see com.izforge.izpack.installer.InstallerListener#beforeDir(java.io.File,
200     * com.izforge.izpack.PackFile)
201     */
202    public void beforeDir(File dir, PackFile pf) throws Exception
203    {
204        // Do nothing
205        ;
206    }
207
208    /**
209     * Returns current specification helper.
210     * 
211     * @return current specification helper
212     */
213    public SpecHelper getSpecHelper()
214    {
215        return specHelper;
216    }
217
218    /**
219     * Sets the given specification helper to the current used helper.
220     * 
221     * @param helper specification helper which should be used
222     */
223    public void setSpecHelper(SpecHelper helper)
224    {
225        specHelper = helper;
226    }
227
228    /**
229     * Returns the current installdata object.
230     * 
231     * @return current installdata object
232     */
233    public AutomatedInstallData getInstalldata()
234    {
235        return installdata;
236    }
237
238    /**
239     * Sets the installdata object.
240     * 
241     * @param data installdata object which should be set to current
242     */
243    public void setInstalldata(AutomatedInstallData data)
244    {
245        installdata = data;
246    }
247
248    /**
249     * Returns the count of listeners which are registered as progress bar caller.
250     * 
251     * @return the count of listeners which are registered as progress bar caller
252     */
253    public static int getProgressBarCallerCount()
254    {
255        return (progressBarCaller.size());
256    }
257
258    /**
259     * Returns the progress bar caller id of this object.
260     * 
261     * @return the progress bar caller id of this object
262     */
263    protected int getProgressBarCallerId()
264    {
265        for (int i = 0; i < progressBarCaller.size(); ++i)
266        {
267            if (progressBarCaller.get(i) == this) return (i + 1);
268        }
269        return (0);
270    }
271
272    /**
273     * Sets this object as progress bar caller.
274     */
275    protected void setProgressBarCaller()
276    {
277        progressBarCaller.add(this);
278
279    }
280
281    /**
282     * Returns whether this object should inform the progress bar or not.
283     * 
284     * @return whether this object should inform the progress bar or not
285     */
286    protected boolean informProgressBar()
287    {
288        return (doInformProgressBar);
289    }
290
291    /**
292     * Returns the language dependant message from the resource CustomActionsLang.xml or the common
293     * language pack for the given id. If no string will be found, the id returns.
294     * 
295     * @param id string id for which the message should be resolved
296     * @return the related language dependant message
297     */
298    protected String getMsg(String id)
299    {
300        String retval = id;
301        if (SimpleInstallerListener.langpack != null)
302        {
303            retval = SimpleInstallerListener.langpack.getString(id);
304        }
305        if (retval.equals(id) && getInstalldata() != null)
306        {
307            retval = getInstalldata().langpack.getString(id);
308        }
309        return (retval);
310    }
311}