1 /* 
2  * Copyright 2005 Paul Hinds
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
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/**
26 * <p>Input type to select a directory </p>
27 * <p> </p>
28 * <p>Copyright: Copyright (c) 2004</p>
29 * <p>Company: tp23</p>
30 * @author Paul Hinds
31 * @version $Id: DirectoryInput.java,v 1.5 2007/01/28 10:25:48 teknopaul Exp $
32 */
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    /**
47     * Called to validate the user input
48     */
49    public boolean validate(InstallerContext cxt) throws ValidationException{
50        if (getInputResult() == null)return false;
51        MessageRenderer mr = cxt.getMessageRenderer();
52        String selectedName = getInputResult();
53        // handle no directory option
54        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                    //FIXME should not throw here, should do something better so users on linux can chmod where necessary
77                    // then try again
78                    throw new ValidationException(res.getString("canNotCreateFile"),ex);
79                    
80                }
81            }
82        }
83        if(InputField.isTrue(checkExists)){
84//          if( ( !file.exists() || !file.isDirectory() ) && triedToCreate){
85//              //TODO add some usefull text here to explain that we can not continue
86//          }
87            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    /**
22     * Used by checkConfig to validate the configuration file.
23     * Not used at runtime.
24     * @return boolean
25     */
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