1 8 package com.finalist.jaggenerator.menu; 9 10 import java.awt.event.ActionEvent ; 11 import java.awt.event.ActionListener ; 12 import java.io.File ; 13 import java.io.FileInputStream ; 14 import java.io.FileNotFoundException ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.util.ArrayList ; 18 import java.util.Collections ; 19 import java.util.Date ; 20 import java.util.Iterator ; 21 import java.util.Properties ; 22 import java.util.TreeMap ; 23 24 import javax.swing.JMenu ; 25 import javax.swing.JMenuItem ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import com.finalist.jaggenerator.JagGenerator; 31 32 public class RecentMenu extends JMenu implements ActionListener { 33 34 37 static Log log = LogFactory.getLog(RecentMenu.class); 38 39 private static final long serialVersionUID = 1L; 40 41 private static final String FS = System.getProperty("file.separator"); 42 43 44 private static String CONFIG_DIR = System.getProperty("user.home") + FS 45 + ".jag"; 46 47 private static String CONFIG_FILE = CONFIG_DIR + FS + "recent.properties"; 48 49 private static final int MAX_RECENT = 10; 50 51 private static final String RECENT_FILE_SELECTED = "_recent_file_selected"; 52 53 private JagGenerator mainApp = null; 54 55 public RecentMenu() { 56 initialize(); 57 } 58 59 private void initialize() { 60 checkDir(CONFIG_DIR); 61 loadRecentList(); 62 } 63 64 private void loadRecentList() { 65 int i = 1; 66 boolean mod = false; 67 removeAll(); 68 TreeMap recent = getRecentFiles(); 69 Iterator iter = recent.keySet().iterator(); 70 71 while (iter.hasNext()) { 72 String array_element = (String ) recent.get(iter.next()); 73 File f = new File (array_element); 76 if (f.canRead()) { 77 JMenuItem jmi = new JMenuItem ("" + i + " " + f.getName()); 78 jmi.setToolTipText(array_element); 79 jmi.setMnemonic(i + '0'); 80 jmi.setActionCommand(RECENT_FILE_SELECTED); 81 jmi.setName(array_element); 82 jmi.addActionListener(this); 83 add(jmi); 84 i++; 85 } else { 86 mod = true; 87 recent.remove(array_element); 88 } 89 if (mod) 90 saveRecentFileList(recent); 91 } 92 93 } 94 95 private TreeMap getRecentFiles() { 96 TreeMap ret = null; 97 Properties prop = new Properties (); 98 try { 99 if (new File (CONFIG_FILE).exists()) 100 prop.load(new FileInputStream (CONFIG_FILE)); 101 else 102 createPropertiesFile(); 103 ret = new TreeMap (Collections.reverseOrder()); 104 ret.putAll(prop); 105 if (!ret.isEmpty()) 106 JagGenerator.setFileChooserStartDir( 107 JagGenerator.FILECHOOSER_APPFILE_OPEN, new File ( 108 (String ) ret.get(ret.firstKey())).getParentFile()); 109 } catch (FileNotFoundException e) { 110 log.error("", e); 111 } catch (IOException e) { 112 log.error("", e); 113 } 114 return ret; 115 } 116 117 121 public static void checkDir(String name) { 122 123 java.io.File dir = new java.io.File (name); 124 if (dir.exists()) { 125 if (!dir.isDirectory()) { 126 log.error("Application must be able to read from: " 127 + dir.getAbsolutePath()); 128 createTmpProperties(); 129 } 130 } else { 131 if (!dir.mkdirs()) { 132 log.error("Application must be able to write to: " 133 + dir.getAbsolutePath()); 134 createTmpProperties(); 135 } 136 } 137 } 138 139 142 private static void createTmpProperties() { 143 CONFIG_DIR = System.getProperty("java.io.tmpdir"); 144 CONFIG_FILE = CONFIG_DIR + FS + "recent.properties"; 145 Properties prop = new Properties (); 146 try { 147 prop.store(new FileOutputStream (CONFIG_FILE), ""); 148 } catch (FileNotFoundException e) { 149 log.error("", e); 151 } catch (IOException e) { 152 log.error("", e); 154 } 155 } 156 157 private static void createPropertiesFile() { 158 Properties prop = new Properties (); 159 try { 160 prop.store(new FileOutputStream (CONFIG_FILE), ""); 161 } catch (FileNotFoundException e) { 162 log.error("", e); 164 } catch (IOException e) { 165 log.error("", e); 167 } 168 } 169 170 public void actionPerformed(ActionEvent e) { 171 if (e.getSource() instanceof JMenuItem ) { 172 JMenuItem jmi = (JMenuItem ) e.getSource(); 173 if (jmi.getActionCommand().equals(RECENT_FILE_SELECTED)) { 174 getMainApp().loadApplicationFile(new File (jmi.getName())); 175 } 176 } 177 } 178 179 public JagGenerator getMainApp() { 180 return mainApp; 181 } 182 183 public void setMainApp(JagGenerator mainApp) { 184 this.mainApp = mainApp; 185 } 186 187 public void addToRecentList(String fullFilePath) { 188 Date d = new Date (); 189 TreeMap recent = getRecentFiles(); 190 while (recent.size() >= MAX_RECENT) { 191 recent.remove(recent.lastKey()); 192 } 193 if (recent.containsValue(fullFilePath)) { 194 removeFromTreeMap(recent, fullFilePath); 195 } 196 recent.put(new Long (d.getTime()).toString(), fullFilePath); 197 saveRecentFileList(recent); 198 loadRecentList(); 199 } 200 201 private void removeFromTreeMap(TreeMap recent, String fullFilePath) { 202 Iterator iter = recent.keySet().iterator(); 203 ArrayList al = new ArrayList (); 204 while (iter.hasNext()) { 205 Object key = iter.next(); 206 if (recent.get(key).equals(fullFilePath)) 207 al.add(key); 208 } 209 iter = al.iterator(); 210 while (iter.hasNext()) 211 recent.remove(iter.next()); 212 } 213 214 private void saveRecentFileList(TreeMap recent) { 215 Properties props = new Properties (); 216 props.putAll(recent); 217 try { 218 props.store(new FileOutputStream (CONFIG_FILE), 219 "JAG recent project list"); 220 } catch (FileNotFoundException e) { 221 log.error("", e); 223 } catch (IOException e) { 224 log.error("", e); 226 } 227 } 228 229 public void removeFromRecentList(String absolutePath) { 230 TreeMap recent = getRecentFiles(); 231 recent.remove(absolutePath); 232 saveRecentFileList(recent); 233 } 234 235 } 236 | Popular Tags |