KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > actions > NewFile


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.actions;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.beans.BeanInfo JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.text.MessageFormat JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Set JavaDoc;
32 import javax.swing.*;
33 import javax.swing.JPopupMenu.Separator;
34 import javax.swing.event.PopupMenuEvent JavaDoc;
35 import javax.swing.event.PopupMenuListener JavaDoc;
36 import org.netbeans.api.project.Project;
37 import org.netbeans.modules.project.ui.NewFileWizard;
38 import org.netbeans.modules.project.ui.NoProjectNew;
39 import org.netbeans.modules.project.ui.OpenProjectList;
40 import org.netbeans.modules.project.ui.ProjectUtilities;
41 import org.netbeans.spi.project.ui.templates.support.Templates;
42 import org.openide.ErrorManager;
43 import org.openide.awt.Mnemonics;
44 import org.openide.filesystems.FileObject;
45 import org.openide.loaders.DataFolder;
46 import org.openide.loaders.DataObject;
47 import org.openide.loaders.DataObjectNotFoundException;
48 import org.openide.nodes.Node;
49 import org.openide.util.Lookup;
50 import org.openide.util.Mutex;
51 import org.openide.util.NbBundle;
52 import org.openide.util.Utilities;
53 import org.openide.util.WeakListeners;
54 import org.openide.util.actions.Presenter.Popup;
55
56 /** Action for invoking the project sensitive NewFile Wizard
57  */

58 public class NewFile extends ProjectAction implements PropertyChangeListener JavaDoc, Popup, PopupMenuListener JavaDoc {
59
60     private static final Icon ICON = new ImageIcon( Utilities.loadImage( "org/netbeans/modules/project/ui/resources/newFile.gif" ) ); //NOI18N
61
private static final String JavaDoc NAME = NbBundle.getMessage( NewFile.class, "LBL_NewFileAction_Name" ); // NI18N
62
private static final String JavaDoc _SHORT_DESCRIPTION = NbBundle.getMessage( NewFile.class, "LBL_NewFileAction_Tooltip" ); // NI18N
63
private static final String JavaDoc POPUP_NAME = NbBundle.getMessage( NewFile.class, "LBL_NewFileAction_PopupName" ); // NOI18N
64
private static final String JavaDoc FILE_POPUP_NAME = NbBundle.getMessage( NewFile.class, "LBL_NewFileAction_File_PopupName" ); // NOI18N
65
private static final String JavaDoc TEMPLATE_NAME_FORMAT = NbBundle.getMessage( NewFile.class, "LBL_NewFileAction_Template_PopupName" ); // NOI18N
66

67     private JMenu subMenu;
68     
69     public NewFile() {
70         this( null );
71     }
72     
73     public NewFile( Lookup context ) {
74         super( (String JavaDoc)null, NAME, ICON, context ); //NOI18N
75
putValue("iconBase","org/netbeans/modules/project/ui/resources/newFile.gif"); //NOI18N
76
putValue(SHORT_DESCRIPTION, _SHORT_DESCRIPTION);
77         OpenProjectList.getDefault().addPropertyChangeListener( WeakListeners.propertyChange( this, OpenProjectList.getDefault() ) );
78         refresh( getLookup() );
79     }
80
81     protected void refresh( Lookup context ) {
82         // #59615: update synch if possible; only replan if not already in EQ.
83
Mutex.EVENT.readAccess(new Runnable JavaDoc() {
84             public void run() {
85                 setEnabled(OpenProjectList.getDefault().getOpenProjects().length > 0);
86                 setDisplayName(NAME);
87             }
88         });
89     }
90
91     public JMenuItem getPopupPresenter() {
92         JMenuItem menu = new JMenuItem(this);
93         menu.setIcon(null);
94         Mnemonics.setLocalizedText(menu, (String JavaDoc) getValue(Action.NAME));
95         // XXX accelerator not displayed here for some reason...why???
96
return menu;
97     }
98
99     //private NewFileWizard wizardIterator;
100

101     protected void actionPerformed( Lookup context ) {
102         doPerform( context, null, true );
103     }
104         
105     private void doPerform( Lookup context, DataObject template, boolean inProject ) {
106         
107         if ( context == null ) {
108             context = getLookup();
109         }
110     
111         if ( !inProject ) {
112             // Context outside of projects
113
NoProjectNew.showDialog( template, preselectedFolder( context ) );
114             return;
115         }
116         
117         NewFileWizard wd = new NewFileWizard( preselectedProject( context ) /* , null */ );
118
119         DataFolder preselectedFolder = preselectedFolder( context );
120         if ( preselectedFolder != null ) {
121             wd.setTargetFolder( preselectedFolder );
122         }
123
124         try {
125             Set JavaDoc resultSet = template == null ? wd.instantiate () : wd.instantiate( template );
126             
127             if (resultSet == null || resultSet.isEmpty ()) {
128                 // no new object, no work
129
return ;
130             }
131             
132             Iterator JavaDoc it = resultSet.iterator ();
133             
134             while (it.hasNext ()) {
135                 Object JavaDoc obj = it.next ();
136                 DataObject newDO = null;
137                 if (obj instanceof DataObject) {
138                     newDO = (DataObject) obj;
139                 } else if (obj instanceof FileObject) {
140                     try {
141                         newDO = DataObject.find ((FileObject) obj);
142                     } catch (DataObjectNotFoundException x) {
143                         // XXX
144
assert false : obj;
145                     }
146                 } else {
147                     assert false : obj;
148                 }
149                 if (newDO != null) {
150                     ProjectUtilities.openAndSelectNewObject (newDO);
151                 }
152             }
153         }
154         catch ( IOException JavaDoc e ) {
155             ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, e);
156         }
157         
158         // Update the Templates LRU for given project
159
Project project = Templates.getProject( wd );
160         FileObject foTemplate = Templates.getTemplate( wd );
161         OpenProjectList.getDefault().updateTemplatesLRU( foTemplate );
162
163     }
164     
165     // Context Aware action implementation -------------------------------------
166

167     public Action createContextAwareInstance( Lookup actionContext ) {
168         return new NewFile( actionContext );
169     }
170     
171     // Presenter.Popup implementation ------------------------------------------
172

173     public JMenuItem getSubmenuPopupPresenter() {
174         if (subMenu == null) {
175             subMenu = new JMenu(POPUP_NAME);
176             subMenu.getPopupMenu().addPopupMenuListener(this);
177         }
178         return subMenu;
179     }
180     
181     private void fillSubMenu() {
182         Project projects[] = ActionsUtil.getProjectsFromLookup( getLookup(), null );
183         if ( projects != null && projects.length > 0 ) {
184             fillSubMenu(subMenu, projects[0]);
185         }
186         else {
187             // When no project is seleceted only file and folder can be created
188
fillNonProjectSubMenu(subMenu);
189         }
190     }
191     
192     // Private methods ---------------------------------------------------------
193

194     private Project preselectedProject( Lookup context ) {
195         Project preselectedProject = null;
196
197         // if ( activatedNodes != null && activatedNodes.length != 0 ) {
198

199         Project[] projects = ActionsUtil.getProjectsFromLookup( context, null );
200         if ( projects.length > 0 ) {
201             preselectedProject = projects[0];
202         }
203
204         
205         if ( preselectedProject == null ) {
206             // No project context => use main project
207
preselectedProject = OpenProjectList.getDefault().getMainProject();
208             if ( preselectedProject == null ) {
209                 // No main project => use the first one
210
preselectedProject = OpenProjectList.getDefault().getOpenProjects()[0];
211             }
212         }
213
214         if ( preselectedProject == null ) {
215             assert false : "Action should be disabled"; // NOI18N
216
}
217
218         return preselectedProject;
219     }
220
221     private DataFolder preselectedFolder( Lookup context ) {
222         
223         DataFolder preselectedFolder = null;
224         
225         // Try to find selected folder
226
preselectedFolder = (DataFolder)context.lookup( DataFolder.class );
227         if ( preselectedFolder == null ) {
228             // No folder selectd try with DataObject
229
DataObject dobj = (DataObject)context.lookup( DataObject.class );
230             if ( dobj != null) {
231                 // DataObject found => we'll use the parent folder
232
preselectedFolder = dobj.getFolder();
233             }
234         }
235         
236         return preselectedFolder;
237     }
238
239     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
240         refresh( Lookup.EMPTY );
241     }
242     
243     public static String JavaDoc TEMPLATE_PROPERTY = "org.netbeans.modules.project.ui.actions.NewFile.Template"; // NOI18N
244
public static String JavaDoc IN_PROJECT_PROPERTY = "org.netbeans.modules.project.ui.actions.NewFile.InProject"; // NOI18N
245

246     
247      private void fillSubMenu(JMenu menuItem, Project project) {
248         menuItem.removeAll();
249         
250         ActionListener JavaDoc menuListener = new PopupListener();
251         
252         JMenuItem fileItem = new JMenuItem( FILE_POPUP_NAME, (Icon)getValue( Action.SMALL_ICON ) );
253         fileItem.addActionListener( menuListener );
254         fileItem.putClientProperty( TEMPLATE_PROPERTY, null );
255         menuItem.add( fileItem );
256                 
257         List JavaDoc lruList = OpenProjectList.getDefault().getTemplatesLRU( project );
258         boolean first = true;
259         for( Iterator JavaDoc it = lruList.iterator(); it.hasNext(); ) {
260             DataObject template = (DataObject)it.next();
261             
262             Node delegate = template.getNodeDelegate();
263             JMenuItem item = new JMenuItem(
264                 MessageFormat.format( TEMPLATE_NAME_FORMAT, new Object JavaDoc[] { delegate.getDisplayName() } ),
265                 new ImageIcon( delegate.getIcon( BeanInfo.ICON_COLOR_16x16 ) ) );
266             item.addActionListener( menuListener );
267             item.putClientProperty( TEMPLATE_PROPERTY, template );
268             if ( first ) {
269                 menuItem.add( new Separator() );
270                 first = false;
271             }
272             menuItem.add( item );
273         }
274     }
275     
276     
277     private void fillNonProjectSubMenu(JMenu menuItem) {
278         menuItem.removeAll();
279         
280         ActionListener JavaDoc menuListener = new PopupListener();
281         
282         DataFolder preselectedFolder = preselectedFolder( getLookup() );
283         
284         boolean canWrite;
285         if ( preselectedFolder == null ) {
286             canWrite = false;
287         }
288         else {
289             FileObject pf = preselectedFolder.getPrimaryFile();
290             canWrite = pf != null && pf.canWrite();
291         }
292         
293         DataObject templates[] = NoProjectNew.getTemplates();
294         for( int i = 0; i < templates.length; i++ ) {
295             Node n = templates[i].getNodeDelegate();
296             JMenuItem item = new JMenuItem(
297                 MessageFormat.format( TEMPLATE_NAME_FORMAT, new Object JavaDoc[] { n.getDisplayName() } ),
298                                       new ImageIcon( n.getIcon( BeanInfo.ICON_COLOR_16x16 ) ) );
299             item.addActionListener( menuListener );
300             item.putClientProperty( TEMPLATE_PROPERTY, templates[i] );
301             item.putClientProperty( IN_PROJECT_PROPERTY, Boolean.FALSE );
302             item.setEnabled( canWrite );
303             menuItem.add( item );
304         }
305     }
306     
307     private class PopupListener implements ActionListener JavaDoc {
308                 
309         public void actionPerformed( ActionEvent JavaDoc e ) {
310             JMenuItem source = (JMenuItem)e.getSource();
311
312             Boolean JavaDoc inProject = (Boolean JavaDoc)source.getClientProperty( IN_PROJECT_PROPERTY );
313             DataObject template = (DataObject)source.getClientProperty( TEMPLATE_PROPERTY );
314             
315             if ( inProject != null && inProject == Boolean.FALSE ) {
316                 doPerform( null, template, false );
317             }
318             else {
319                 doPerform( null, template, true );
320             }
321         }
322         
323     }
324     
325     // Implementation of PopupMenuListener -------------------------------------
326

327     public void popupMenuWillBecomeVisible(PopupMenuEvent JavaDoc e) {
328         fillSubMenu();
329     }
330     
331     public void popupMenuWillBecomeInvisible(PopupMenuEvent JavaDoc e) {
332     }
333
334     public void popupMenuCanceled(PopupMenuEvent JavaDoc e) {
335     }
336     
337     /**
338      * Variant for folder context menus that makes a submenu.
339      */

340     public static final class WithSubMenu extends NewFile {
341         
342         public WithSubMenu() {}
343         
344         private WithSubMenu(Lookup actionContext) {
345             super(actionContext);
346         }
347         
348         public JMenuItem getPopupPresenter() {
349             return getSubmenuPopupPresenter();
350         }
351         
352         public Action createContextAwareInstance(Lookup actionContext) {
353             return new WithSubMenu(actionContext);
354         }
355
356     }
357     
358 }
359
Popular Tags