1 22 23 package org.gjt.sp.jedit.menu; 24 25 import java.awt.event.*; 27 import javax.swing.*; 28 import javax.swing.event.*; 29 import java.util.*; 30 import org.gjt.sp.jedit.browser.FileCellRenderer; 31 import org.gjt.sp.jedit.*; 32 34 public class RecentFilesProvider implements DynamicMenuProvider 35 { 36 public boolean updateEveryTime() 38 { 39 return false; 40 } 42 public void update(JMenu menu) 44 { 45 final View view = GUIUtilities.getView(menu); 46 47 ActionListener actionListener = new ActionListener() 49 { 50 public void actionPerformed(ActionEvent evt) 51 { 52 jEdit.openFile(view,evt.getActionCommand()); 53 view.getStatus().setMessage(null); 54 } 55 }; 57 74 76 ChangeListener changeListener = new ChangeListener() 78 { 79 public void stateChanged(ChangeEvent e) 80 { 81 JMenuItem menuItem = (JMenuItem) e.getSource(); 82 83 view.getStatus().setMessage(menuItem.isArmed()?menuItem.getActionCommand():null); 84 } 85 }; 87 List recentVector = BufferHistory.getHistory(); 88 89 if(recentVector.isEmpty()) 90 { 91 JMenuItem menuItem = new JMenuItem( 92 jEdit.getProperty("no-recent-files.label")); 93 menuItem.setEnabled(false); 94 menu.add(menuItem); 95 return; 96 } 97 98 final List<JMenuItem> menuItems = new ArrayList<JMenuItem>(); 99 final JTextField text = new JTextField(); 100 text.setToolTipText(jEdit.getProperty("recent-files.textfield.tooltip")); 101 menu.add(text); 102 text.addKeyListener(new KeyAdapter() 103 { 104 public void keyReleased(KeyEvent e) 105 { 106 String typedText = text.getText(); 107 for (JMenuItem tempMenuItem : menuItems) 108 { 109 if (typedText.length() == 0) 110 { 111 tempMenuItem.setEnabled(true); 112 } 113 else 114 { 115 String fileName = tempMenuItem.getText(); 116 boolean matchesStart = fileName.toLowerCase().startsWith(typedText.toLowerCase()); 117 tempMenuItem.setEnabled(matchesStart); 118 } 119 } 120 } 121 }); 122 123 boolean sort = jEdit.getBooleanProperty("sortRecent"); 124 125 int maxItems = jEdit.getIntegerProperty("menu.spillover",20); 126 127 Iterator iter = recentVector.iterator(); 128 while(iter.hasNext()) 129 { 130 String path = ((BufferHistory.Entry)iter.next()).path; 131 JMenuItem menuItem = new JMenuItem(MiscUtilities 132 .getFileName(path)); 133 menuItem.setActionCommand(path); 134 menuItem.addActionListener(actionListener); 135 menuItem.addChangeListener(changeListener); 137 138 menuItem.setIcon(FileCellRenderer.fileIcon); 139 140 menuItems.add(menuItem); 141 if(!sort) 142 { 143 if(menu.getMenuComponentCount() >= maxItems 144 && iter.hasNext()) 145 { 146 JMenu newMenu = new JMenu( 147 jEdit.getProperty("common.more")); 148 menu.add(newMenu); 149 menu = newMenu; 150 } 151 152 menu.add(menuItem); 153 } 154 } 155 156 if(sort) 157 { 158 Collections.sort(menuItems, 159 new MiscUtilities.MenuItemCompare()); 160 for(int i = 0; i < menuItems.size(); i++) 161 { 162 if(menu.getMenuComponentCount() >= maxItems 163 && i != 0) 164 { 165 JMenu newMenu = new JMenu( 166 jEdit.getProperty("common.more")); 167 menu.add(newMenu); 168 menu = newMenu; 169 } 170 171 menu.add((JMenuItem)menuItems.get(i)); 172 } 173 } 174 JMenuItem menuItem = new JMenuItem(jEdit.getProperty("clear-recent-files.label")); 175 menuItem.addActionListener(new ActionListener() 176 { 177 public void actionPerformed(ActionEvent e) 178 { 179 BufferHistory.clear(); 180 } 181 }); 182 menu.addSeparator(); 183 menu.add(menuItem); 184 } } 186 | Popular Tags |