KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > openfile > RecentFileAction


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.openfile;
21 import java.awt.Image JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.beans.BeanInfo JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import javax.swing.AbstractAction JavaDoc;
28 import javax.swing.Action JavaDoc;
29 import javax.swing.ImageIcon JavaDoc;
30 import javax.swing.ImageIcon JavaDoc;
31 import javax.swing.JComponent JavaDoc;
32 import javax.swing.JMenu JavaDoc;
33 import javax.swing.JMenuItem JavaDoc;
34 import javax.swing.event.PopupMenuEvent JavaDoc;
35 import javax.swing.event.PopupMenuListener JavaDoc;
36 import org.netbeans.modules.openfile.RecentFiles.HistoryItem;
37 import org.openide.awt.DynamicMenuContent;
38 import org.openide.filesystems.FileObject;
39 import org.openide.loaders.DataObject;
40 import org.openide.loaders.DataObjectNotFoundException;
41 import org.openide.util.NbBundle;
42 import org.openide.util.actions.Presenter;
43
44 /**
45  * Action that presents list of recently closed files/documents.
46  *
47  * @author Dafe Simonek
48  */

49 public class RecentFileAction extends AbstractAction JavaDoc implements Presenter.Menu, PopupMenuListener JavaDoc {
50
51     /** property of menu items where we store fileobject to open */
52     private static final String JavaDoc FO_PROP = "RecentFileAction.Recent_FO";
53
54     /** number of maximum shown items in submenu */
55     private static final int MAX_COUNT = 15;
56             
57     private JMenu JavaDoc menu;
58     
59     public RecentFileAction() {
60         super(NbBundle.getMessage(RecentFileAction.class, "LBL_RecentFileAction_Name")); // NOI18N
61
}
62     
63     /********* Presenter.Menu impl **********/
64     
65     public JMenuItem JavaDoc getMenuPresenter() {
66         if (menu == null) {
67             menu = new UpdatingMenu(this);
68             menu.setMnemonic(NbBundle.getMessage(RecentFileAction.class,
69                                                  "MNE_RecentFileAction_Name").charAt(0));
70             menu.getPopupMenu().addPopupMenuListener(this);
71         }
72         return menu;
73     }
74     
75     /******* PopupMenuListener impl *******/
76     
77     public void popupMenuWillBecomeVisible(PopupMenuEvent JavaDoc arg0) {
78         fillSubMenu();
79     }
80     
81     public void popupMenuWillBecomeInvisible(PopupMenuEvent JavaDoc arg0) {
82         menu.removeAll();
83     }
84     
85     public void popupMenuCanceled(PopupMenuEvent JavaDoc arg0) {
86     }
87     
88     /** Fills submenu with recently closed files got from RecentFiles support */
89     private void fillSubMenu () {
90         List JavaDoc<RecentFiles.HistoryItem> files = RecentFiles.getRecentFiles();
91
92         int counter = 0;
93         for (HistoryItem hItem : files) {
94             // allow only up to max items
95
if (++counter > MAX_COUNT) {
96                 break;
97             }
98             // obtain icon for fileobject
99
Image JavaDoc icon = null;
100             try {
101                 DataObject dObj = DataObject.find(hItem.getFile());
102                 icon = dObj.getNodeDelegate().getIcon(BeanInfo.ICON_COLOR_16x16);
103             } catch (DataObjectNotFoundException ex) {
104                 // should not happen, log and skip to next
105
Logger.getLogger(RecentFiles.class.getName()).log(
106                         Level.INFO, ex.getMessage(), ex);
107                 continue;
108             }
109             // create and configure menu item
110
JMenuItem JavaDoc jmi = null;
111             if (icon != null) {
112                 jmi = new JMenuItem JavaDoc(hItem.getFile().getNameExt(), new ImageIcon JavaDoc(icon));
113             } else {
114                 jmi = new JMenuItem JavaDoc(hItem.getFile().getNameExt());
115             }
116             jmi.putClientProperty(FO_PROP, hItem.getFile());
117             jmi.addActionListener(this);
118             menu.add(jmi);
119         }
120     }
121     
122     /** Opens recently closed file, using OpenFile support.
123      *
124      * Note that method works as action handler for individual submenu items
125      * created in fillSubMenu, not for whole RecentFileAction.
126      */

127     public void actionPerformed(ActionEvent JavaDoc evt) {
128         JMenuItem JavaDoc source = (JMenuItem JavaDoc) evt.getSource();
129         FileObject fo = (FileObject) source.getClientProperty(FO_PROP);
130         if (fo != null) {
131             OpenFile.open(fo, -1);
132         }
133     }
134     
135     /** Menu that checks its enabled state just before is populated */
136     private static class UpdatingMenu extends JMenu JavaDoc implements DynamicMenuContent {
137         
138         private final JComponent JavaDoc[] content = new JComponent JavaDoc[] { this };
139         
140         public UpdatingMenu (Action JavaDoc action) {
141             super(action);
142         }
143     
144         public JComponent JavaDoc[] getMenuPresenters() {
145             setEnabled(!RecentFiles.getRecentFiles().isEmpty());
146             return content;
147         }
148
149         public JComponent JavaDoc[] synchMenuPresenters(JComponent JavaDoc[] items) {
150             return getMenuPresenters();
151         }
152     }
153     
154 }
155
Popular Tags