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 | */
|
---|
16 | package org.tp23.antinstaller.input;
|
---|
17 |
|
---|
18 | import java.io.File;
|
---|
19 | import java.util.ResourceBundle;
|
---|
20 |
|
---|
21 | import org.tp23.antinstaller.InstallerContext;
|
---|
22 | import org.tp23.antinstaller.ValidationException;
|
---|
23 | import 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 | */
|
---|
33 | public class DirectoryInput
|
---|
34 | extends OSSpecific {
|
---|
35 |
|
---|
36 | private boolean abort = false;
|
---|
37 | private String create;
|
---|
38 | private String checkExists;
|
---|
39 |
|
---|
40 | public DirectoryInput() {
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Called to validate the user input
|
---|
45 | */
|
---|
46 | public boolean validate(InstallerContext cxt) throws ValidationException{
|
---|
47 | if (getInputResult() == null)return false;
|
---|
48 | MessageRenderer mr = cxt.getMessageRenderer();
|
---|
49 | String selectedName = getInputResult();
|
---|
50 | // handle no directory option
|
---|
51 | if( "".equals(selectedName) ){
|
---|
52 | if( InputField.isTrue(create) || InputField.isTrue(checkExists) ){
|
---|
53 | mr.printMessage(org.tp23.antinstaller.Installer.langPack.getString("dirNotExist"));
|
---|
54 | return false;
|
---|
55 | }
|
---|
56 | else {
|
---|
57 | return true;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | File file = new File(selectedName);
|
---|
61 | if(InputField.isTrue(create)){
|
---|
62 | if(!file.exists()){
|
---|
63 | try {
|
---|
64 | if(mr.prompt(org.tp23.antinstaller.Installer.langPack.getString("dirNotExistCreate") + "\n" + file.getAbsolutePath())){
|
---|
65 | boolean ok = file.mkdirs();
|
---|
66 | if(!ok){
|
---|
67 | mr.printMessage(org.tp23.antinstaller.Installer.langPack.getString("dirNotCreated"));
|
---|
68 | }
|
---|
69 | } else {
|
---|
70 | //make them stay on the page until they manage to select an existent folder
|
---|
71 | return false;
|
---|
72 | }
|
---|
73 | }
|
---|
74 | catch (Exception ex) {
|
---|
75 | mr.printMessage(org.tp23.antinstaller.Installer.langPack.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(org.tp23.antinstaller.Installer.langPack.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(org.tp23.antinstaller.Installer.langPack.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) {
|
---|
100 | this.abort = abort;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public String getCreate() {
|
---|
104 | return create;
|
---|
105 | }
|
---|
106 | public void setCreate(String create) {
|
---|
107 | this.create = create;
|
---|
108 | }
|
---|
109 | public void setValue(String dir){
|
---|
110 | setInputResult(dir);
|
---|
111 | }
|
---|
112 |
|
---|
113 | public String getCheckExists() {
|
---|
114 | return checkExists;
|
---|
115 | }
|
---|
116 |
|
---|
117 | public void setCheckExists(String checkExists) {
|
---|
118 | this.checkExists = checkExists;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Used by checkConfig to validate the configuration file.
|
---|
123 | * Not used at runtime.
|
---|
124 | * @return boolean
|
---|
125 | */
|
---|
126 | public boolean validateObject() {
|
---|
127 | if(getDisplayText() == null){
|
---|
128 | System.out.println("Directory:displayText must be set");
|
---|
129 | return false;
|
---|
130 | }
|
---|
131 | if(getProperty() == null){
|
---|
132 | System.out.println("Directory:property must be set");
|
---|
133 | return false;
|
---|
134 | }
|
---|
135 | if(getDefaultValue() == null){
|
---|
136 | System.out.println("Directory:defaultValue must be set");
|
---|
137 | return false;
|
---|
138 | }
|
---|
139 | if(getDefaultValue().equals("")){
|
---|
140 | if( isTrue(getCreate()) || isTrue(getCheckExists()) ) {
|
---|
141 | System.out.println("Directory:defaultValue must be set if checkExists or create are true");
|
---|
142 | return false;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | if(!InputField.optionalBoolean(getCreate())){
|
---|
146 | System.out.println("Directory:create must be true or false or null");
|
---|
147 | return false;
|
---|
148 | }
|
---|
149 | if(!InputField.optionalBoolean(getCheckExists())){
|
---|
150 | System.out.println("Directory:checkExists must be true or false or null");
|
---|
151 | return false;
|
---|
152 | }
|
---|
153 | return true;
|
---|
154 | }
|
---|
155 |
|
---|
156 | }
|
---|