svn-gvsig-desktop / trunk / build / distribution / IzPack / src / lib / com / izforge / izpack / panels / PathInputPanel.java @ 21757
History | View | Annotate | Download (12.5 KB)
1 |
/*
|
---|---|
2 |
* $Id: PathInputPanel.java 5819 2006-06-14 07:29:09Z cesar $
|
3 |
* IzPack
|
4 |
* Copyright (C) 2004 Klaus Bartz
|
5 |
*
|
6 |
* File : PathInputPanel.java
|
7 |
* Description : A base panel to handle selection of a paths.
|
8 |
* Author's email : bartzkau@users.berlios.de
|
9 |
*
|
10 |
* This program is free software; you can redistribute it and/or
|
11 |
* modify it under the terms of the GNU General Public License
|
12 |
* as published by the Free Software Foundation; either version 2
|
13 |
* of the License, or any later version.
|
14 |
*
|
15 |
* This program is distributed in the hope that it will be useful,
|
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18 |
* GNU General Public License for more details.
|
19 |
*
|
20 |
* You should have received a copy of the GNU General Public License
|
21 |
* along with this program; if not, write to the Free Software
|
22 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
23 |
*/
|
24 |
package com.izforge.izpack.panels; |
25 |
|
26 |
import java.awt.GridBagConstraints; |
27 |
import java.awt.Insets; |
28 |
import java.awt.event.ActionEvent; |
29 |
import java.awt.event.ActionListener; |
30 |
import java.io.BufferedReader; |
31 |
import java.io.File; |
32 |
import java.io.IOException; |
33 |
import java.io.InputStream; |
34 |
import java.io.InputStreamReader; |
35 |
|
36 |
import javax.swing.JLabel; |
37 |
|
38 |
import com.izforge.izpack.installer.InstallData; |
39 |
import com.izforge.izpack.installer.InstallerFrame; |
40 |
import com.izforge.izpack.installer.IzPanel; |
41 |
import com.izforge.izpack.util.*; |
42 |
import com.izforge.izpack.installer.ResourceNotFoundException; |
43 |
|
44 |
/**
|
45 |
* Base class for panels which asks for paths.
|
46 |
*
|
47 |
* @author Klaus Bartz
|
48 |
*
|
49 |
*/
|
50 |
public class PathInputPanel extends IzPanel implements ActionListener |
51 |
{ |
52 |
/** Flag whether the choosen path must exist or not */
|
53 |
protected boolean mustExist = false; |
54 |
|
55 |
/** Files which should be exist */
|
56 |
protected String [] existFiles = null; |
57 |
|
58 |
/** The path which was chosen */
|
59 |
// protected String chosenPath;
|
60 |
|
61 |
/** The path selection sub panel */
|
62 |
protected PathSelectionPanel pathSelectionPanel;
|
63 |
|
64 |
protected String emptyTargetMsg; |
65 |
protected String warnMsg; |
66 |
|
67 |
protected static String defaultInstallDir = null; |
68 |
|
69 |
/**
|
70 |
* The constructor.
|
71 |
*
|
72 |
* @param parent The parent window.
|
73 |
* @param idata The installation data.
|
74 |
*/
|
75 |
public PathInputPanel(InstallerFrame parent, InstallData idata)
|
76 |
{ |
77 |
super(parent, idata);
|
78 |
// Set default values
|
79 |
emptyTargetMsg = getI18nStringForClass( "empty_target", "TargetPanel"); |
80 |
warnMsg = getI18nStringForClass( "warn", "TargetPanel"); |
81 |
//if( this.class.)
|
82 |
|
83 |
// Customize the default GridBagConstraints.
|
84 |
GridBagConstraints gbConstraint = getDefaultGridBagConstraints();
|
85 |
gbConstraint.gridwidth = GridBagConstraints.REMAINDER;
|
86 |
this.setDefaultGridBagConstraints(gbConstraint);
|
87 |
String introText = getI18nStringForClass( "intro", "PathInputPanel"); |
88 |
if( introText == null || introText.equals("PathInputPanel.intro")) |
89 |
introText = "";
|
90 |
// Intro
|
91 |
// Create and customize constraint for it.
|
92 |
// row 0 column 0
|
93 |
gbConstraint = getNextYGridBagConstraints(); |
94 |
// Create component and add it to this panel.
|
95 |
MultiLineLabel introLabel = createMultiLineLabel(introText); |
96 |
add( introLabel, gbConstraint ); |
97 |
// Label for input
|
98 |
// Create and customize constraint for it.
|
99 |
// row 1 column 0; is the next Y
|
100 |
gbConstraint = getNextYGridBagConstraints(); |
101 |
gbConstraint.gridwidth = GridBagConstraints.RELATIVE;
|
102 |
gbConstraint.insets = new Insets(0,0,10,0); |
103 |
// Create component and add it to this panel.
|
104 |
JLabel infoLabel = createLabel(getI18nStringForClass( "info", "TargetPanel"),"open", JLabel.LEFT); |
105 |
add( infoLabel, gbConstraint ); |
106 |
// Create path selection components and add they to this panel.
|
107 |
pathSelectionPanel = new PathSelectionPanel( this, idata); |
108 |
gbConstraint = getNextYGridBagConstraints(); |
109 |
gbConstraint.gridwidth = GridBagConstraints.REMAINDER;
|
110 |
gbConstraint.fill = GridBagConstraints.HORIZONTAL;
|
111 |
gbConstraint.insets = new Insets(0,0,0,0); |
112 |
add(pathSelectionPanel, gbConstraint ); |
113 |
createLayoutBottom(); |
114 |
// Place a footer as last component, if
|
115 |
completeGridBagLayout(); |
116 |
} |
117 |
|
118 |
/**
|
119 |
* This method does nothing. It is
|
120 |
* called from ctor of PathInputPanel, to
|
121 |
* give in a derived class the possibility to add more
|
122 |
* components under the path input
|
123 |
* components.
|
124 |
*/
|
125 |
public void createLayoutBottom() |
126 |
{ |
127 |
} |
128 |
|
129 |
/**
|
130 |
* Actions-handling method.
|
131 |
*
|
132 |
* @param e The event.
|
133 |
*/
|
134 |
public void actionPerformed(ActionEvent e) |
135 |
{ |
136 |
Object source = e.getSource();
|
137 |
if (source == pathSelectionPanel.getPathInputField())
|
138 |
{ |
139 |
parent.navigateNext(); |
140 |
} |
141 |
|
142 |
} |
143 |
/**
|
144 |
* Indicates wether the panel has been validated or not.
|
145 |
*
|
146 |
* @return Wether the panel has been validated or not.
|
147 |
*/
|
148 |
public boolean isValidated() |
149 |
{ |
150 |
String chosenPath = pathSelectionPanel.getPath();
|
151 |
boolean ok = true; |
152 |
|
153 |
// We put a warning if the specified target is nameless
|
154 |
if (chosenPath.length() == 0) |
155 |
{ |
156 |
if(isMustExist())
|
157 |
{ |
158 |
emitError(parent.langpack.getString("installer.error"),
|
159 |
parent.langpack.getString("PathInputPanel.required") );
|
160 |
return false; |
161 |
} |
162 |
else
|
163 |
{ |
164 |
ok = emitWarning( |
165 |
parent.langpack.getString("installer.warning"),
|
166 |
emptyTargetMsg); |
167 |
} |
168 |
} |
169 |
if (!ok)
|
170 |
return ok;
|
171 |
|
172 |
// Normalize the path
|
173 |
File path = new File(chosenPath).getAbsoluteFile(); |
174 |
chosenPath = path.toString(); |
175 |
pathSelectionPanel.setPath(chosenPath); |
176 |
if( isMustExist() )
|
177 |
{ |
178 |
if (! path.exists())
|
179 |
{ |
180 |
emitError(parent.langpack.getString("installer.error"),
|
181 |
parent.langpack.getString(getI18nStringForClass( "required",
|
182 |
"PathInputPanel")) );
|
183 |
return false; |
184 |
} |
185 |
if( !pathIsValid() )
|
186 |
{ |
187 |
emitError(parent.langpack.getString("installer.error"),
|
188 |
parent.langpack.getString(getI18nStringForClass( "notValid",
|
189 |
"PathInputPanel")) );
|
190 |
return false; |
191 |
} |
192 |
} |
193 |
else
|
194 |
{ |
195 |
// We assume, that we would install something into this dir
|
196 |
if( ! isWriteable() )
|
197 |
{ |
198 |
emitError(parent.langpack.getString("installer.error"),
|
199 |
getI18nStringForClass( "notwritable", "TargetPanel")); |
200 |
return false; |
201 |
} |
202 |
// We put a warning if the directory exists else we warn
|
203 |
// that it will be created
|
204 |
if (path.exists())
|
205 |
{ |
206 |
int res =
|
207 |
askQuestion(parent.langpack.getString("installer.warning"),
|
208 |
warnMsg, AbstractUIHandler.CHOICES_YES_NO, AbstractUIHandler.ANSWER_YES); |
209 |
ok = res == AbstractUIHandler.ANSWER_YES; |
210 |
} else
|
211 |
this.emitNotification(getI18nStringForClass( "createdir", |
212 |
"TargetPanel")+ "\n" + chosenPath); |
213 |
} |
214 |
return ok;
|
215 |
} |
216 |
|
217 |
/**
|
218 |
* Returns whether the chosen path is true or not.
|
219 |
* If existFiles are not null, the existence of it
|
220 |
* under the choosen path are detected.
|
221 |
* This method can be also implemented in derived classes
|
222 |
* to handle special verification of the path.
|
223 |
* @return true if existFiles are exist or not defined,
|
224 |
* else false
|
225 |
*/
|
226 |
protected boolean pathIsValid() |
227 |
{ |
228 |
if( existFiles == null) |
229 |
return true; |
230 |
for( int i = 0; i < existFiles.length; ++i) |
231 |
{ |
232 |
File path = new File(pathSelectionPanel.getPath(), existFiles[i]).getAbsoluteFile(); |
233 |
if( ! path.exists())
|
234 |
return false; |
235 |
} |
236 |
return true; |
237 |
} |
238 |
|
239 |
|
240 |
/**
|
241 |
* Returns the must exist state.
|
242 |
* @return the must exist state
|
243 |
*/
|
244 |
public boolean isMustExist() |
245 |
{ |
246 |
return mustExist;
|
247 |
} |
248 |
|
249 |
/**
|
250 |
* Sets the must exist state. If it
|
251 |
* is true, the path must exist.
|
252 |
* @param b must exist state
|
253 |
*/
|
254 |
public void setMustExist(boolean b) |
255 |
{ |
256 |
mustExist = b; |
257 |
} |
258 |
|
259 |
/**
|
260 |
* Returns the array of strings which
|
261 |
* are described the files which must exist.
|
262 |
* @return paths of files which must exist
|
263 |
*/
|
264 |
public String[] getExistFiles() |
265 |
{ |
266 |
return existFiles;
|
267 |
} |
268 |
|
269 |
/**
|
270 |
* Sets the paths of files which must
|
271 |
* exist under the chosen path.
|
272 |
* @param strings paths of files which must
|
273 |
* exist under the chosen path
|
274 |
*/
|
275 |
public void setExistFiles(String[] strings) |
276 |
{ |
277 |
existFiles = strings; |
278 |
} |
279 |
/**
|
280 |
* Loads up the "dir" resource associated with TargetPanel. Acceptable dir
|
281 |
* resource names: <code>
|
282 |
* TargetPanel.dir.macosx
|
283 |
* TargetPanel.dir.mac
|
284 |
* TargetPanel.dir.windows
|
285 |
* TargetPanel.dir.unix
|
286 |
* TargetPanel.dir.xxx,
|
287 |
* where xxx is the lower case version of System.getProperty("os.name"),
|
288 |
* with any spaces replace with underscores
|
289 |
* TargetPanel.dir (generic that will be applied if none of above is found)
|
290 |
* </code> As with all IzPack resources, each the above ids should be
|
291 |
* associated with a separate filename, which is set in the install.xml file
|
292 |
* at compile time.
|
293 |
*/
|
294 |
public static void loadDefaultInstallDir(InstallerFrame parentFrame) |
295 |
{ |
296 |
// Load only once ...
|
297 |
if( getDefaultInstallDir() != null) |
298 |
return;
|
299 |
BufferedReader br = null; |
300 |
try
|
301 |
{ |
302 |
InputStream in = null; |
303 |
|
304 |
if (OsVersion.IS_WINDOWS)
|
305 |
in = parentFrame.getResource("TargetPanel.dir.windows");
|
306 |
|
307 |
else if (OsVersion.IS_OSX) |
308 |
in = parentFrame.getResource("TargetPanel.dir.macosx");
|
309 |
else
|
310 |
{ |
311 |
String os = System.getProperty("os.name"); |
312 |
// first try to look up by specific os name
|
313 |
os = os.replace(' ', '_'); // avoid spaces in file names |
314 |
os = os.toLowerCase(); // for consistency among TargetPanel res files
|
315 |
try {
|
316 |
in = parentFrame.getResource("TargetPanel.dir.".concat(os));
|
317 |
} |
318 |
catch (ResourceNotFoundException rnfe) {}
|
319 |
// if not specific os, try getting generic 'unix' resource file
|
320 |
if (in == null) |
321 |
in = parentFrame.getResource("TargetPanel.dir.unix");
|
322 |
|
323 |
// if all those failed, try to look up a generic dir file
|
324 |
if (in == null) { |
325 |
try {
|
326 |
in = parentFrame.getResource("TargetPanel.dir.unix");
|
327 |
} |
328 |
catch (ResourceNotFoundException eee) {}
|
329 |
} |
330 |
|
331 |
} |
332 |
|
333 |
// if all above tests failed, there is no resource file,
|
334 |
// so use system default
|
335 |
if (in == null) { |
336 |
try {
|
337 |
in = parentFrame.getResource("TargetPanel.dir");
|
338 |
} |
339 |
catch (ResourceNotFoundException eee) {}
|
340 |
} |
341 |
|
342 |
// now read the file, once we've identified which one to read
|
343 |
InputStreamReader isr = new InputStreamReader(in); |
344 |
br = new BufferedReader(isr); |
345 |
String line;
|
346 |
while ((line = br.readLine()) != null) |
347 |
{ |
348 |
line = line.trim(); |
349 |
// use the first non-blank line
|
350 |
if (!line.equals("")) |
351 |
break;
|
352 |
} |
353 |
defaultInstallDir = line; |
354 |
} catch (Exception e) |
355 |
{ |
356 |
defaultInstallDir = null;
|
357 |
// leave unset to take the system default set by Installer class
|
358 |
} finally
|
359 |
{ |
360 |
try
|
361 |
{ |
362 |
if (br != null) |
363 |
br.close(); |
364 |
} catch (IOException ignored) |
365 |
{ |
366 |
} |
367 |
} |
368 |
} |
369 |
|
370 |
/**
|
371 |
* This method determines whether the chosen dir
|
372 |
* is writeable or not.
|
373 |
* @return whether the chosen dir is writeable or not
|
374 |
*/
|
375 |
public boolean isWriteable() |
376 |
{ |
377 |
File existParent = IoHelper.existingParent(
|
378 |
new File(pathSelectionPanel.getPath())); |
379 |
if( existParent == null) |
380 |
return false; |
381 |
// On windows we cannot use canWrite because
|
382 |
// it looks to the dos flags which are not valid
|
383 |
// on NT or 2k XP or ...
|
384 |
if( OsVersion.IS_WINDOWS )
|
385 |
{ |
386 |
File tmpFile;
|
387 |
try
|
388 |
{ |
389 |
tmpFile = File.createTempFile("izWrTe", ".tmp", existParent); |
390 |
tmpFile.deleteOnExit(); |
391 |
} |
392 |
catch (IOException e) |
393 |
{ |
394 |
Debug.trace( e.toString() ); |
395 |
return false; |
396 |
} |
397 |
return true; |
398 |
} |
399 |
else
|
400 |
return existParent.canWrite();
|
401 |
} |
402 |
|
403 |
/**
|
404 |
* Returns the default for the installation directory.
|
405 |
* @return the default for the installation directory
|
406 |
*/
|
407 |
public static String getDefaultInstallDir() |
408 |
{ |
409 |
return defaultInstallDir;
|
410 |
} |
411 |
|
412 |
/**
|
413 |
* Sets the default for the installation directory to the
|
414 |
* given string.
|
415 |
* @param string path for default for the installation directory
|
416 |
*/
|
417 |
public static void setDefaultInstallDir(String string) |
418 |
{ |
419 |
defaultInstallDir = string; |
420 |
} |
421 |
|
422 |
|
423 |
} |