KickJava   Java API By Example, From Geeks To Geeks.

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


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.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import javax.swing.Action JavaDoc;
28 import javax.swing.JMenu JavaDoc;
29 import javax.swing.JMenuItem JavaDoc;
30 import javax.swing.JRadioButtonMenuItem JavaDoc;
31 import org.netbeans.api.project.Project;
32 import org.netbeans.api.project.ProjectUtils;
33 import org.netbeans.modules.project.ui.OpenProjectList;
34 import org.netbeans.api.project.ProjectInformation;
35 import org.openide.util.Lookup;
36 import org.openide.util.NbBundle;
37 import org.openide.util.WeakListeners;
38 import org.openide.util.actions.Presenter;
39
40 public class SetMainProject extends ProjectAction implements Presenter.Menu, PropertyChangeListener JavaDoc {
41     
42     private static final String JavaDoc namePattern = NbBundle.getMessage( SetMainProject.class, "LBL_SetAsMainProjectAction_Name" ); // NOI18N
43

44     /** Key for remembering project in JMenuItem
45      */

46     private static final String JavaDoc PROJECT_KEY = "org.netbeans.modules.project.ui.MainProjectItem"; // NOI18N
47

48     protected JMenu JavaDoc subMenu;
49     
50     // private PropertyChangeListener wpcl;
51

52     public SetMainProject() {
53         this( null );
54     }
55     
56     public SetMainProject( Lookup context ) {
57         super( SetMainProject.class.getName() /*this is a fake command to make ActionUtils.SHORCUTS_MANAGER work*/, namePattern, null, context );
58         // wpcl = WeakListeners.propertyChange( this, OpenProjectList.getDefault() );
59
// OpenProjectList.getDefault().addPropertyChangeListener( wpcl );
60
if ( context == null ) {
61             OpenProjectList.getDefault().addPropertyChangeListener( WeakListeners.propertyChange( this, OpenProjectList.getDefault() ) );
62         }
63         refresh( getLookup() );
64     }
65     
66     protected void actionPerformed( Lookup context ) {
67         Project[] projects = ActionsUtil.getProjectsFromLookup( context, null );
68         
69         if ( projects != null && projects.length > 0 )
70         OpenProjectList.getDefault().setMainProject( projects[0] );
71         
72     }
73     
74     public void refresh( Lookup context ) {
75         
76         super.refresh( context );
77         
78         Project[] projects = ActionsUtil.getProjectsFromLookup( context, null );
79         if ( projects.length != 1 /* Some projects have to be open !OpenProjectList.getDefault().isOpen( projects[0] ) */ ) {
80             setEnabled( false );
81         }
82         else {
83             setEnabled( true );
84         }
85     }
86     
87     public Action JavaDoc createContextAwareInstance( Lookup actionContext ) {
88         return new SetMainProject( actionContext );
89     }
90     
91     public JMenuItem JavaDoc getMenuPresenter() {
92         createSubMenu();
93         return subMenu;
94     }
95         
96     private void createSubMenu() {
97         
98         Project projects[] = OpenProjectList.getDefault().getOpenProjects();
99         
100         Arrays.sort( projects, OpenProjectList.PROJECT_BY_DISPLAYNAME );
101         
102         // Enable disable the action according to number of open projects
103
if ( projects == null || projects.length == 0 ) {
104             setEnabled( false );
105         }
106         else {
107             setEnabled( true );
108         }
109         
110         if ( subMenu == null ) {
111             String JavaDoc label = NbBundle.getMessage( SetMainProject.class, "LBL_SetMainProjectAction_Name" ); // NOI18N
112
subMenu = new JMenu JavaDoc( label );
113             //ok to have mnenomics here, not shown on mac anyway
114
subMenu.setMnemonic (NbBundle.getMessage (SetMainProject.class, "MNE_SetMainProjectAction_Name").charAt (0)); // NOI18N
115
//#70835: the menu bar holds only subMenu not this action. As this action listens only weakly on OPL, noone holds this action.
116
//The action is the garbage collected and the sub menu does not react on changes of opened projects.
117
//The action instance has to exists as long as the subMenu:
118
subMenu.putClientProperty(SetMainProject.class, this);
119         }
120         
121         subMenu.removeAll();
122         ActionListener JavaDoc jmiActionListener = new MenuItemActionListener();
123         
124         
125         // Fill menu with items
126
for ( int i = 0; i < projects.length; i++ ) {
127             ProjectInformation pi = ProjectUtils.getInformation(projects[i]);
128             JRadioButtonMenuItem JavaDoc jmi = new JRadioButtonMenuItem JavaDoc(pi.getDisplayName(), pi.getIcon(), false);
129             subMenu.add( jmi );
130             jmi.putClientProperty( PROJECT_KEY, projects[i] );
131             jmi.addActionListener( jmiActionListener );
132         }
133
134         // Set main project
135
selectMainProject();
136         
137         subMenu.setEnabled( projects.length > 0 );
138         
139     }
140     
141     private void selectMainProject() {
142         
143         for( int i = 0; i < subMenu.getItemCount(); i++ ) {
144             JMenuItem JavaDoc jmi = subMenu.getItem( i );
145             Project project = (Project)jmi.getClientProperty( PROJECT_KEY );
146             
147             if ( jmi instanceof JRadioButtonMenuItem JavaDoc ) {
148                 if ( OpenProjectList.getDefault().isMainProject( project ) ) {
149                     ((JRadioButtonMenuItem JavaDoc)jmi).setSelected( true );
150                 }
151                 else {
152                     ((JRadioButtonMenuItem JavaDoc)jmi).setSelected( false );
153                 }
154             }
155         }
156         
157     }
158     
159     // Implementation of change listener ---------------------------------------
160

161     
162     public void propertyChange( PropertyChangeEvent JavaDoc e ) {
163         
164         if ( OpenProjectList.PROPERTY_OPEN_PROJECTS.equals( e.getPropertyName() ) ) {
165             createSubMenu();
166         }
167         else if ( OpenProjectList.PROPERTY_MAIN_PROJECT.equals( e.getPropertyName() ) && subMenu != null ) {
168             selectMainProject();
169         }
170         
171         
172     }
173     
174     // Innerclasses ------------------------------------------------------------
175

176     private static class MenuItemActionListener implements ActionListener JavaDoc {
177         
178         public void actionPerformed( ActionEvent JavaDoc e ) {
179             
180             if ( e.getSource() instanceof JMenuItem JavaDoc ) {
181                 JMenuItem JavaDoc jmi = (JMenuItem JavaDoc)e.getSource();
182                 Project project = (Project)jmi.getClientProperty( PROJECT_KEY );
183                 if ( project != null ) {
184                     OpenProjectList.getDefault().setMainProject( project );
185                 }
186                 
187             }
188             
189         }
190         
191     }
192     
193     /**
194      * Variant which behaves just like the menu presenter without a context, but
195      * can be displayed in a context menu.
196      */

197     public static final class PopupWithoutContext extends SetMainProject implements Presenter.Popup {
198         
199         public PopupWithoutContext() {}
200         
201         private PopupWithoutContext(Lookup actionContext) {
202             super(actionContext);
203         }
204         
205         public JMenuItem JavaDoc getPopupPresenter() {
206             // Hack!
207
subMenu = null;
208             return getMenuPresenter();
209         }
210         
211         public Action JavaDoc createContextAwareInstance(Lookup actionContext) {
212             return new PopupWithoutContext(actionContext);
213         }
214
215     }
216     
217 }
218
Popular Tags