KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.Action JavaDoc;
23 import javax.swing.Icon JavaDoc;
24 import javax.swing.ImageIcon JavaDoc;
25 import javax.swing.JMenuItem JavaDoc;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.spi.project.ActionProvider;
28 import org.openide.awt.Actions;
29 import org.openide.awt.Mnemonics;
30 import org.openide.filesystems.FileObject;
31 import org.openide.util.Lookup;
32 import org.openide.util.Utilities;
33 import org.openide.util.actions.Presenter;
34
35 /** An action sensitive to selected node. Used for 1-off actions
36  */

37 public final class FileCommandAction extends ProjectAction {
38
39     private String JavaDoc presenterName;
40         
41     public FileCommandAction( String JavaDoc command, String JavaDoc namePattern, String JavaDoc iconResource, Lookup lookup ) {
42         this( command, namePattern, new ImageIcon JavaDoc( Utilities.loadImage( iconResource ) ), lookup );
43     }
44     
45     public FileCommandAction( String JavaDoc command, String JavaDoc namePattern, Icon JavaDoc icon, Lookup lookup ) {
46         super( command, namePattern, icon, lookup );
47         assert namePattern != null : "Name patern must not be null";
48         presenterName = ActionsUtil.formatName( getNamePattern(), 0, "" );
49         setDisplayName( presenterName );
50         putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName));
51     }
52     
53     protected void refresh( Lookup context ) {
54         Project[] projects = ActionsUtil.getProjectsFromLookup( context, getCommand() );
55
56         if ( projects.length != 1 ) {
57             setEnabled( false ); // Zero or more than one projects found or command not supported
58
presenterName = ActionsUtil.formatName( getNamePattern(), 0, "" );
59         }
60         else {
61             FileObject[] files = ActionsUtil.getFilesFromLookup( context, projects[0] );
62             setEnabled( true );
63             presenterName = ActionsUtil.formatName( getNamePattern(), files.length, files.length > 0 ? files[0].getNameExt() : "" ); // NOI18N
64
}
65         
66         setLocalizedTextToMenuPresented(presenterName);
67         
68         putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName));
69     }
70     
71     protected void actionPerformed( Lookup context ) {
72                 
73         Project[] projects = ActionsUtil.getProjectsFromLookup( context, getCommand() );
74
75         if ( projects.length == 1 ) {
76             ActionProvider ap = (ActionProvider)projects[0].getLookup().lookup( ActionProvider.class );
77             ap.invokeAction( getCommand(), context );
78         }
79         
80     }
81     
82
83     public Action JavaDoc createContextAwareInstance( Lookup actionContext ) {
84         
85         return new FileCommandAction( getCommand(), getNamePattern(), (Icon JavaDoc)getValue( SMALL_ICON ), actionContext );
86     }
87
88     
89 }
Popular Tags