KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import javax.swing.AbstractAction JavaDoc;
31 import javax.swing.Action JavaDoc;
32 import javax.swing.ImageIcon JavaDoc;
33 import javax.swing.JComponent JavaDoc;
34 import javax.swing.JMenu JavaDoc;
35 import javax.swing.JMenuItem JavaDoc;
36 import javax.swing.event.ChangeEvent JavaDoc;
37 import javax.swing.event.ChangeListener JavaDoc;
38 import javax.swing.event.PopupMenuEvent JavaDoc;
39 import javax.swing.event.PopupMenuListener JavaDoc;
40 import org.netbeans.api.project.Project;
41 import org.netbeans.api.project.ProjectUtils;
42 import org.netbeans.modules.project.ui.OpenProjectList;
43 import org.netbeans.api.project.ProjectInformation;
44 import org.netbeans.api.project.ProjectManager;
45 import org.netbeans.modules.project.ui.ProjectTab;
46 import org.openide.filesystems.FileStateInvalidException;
47 import org.openide.nodes.Node;
48 import org.openide.util.NbBundle;
49 import org.openide.util.Utilities;
50 import org.openide.awt.DynamicMenuContent;
51 import org.openide.util.WeakListeners;
52 import org.openide.util.actions.Presenter;
53 import org.openide.filesystems.FileChangeAdapter;
54 import org.openide.filesystems.FileEvent;
55 import org.openide.filesystems.FileObject;
56 import org.openide.filesystems.URLMapper;
57
58 public class RecentProjects extends AbstractAction JavaDoc implements Presenter.Menu, Presenter.Popup, PropertyChangeListener JavaDoc {
59     
60     /** Key for remembering project in JMenuItem
61      */

62     private static final String JavaDoc PROJECT_URL_KEY = "org.netbeans.modules.project.ui.RecentProjectItem.Project_URL"; // NOI18N
63
private final ProjectDirListener prjDirListener = new ProjectDirListener();
64     
65     private UpdatingMenu subMenu;
66     
67     private boolean recreate;
68     
69     public RecentProjects() {
70         super( NbBundle.getMessage(RecentProjects.class, "LBL_RecentProjectsAction_Name")); // NOI18N
71
OpenProjectList.getDefault().addPropertyChangeListener( WeakListeners.propertyChange( this, OpenProjectList.getDefault() ) );
72         recreate = true;
73     }
74     
75         
76     public boolean isEnabled() {
77         return !OpenProjectList.getDefault().isRecentProjectsEmpty();
78     }
79     
80     /** Perform the action. Tries the performer and then scans the ActionMap
81      * of selected topcomponent.
82      */

83     public void actionPerformed(ActionEvent JavaDoc ev) {
84         // no operation
85
}
86     
87     public JMenuItem JavaDoc getMenuPresenter() {
88         createMainSubMenu();
89         return subMenu;
90     }
91     
92     public JMenuItem JavaDoc getPopupPresenter() {
93         JMenu JavaDoc menu = createSubMenu();
94         fillSubMenu(menu);
95         return menu;
96     }
97     
98     private UpdatingMenu createSubMenu() {
99         UpdatingMenu menu = new UpdatingMenu(this);
100         //ok to have mnenomics here, not shown on mac anyway
101
menu.setMnemonic(NbBundle.getMessage(RecentProjects.class, "MNE_RecentProjectsAction_Name").charAt(0));
102         return menu;
103     }
104     
105     private void createMainSubMenu() {
106         if ( subMenu == null ) {
107             subMenu = createSubMenu();
108             // model listening is the only lazy menu procedure that works on macosx
109
subMenu.getModel().addChangeListener(subMenu);
110
111         }
112     }
113         
114     private void fillSubMenu(JMenu JavaDoc menu) {
115         menu.removeAll();
116         
117         List JavaDoc projects = OpenProjectList.getDefault().getRecentProjects();
118         if ( projects.isEmpty() ) {
119             menu.setEnabled( false );
120             return;
121         }
122         
123         menu.setEnabled( true );
124         ActionListener JavaDoc jmiActionListener = new MenuItemActionListener();
125                         
126         // Fill menu with items
127

128         for ( Iterator JavaDoc it = projects.iterator(); it.hasNext(); ) {
129             Project p = (Project)it.next();
130             FileObject prjDir = p.getProjectDirectory();
131             try {
132                 URL JavaDoc prjDirURL = prjDir.getURL();
133                 if ( prjDirURL == null || prjDir == null || !prjDir.isValid()) {
134                     continue;
135                 }
136                 prjDir.removeFileChangeListener(prjDirListener);
137                 prjDir.addFileChangeListener(prjDirListener);
138                 ProjectInformation pi = ProjectUtils.getInformation(p);
139                 JMenuItem JavaDoc jmi = new JMenuItem JavaDoc(pi.getDisplayName(), pi.getIcon());
140                 menu.add( jmi );
141                 jmi.putClientProperty( PROJECT_URL_KEY, prjDirURL );
142                 jmi.addActionListener( jmiActionListener );
143             }
144             catch( FileStateInvalidException ex ) {
145                 // Don't put the project into the menu
146
}
147         }
148     }
149
150     // Implementation of change listener ---------------------------------------
151

152     
153     public void propertyChange( PropertyChangeEvent JavaDoc e ) {
154         
155         if ( OpenProjectList.PROPERTY_RECENT_PROJECTS.equals( e.getPropertyName() ) ) {
156             createMainSubMenu();
157             subMenu.setEnabled( !OpenProjectList.getDefault().isRecentProjectsEmpty() );
158             recreate = true;
159         }
160         
161     }
162     
163     
164     
165     // Innerclasses ------------------------------------------------------------
166

167     private static class MenuItemActionListener implements ActionListener JavaDoc {
168         
169         public void actionPerformed( ActionEvent JavaDoc e ) {
170             
171             if ( e.getSource() instanceof JMenuItem JavaDoc ) {
172                 JMenuItem JavaDoc jmi = (JMenuItem JavaDoc)e.getSource();
173                 
174                 URL JavaDoc url = (URL JavaDoc)jmi.getClientProperty( PROJECT_URL_KEY );
175                 Project project = null;
176
177                 FileObject dir = URLMapper.findFileObject( url );
178                 if ( dir != null && dir.isFolder() ) {
179                     try {
180                         project = ProjectManager.getDefault().findProject( dir );
181                     }
182                     catch ( IOException JavaDoc ioEx ) {
183                         // Ignore invalid folders
184
}
185                 }
186                 
187                 if ( project != null ) {
188                     OpenProjectList.getDefault().open( new Project[] {project}, false, true );
189                     ProjectTab ptLogial = ProjectTab.findDefault (ProjectTab.ID_LOGICAL);
190                     Node root = ptLogial.getExplorerManager ().getRootContext ();
191                     Node projNode = root.getChildren ().findChild ( project.getProjectDirectory().getName ());
192                     try {
193                         ptLogial.getExplorerManager ().setSelectedNodes (new Node[] {projNode});
194                         ptLogial.open ();
195                         ptLogial.requestActive ();
196                     } catch (Exception JavaDoc ignore) {
197                         // may ignore it
198
}
199                 }
200                 
201             }
202             
203         }
204         
205     }
206     
207     private class ProjectDirListener extends FileChangeAdapter {
208         public void fileDeleted(FileEvent fe) {
209             recreate = true;
210         }
211     }
212     
213     private class UpdatingMenu extends JMenu JavaDoc implements /*DynamicMenuContent,*/ ChangeListener JavaDoc {
214         
215         public UpdatingMenu(Action JavaDoc action) {
216             super(action);
217         }
218         
219 // public JComponent[] synchMenuPresenters(JComponent[] items) {
220
// return getMenuPresenters();
221
// }
222
//
223
// public JComponent[] getMenuPresenters() {
224
// return new JComponent[] { this };
225
// }
226

227         public void stateChanged(ChangeEvent JavaDoc e) {
228             if (recreate && getModel().isSelected()) {
229                 fillSubMenu(this);
230                 recreate = false;
231             }
232         }
233         
234     }
235     
236 }
237
Popular Tags