KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > panels > PacksPanelAutomationHelper


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2003 Jonathan Halliday
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.izforge.izpack.panels;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import net.n3.nanoxml.XMLElement;
28
29 import com.izforge.izpack.Pack;
30 import com.izforge.izpack.installer.AutomatedInstallData;
31 import com.izforge.izpack.installer.PanelAutomation;
32
33 /**
34  * Functions to support automated usage of the PacksPanel
35  *
36  * @author Jonathan Halliday
37  * @author Julien Ponge
38  */

39 public class PacksPanelAutomationHelper implements PanelAutomation
40 {
41
42     /**
43      * Asks to make the XML panel data.
44      *
45      * @param idata The installation data.
46      * @param panelRoot The XML tree to write the data in.
47      */

48     public void makeXMLData(AutomatedInstallData idata, XMLElement panelRoot)
49     {
50         // We add each pack to the panelRoot element
51
for (int i = 0; i < idata.allPacks.size(); i++)
52         {
53             Pack pack = (Pack) idata.allPacks.get(i);
54             XMLElement el = new XMLElement("pack");
55             el.setAttribute("index", Integer.toString(i));
56             el.setAttribute("name", pack.name);
57             Boolean JavaDoc selected = Boolean.valueOf(idata.selectedPacks.contains(pack));
58             el.setAttribute("selected", selected.toString());
59
60             panelRoot.addChild(el);
61         }
62     }
63
64     /**
65      * Asks to run in the automated mode.
66      *
67      * @param idata The installation data.
68      * @param panelRoot The root of the panel data.
69      *
70      * @return true if all packs were found and selected, false if something was wrong.
71      */

72     public boolean runAutomated(AutomatedInstallData idata, XMLElement panelRoot)
73     {
74         // We get the packs markups
75
Vector JavaDoc pm = panelRoot.getChildrenNamed("pack");
76
77         boolean result = true;
78         
79         // We figure out the selected ones
80
int size = pm.size();
81         idata.selectedPacks.clear();
82         for (int i = 0; i < size; i++)
83         {
84             XMLElement el = (XMLElement) pm.get(i);
85             Boolean JavaDoc selected = Boolean.TRUE; // No longer needed.
86

87             if (selected.booleanValue())
88             {
89                 String JavaDoc index_str = el.getAttribute("index");
90
91                 // be liberal in what we accept
92
// (For example, this allows auto-installer files to be fitted
93
// to automatically generated installers, yes I need this! tschwarze.)
94
if (index_str != null)
95                 {
96                     try
97                     {
98                         int index = Integer.parseInt(index_str);
99                         if ((index >= 0) && (index < idata.availablePacks.size()))
100                         {
101                             idata.selectedPacks.add(idata.availablePacks.get(index));
102                         }
103                         else
104                         {
105                             System.err.println("Invalid pack index \"" + index_str + "\" in line "
106                                     + el.getLineNr());
107                             result = false;
108                         }
109                     }
110                     catch (NumberFormatException JavaDoc e)
111                     {
112                         System.err.println("Invalid pack index \"" + index_str + "\" in line "
113                                 + el.getLineNr());
114                         result = false;
115                     }
116                 }
117                 else
118                 {
119                     String JavaDoc name = el.getAttribute("name");
120
121                     if (name != null)
122                     {
123                         // search for pack with that name
124
Iterator JavaDoc pack_it = idata.availablePacks.iterator();
125
126                         boolean found = false;
127
128                         while ((!found) && pack_it.hasNext())
129                         {
130                             Pack pack = (Pack) pack_it.next();
131
132                             if (pack.name.equals(name))
133                             {
134                                 idata.selectedPacks.add(pack);
135                                 found = true;
136                             }
137
138                         }
139
140                         if (!found)
141                         {
142                             System.err.println("Could not find selected pack named \"" + name
143                                     + "\" in line " + el.getLineNr());
144                             result = false;
145                         }
146
147                     }
148
149                 }
150
151             }
152
153         }
154
155         return result;
156     }
157
158 }
159
Popular Tags