KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jaggenerator > menu > RecentMenu


1 /* RecentMenu.java
2  * Created on Apr 12, 2006
3  *
4  * Last modified: $Date: 2006/05/21 07:57:49 $
5  * @version $Revision: 1.2 $
6  * @author afeltes
7  */

8 package com.finalist.jaggenerator.menu;
9
10 import java.awt.event.ActionEvent JavaDoc;
11 import java.awt.event.ActionListener JavaDoc;
12 import java.io.File JavaDoc;
13 import java.io.FileInputStream JavaDoc;
14 import java.io.FileNotFoundException JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Properties JavaDoc;
22 import java.util.TreeMap JavaDoc;
23
24 import javax.swing.JMenu JavaDoc;
25 import javax.swing.JMenuItem JavaDoc;
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 JavaDoc implements ActionListener JavaDoc {
33
34     /**
35      *
36      */

37     static Log log = LogFactory.getLog(RecentMenu.class);
38
39     private static final long serialVersionUID = 1L;
40
41     private static final String JavaDoc FS = System.getProperty("file.separator");
42
43     /* In case there is no $HOME, it will write to $TEMP */
44     private static String JavaDoc CONFIG_DIR = System.getProperty("user.home") + FS
45             + ".jag";
46
47     private static String JavaDoc CONFIG_FILE = CONFIG_DIR + FS + "recent.properties";
48
49     private static final int MAX_RECENT = 10;
50
51     private static final String JavaDoc 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 JavaDoc recent = getRecentFiles();
69         Iterator JavaDoc iter = recent.keySet().iterator();
70
71         while (iter.hasNext()) {
72             String JavaDoc array_element = (String JavaDoc) recent.get(iter.next());
73             // Object obj = iter.next();
74
// String array_element = "";
75
File JavaDoc f = new File JavaDoc(array_element);
76             if (f.canRead()) {
77                 JMenuItem JavaDoc jmi = new JMenuItem JavaDoc("" + 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 JavaDoc getRecentFiles() {
96         TreeMap JavaDoc ret = null;
97         Properties JavaDoc prop = new Properties JavaDoc();
98         try {
99             if (new File JavaDoc(CONFIG_FILE).exists())
100                 prop.load(new FileInputStream JavaDoc(CONFIG_FILE));
101             else
102                 createPropertiesFile();
103             ret = new TreeMap JavaDoc(Collections.reverseOrder());
104             ret.putAll(prop);
105             if (!ret.isEmpty())
106                 JagGenerator.setFileChooserStartDir(
107                         JagGenerator.FILECHOOSER_APPFILE_OPEN, new File JavaDoc(
108                                 (String JavaDoc) ret.get(ret.firstKey())).getParentFile());
109         } catch (FileNotFoundException JavaDoc e) {
110             log.error("", e);
111         } catch (IOException JavaDoc e) {
112             log.error("", e);
113         }
114         return ret;
115     }
116
117     /**
118      * Check if directory exists and if it writeable, or create it if it does
119      * not
120      */

121     public static void checkDir(String JavaDoc name) {
122
123         java.io.File JavaDoc dir = new java.io.File JavaDoc(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     /**
140      *
141      */

142     private static void createTmpProperties() {
143         CONFIG_DIR = System.getProperty("java.io.tmpdir");
144         CONFIG_FILE = CONFIG_DIR + FS + "recent.properties";
145         Properties JavaDoc prop = new Properties JavaDoc();
146         try {
147             prop.store(new FileOutputStream JavaDoc(CONFIG_FILE), "");
148         } catch (FileNotFoundException JavaDoc e) {
149             // TODO Auto-generated catch block
150
log.error("", e);
151         } catch (IOException JavaDoc e) {
152             // TODO Auto-generated catch block
153
log.error("", e);
154         }
155     }
156
157     private static void createPropertiesFile() {
158         Properties JavaDoc prop = new Properties JavaDoc();
159         try {
160             prop.store(new FileOutputStream JavaDoc(CONFIG_FILE), "");
161         } catch (FileNotFoundException JavaDoc e) {
162             // TODO Auto-generated catch block
163
log.error("", e);
164         } catch (IOException JavaDoc e) {
165             // TODO Auto-generated catch block
166
log.error("", e);
167         }
168     }
169
170     public void actionPerformed(ActionEvent JavaDoc e) {
171         if (e.getSource() instanceof JMenuItem JavaDoc) {
172             JMenuItem JavaDoc jmi = (JMenuItem JavaDoc) e.getSource();
173             if (jmi.getActionCommand().equals(RECENT_FILE_SELECTED)) {
174                 getMainApp().loadApplicationFile(new File JavaDoc(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 JavaDoc fullFilePath) {
188         Date JavaDoc d = new Date JavaDoc();
189         TreeMap JavaDoc 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 JavaDoc(d.getTime()).toString(), fullFilePath);
197         saveRecentFileList(recent);
198         loadRecentList();
199     }
200
201     private void removeFromTreeMap(TreeMap JavaDoc recent, String JavaDoc fullFilePath) {
202         Iterator JavaDoc iter = recent.keySet().iterator();
203         ArrayList JavaDoc al = new ArrayList JavaDoc();
204         while (iter.hasNext()) {
205             Object JavaDoc 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 JavaDoc recent) {
215         Properties JavaDoc props = new Properties JavaDoc();
216         props.putAll(recent);
217         try {
218             props.store(new FileOutputStream JavaDoc(CONFIG_FILE),
219                     "JAG recent project list");
220         } catch (FileNotFoundException JavaDoc e) {
221             // TODO Auto-generated catch block
222
log.error("", e);
223         } catch (IOException JavaDoc e) {
224             // TODO Auto-generated catch block
225
log.error("", e);
226         }
227     }
228
229     public void removeFromRecentList(String JavaDoc absolutePath) {
230         TreeMap JavaDoc recent = getRecentFiles();
231         recent.remove(absolutePath);
232         saveRecentFileList(recent);
233     }
234
235 }
236
Popular Tags