~ubuntu-branches/ubuntu/wily/389-ds-console/wily-proposed

« back to all changes in this revision

Viewing changes to src/com/netscape/admin/dirserv/panel/DSEntryFile.java

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-03-15 19:58:37 UTC
  • Revision ID: package-import@ubuntu.com-20120315195837-296zyft51thld8q7
Tags: upstream-1.2.6
ImportĀ upstreamĀ versionĀ 1.2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** BEGIN COPYRIGHT BLOCK
 
2
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 
3
 * Copyright (C) 2005 Red Hat, Inc.
 
4
 * All rights reserved.
 
5
 * 
 
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.
 
9
 * 
 
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.
 
14
 * 
 
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 **/
 
19
 
 
20
package com.netscape.admin.dirserv.panel;
 
21
 
 
22
import java.io.File;
 
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.*;
 
32
 
 
33
/**
 
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
 
38
 *
 
39
 * @version
 
40
 * @author
 
41
 */
 
42
class DSEntryFile extends DSEntryText {
 
43
    /**
 
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
 
47
     * file name is valid.
 
48
     *
 
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)
 
52
     */
 
53
    DSEntryFile(
 
54
        String model,
 
55
        JTextField view,
 
56
        boolean isFile
 
57
    ) {
 
58
        super(model, view);
 
59
        this.isFile = isFile;
 
60
    }
 
61
 
 
62
    DSEntryFile(
 
63
        String model,
 
64
        JTextField view
 
65
    ) {
 
66
        super(model, view);
 
67
        isFile = true;
 
68
    }
 
69
 
 
70
    DSEntryFile(
 
71
        String model,
 
72
        JComponent view1, JComponent view2) {
 
73
        super(model, view1, view2);
 
74
        this.isFile = true;
 
75
    }
 
76
  
 
77
    DSEntryFile(
 
78
        String model,
 
79
        JComponent view1, JComponent view2, boolean isFile) {
 
80
        super(model, view1, view2);
 
81
        this.isFile = isFile;
 
82
    }
 
83
 
 
84
    DSEntryFile(
 
85
        String model,
 
86
        JComponent view1, JComponent view2, boolean isFile, boolean local) {
 
87
        super(model, view1, view2);
 
88
        this.isFile = isFile;
 
89
        this.isLocal = local;
 
90
    }
 
91
 
 
92
        DSEntryFile(String model,
 
93
                                JTextField view1, 
 
94
                                boolean isFile, 
 
95
                                boolean local) {
 
96
                super(model, view1);
 
97
        this.isFile = isFile;
 
98
        this.isLocal = local;
 
99
        }
 
100
 
 
101
        /**
 
102
          * Change a DOS-style file name to use backslash instead of forward
 
103
          * slash
 
104
          */
 
105
        private String uncanonicalize( String filename ) {
 
106
                if ( (filename != null) && (filename.length() >= 2) &&
 
107
                         (filename.charAt(1) == ':') ) {
 
108
                        filename = filename.replace( '/', '\\' );
 
109
                }
 
110
                return filename;
 
111
        }
 
112
 
 
113
    /**
 
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.
 
120
     *
 
121
     * @return false if the text field does not contain a valid integer
 
122
     */
 
123
        public boolean dsValidate() {
 
124
                
 
125
                JTextField tf = (JTextField)getView(0);
 
126
 
 
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
 
131
        
 
132
        if (!isLocal)
 
133
            return true;
 
134
 
 
135
        if (file.isDirectory() && isFile) {
 
136
            // We are only looking for non-directories
 
137
            err = "directoryButNeedFile";
 
138
            args = new String[1];
 
139
            args[0] = name;
 
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];
 
146
            args[0] = name;
 
147
            Debug.println("\nYou need to enter a directory name: " +
 
148
                                                           file.getPath()); 
 
149
        } else {
 
150
            String parentName = file.getParent();
 
151
                        File parent = null;
 
152
                        if ( parentName != null ) {
 
153
                                parent = new File( parentName );
 
154
                        } else {
 
155
                                Debug.println( "DSEntryFile.dsValidate: null parent to " +
 
156
                                                                         name );
 
157
                        }
 
158
                if ( (parent == null) || !parent.isDirectory() ) {
 
159
                    err = "notValidDirectory";
 
160
                    args = new String[1];
 
161
                    args[0] = name;
 
162
                    Debug.println("Not a valid directory: " + name);
 
163
                }
 
164
            }
 
165
            
 
166
        if (err != null) { // popup an error dialog
 
167
            reportError(err, args, tf);
 
168
        }
 
169
        
 
170
                return (err == null); // if err was set, there must have been an error
 
171
        }
 
172
    
 
173
        public int validate() {
 
174
                JTextField tf = (JTextField)getView(0);
 
175
 
 
176
                String name = uncanonicalize( tf.getText() );
 
177
        File file = new File(name);
 
178
        
 
179
        if (isLocal) {
 
180
            if ((file.isDirectory() && isFile) || 
 
181
                (file.isFile() && !isFile)) {
 
182
                 return 1;
 
183
            } else {
 
184
                String parentName = file.getParent();
 
185
                        File parent = null;
 
186
                        if ( parentName != null )
 
187
                                parent = new File( parentName );
 
188
                    if ( (parent == null) || !parent.isDirectory() ) {
 
189
                     return 1;
 
190
                    }
 
191
                }
 
192
        }
 
193
 
 
194
            return 0;    
 
195
        }
 
196
 
 
197
    // is a directory when false
 
198
    private boolean isFile = true;
 
199
    private boolean isLocal = true;
 
200
}