1 19 package org.netbeans.modules.xml.tools.generator; 20 21 import java.io.IOException ; 22 import java.io.File ; 23 import javax.swing.*; 24 import javax.swing.event.DocumentListener ; 25 import javax.swing.event.DocumentEvent ; 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 39 public final class SelectFileDialog { 40 41 private Util.NameCheck check; 42 43 private Prompt selectDD; 44 private FileObject folder; 45 private String ext; 46 47 53 public SelectFileDialog (FileObject folder, String name, String ext) { 54 55 this (folder, name, ext, Util.JAVA_CHECK); 56 } 57 58 public SelectFileDialog (FileObject folder, String name, String 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, Util.THIS.getString("PROP_fileName"), 65 name 66 ); 67 } 68 69 73 public FileObject getFileObject () throws IOException { 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 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 { 93 94 if ( ( tempFile != null ) && 95 tempFile.isVirtual() ) { 96 tempFile.delete(); 97 } 98 99 try { 100 folder.createData (newName, ext); 101 } catch (IOException 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() ) 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 } 126 return newFO; 127 } 128 129 130 131 private class Prompt extends NotifyDescriptor.InputLine implements DocumentListener { 132 133 public Prompt(String title, String label, String text) { 134 super(label, title); 135 setInputText(text); 136 textField.getDocument().addDocumentListener(this); 137 } 138 139 public void changedUpdate(DocumentEvent e) { 140 verifyInput(); 141 } 142 143 public void insertUpdate(DocumentEvent e) { 144 verifyInput(); 145 } 146 147 public void removeUpdate(DocumentEvent e) { 148 verifyInput(); 149 } 150 151 private void verifyInput() { 152 String typedText = textField.getText(); 153 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 |