001: /* RecentMenu.java
002: * Created on Apr 12, 2006
003: *
004: * Last modified: $Date: 2006/05/21 07:57:49 $
005: * @version $Revision: 1.2 $
006: * @author afeltes
007: */
008: package com.finalist.jaggenerator.menu;
009:
010: import java.awt.event.ActionEvent;
011: import java.awt.event.ActionListener;
012: import java.io.File;
013: import java.io.FileInputStream;
014: import java.io.FileNotFoundException;
015: import java.io.FileOutputStream;
016: import java.io.IOException;
017: import java.util.ArrayList;
018: import java.util.Collections;
019: import java.util.Date;
020: import java.util.Iterator;
021: import java.util.Properties;
022: import java.util.TreeMap;
023:
024: import javax.swing.JMenu;
025: import javax.swing.JMenuItem;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: import com.finalist.jaggenerator.JagGenerator;
031:
032: public class RecentMenu extends JMenu implements ActionListener {
033:
034: /**
035: *
036: */
037: static Log log = LogFactory.getLog(RecentMenu.class);
038:
039: private static final long serialVersionUID = 1L;
040:
041: private static final String FS = System
042: .getProperty("file.separator");
043:
044: /* In case there is no $HOME, it will write to $TEMP */
045: private static String CONFIG_DIR = System.getProperty("user.home")
046: + FS + ".jag";
047:
048: private static String CONFIG_FILE = CONFIG_DIR + FS
049: + "recent.properties";
050:
051: private static final int MAX_RECENT = 10;
052:
053: private static final String RECENT_FILE_SELECTED = "_recent_file_selected";
054:
055: private JagGenerator mainApp = null;
056:
057: public RecentMenu() {
058: initialize();
059: }
060:
061: private void initialize() {
062: checkDir(CONFIG_DIR);
063: loadRecentList();
064: }
065:
066: private void loadRecentList() {
067: int i = 1;
068: boolean mod = false;
069: removeAll();
070: TreeMap recent = getRecentFiles();
071: Iterator iter = recent.keySet().iterator();
072:
073: while (iter.hasNext()) {
074: String array_element = (String) recent.get(iter.next());
075: // Object obj = iter.next();
076: // String array_element = "";
077: File f = new File(array_element);
078: if (f.canRead()) {
079: JMenuItem jmi = new JMenuItem("" + i + " "
080: + f.getName());
081: jmi.setToolTipText(array_element);
082: jmi.setMnemonic(i + '0');
083: jmi.setActionCommand(RECENT_FILE_SELECTED);
084: jmi.setName(array_element);
085: jmi.addActionListener(this );
086: add(jmi);
087: i++;
088: } else {
089: mod = true;
090: recent.remove(array_element);
091: }
092: if (mod)
093: saveRecentFileList(recent);
094: }
095:
096: }
097:
098: private TreeMap getRecentFiles() {
099: TreeMap ret = null;
100: Properties prop = new Properties();
101: try {
102: if (new File(CONFIG_FILE).exists())
103: prop.load(new FileInputStream(CONFIG_FILE));
104: else
105: createPropertiesFile();
106: ret = new TreeMap(Collections.reverseOrder());
107: ret.putAll(prop);
108: if (!ret.isEmpty())
109: JagGenerator.setFileChooserStartDir(
110: JagGenerator.FILECHOOSER_APPFILE_OPEN,
111: new File((String) ret.get(ret.firstKey()))
112: .getParentFile());
113: } catch (FileNotFoundException e) {
114: log.error("", e);
115: } catch (IOException e) {
116: log.error("", e);
117: }
118: return ret;
119: }
120:
121: /**
122: * Check if directory exists and if it writeable, or create it if it does
123: * not
124: */
125: public static void checkDir(String name) {
126:
127: java.io.File dir = new java.io.File(name);
128: if (dir.exists()) {
129: if (!dir.isDirectory()) {
130: log.error("Application must be able to read from: "
131: + dir.getAbsolutePath());
132: createTmpProperties();
133: }
134: } else {
135: if (!dir.mkdirs()) {
136: log.error("Application must be able to write to: "
137: + dir.getAbsolutePath());
138: createTmpProperties();
139: }
140: }
141: }
142:
143: /**
144: *
145: */
146: private static void createTmpProperties() {
147: CONFIG_DIR = System.getProperty("java.io.tmpdir");
148: CONFIG_FILE = CONFIG_DIR + FS + "recent.properties";
149: Properties prop = new Properties();
150: try {
151: prop.store(new FileOutputStream(CONFIG_FILE), "");
152: } catch (FileNotFoundException e) {
153: // TODO Auto-generated catch block
154: log.error("", e);
155: } catch (IOException e) {
156: // TODO Auto-generated catch block
157: log.error("", e);
158: }
159: }
160:
161: private static void createPropertiesFile() {
162: Properties prop = new Properties();
163: try {
164: prop.store(new FileOutputStream(CONFIG_FILE), "");
165: } catch (FileNotFoundException e) {
166: // TODO Auto-generated catch block
167: log.error("", e);
168: } catch (IOException e) {
169: // TODO Auto-generated catch block
170: log.error("", e);
171: }
172: }
173:
174: public void actionPerformed(ActionEvent e) {
175: if (e.getSource() instanceof JMenuItem) {
176: JMenuItem jmi = (JMenuItem) e.getSource();
177: if (jmi.getActionCommand().equals(RECENT_FILE_SELECTED)) {
178: getMainApp().loadApplicationFile(
179: new File(jmi.getName()));
180: }
181: }
182: }
183:
184: public JagGenerator getMainApp() {
185: return mainApp;
186: }
187:
188: public void setMainApp(JagGenerator mainApp) {
189: this .mainApp = mainApp;
190: }
191:
192: public void addToRecentList(String fullFilePath) {
193: Date d = new Date();
194: TreeMap recent = getRecentFiles();
195: while (recent.size() >= MAX_RECENT) {
196: recent.remove(recent.lastKey());
197: }
198: if (recent.containsValue(fullFilePath)) {
199: removeFromTreeMap(recent, fullFilePath);
200: }
201: recent.put(new Long(d.getTime()).toString(), fullFilePath);
202: saveRecentFileList(recent);
203: loadRecentList();
204: }
205:
206: private void removeFromTreeMap(TreeMap recent, String fullFilePath) {
207: Iterator iter = recent.keySet().iterator();
208: ArrayList al = new ArrayList();
209: while (iter.hasNext()) {
210: Object key = iter.next();
211: if (recent.get(key).equals(fullFilePath))
212: al.add(key);
213: }
214: iter = al.iterator();
215: while (iter.hasNext())
216: recent.remove(iter.next());
217: }
218:
219: private void saveRecentFileList(TreeMap recent) {
220: Properties props = new Properties();
221: props.putAll(recent);
222: try {
223: props.store(new FileOutputStream(CONFIG_FILE),
224: "JAG recent project list");
225: } catch (FileNotFoundException e) {
226: // TODO Auto-generated catch block
227: log.error("", e);
228: } catch (IOException e) {
229: // TODO Auto-generated catch block
230: log.error("", e);
231: }
232: }
233:
234: public void removeFromRecentList(String absolutePath) {
235: TreeMap recent = getRecentFiles();
236: recent.remove(absolutePath);
237: saveRecentFileList(recent);
238: }
239:
240: }
|