Source Code Cross Referenced for RecentMenu.java in  » J2EE » jag » com » finalist » jaggenerator » menu » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » jag » com.finalist.jaggenerator.menu 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.