1
16package org.tp23.antinstaller.input;
17
18import java.io.File;
19import java.util.ResourceBundle;
20
21import org.tp23.antinstaller.InstallerContext;
22import org.tp23.antinstaller.ValidationException;
23import org.tp23.antinstaller.renderer.MessageRenderer;
24
25
33public class DirectoryInput
34 extends OSSpecific {
35
36 private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.Res");
37
38
39 private boolean abort = false;
40 private String create;
41 private String checkExists;
42
43 public DirectoryInput() {
44 }
45
46
49 public boolean validate(InstallerContext cxt) throws ValidationException{
50 if (getInputResult() == null)return false;
51 MessageRenderer mr = cxt.getMessageRenderer();
52 String selectedName = getInputResult();
53 if( "".equals(selectedName) ){
55 if( InputField.isTrue(create) || InputField.isTrue(checkExists) ){
56 mr.printMessage(res.getString("dirNotExist"));
57 return false;
58 }
59 else {
60 return true;
61 }
62 }
63 File file = new File(selectedName);
64 if(InputField.isTrue(create)){
65 if(!file.exists()){
66 try {
67 if(mr.prompt(res.getString("dirNotExistCreate") + "\n" + file.getAbsolutePath())){
68 boolean ok = file.mkdirs();
69 if(!ok){
70 mr.printMessage(res.getString("dirNotCreated"));
71 }
72 }
73 }
74 catch (Exception ex) {
75 mr.printMessage(res.getString("canNotCreateFile") + "\n" + file.getAbsolutePath());
76 throw new ValidationException(res.getString("canNotCreateFile"),ex);
79
80 }
81 }
82 }
83 if(InputField.isTrue(checkExists)){
84 if(!file.exists() || !file.isDirectory()){
88 mr.printMessage(res.getString("dirNotExist") + "\n" + file.getAbsolutePath());
89 return false;
90 }
91 }
92 return true;
93 }
94
95 public boolean isAbort() {
96 return abort;
97 }
98
99 public void setAbort(boolean abort) {
00 this.abort = abort;
01 }
02
03 public String getCreate() {
04 return create;
05 }
06 public void setCreate(String create) {
07 this.create = create;
08 }
09 public void setValue(String dir){
10 setInputResult(dir);
11 }
12
13 public String getCheckExists() {
14 return checkExists;
15 }
16
17 public void setCheckExists(String checkExists) {
18 this.checkExists = checkExists;
19 }
20
21
26 public boolean validateObject() {
27 if(getDisplayText() == null){
28 System.out.println("Directory:displayText must be set");
29 return false;
30 }
31 if(getProperty() == null){
32 System.out.println("Directory:property must be set");
33 return false;
34 }
35 if(getDefaultValue() == null){
36 System.out.println("Directory:defaultValue must be set");
37 return false;
38 }
39 if(getDefaultValue().equals("")){
40 if( isTrue(getCreate()) || isTrue(getCheckExists()) ) {
41 System.out.println("Directory:defaultValue must be set if checkExists or create are true");
42 return false;
43 }
44 }
45 if(!InputField.optionalBoolean(getCreate())){
46 System.out.println("Directory:create must be true or false or null");
47 return false;
48 }
49 if(!InputField.optionalBoolean(getCheckExists())){
50 System.out.println("Directory:checkExists must be true or false or null");
51 return false;
52 }
53 return true;
54 }
55
56}
57