KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > NoProjectNew


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
20 package org.netbeans.modules.project.ui;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import javax.swing.JButton JavaDoc;
29 import javax.swing.event.DocumentListener JavaDoc;
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 /**
42  *
43  * @author phrebejk
44  */

45 public class NoProjectNew extends javax.swing.JPanel JavaDoc implements ActionListener JavaDoc, DocumentListener JavaDoc {
46     
47     public static final int TYPE_FILE = 0;
48     public static final int TYPE_FOLDER = 1;
49     
50     private static final String JavaDoc FILE_NAME = NbBundle.getMessage( NoProjectNew.class, "LBL_NonProject_File_Name" ); // NOI18N
51
private static final String JavaDoc FILE_TITLE = NbBundle.getMessage( NoProjectNew.class, "LBL_NonProject_File_Title" ); // NOI18N;
52
private static final String JavaDoc FILE_TEXT_FIELD_ACD = NbBundle.getMessage( NoProjectNew.class, "ACD_NonProject_File_TextField" ); // NOI18N;
53
private static final String JavaDoc FILE_DIALOG_ACD = NbBundle.getMessage( NoProjectNew.class, "ACD_NonProject_File_Dialog" ); // NOI18N;
54
private static final String JavaDoc FOLDER_NAME = NbBundle.getMessage( NoProjectNew.class, "LBL_NonProject_Folder_Name" ); // NOI18N;
55
private static final String JavaDoc FOLDER_TITLE = NbBundle.getMessage( NoProjectNew.class, "LBL_NonProject_Folder_Title" ); // NOI18N;
56
private static final String JavaDoc FOLDER_TEXT_FIELD_ACD = NbBundle.getMessage( NoProjectNew.class, "ACD_NonProject_Folder_TextField" ); // NOI18N;
57
private static final String JavaDoc FOLDER_DIALOG_ACD = NbBundle.getMessage( NoProjectNew.class, "ACD_NonProject_Folder_Dialog" ); // NOI18N;
58

59     public static final String JavaDoc COMMAND_OK = "OK";
60     public static final String JavaDoc COMMAND_CANCEL = "CANCEL";
61         
62     private static DataObject[] templates;
63     
64     private int type;
65     private DataFolder targetFolder;
66     private String JavaDoc result;
67     private JButton JavaDoc okOption;
68     
69     /** Creates new form BrowseFolders */
70     public NoProjectNew( int type, DataFolder targetFolder, JButton JavaDoc 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     /** This method is called from within the constructor to
96      * initialize the form.
97      * WARNING: Do NOT modify this code. The content of this method is
98      * always regenerated by the Form Editor.
99      */

100     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
101
private void initComponents() {
102         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
103
104         nameLabel = new javax.swing.JLabel JavaDoc();
105         nameTextField = new javax.swing.JTextField JavaDoc();
106
107         setLayout(new java.awt.GridBagLayout JavaDoc());
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 JavaDoc();
113         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
114         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
115         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 6);
116         add(nameLabel, gridBagConstraints);
117
118         nameTextField.setColumns(25);
119         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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     // </editor-fold>//GEN-END:initComponents
129

130     
131     // Variables declaration - do not modify//GEN-BEGIN:variables
132
public javax.swing.JLabel JavaDoc nameLabel;
133     public javax.swing.JTextField JavaDoc nameTextField;
134     // End of variables declaration//GEN-END:variables
135

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 JavaDoc options[] = new JButton JavaDoc[] {
147             new JButton JavaDoc( NbBundle.getMessage( NoProjectNew.class, "LBL_NonProject_OK_Button") ), // NOI18N
148
new JButton JavaDoc( NbBundle.getMessage( NoProjectNew.class, "LBL_NonProject_Cancel_Button") ), // NOI18N
149
};
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, // innerPane
160
type == TYPE_FILE ? FILE_TITLE : FOLDER_TITLE, // displayName
161
true, // modal
162
options, // options
163
options[ 0 ], // initial value
164
DialogDescriptor.BOTTOM_ALIGN, // options align
165
null, // helpCtx
166
null ); // listener
167

168         dialogDescriptor.setClosingOptions( new Object JavaDoc[] { options[ 0 ], options[ 1 ] } );
169             
170         Dialog JavaDoc 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 JavaDoc<DataObject> tList = new ArrayList JavaDoc<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     // ActionListener implementation -------------------------------------------
200

201     public void actionPerformed( ActionEvent JavaDoc e ) {
202         result = COMMAND_OK.equals( e.getActionCommand() ) ? getFileName() : null;
203     }
204     
205     // Document listener implementation ----------------------------------------
206

207     public void insertUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
208         changedUpdate( e );
209     }
210     
211     public void removeUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
212         changedUpdate( e );
213     }
214     
215     public void changedUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
216         
217         String JavaDoc 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     // Private methods ---------------------------------------------------------
234

235     private static DataObject findTemplate( String JavaDoc 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 JavaDoc getFileName() {
250         String JavaDoc name = nameTextField.getText().trim();
251         return name.replace( File.separatorChar, '/' ); // NOI18N
252
}
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                 // No data object no open
273
}
274             catch ( IOException JavaDoc e ) {
275                 // XXX
276
}
277             
278             if ( result != null ) {
279                 
280                 // handle new template in SystemFileSystem
281
DataObject rootDO = findTemplate ("/Templates"); // NOI18N
282
if (rootDO != null && dObj != null) {
283                     if (FileUtil.isParentOf (rootDO.getPrimaryFile (), dObj.getPrimaryFile ())) {
284                         try {
285                             dObj.setTemplate (true);
286                         } catch (IOException JavaDoc e) {
287                             // can ignore
288
}
289                     }
290                 }
291                 
292                 ProjectUtilities.openAndSelectNewObject( dObj );
293             }
294         }
295     }
296     
297 }
298
Popular Tags