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

Last change on this file since 15210 was 15210, checked in by oranfry, 16 years ago

Lots of changes to the installer. Now only look in LanguagePack resource bundle for strings.

File size: 4.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 */
33public class DirectoryInput
34 extends OSSpecific {
35
36 private static final ResourceBundle res = ResourceBundle.getBundle("resources.LanguagePack");
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) {
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}
Note: See TracBrowser for help on using the repository browser.