001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
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:
022: package com.izforge.izpack.panels;
023:
024: import java.util.Iterator;
025: import java.util.Vector;
026:
027: import net.n3.nanoxml.XMLElement;
028:
029: import com.izforge.izpack.Pack;
030: import com.izforge.izpack.installer.AutomatedInstallData;
031: import 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: */
039: public class PacksPanelAutomationHelper implements PanelAutomation {
040:
041: /**
042: * Asks to make the XML panel data.
043: *
044: * @param idata The installation data.
045: * @param panelRoot The XML tree to write the data in.
046: */
047: public void makeXMLData(AutomatedInstallData idata,
048: XMLElement panelRoot) {
049: // We add each pack to the panelRoot element
050: for (int i = 0; i < idata.allPacks.size(); i++) {
051: Pack pack = idata.allPacks.get(i);
052: XMLElement el = new XMLElement("pack");
053: el.setAttribute("index", Integer.toString(i));
054: el.setAttribute("name", pack.name);
055: Boolean selected = idata.selectedPacks.contains(pack);
056: el.setAttribute("selected", selected.toString());
057:
058: panelRoot.addChild(el);
059: }
060: }
061:
062: /**
063: * Asks to run in the automated mode.
064: *
065: * @param idata The installation data.
066: * @param panelRoot The root of the panel data.
067: *
068: * @return true if all packs were found and selected, false if something was wrong.
069: */
070: public boolean runAutomated(AutomatedInstallData idata,
071: XMLElement panelRoot) {
072: // We get the packs markups
073: Vector<XMLElement> pm = panelRoot.getChildrenNamed("pack");
074:
075: boolean result = true;
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: XMLElement el = pm.get(i);
082: Boolean selected = Boolean.TRUE; // No longer needed.
083:
084: if (selected) {
085: String index_str = el.getAttribute("index");
086:
087: // be liberal in what we accept
088: // (For example, this allows auto-installer files to be fitted
089: // to automatically generated installers, yes I need this! tschwarze.)
090: if (index_str != null) {
091: try {
092: int index = Integer.parseInt(index_str);
093: if ((index >= 0)
094: && (index < idata.availablePacks.size())) {
095: if (el.getAttribute("selected")
096: .equalsIgnoreCase("true")
097: || el.getAttribute("selected")
098: .equalsIgnoreCase("on")) {
099: idata.selectedPacks
100: .add(idata.availablePacks
101: .get(index));
102: }
103: } else {
104: System.err.println("Invalid pack index \""
105: + index_str + "\" in line "
106: + el.getLineNr());
107: result = false;
108: }
109: } catch (NumberFormatException e) {
110: System.err.println("Invalid pack index \""
111: + index_str + "\" in line "
112: + el.getLineNr());
113: result = false;
114: }
115: } else {
116: String name = el.getAttribute("name");
117:
118: if (name != null) {
119: // search for pack with that name
120: Iterator pack_it = idata.availablePacks
121: .iterator();
122:
123: boolean found = false;
124:
125: while ((!found) && pack_it.hasNext()) {
126: Pack pack = (Pack) pack_it.next();
127:
128: if (pack.name.equals(name)) {
129: idata.selectedPacks.add(pack);
130: found = true;
131: }
132:
133: }
134:
135: if (!found) {
136: System.err
137: .println("Could not find selected pack named \""
138: + name
139: + "\" in line "
140: + el.getLineNr());
141: result = false;
142: }
143:
144: }
145:
146: }
147:
148: }
149:
150: }
151:
152: return result;
153: }
154:
155: }
|