1 33 34 package edu.rice.cs.drjava.ui; 35 36 import javax.swing.*; 37 import java.awt.event.*; 38 import java.io.File ; 39 import java.io.IOException ; 40 import java.util.Vector ; 41 42 import edu.rice.cs.drjava.DrJava; 43 import edu.rice.cs.drjava.model.*; 44 import edu.rice.cs.drjava.config.*; 45 46 import edu.rice.cs.util.FileOpenSelector; 47 48 53 public class RecentFileManager implements OptionConstants { 54 55 protected int _pos; 56 57 58 protected Vector <File > _recentFiles; 59 60 61 protected Vector <JMenuItem> _recentMenuItems; 62 63 64 protected int MAX = DrJava.getConfig().getSetting(RECENT_FILES_MAX_SIZE).intValue(); 65 66 67 protected JMenu _fileMenu; 68 69 70 protected VectorOption<File > _settingConfigConstant; 71 72 73 protected RecentFileAction _recentFileAction; 74 75 79 public RecentFileManager(int pos, JMenu fileMenu, RecentFileAction action, VectorOption<File > settingConfigConstant) { 80 _pos = pos; 81 _fileMenu = fileMenu; 82 _recentFileAction = action; 83 _recentFiles = new Vector <File >(); 84 _recentMenuItems = new Vector <JMenuItem>(); 85 _settingConfigConstant = settingConfigConstant; 86 87 Vector <File > files = DrJava.getConfig().getSetting(_settingConfigConstant); 89 90 for (int i = files.size() - 1; i >= 0; i--) { 91 File f = files.get(i); 92 if (f.exists()) updateOpenFiles(f); 93 } 94 } 95 96 97 public Vector <File > getFileVector() { return _recentFiles; } 98 99 102 public void updateMax(int newMax) { MAX = newMax; } 103 104 105 public void saveRecentFiles() { 106 DrJava.getConfig().setSetting(_settingConfigConstant,_recentFiles); 107 } 108 109 110 public void updateOpenFiles(final File file) { 111 112 if (_recentMenuItems.size() == 0) { 113 _fileMenu.insertSeparator(_pos); _pos++; 115 } 116 117 final FileOpenSelector recentSelector = new FileOpenSelector() { 118 public File [] getFiles() { return new File [] { file }; } 119 }; 120 121 JMenuItem newItem = new JMenuItem(""); 122 newItem.addActionListener(new AbstractAction("Open " + file.getName()) { 123 public void actionPerformed(ActionEvent ae) { 124 if (_recentFileAction != null) { 125 _recentFileAction.actionPerformed(recentSelector); 126 } 127 } 128 }); 129 try { newItem.setToolTipText(file.getCanonicalPath()); } 130 catch(IOException e) { 131 } 133 removeIfInList(file); 134 _recentMenuItems.add(0,newItem); 135 _recentFiles.add(0,file); 136 numberItems(); 137 _fileMenu.insert(newItem,_pos); 138 } 139 140 144 public void removeIfInList(File file) { 145 File canonical = null; 147 try { canonical = file.getCanonicalFile(); } 148 catch (IOException ioe) { 149 } 151 152 for (int i = 0; i < _recentFiles.size(); i++) { 153 File currFile = _recentFiles.get(i); 154 boolean match; 155 if (canonical != null) { 156 try { match = currFile.getCanonicalFile().equals(canonical); } 157 catch (IOException ioe) { 158 match = currFile.equals(file); 160 } 161 } 162 else { 163 match = currFile.equals(file); 165 } 166 167 if (match) { 168 _recentFiles.remove(i); 169 JMenuItem menuItem = _recentMenuItems.get(i); 170 _fileMenu.remove(menuItem); 171 _recentMenuItems.remove(i); 172 break; 173 } 174 } 175 } 176 177 181 public void numberItems() { 182 int delPos = _recentMenuItems.size(); 183 boolean wasEmpty = (delPos == 0); 184 while (delPos > MAX) { 185 JMenuItem delItem = _recentMenuItems.get(delPos - 1); 186 _recentMenuItems.remove(delPos - 1); 187 _recentFiles.remove(delPos - 1); 188 _fileMenu.remove(delItem); 189 190 delPos = _recentMenuItems.size(); 191 } 192 JMenuItem currItem; 193 for (int i=0; i< _recentMenuItems.size(); i++ ) { 194 currItem = _recentMenuItems.get(i); 195 currItem.setText((i+1) + ". " + _recentFiles.get(i).getName()); 196 } 197 if (MAX == 0 && !wasEmpty) { _fileMenu.remove(--_pos); } 199 } 200 201 206 public interface RecentFileAction { 207 public void actionPerformed(FileOpenSelector selector); 208 } 209 } 210 | Popular Tags |