forked from jponge/izpack
-
Notifications
You must be signed in to change notification settings - Fork 225
/
PacksConsolePanel.java
executable file
·225 lines (201 loc) · 7.3 KB
/
PacksConsolePanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* IzPack - Copyright 2001-2012 Julien Ponge, All Rights Reserved.
*
* http://izpack.org/
* http://izpack.codehaus.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.izforge.izpack.panels.packs;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import com.izforge.izpack.api.adaptator.IXMLElement;
import com.izforge.izpack.api.data.InstallData;
import com.izforge.izpack.api.data.Pack;
import com.izforge.izpack.api.exception.ResourceNotFoundException;
import com.izforge.izpack.api.handler.Prompt;
import com.izforge.izpack.api.handler.Prompt.Option;
import com.izforge.izpack.api.handler.Prompt.Options;
import com.izforge.izpack.api.handler.Prompt.Type;
import com.izforge.izpack.api.resource.Messages;
import com.izforge.izpack.installer.console.AbstractConsolePanel;
import com.izforge.izpack.installer.console.ConsolePanel;
import com.izforge.izpack.installer.panel.PanelView;
import com.izforge.izpack.installer.util.PackHelper;
import com.izforge.izpack.util.Console;
/**
* Console implementation for the TreePacksPanel.
* <p/>
* Based on PacksConsolePanelHelper
*
* @author Sergiy Shyrkov
* @author Dustin Kut Moy Cheung
*/
public class PacksConsolePanel extends AbstractConsolePanel implements ConsolePanel
{
private Messages messages;
private HashMap<String, Pack> names;
private final Prompt prompt;
private final InstallData installData;
public PacksConsolePanel(PanelView<ConsolePanel> panel, InstallData installData, Prompt prompt)
{
super(panel);
this.prompt = prompt;
this.installData = installData;
//load the packs lang messages if exists
try
{
messages = installData.getMessages().newMessages(PackHelper.LANG_FILE_NAME);
}
catch (ResourceNotFoundException exception)
{
// no packs messages resource, so fall back to the default
messages = installData.getMessages();
}
}
/**
* Runs the panel using the supplied properties.
*
* @param installData the installation data
* @param properties the properties
* @return <tt>true</tt> if the installation is successful, otherwise <tt>false</tt>
*/
@Override
public boolean run(InstallData installData, Properties properties)
{
return true;
}
/**
* Runs the panel using the specified console.
*
* @param installData the installation data
* @param console the console
* @return <tt>true</tt> if the panel ran successfully, otherwise <tt>false</tt>
*/
@Override
public boolean run(InstallData installData, Console console)
{
out(Type.INFORMATION, "");
List<Pack> selectedPacks = new LinkedList<Pack>();
computePacks(installData.getAvailablePacks());
// private HashMap<String, Pack> names;
for (String key : names.keySet())
{
drawHelper(key, selectedPacks, installData);
}
out(Type.INFORMATION, "Done!");
installData.setSelectedPacks(selectedPacks);
if (selectedPacks.size() == 0)
{
out(Type.WARNING, "You have not selected any packs!");
out(Type.WARNING, "Are you sure you want to continue?");
}
return promptEndPanel(installData, console);
}
private void out(Type type, String message)
{
prompt.message(type, message);
}
/**
* It is used to "draw" the appropriate tree-like structure of the packs and ask if you want to install
* the pack. The pack will automatically be selected if it is required; otherwise you will be prompted if
* you want to install that pack. If a pack is not selected, then their child packs won't be installed as
* well and you won't be prompted to install them.
*
* @param pack - the pack to install
* @param selectedPacks - the packs that are selected by the user are added there
* @param installData - Database of izpack
*/
private void drawHelper(final String pack, final List<Pack> selectedPacks, final InstallData installData)
{
Pack p = names.get(pack);
Boolean conditionSatisfied = checkCondition(installData, p);
//get the pack localized name
String packName = PackHelper.getPackName(p, messages);
final boolean required = p.isRequired();
final boolean packConditionTrue = conditionSatisfied == null || conditionSatisfied.booleanValue();
if (packConditionTrue)
{
// Pack 'condition' not set or 'condition' evaluates true
if (required)
{
// Force selecting the pack
out(Type.INFORMATION, " [x] Pack '" + packName + "' required");
selectedPacks.add(p);
}
else
{
if (askUser(" [x] Include optional pack '" + packName + "'", Option.YES))
{
selectedPacks.add(p);
}
}
}
else
{
// Force not selecting the pack
out(Type.INFORMATION, " [ ] Pack '" + packName + "' not enabled");
}
}
/**
* helper method to know if the condition assigned to the pack is satisfied
*
* @param installData - the data of izpack
* @param pack - the pack whose condition needs to be checked·
* @return true - if the condition is satisfied
* false - if condition not satisfied
* null - if no condition assigned
*/
private Boolean checkCondition(InstallData installData, Pack pack)
{
if (pack.hasCondition())
{
return installData.getRules().isConditionTrue(pack.getCondition());
}
else
{
return null;
}
}
/**
* Helper method to read the input of user
* Method returns true if user types "y", "yes" or <Enter>·
*
* @return boolean - true if condition above satisfied. Otherwise false
*/
private boolean askUser(String message, Option defaultOption)
{
return Option.YES == prompt.confirm(Type.QUESTION, message, Options.YES_NO, defaultOption);
}
/**
* Computes pack related installDataGUI like the names or the dependencies state.
*
* @param packs The list of packs.
*/
private void computePacks(List<Pack> packs)
{
names = new LinkedHashMap<String, Pack>();
for (Pack pack : packs)
{
names.put(pack.getName(), pack);
}
}
@Override
public void createInstallationRecord(IXMLElement panelRoot)
{
new PacksPanelAutomationHelper().createInstallationRecord(installData, panelRoot);
}
}