source: release-kits/shared/ant-installer/src/org/tp23/antinstaller/input/DirectoryInput.java@ 17622

Last change on this file since 17622 was 17622, checked in by oranfry, 15 years ago

extra options to directory chooser and page

File size: 5.4 KB
Line 
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 */
33
34public class DirectoryInput extends OSSpecific {
35
36 private boolean abort = false;
37 private String create;
38 private String checkExists;
39
40 //to detect previous installations
41 private String flagFile;
42 private String flagFileExistsProperty;
43
44 public DirectoryInput() {
45 }
46
47 /**
48 * Called to validate the user input
49 */
50 public boolean validate(InstallerContext cxt) throws ValidationException{
51 if (getInputResult() == null)return false;
52 MessageRenderer mr = cxt.getMessageRenderer();
53 String selectedName = getInputResult();
54 // handle no directory option
55 if( "".equals(selectedName) ){
56 if( InputField.isTrue(create) || InputField.isTrue(checkExists) ){
57 mr.printMessage(org.tp23.antinstaller.Installer.langPack.getString("dirNotExist"));
58 return false;
59 }
60 else {
61 return true;
62 }
63 }
64 File file = new File(selectedName);
65 if(InputField.isTrue(create)){
66 if(!file.exists()){
67 try {
68 if(mr.prompt(org.tp23.antinstaller.Installer.langPack.getString("dirNotExistCreate") + "\n" + file.getAbsolutePath())){
69 boolean ok = file.mkdirs();
70 if(!ok){
71 mr.printMessage(org.tp23.antinstaller.Installer.langPack.getString("dirNotCreated"));
72 }
73 } else {
74 //make them stay on the page until they manage to select an existent folder
75 return false;
76 }
77 }
78 catch (Exception ex) {
79 mr.printMessage(org.tp23.antinstaller.Installer.langPack.getString("canNotCreateFile") + "\n" + file.getAbsolutePath());
80 //FIXME should not throw here, should do something better so users on linux can chmod where necessary
81 // then try again
82 throw new ValidationException(org.tp23.antinstaller.Installer.langPack.getString("canNotCreateFile"),ex);
83
84 }
85 }
86 }
87 if(InputField.isTrue(checkExists)){
88// if( ( !file.exists() || !file.isDirectory() ) && triedToCreate){
89// //TODO add some usefull text here to explain that we can not continue
90// }
91 if(!file.exists() || !file.isDirectory()){
92 mr.printMessage(org.tp23.antinstaller.Installer.langPack.getString("dirNotExist") + "\n" + file.getAbsolutePath());
93 return false;
94 }
95 }
96 return true;
97 }
98
99 public boolean isAbort() {
100 return abort;
101 }
102
103 public void setAbort(boolean abort) {
104 this.abort = abort;
105 }
106
107 public String getCreate() {
108 return create;
109 }
110 public void setCreate(String create) {
111 this.create = create;
112 }
113 public void setValue(String dir){
114 setInputResult(dir);
115 }
116
117 public String getCheckExists() {
118 return checkExists;
119 }
120
121 public void setCheckExists(String checkExists) {
122 this.checkExists = checkExists;
123 }
124
125 public String getFlagFile() {
126 return flagFile;
127 }
128
129 public void setFlagFile(String flagFile) {
130 this.flagFile = flagFile;
131 }
132
133 public String getFlagFileExistsProperty() {
134 return flagFileExistsProperty;
135 }
136
137 public void setFlagFileExistsProperty( String flagFileExistsProperty ) {
138 this.flagFileExistsProperty = flagFileExistsProperty;
139 }
140
141 /**
142 * Used by checkConfig to validate the configuration file.
143 * Not used at runtime.
144 * @return boolean
145 */
146 public boolean validateObject() {
147 if(getDisplayText() == null){
148 System.out.println("Directory:displayText must be set");
149 return false;
150 }
151 if(getProperty() == null){
152 System.out.println("Directory:property must be set");
153 return false;
154 }
155 if(getDefaultValue() == null){
156 System.out.println("Directory:defaultValue must be set");
157 return false;
158 }
159 if(getDefaultValue().equals("")){
160 if( isTrue(getCreate()) || isTrue(getCheckExists()) ) {
161 System.out.println("Directory:defaultValue must be set if checkExists or create are true");
162 return false;
163 }
164 }
165 if(!InputField.optionalBoolean(getCreate())){
166 System.out.println("Directory:create must be true or false or null");
167 return false;
168 }
169 if(!InputField.optionalBoolean(getCheckExists())){
170 System.out.println("Directory:checkExists must be true or false or null");
171 return false;
172 }
173 return true;
174 }
175
176 public void setInputResult(String inputResult) {
177 resultContainer.setProperty(getProperty(), inputResult);
178
179 if ( flagFile != null ) {
180 File ff = new File( inputResult + File.separator + flagFile );
181 Boolean ffExists = new Boolean(ff.exists());
182 resultContainer.setProperty( flagFileExistsProperty, ffExists.toString() );
183 }
184
185 }
186
187
188}
Note: See TracBrowser for help on using the repository browser.