1 19 20 package org.netbeans.modules.project.ui; 21 22 import java.awt.Dialog ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.util.ArrayList ; 28 import javax.swing.JButton ; 29 import javax.swing.event.DocumentListener ; 30 import org.openide.DialogDescriptor; 31 import org.openide.DialogDisplayer; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.filesystems.Repository; 35 import org.openide.loaders.DataObject; 36 import org.openide.loaders.DataFolder; 37 import org.openide.loaders.DataObjectNotFoundException; 38 import org.openide.util.NbBundle; 39 40 41 45 public class NoProjectNew extends javax.swing.JPanel implements ActionListener , DocumentListener { 46 47 public static final int TYPE_FILE = 0; 48 public static final int TYPE_FOLDER = 1; 49 50 private static final String FILE_NAME = NbBundle.getMessage( NoProjectNew.class, "LBL_NonProject_File_Name" ); private static final String FILE_TITLE = NbBundle.getMessage( NoProjectNew.class, "LBL_NonProject_File_Title" ); private static final String FILE_TEXT_FIELD_ACD = NbBundle.getMessage( NoProjectNew.class, "ACD_NonProject_File_TextField" ); private static final String FILE_DIALOG_ACD = NbBundle.getMessage( NoProjectNew.class, "ACD_NonProject_File_Dialog" ); private static final String FOLDER_NAME = NbBundle.getMessage( NoProjectNew.class, "LBL_NonProject_Folder_Name" ); private static final String FOLDER_TITLE = NbBundle.getMessage( NoProjectNew.class, "LBL_NonProject_Folder_Title" ); private static final String FOLDER_TEXT_FIELD_ACD = NbBundle.getMessage( NoProjectNew.class, "ACD_NonProject_Folder_TextField" ); private static final String FOLDER_DIALOG_ACD = NbBundle.getMessage( NoProjectNew.class, "ACD_NonProject_Folder_Dialog" ); 59 public static final String COMMAND_OK = "OK"; 60 public static final String COMMAND_CANCEL = "CANCEL"; 61 62 private static DataObject[] templates; 63 64 private int type; 65 private DataFolder targetFolder; 66 private String result; 67 private JButton okOption; 68 69 70 public NoProjectNew( int type, DataFolder targetFolder, JButton okOption ) { 71 initComponents(); 72 nameTextField.getDocument().addDocumentListener( this ); 73 74 this.type = type; 75 this.targetFolder = targetFolder; 76 this.okOption = okOption; 77 78 switch ( type ) { 79 case TYPE_FILE: 80 org.openide.awt.Mnemonics.setLocalizedText(nameLabel, FILE_NAME); 81 nameTextField.getAccessibleContext().setAccessibleDescription(FILE_TEXT_FIELD_ACD); 82 getAccessibleContext().setAccessibleDescription(FILE_DIALOG_ACD); 83 break; 84 case TYPE_FOLDER: 85 org.openide.awt.Mnemonics.setLocalizedText(nameLabel, FOLDER_NAME); 86 nameTextField.getAccessibleContext().setAccessibleDescription(FOLDER_TEXT_FIELD_ACD); 87 getAccessibleContext().setAccessibleDescription(FOLDER_DIALOG_ACD); 88 break; 89 } 90 91 this.okOption.setEnabled( false ); 92 93 } 94 95 100 private void initComponents() { 102 java.awt.GridBagConstraints gridBagConstraints; 103 104 nameLabel = new javax.swing.JLabel (); 105 nameTextField = new javax.swing.JTextField (); 106 107 setLayout(new java.awt.GridBagLayout ()); 108 109 setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 12, 12)); 110 nameLabel.setLabelFor(nameTextField); 111 nameLabel.setText("Folders:"); 112 gridBagConstraints = new java.awt.GridBagConstraints (); 113 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 114 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 115 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 6); 116 add(nameLabel, gridBagConstraints); 117 118 nameTextField.setColumns(25); 119 gridBagConstraints = new java.awt.GridBagConstraints (); 120 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 121 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 122 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 123 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 124 gridBagConstraints.weightx = 1.0; 125 add(nameTextField, gridBagConstraints); 126 127 } 128 130 131 public javax.swing.JLabel nameLabel; 133 public javax.swing.JTextField nameTextField; 134 136 public static void showDialog( DataObject template, DataFolder targetFolder ) { 137 138 int type; 139 if ( template.getPrimaryFile().getName().equals( "file") ) { 140 type = TYPE_FILE; 141 } 142 else { 143 type = TYPE_FOLDER; 144 } 145 146 JButton options[] = new JButton [] { 147 new JButton ( NbBundle.getMessage( NoProjectNew.class, "LBL_NonProject_OK_Button") ), new JButton ( NbBundle.getMessage( NoProjectNew.class, "LBL_NonProject_Cancel_Button") ), }; 150 151 NoProjectNew npn = new NoProjectNew( type, targetFolder, options[0] ); 152 153 options[ 0 ].setActionCommand( COMMAND_OK ); 154 options[ 0 ].addActionListener( npn ); 155 options[ 1 ].setActionCommand( COMMAND_CANCEL ); 156 options[ 1 ].addActionListener( npn ); 157 158 DialogDescriptor dialogDescriptor = new DialogDescriptor( 159 npn, type == TYPE_FILE ? FILE_TITLE : FOLDER_TITLE, true, options, options[ 0 ], DialogDescriptor.BOTTOM_ALIGN, null, null ); 168 dialogDescriptor.setClosingOptions( new Object [] { options[ 0 ], options[ 1 ] } ); 169 170 Dialog dialog = DialogDisplayer.getDefault().createDialog( dialogDescriptor ); 171 dialog.setVisible(true); 172 npn.createFile(); 173 174 } 175 176 public static DataObject[] getTemplates() { 177 178 if ( templates == null ) { 179 180 ArrayList <DataObject> tList = new ArrayList <DataObject>( 2 ); 181 DataObject template; 182 183 template = findTemplate( "Templates/Other/file" ); 184 if ( template != null ) { 185 tList.add( template ); 186 } 187 188 template = findTemplate( "Templates/Other/Folder" ); 189 if ( template != null ) { 190 tList.add( template ); 191 } 192 193 templates = new DataObject[tList.size()]; 194 tList.toArray( templates ); 195 } 196 return templates; 197 } 198 199 201 public void actionPerformed( ActionEvent e ) { 202 result = COMMAND_OK.equals( e.getActionCommand() ) ? getFileName() : null; 203 } 204 205 207 public void insertUpdate(javax.swing.event.DocumentEvent e) { 208 changedUpdate( e ); 209 } 210 211 public void removeUpdate(javax.swing.event.DocumentEvent e) { 212 changedUpdate( e ); 213 } 214 215 public void changedUpdate(javax.swing.event.DocumentEvent e) { 216 217 String fileName = getFileName(); 218 219 if ( fileName.length() == 0 ) { 220 okOption.setEnabled( false ); 221 return; 222 } 223 224 FileObject fo = targetFolder.getPrimaryFile().getFileObject( fileName ); 225 if ( fo != null ) { 226 okOption.setEnabled( false ); 227 return; 228 } 229 230 okOption.setEnabled( true ); 231 } 232 233 235 private static DataObject findTemplate( String name ) { 236 FileObject tFo = Repository.getDefault().getDefaultFileSystem().findResource( name ); 237 if ( tFo == null ) { 238 return null; 239 } 240 try { 241 return DataObject.find( tFo ); 242 } 243 catch ( DataObjectNotFoundException e ) { 244 return null; 245 } 246 247 } 248 249 private String getFileName() { 250 String name = nameTextField.getText().trim(); 251 return name.replace( File.separatorChar, '/' ); } 253 254 private void createFile() { 255 if ( result != null ) { 256 257 if ( !targetFolder.getPrimaryFile().canWrite() ) { 258 return; 259 } 260 261 DataObject dObj = null; 262 263 try { 264 FileObject fo = type == TYPE_FILE ? 265 FileUtil.createData( targetFolder.getPrimaryFile(), result ) : 266 FileUtil.createFolder( targetFolder.getPrimaryFile(), result ); 267 if ( fo != null ) { 268 dObj = DataObject.find( fo ); 269 } 270 } 271 catch ( DataObjectNotFoundException e ) { 272 } 274 catch ( IOException e ) { 275 } 277 278 if ( result != null ) { 279 280 DataObject rootDO = findTemplate ("/Templates"); if (rootDO != null && dObj != null) { 283 if (FileUtil.isParentOf (rootDO.getPrimaryFile (), dObj.getPrimaryFile ())) { 284 try { 285 dObj.setTemplate (true); 286 } catch (IOException e) { 287 } 289 } 290 } 291 292 ProjectUtilities.openAndSelectNewObject( dObj ); 293 } 294 } 295 } 296 297 } 298 | Popular Tags |