1 19 20 package org.netbeans.modules.openfile; 21 import java.awt.Image ; 22 import java.awt.event.ActionEvent ; 23 import java.beans.BeanInfo ; 24 import java.util.List ; 25 import java.util.logging.Level ; 26 import java.util.logging.Logger ; 27 import javax.swing.AbstractAction ; 28 import javax.swing.Action ; 29 import javax.swing.ImageIcon ; 30 import javax.swing.ImageIcon ; 31 import javax.swing.JComponent ; 32 import javax.swing.JMenu ; 33 import javax.swing.JMenuItem ; 34 import javax.swing.event.PopupMenuEvent ; 35 import javax.swing.event.PopupMenuListener ; 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 49 public class RecentFileAction extends AbstractAction implements Presenter.Menu, PopupMenuListener { 50 51 52 private static final String FO_PROP = "RecentFileAction.Recent_FO"; 53 54 55 private static final int MAX_COUNT = 15; 56 57 private JMenu menu; 58 59 public RecentFileAction() { 60 super(NbBundle.getMessage(RecentFileAction.class, "LBL_RecentFileAction_Name")); } 62 63 64 65 public JMenuItem 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 76 77 public void popupMenuWillBecomeVisible(PopupMenuEvent arg0) { 78 fillSubMenu(); 79 } 80 81 public void popupMenuWillBecomeInvisible(PopupMenuEvent arg0) { 82 menu.removeAll(); 83 } 84 85 public void popupMenuCanceled(PopupMenuEvent arg0) { 86 } 87 88 89 private void fillSubMenu () { 90 List <RecentFiles.HistoryItem> files = RecentFiles.getRecentFiles(); 91 92 int counter = 0; 93 for (HistoryItem hItem : files) { 94 if (++counter > MAX_COUNT) { 96 break; 97 } 98 Image 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 Logger.getLogger(RecentFiles.class.getName()).log( 106 Level.INFO, ex.getMessage(), ex); 107 continue; 108 } 109 JMenuItem jmi = null; 111 if (icon != null) { 112 jmi = new JMenuItem (hItem.getFile().getNameExt(), new ImageIcon (icon)); 113 } else { 114 jmi = new JMenuItem (hItem.getFile().getNameExt()); 115 } 116 jmi.putClientProperty(FO_PROP, hItem.getFile()); 117 jmi.addActionListener(this); 118 menu.add(jmi); 119 } 120 } 121 122 127 public void actionPerformed(ActionEvent evt) { 128 JMenuItem source = (JMenuItem ) evt.getSource(); 129 FileObject fo = (FileObject) source.getClientProperty(FO_PROP); 130 if (fo != null) { 131 OpenFile.open(fo, -1); 132 } 133 } 134 135 136 private static class UpdatingMenu extends JMenu implements DynamicMenuContent { 137 138 private final JComponent [] content = new JComponent [] { this }; 139 140 public UpdatingMenu (Action action) { 141 super(action); 142 } 143 144 public JComponent [] getMenuPresenters() { 145 setEnabled(!RecentFiles.getRecentFiles().isEmpty()); 146 return content; 147 } 148 149 public JComponent [] synchMenuPresenters(JComponent [] items) { 150 return getMenuPresenters(); 151 } 152 } 153 154 } 155 | Popular Tags |