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;
021
022import java.io.Serializable;
023import java.text.DecimalFormat;
024import java.util.ArrayList;
025import java.util.HashSet;
026import java.util.List;
027import java.util.Set;
028
029import com.izforge.izpack.compiler.PackInfo;
030
031/**
032 * Represents a Pack.
033 * 
034 * @author Julien Ponge
035 */
036public class Pack implements Serializable
037{
038
039    static final long serialVersionUID = -5458360562175088671L;
040
041    public boolean loose;
042
043    /** The pack name. */
044    public String name;
045
046    /** The langpack id */
047    public String id;
048
049    /** An association of this pack to zero or more installation groups. An
050     * installation group is just a named collection of packs to allow for
051     * different pack collections to be selected, for example: minimal,
052     * default, all.
053     */
054    public Set installGroups = new HashSet();
055
056    /** The group the pack is associated with. The pack group identifies
057     * packs with common functionality to allow for grouping of packs in a
058     * tree in the TargetPanel for example.
059     */
060    public String group;
061
062    /** The pack description. */
063    public String description;
064
065    /** The target operation system of this pack */
066    public List osConstraints = null;
067
068    /** The list of packs this pack depends on */
069    public List dependencies = null;
070
071    /** Reverse dependencies(childs) */
072    public List revDependencies = null;
073
074    /** True if the pack is required. */
075    public boolean required;
076
077    /** The bumber of bytes contained in the pack. */
078    public long nbytes;
079
080    /** Whether this pack is suggested (preselected for installation). */
081    public boolean preselected;
082
083    /** The color of the node. This is used for the dependency graph algorithms */
084    public int color;
085
086    /** white colour */
087    public final static int WHITE = 0;
088
089    /** grey colour */
090    public final static int GREY = 1;
091
092    /** black colour */
093    public final static int BLACK = 2;
094
095    /**
096     * The constructor.
097     * 
098     * @param name The pack name.
099     * @param description The pack description.
100     * @param osConstraints the OS constraint (or null for any OS)
101     * @param required Indicates wether the pack is required or not.
102     * @param preselected This pack will be selected automatically.
103     */
104    public Pack(String name, String id, String description, List osConstraints, List dependencies,
105            boolean required, boolean preselected, boolean loose)
106    {
107        this.name = name;
108        this.id = id;
109        this.description = description;
110        this.osConstraints = osConstraints;
111        this.dependencies = dependencies;
112        this.required = required;
113        this.preselected = preselected;
114        this.loose = loose;
115        nbytes = 0;
116        color = PackInfo.WHITE;
117    }
118
119    /**
120     * To a String (usefull for JLists).
121     * 
122     * @return The String representation of the pack.
123     */
124    public String toString()
125    {
126        return name + " (" + description + ")";
127    }
128
129    /** getter method */
130    public List getDependencies()
131    {
132        return dependencies;
133    }
134
135    /**
136     * This adds a reverse dependency. With a reverse dependency we imply a child dependency or the
137     * dependents on this pack
138     * 
139     * @param name The name of the pack that depents to this pack
140     */
141    public void addRevDep(String name)
142    {
143        if (revDependencies == null) revDependencies = new ArrayList();
144        revDependencies.add(name);
145    }
146
147    /**
148     * Creates a text list of all the packs it depend on
149     * 
150     * @return the created text
151     */
152    public String depString()
153    {
154        String text = "";
155        if (dependencies == null) return text;
156        String name = null;
157        for (int i = 0; i < dependencies.size() - 1; i++)
158        {
159            name = (String) dependencies.get(i);
160            text += name + ",";
161        }
162        name = (String) dependencies.get(dependencies.size() - 1);
163        text += name;
164        return text;
165
166    }
167
168    /** Used of conversions. */
169    private final static double KILOBYTES = 1024.0;
170
171    /** Used of conversions. */
172    private final static double MEGABYTES = 1024.0 * 1024.0;
173
174    /** Used of conversions. */
175    private final static double GIGABYTES = 1024.0 * 1024.0 * 1024.0;
176
177    /** Used of conversions. */
178    private final static DecimalFormat formatter = new DecimalFormat("#,###.##");
179
180    /**
181     * Convert bytes into appropiate mesaurements.
182     * 
183     * @param bytes A number of bytes to convert to a String.
184     * @return The String-converted value.
185     */
186    public static String toByteUnitsString(int bytes)
187    {
188        if (bytes < KILOBYTES)
189            return String.valueOf(bytes) + " bytes";
190        else if (bytes < (MEGABYTES))
191        {
192            double value = bytes / KILOBYTES;
193            return formatter.format(value) + " KB";
194        }
195        else if (bytes < (GIGABYTES))
196        {
197            double value = bytes / MEGABYTES;
198            return formatter.format(value) + " MB";
199        }
200        else
201        {
202            double value = bytes / GIGABYTES;
203            return formatter.format(value) + " GB";
204        }
205    }
206}