1
/** BEGIN COPYRIGHT BLOCK
2
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
3
* Copyright (C) 2005 Red Hat, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
* END COPYRIGHT BLOCK **/
20
package com.netscape.admin.dirserv.panel;
23
import java.awt.Color;
24
import java.util.Vector;
25
import java.util.Enumeration;
26
import javax.swing.JComponent;
27
import javax.swing.JTextField;
28
import javax.swing.UIManager;
29
import javax.swing.AbstractButton;
30
import com.netscape.admin.dirserv.*;
31
import com.netscape.management.client.util.*;
34
* A Directory Server attribute must implement a subclass in order
35
* to be able to update itself with its representation stored in the
36
* Directory Server, display that value to the user, allow the user to
37
* edit that value, and store the value back to the Directory Server
42
class DSEntryFile extends DSEntryText {
44
* Extends the functionality of the base DSEntry, specialized to
45
* handle text entry fields which accept file and directory names. One
46
* model value, one widget. This class verifies that the path and/or
49
* @param model the initial value, if any, or just null
50
* @param view the JTextField used as the text entry field
51
* @param isFile sets whether the path should be a file or directory (used when validating)
72
JComponent view1, JComponent view2) {
73
super(model, view1, view2);
79
JComponent view1, JComponent view2, boolean isFile) {
80
super(model, view1, view2);
86
JComponent view1, JComponent view2, boolean isFile, boolean local) {
87
super(model, view1, view2);
92
DSEntryFile(String model,
102
* Change a DOS-style file name to use backslash instead of forward
105
private String uncanonicalize( String filename ) {
106
if ( (filename != null) && (filename.length() >= 2) &&
107
(filename.charAt(1) == ':') ) {
108
filename = filename.replace( '/', '\\' );
114
* several types of validation are done; first, the text field is checked
115
* to see if the user has typed in anything. Next, the file name is checked
116
* to see if it is a directory and returns
117
* true if isFile = false. Then it varifies that the parent of the file
118
* is a directory. If any of these conditions fail,
119
* false is returned, otherwise, true is returned.
121
* @return false if the text field does not contain a valid integer
123
public boolean dsValidate() {
125
JTextField tf = (JTextField)getView(0);
127
String name = uncanonicalize( tf.getText() );
128
File file = new File(name);
129
String err = null; // error number
130
String args[] = null; // additional arguments for error message
135
if (file.isDirectory() && isFile) {
136
// We are only looking for non-directories
137
err = "directoryButNeedFile";
138
args = new String[1];
140
Debug.println("You need to enter a file name as well as " +
141
"a dir: " + file.getPath());
142
} else if (file.isFile() && !isFile) {
143
// Want a directory but got a file
144
err = "fileButNeedDirectory";
145
args = new String[1];
147
Debug.println("\nYou need to enter a directory name: " +
150
String parentName = file.getParent();
152
if ( parentName != null ) {
153
parent = new File( parentName );
155
Debug.println( "DSEntryFile.dsValidate: null parent to " +
158
if ( (parent == null) || !parent.isDirectory() ) {
159
err = "notValidDirectory";
160
args = new String[1];
162
Debug.println("Not a valid directory: " + name);
166
if (err != null) { // popup an error dialog
167
reportError(err, args, tf);
170
return (err == null); // if err was set, there must have been an error
173
public int validate() {
174
JTextField tf = (JTextField)getView(0);
176
String name = uncanonicalize( tf.getText() );
177
File file = new File(name);
180
if ((file.isDirectory() && isFile) ||
181
(file.isFile() && !isFile)) {
184
String parentName = file.getParent();
186
if ( parentName != null )
187
parent = new File( parentName );
188
if ( (parent == null) || !parent.isDirectory() ) {
197
// is a directory when false
198
private boolean isFile = true;
199
private boolean isLocal = true;