KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import javax.swing.Action JavaDoc;
25 import javax.swing.JMenuItem JavaDoc;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.modules.project.ui.OpenProjectList;
28 import org.netbeans.modules.project.ui.ProjectUtilities;
29 import org.openide.util.Lookup;
30 import org.openide.util.NbBundle;
31 import org.openide.util.WeakListeners;
32 import org.openide.util.actions.Presenter;
33
34 /** Action for removing project from the open projects tab
35  */

36 public class CloseProject extends ProjectAction implements PropertyChangeListener JavaDoc, Presenter.Popup {
37     
38     private static final String JavaDoc namePattern = NbBundle.getMessage( CloseProject.class, "LBL_CloseProjectAction_Name" ); // NOI18N
39
private static final String JavaDoc namePatternPopup = NbBundle.getMessage( CloseProject.class, "LBL_CloseProjectAction_Popup_Name" ); // NOI18N
40

41     private String JavaDoc popupName;
42     
43     private PropertyChangeListener JavaDoc wpcl;
44    
45     /** Creates a new instance of BrowserAction */
46     public CloseProject() {
47         this( null );
48     }
49     
50     public CloseProject( Lookup context ) {
51         super( (String JavaDoc)null, namePattern, null, context );
52         wpcl = WeakListeners.propertyChange( this, OpenProjectList.getDefault() );
53         OpenProjectList.getDefault().addPropertyChangeListener( wpcl );
54         refresh( getLookup() );
55     }
56         
57     protected void actionPerformed( Lookup context ) {
58         Project[] projects = ActionsUtil.getProjectsFromLookup( context, null );
59         // show all modified documents, if an user cancel it then no project is closed
60
OpenProjectList.getDefault().close( projects, true );
61     }
62     
63     public void refresh( Lookup context ) {
64         
65         super.refresh( context );
66         
67         Project[] projects = ActionsUtil.getProjectsFromLookup( context, null );
68         // XXX make it work better for mutliple open projects
69
if ( projects.length == 0 || !OpenProjectList.getDefault().isOpen( projects[0] ) ) {
70             setEnabled( false );
71             // setDisplayName( ActionsUtil.formatProjectSensitiveName( namePattern, new Project[0] ) );
72
popupName = ActionsUtil.formatProjectSensitiveName( namePatternPopup, new Project[0] );
73         }
74         else {
75             setEnabled( true );
76             // setDisplayName( ActionsUtil.formatProjectSensitiveName( namePattern, projects ) );
77
popupName = ActionsUtil.formatProjectSensitiveName( namePatternPopup, projects );
78         }
79     }
80     
81     public Action JavaDoc createContextAwareInstance( Lookup actionContext ) {
82         return new CloseProject( actionContext );
83     }
84     
85     public void propertyChange( PropertyChangeEvent JavaDoc evt ) {
86         refresh( getLookup() );
87     }
88     
89     // Implementation of Presenter.Popup ---------------------------------------
90

91     public JMenuItem JavaDoc getPopupPresenter() {
92         JMenuItem JavaDoc popupPresenter = new JMenuItem JavaDoc( this );
93
94         popupPresenter.setIcon( null );
95         popupPresenter.setText( popupName );
96         
97         return popupPresenter;
98     }
99     
100 }
101
Popular Tags