KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > RecentFileManager


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.ui;
35
36 import javax.swing.*;
37 import java.awt.event.*;
38 import java.io.File JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.util.Vector JavaDoc;
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 /**
49  * Manages a list of the most recently used files to be displayed
50  * in the File menu.
51  * @version $Id: RecentFileManager.java 4076 2007-01-19 23:01:04Z dlsmith $
52  */

53 public class RecentFileManager implements OptionConstants {
54   /** Position in the file menu for the next insert. */
55   protected int _pos;
56
57   /** All of the recently used files in the list, in order. */
58   protected Vector JavaDoc<File JavaDoc> _recentFiles;
59
60   /** Menu items corresponding to each file in _recentFiles. */
61   protected Vector JavaDoc<JMenuItem> _recentMenuItems;
62
63   /** The maximum number of files to display in the list. */
64   protected int MAX = DrJava.getConfig().getSetting(RECENT_FILES_MAX_SIZE).intValue();
65
66   /** The File menu containing the entries. */
67   protected JMenu _fileMenu;
68
69   /** The OptionConstant that should be used to retrieve the list of recent files. */
70   protected VectorOption<File JavaDoc> _settingConfigConstant;
71
72   /** An action that will be invoked when the file is clicked. */
73   protected RecentFileAction _recentFileAction;
74
75   /** Creates a new RecentFileManager.
76    * @param pos Position in the file menu
77    * @param fileMenu File menu to add the entry to
78    */

79   public RecentFileManager(int pos, JMenu fileMenu, RecentFileAction action, VectorOption<File JavaDoc> settingConfigConstant) {
80     _pos = pos;
81     _fileMenu = fileMenu;
82     _recentFileAction = action;
83     _recentFiles = new Vector JavaDoc<File JavaDoc>();
84     _recentMenuItems = new Vector JavaDoc<JMenuItem>();
85     _settingConfigConstant = settingConfigConstant;
86
87     // Add each of the files stored in the config
88
Vector JavaDoc<File JavaDoc> files = DrJava.getConfig().getSetting(_settingConfigConstant);
89     
90     for (int i = files.size() - 1; i >= 0; i--) {
91       File JavaDoc f = files.get(i);
92       if (f.exists()) updateOpenFiles(f);
93     }
94   }
95
96   /** Returns the list of recently used files, in order. */
97   public Vector JavaDoc<File JavaDoc> getFileVector() { return _recentFiles; }
98
99   /** Changes the maximum number of files to display in the list.
100    * @param newMax The new maximum number of files to display
101    */

102   public void updateMax(int newMax) { MAX = newMax; }
103
104   /** Saves the current list of files to the config object. */
105   public void saveRecentFiles() {
106     DrJava.getConfig().setSetting(_settingConfigConstant,_recentFiles);
107   }
108
109   /** Updates the list after the given file has been opened. */
110   public void updateOpenFiles(final File JavaDoc file) {
111     
112     if (_recentMenuItems.size() == 0) {
113       _fileMenu.insertSeparator(_pos); //one at top
114
_pos++;
115     }
116     
117     final FileOpenSelector recentSelector = new FileOpenSelector() {
118       public File JavaDoc[] getFiles() { return new File JavaDoc[] { 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 JavaDoc e) {
131       // don't worry about it at this point
132
}
133     removeIfInList(file);
134     _recentMenuItems.add(0,newItem);
135     _recentFiles.add(0,file);
136     numberItems();
137     _fileMenu.insert(newItem,_pos);
138   }
139
140   /** Removes the given file from the list if it is already there.
141    * Only removes the first occurrence of the file, since each
142    * entry should be unique (based on canonical path).
143    */

144   public void removeIfInList(File JavaDoc file) {
145     // Use canonical path if possible
146
File JavaDoc canonical = null;
147     try { canonical = file.getCanonicalFile(); }
148     catch (IOException JavaDoc ioe) {
149       // Oh well, compare against the file as is
150
}
151
152     for (int i = 0; i < _recentFiles.size(); i++) {
153       File JavaDoc currFile = _recentFiles.get(i);
154       boolean match;
155       if (canonical != null) {
156         try { match = currFile.getCanonicalFile().equals(canonical); }
157         catch (IOException JavaDoc ioe) {
158           // Oh well, compare the files themselves
159
match = currFile.equals(file);
160         }
161       }
162       else {
163         // (couldn't find canonical for file; compare as is)
164
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   /**
178    * Trims the recent file list to the configured size and numbers the
179    * remaining files according to their position in the list
180    */

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     // remove the separator
198
if (MAX == 0 && !wasEmpty) { _fileMenu.remove(--_pos); }
199   }
200   
201   /**
202    * This interface is to be implemented and passed to the manager
203    * upon creation. This action specifies what is performed when the
204    * user selects a file from the list
205    */

206   public interface RecentFileAction {
207     public void actionPerformed(FileOpenSelector selector);
208   }
209 }
210
Popular Tags