KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tools > generator > SelectFileDialog


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.tools.generator;
20
21 import java.io.IOException JavaDoc;
22 import java.io.File JavaDoc;
23 import javax.swing.*;
24 import javax.swing.event.DocumentListener JavaDoc;
25 import javax.swing.event.DocumentEvent JavaDoc;
26 import java.awt.*;
27
28 import org.openide.*;
29 import org.openide.loaders.DataObject;
30 import org.openide.filesystems.*;
31 import org.openide.util.*;
32
33 import org.netbeans.modules.xml.core.lib.GuiUtil;
34
35 /**
36  * Extremely simple dialog with one input line. Invoke using
37  * {@link #getFileObject} method.
38  */

39 public final class SelectFileDialog {
40
41     private Util.NameCheck check;
42
43     private Prompt selectDD;
44     private FileObject folder;
45     private String JavaDoc ext;
46
47     /**
48      *
49      * @param folder parent folder that will host created file
50      * @param name default file name
51      * @param ext default file.extension
52      */

53     public SelectFileDialog (FileObject folder, String JavaDoc name, String JavaDoc ext) {
54
55         this (folder, name, ext, Util.JAVA_CHECK);
56     }
57
58     public SelectFileDialog (FileObject folder, String JavaDoc name, String JavaDoc ext, Util.NameCheck check) {
59         this.folder = folder;
60         this.ext = ext;
61         this.check = check;
62         this.selectDD = new Prompt (
63                 Util.THIS.getString ("PROP_fileNameTitle") + " *." + ext, // NOI18N
64
Util.THIS.getString("PROP_fileName"),
65                 name
66         );
67     }
68
69     /**
70      * Get file object that have user selected
71      * @throws IOException if cancelled or invalid data entered
72      */

73     public FileObject getFileObject () throws IOException JavaDoc {
74         FileObject newFO = null;
75
76         while ( newFO == null ) {
77             DialogDisplayer.getDefault().notify(selectDD);
78             if (selectDD.getValue() != NotifyDescriptor.OK_OPTION) {
79                 throw new UserCancelException();
80             }
81             final String JavaDoc newName = selectDD.getInputText();
82
83             newFO = folder.getFileObject (newName, ext);
84         
85             if ( ( newFO == null ) ||
86                  ( newFO.isVirtual() == true ) ) {
87
88                 FileSystem fs = folder.getFileSystem();
89                 final FileObject tempFile = newFO;
90                 
91                 fs.runAtomicAction (new FileSystem.AtomicAction () {
92                         public void run () throws IOException JavaDoc {
93
94                             if ( ( tempFile != null ) &&
95                                  tempFile.isVirtual() ) {
96                                 tempFile.delete();
97                             }
98
99                             try {
100                                 folder.createData (newName, ext);
101                             } catch (IOException JavaDoc exc) {
102                                 NotifyDescriptor desc = new NotifyDescriptor.Message
103                                     (Util.THIS.getString ("MSG_cannot_create_data", newName + "." + ext), NotifyDescriptor.WARNING_MESSAGE);
104                                 DialogDisplayer.getDefault().notify (desc);
105
106                                 if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (exc);
107                             }
108                         }
109                     });
110                 
111                 newFO = folder.getFileObject (newName, ext);
112
113             } else if (newFO != null) {
114                 DataObject data = DataObject.find(newFO);
115                 if (data.isModified() || data.isValid() == false) {
116                     NotifyDescriptor message = new NotifyDescriptor.Message(Util.THIS.getString("BK0001"), NotifyDescriptor.WARNING_MESSAGE);
117                     DialogDisplayer.getDefault().notify(message);
118                     throw new UserCancelException();
119                 } else if (! GuiUtil.confirmAction (Util.THIS.getString ("PROP_replaceMsg",
120                                                                 newName, ext ) )) {
121                     throw new UserCancelException();
122                 }
123             }
124         } // while
125

126         return newFO;
127     }
128
129
130     /** One input line, verified on content change. */
131     private class Prompt extends NotifyDescriptor.InputLine implements DocumentListener JavaDoc{
132
133         public Prompt(String JavaDoc title, String JavaDoc label, String JavaDoc text) {
134             super(label, title);
135             setInputText(text);
136             textField.getDocument().addDocumentListener(this);
137         }
138
139         public void changedUpdate(DocumentEvent JavaDoc e) {
140             verifyInput();
141         }
142
143         public void insertUpdate(DocumentEvent JavaDoc e) {
144             verifyInput();
145         }
146
147         public void removeUpdate(DocumentEvent JavaDoc e) {
148             verifyInput();
149         }
150
151         private void verifyInput() {
152             String JavaDoc typedText = textField.getText();
153             // no relative paths allowed #24693
154
if (typedText.indexOf(File.separatorChar) != -1) {
155                 selectDD.setValid(false);
156             } else {
157                 selectDD.setValid(check.checkName(typedText));
158             }
159         }
160
161     }
162     
163 }
164
Popular Tags