KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > browser > RecentMenu


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.browser;
9
10 import org.gjt.jclasslib.util.GUIHelper;
11
12 import javax.swing.*;
13 import java.awt.event.ActionEvent JavaDoc;
14 import java.awt.event.ActionListener JavaDoc;
15 import java.io.File JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.util.*;
18 import java.util.prefs.BackingStoreException JavaDoc;
19 import java.util.prefs.Preferences JavaDoc;
20
21 /**
22     Menu that holds recent workspace files.
23
24     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
25     @version $Revision: 1.1 $ $Date: 2003/08/18 08:00:22 $
26 */

27 public class RecentMenu extends JMenu implements ActionListener JavaDoc {
28
29     private static final int RECENT_PROJECTS_MAX_SIZE = 10;
30     private static final String JavaDoc SETTINGS_RECENT_WORKSPACES = "recentWorkspaces";
31
32     private static final String JavaDoc ACTION_CLEAR_LIST = "clearList";
33     private BrowserMDIFrame frame;
34
35     private LinkedList recentWorkspaces = new LinkedList();
36
37     /**
38         Constructor.
39         @param frame the parent frame.
40      */

41     public RecentMenu(BrowserMDIFrame frame) {
42         this.frame = frame;
43         setText("Reopen workspace");
44         setIcon(GUIHelper.ICON_EMPTY);
45     }
46
47     public void menuSelectionChanged(boolean isIncluded) {
48         super.menuSelectionChanged(isIncluded);
49
50         updateContents();
51     }
52
53     public void actionPerformed(final ActionEvent JavaDoc event) {
54
55         if (event.getActionCommand().equals(ACTION_CLEAR_LIST)) {
56             recentWorkspaces.clear();
57         } else {
58             setPopupMenuVisible(false);
59             SwingUtilities.invokeLater(new Runnable JavaDoc() {
60                 public void run() {
61                     frame.openWorkspace(new File JavaDoc(((JMenuItem)event.getSource()).getText()));
62                 }
63             });
64
65         }
66     }
67
68     /**
69         Add a file to the list of recently useed workspaces.
70         @param file the workspace file.
71      */

72     public void addRecentWorkspace(File JavaDoc file) {
73
74         try {
75             String JavaDoc fileName = file.getCanonicalFile().getAbsolutePath();
76             recentWorkspaces.remove(fileName);
77             recentWorkspaces.addFirst(fileName);
78             if (recentWorkspaces.size() > RECENT_PROJECTS_MAX_SIZE) {
79                 recentWorkspaces.removeLast();
80             }
81         } catch (IOException JavaDoc e) {
82         }
83     }
84
85     /**
86         Read the list of recently used workspaces from the preferences store.
87         @param preferences the preferences node
88      */

89     public void read(Preferences JavaDoc preferences) {
90
91         recentWorkspaces.clear();
92
93         TreeMap numberToFile = new TreeMap();
94         Preferences JavaDoc recentNode = preferences.node(SETTINGS_RECENT_WORKSPACES);
95         try {
96             String JavaDoc[] keys = recentNode.keys();
97             for (int i = 0; i < keys.length; i++) {
98                 String JavaDoc key = keys[i];
99                 String JavaDoc fileName = recentNode.get(key, null);
100                 if (fileName != null) {
101                     numberToFile.put(new Integer JavaDoc(key), fileName);
102                 }
103             }
104             recentWorkspaces.addAll(numberToFile.values());
105         } catch (BackingStoreException JavaDoc ex) {
106         }
107     }
108
109     /**
110         Save the list of recently used workspaces to the preferences store.
111         @param preferences the preferences node
112      */

113     public void save(Preferences JavaDoc preferences) {
114
115         Preferences JavaDoc recentNode = preferences.node(SETTINGS_RECENT_WORKSPACES);
116         try {
117             recentNode.clear();
118         } catch (BackingStoreException JavaDoc e) {
119         }
120         int count = 0;
121         Iterator it = recentWorkspaces.iterator();
122         while (it.hasNext()) {
123             String JavaDoc fileName = (String JavaDoc)it.next();
124             recentNode.put(String.valueOf(count++), fileName);
125         }
126     }
127
128     private void updateContents() {
129
130         removeAll();
131         if (recentWorkspaces.size() > 0) {
132             Iterator it = recentWorkspaces.iterator();
133             while (it.hasNext()) {
134                 String JavaDoc fileName = (String JavaDoc)it.next();
135                 JMenuItem menuItem = new JMenuItem(fileName);
136                 menuItem.addActionListener(this);
137                 add(menuItem);
138             }
139             addSeparator();
140             JMenuItem menuItem = new JMenuItem("Clear list");
141             menuItem.setActionCommand(ACTION_CLEAR_LIST);
142             menuItem.addActionListener(this);
143             add(menuItem);
144         } else {
145             JMenuItem menuItem = new JMenuItem("(Empty)");
146             menuItem.setEnabled(false);
147             add(menuItem);
148         }
149     }
150
151 }
152
Popular Tags