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 |
|
---|
34 | public 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 | return false;
|
---|
73 | }
|
---|
74 | } else {
|
---|
75 | //make them stay on the page until they manage to select an existent folder
|
---|
76 | return false;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | catch (Exception ex) {
|
---|
80 | mr.printMessage(org.tp23.antinstaller.Installer.langPack.getString("canNotCreateFile") + "\n" + file.getAbsolutePath());
|
---|
81 | //FIXME should not throw here, should do something better so users on linux can chmod where necessary
|
---|
82 | // then try again
|
---|
83 | throw new ValidationException(org.tp23.antinstaller.Installer.langPack.getString("canNotCreateFile"),ex);
|
---|
84 |
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | if(InputField.isTrue(checkExists)){
|
---|
89 | // if( ( !file.exists() || !file.isDirectory() ) && triedToCreate){
|
---|
90 | // //TODO add some usefull text here to explain that we can not continue
|
---|
91 | // }
|
---|
92 | if(!file.exists() || !file.isDirectory()){
|
---|
93 | mr.printMessage(org.tp23.antinstaller.Installer.langPack.getString("dirNotExist") + "\n" + file.getAbsolutePath());
|
---|
94 | return false;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | return true;
|
---|
98 | }
|
---|
99 |
|
---|
100 | public boolean isAbort() {
|
---|
101 | return abort;
|
---|
102 | }
|
---|
103 |
|
---|
104 | public void setAbort(boolean abort) {
|
---|
105 | this.abort = abort;
|
---|
106 | }
|
---|
107 |
|
---|
108 | public String getCreate() {
|
---|
109 | return create;
|
---|
110 | }
|
---|
111 | public void setCreate(String create) {
|
---|
112 | this.create = create;
|
---|
113 | }
|
---|
114 | public void setValue(String dir){
|
---|
115 | setInputResult(dir);
|
---|
116 | }
|
---|
117 |
|
---|
118 | public String getCheckExists() {
|
---|
119 | return checkExists;
|
---|
120 | }
|
---|
121 |
|
---|
122 | public void setCheckExists(String checkExists) {
|
---|
123 | this.checkExists = checkExists;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public String getFlagFile() {
|
---|
127 | return flagFile;
|
---|
128 | }
|
---|
129 |
|
---|
130 | public void setFlagFile(String flagFile) {
|
---|
131 | this.flagFile = flagFile;
|
---|
132 | }
|
---|
133 |
|
---|
134 | public String getFlagFileExistsProperty() {
|
---|
135 | return flagFileExistsProperty;
|
---|
136 | }
|
---|
137 |
|
---|
138 | public void setFlagFileExistsProperty( String flagFileExistsProperty ) {
|
---|
139 | this.flagFileExistsProperty = flagFileExistsProperty;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Used by checkConfig to validate the configuration file.
|
---|
144 | * Not used at runtime.
|
---|
145 | * @return boolean
|
---|
146 | */
|
---|
147 | public boolean validateObject() {
|
---|
148 | if(getDisplayText() == null){
|
---|
149 | System.out.println("Directory:displayText must be set");
|
---|
150 | return false;
|
---|
151 | }
|
---|
152 | if(getProperty() == null){
|
---|
153 | System.out.println("Directory:property must be set");
|
---|
154 | return false;
|
---|
155 | }
|
---|
156 | if(getDefaultValue() == null){
|
---|
157 | System.out.println("Directory:defaultValue must be set");
|
---|
158 | return false;
|
---|
159 | }
|
---|
160 | if(getDefaultValue().equals("")){
|
---|
161 | if( isTrue(getCreate()) || isTrue(getCheckExists()) ) {
|
---|
162 | System.out.println("Directory:defaultValue must be set if checkExists or create are true");
|
---|
163 | return false;
|
---|
164 | }
|
---|
165 | }
|
---|
166 | if(!InputField.optionalBoolean(getCreate())){
|
---|
167 | System.out.println("Directory:create must be true or false or null");
|
---|
168 | return false;
|
---|
169 | }
|
---|
170 | if(!InputField.optionalBoolean(getCheckExists())){
|
---|
171 | System.out.println("Directory:checkExists must be true or false or null");
|
---|
172 | return false;
|
---|
173 | }
|
---|
174 | return true;
|
---|
175 | }
|
---|
176 |
|
---|
177 | public void setInputResult(String inputResult) {
|
---|
178 | resultContainer.setProperty(getProperty(), inputResult);
|
---|
179 |
|
---|
180 | if ( flagFile != null ) {
|
---|
181 | File ff = new File( inputResult + File.separator + flagFile );
|
---|
182 | Boolean ffExists = new Boolean(ff.exists());
|
---|
183 | resultContainer.setProperty( flagFileExistsProperty, ffExists.toString() );
|
---|
184 | }
|
---|
185 |
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | }
|
---|