KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Copyright 2002 Elmar Grom
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */

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

41 public class UserInputPanelAutomationHelper implements PanelAutomation
42 {
43
44     // ------------------------------------------------------
45
// automatic script section keys
46
// ------------------------------------------------------
47
private static final String JavaDoc AUTO_KEY_USER_INPUT = "userInput";
48
49     private static final String JavaDoc AUTO_KEY_ENTRY = "entry";
50
51     // ------------------------------------------------------
52
// automatic script keys attributes
53
// ------------------------------------------------------
54
private static final String JavaDoc AUTO_ATTRIBUTE_KEY = "key";
55
56     private static final String JavaDoc AUTO_ATTRIBUTE_VALUE = "value";
57
58     // ------------------------------------------------------
59
// String-String key-value pairs
60
// ------------------------------------------------------
61
private Map JavaDoc entries;
62
63     /**
64      * Default constructor, used during automated installation.
65      */

66     public UserInputPanelAutomationHelper()
67     {
68         this.entries = null;
69     }
70
71     /**
72      *
73      * @param entries String-String key-value pairs representing the state of the Panel
74      */

75     public UserInputPanelAutomationHelper(Map JavaDoc entries)
76     {
77         this.entries = entries;
78     }
79
80     /**
81      * Serialize state to XML and insert under panelRoot.
82      *
83      * @param idata The installation data.
84      * @param panelRoot The XML root element of the panels blackbox tree.
85      */

86     public void makeXMLData(AutomatedInstallData idata, XMLElement panelRoot)
87     {
88         XMLElement userInput;
89         XMLElement dataElement;
90
91         // ----------------------------------------------------
92
// add the item that combines all entries
93
// ----------------------------------------------------
94
userInput = new XMLElement(AUTO_KEY_USER_INPUT);
95         panelRoot.addChild(userInput);
96
97         // ----------------------------------------------------
98
// add all entries
99
// ----------------------------------------------------
100
Iterator JavaDoc keys = this.entries.keySet().iterator();
101         while (keys.hasNext())
102         {
103             String JavaDoc key = (String JavaDoc) keys.next();
104             String JavaDoc value = (String JavaDoc) this.entries.get(key);
105
106             dataElement = new XMLElement(AUTO_KEY_ENTRY);
107             dataElement.setAttribute(AUTO_ATTRIBUTE_KEY, key);
108             dataElement.setAttribute(AUTO_ATTRIBUTE_VALUE, value);
109
110             userInput.addChild(dataElement);
111         }
112     }
113
114     /**
115      * Deserialize state from panelRoot and set idata variables accordingly.
116      *
117      * @param idata The installation data.
118      * @param panelRoot The XML root element of the panels blackbox tree.
119      *
120      * @return true if the variables were found and set.
121      */

122     public boolean runAutomated(AutomatedInstallData idata, XMLElement panelRoot)
123     {
124         XMLElement userInput;
125         XMLElement dataElement;
126         String JavaDoc variable;
127         String JavaDoc value;
128
129         // ----------------------------------------------------
130
// get the section containing the user entries
131
// ----------------------------------------------------
132
userInput = panelRoot.getFirstChildNamed(AUTO_KEY_USER_INPUT);
133
134         if (userInput == null) { return false; }
135
136         Vector JavaDoc userEntries = userInput.getChildrenNamed(AUTO_KEY_ENTRY);
137
138         if (userEntries == null) { return false; }
139
140         // ----------------------------------------------------
141
// retieve each entry and substitute the associated
142
// variable
143
// ----------------------------------------------------
144
for (int i = 0; i < userEntries.size(); i++)
145         {
146             dataElement = (XMLElement) userEntries.elementAt(i);
147             variable = dataElement.getAttribute(AUTO_ATTRIBUTE_KEY);
148             value = dataElement.getAttribute(AUTO_ATTRIBUTE_VALUE);
149
150             Debug.trace("UserInputPanel: setting variable " + variable + " to " + value);
151             idata.setVariable(variable, value);
152         }
153         
154         return true;
155     }
156 }
157
Popular Tags