001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.input;
017:
018: import java.io.File;
019:
020: import org.tp23.antinstaller.InstallerContext;
021: import org.tp23.antinstaller.ValidationException;
022: import org.tp23.antinstaller.renderer.AIResourceBundle;
023: import org.tp23.antinstaller.renderer.MessageRenderer;
024:
025: /**
026: * <p>Input type to select a directory </p>
027: * <p> </p>
028: * <p>Copyright: Copyright (c) 2004</p>
029: * <p>Company: tp23</p>
030: * @author Paul Hinds
031: * @version $Id: DirectoryInput.java,v 1.5 2007/01/28 10:25:48 teknopaul Exp $
032: */
033: public class DirectoryInput extends OSSpecific {
034:
035: private static final AIResourceBundle res = new AIResourceBundle();
036:
037: private boolean abort = false;
038: private String create;
039: private String checkExists;
040:
041: public DirectoryInput() {
042: }
043:
044: /**
045: * Called to validate the user input
046: */
047: public boolean validate(InstallerContext cxt)
048: throws ValidationException {
049: if (getInputResult() == null)
050: return false;
051: MessageRenderer mr = cxt.getMessageRenderer();
052: String selectedName = getInputResult();
053: // handle no directory option
054: if ("".equals(selectedName)) {
055: if (InputField.isTrue(create)
056: || InputField.isTrue(checkExists)) {
057: mr.printMessage(res.getString("dir.not.exist"));
058: return false;
059: } else {
060: return true;
061: }
062: }
063: File file = new File(selectedName);
064: if (InputField.isTrue(create)) {
065: if (!file.exists()) {
066: try {
067: if (mr.prompt(res.getString("dir.not.exist.create")
068: + "\n" + file.getAbsolutePath())) {
069: boolean ok = file.mkdirs();
070: if (!ok) {
071: mr.printMessage(res
072: .getString("dir.not.created"));
073: }
074: }
075: } catch (Exception ex) {
076: mr.printMessage(res
077: .getString("can.not.create.file")
078: + "\n" + file.getAbsolutePath());
079: //FIXME should not throw here, should do something better so users on linux can chmod where necessary
080: // then try again
081: throw new ValidationException(res
082: .getString("can.not.create.file"), ex);
083:
084: }
085: }
086: }
087: if (InputField.isTrue(checkExists)) {
088: // if( ( !file.exists() || !file.isDirectory() ) && triedToCreate){
089: // //TODO add some usefull text here to explain that we can not continue
090: // }
091: if (!file.exists() || !file.isDirectory()) {
092: mr.printMessage(res.getString("dir.not.exist") + "\n"
093: + file.getAbsolutePath());
094: return false;
095: }
096: }
097: return true;
098: }
099:
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:
112: public void setCreate(String create) {
113: this .create = create;
114: }
115:
116: public void setValue(String dir) {
117: setInputResult(dir);
118: }
119:
120: public String getCheckExists() {
121: return checkExists;
122: }
123:
124: public void setCheckExists(String checkExists) {
125: this .checkExists = checkExists;
126: }
127:
128: /**
129: * Used by checkConfig to validate the configuration file.
130: * Not used at runtime.
131: * @return boolean
132: */
133: public boolean validateObject() {
134: if (getDisplayText() == null) {
135: System.out.println("Directory:displayText must be set");
136: return false;
137: }
138: if (getProperty() == null) {
139: System.out.println("Directory:property must be set");
140: return false;
141: }
142: if (getDefaultValue() == null) {
143: System.out.println("Directory:defaultValue must be set");
144: return false;
145: }
146: if (getDefaultValue().equals("")) {
147: if (isTrue(getCreate()) || isTrue(getCheckExists())) {
148: System.out
149: .println("Directory:defaultValue must be set if checkExists or create are true");
150: return false;
151: }
152: }
153: if (!InputField.optionalBoolean(getCreate())) {
154: System.out
155: .println("Directory:create must be true or false or null");
156: return false;
157: }
158: if (!InputField.optionalBoolean(getCheckExists())) {
159: System.out
160: .println("Directory:checkExists must be true or false or null");
161: return false;
162: }
163: return true;
164: }
165:
166: }
|