1 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 ; 14 import java.awt.event.ActionListener ; 15 import java.io.File ; 16 import java.io.IOException ; 17 import java.util.*; 18 import java.util.prefs.BackingStoreException ; 19 import java.util.prefs.Preferences ; 20 21 27 public class RecentMenu extends JMenu implements ActionListener { 28 29 private static final int RECENT_PROJECTS_MAX_SIZE = 10; 30 private static final String SETTINGS_RECENT_WORKSPACES = "recentWorkspaces"; 31 32 private static final String ACTION_CLEAR_LIST = "clearList"; 33 private BrowserMDIFrame frame; 34 35 private LinkedList recentWorkspaces = new LinkedList(); 36 37 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 event) { 54 55 if (event.getActionCommand().equals(ACTION_CLEAR_LIST)) { 56 recentWorkspaces.clear(); 57 } else { 58 setPopupMenuVisible(false); 59 SwingUtilities.invokeLater(new Runnable () { 60 public void run() { 61 frame.openWorkspace(new File (((JMenuItem)event.getSource()).getText())); 62 } 63 }); 64 65 } 66 } 67 68 72 public void addRecentWorkspace(File file) { 73 74 try { 75 String 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 e) { 82 } 83 } 84 85 89 public void read(Preferences preferences) { 90 91 recentWorkspaces.clear(); 92 93 TreeMap numberToFile = new TreeMap(); 94 Preferences recentNode = preferences.node(SETTINGS_RECENT_WORKSPACES); 95 try { 96 String [] keys = recentNode.keys(); 97 for (int i = 0; i < keys.length; i++) { 98 String key = keys[i]; 99 String fileName = recentNode.get(key, null); 100 if (fileName != null) { 101 numberToFile.put(new Integer (key), fileName); 102 } 103 } 104 recentWorkspaces.addAll(numberToFile.values()); 105 } catch (BackingStoreException ex) { 106 } 107 } 108 109 113 public void save(Preferences preferences) { 114 115 Preferences recentNode = preferences.node(SETTINGS_RECENT_WORKSPACES); 116 try { 117 recentNode.clear(); 118 } catch (BackingStoreException e) { 119 } 120 int count = 0; 121 Iterator it = recentWorkspaces.iterator(); 122 while (it.hasNext()) { 123 String fileName = (String )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 fileName = (String )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 |