chrisw 2003/09/28 23:40:00 Modified: src/java/org/apache/tools/ant/gui/modules Reopener.java Log: Make reopen use PropertiesManager Submitted by: Jack Woehr Reviewed by: Christoph Wilhelms CVS: ---------------------------------------------------------------------- CVS: PR: CVS: If this change addresses a PR in the problem report tracking CVS: database, then enter the PR number(s) here. CVS: Obtained from: CVS: If this change has been taken from another system, such as NCSA, CVS: then name the system in this line, otherwise delete it. CVS: Submitted by: CVS: If this code has been contributed to Apache by someone else; i.e., CVS: they sent us a patch or a new module, then include their name/email CVS: address here. If this is your work then delete this line. CVS: Reviewed by: CVS: If we are doing pre-commit code reviews and someone else has CVS: reviewed your changes, include their name(s) here. CVS: If you have not had it reviewed then delete this line. Revision Changes Path 1.5 +104 -61 ant-antidote/src/java/org/apache/tools/ant/gui/modules/Reopener.java Index: Reopener.java =================================================================== RCS file: /home/cvs/ant-antidote/src/java/org/apache/tools/ant/gui/modules/Reopener.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- Reopener.java 28 Sep 2003 13:02:59 -0000 1.4 +++ Reopener.java 29 Sep 2003 06:40:00 -0000 1.5 @@ -54,9 +54,6 @@ package org.apache.tools.ant.gui.modules; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; @@ -75,6 +72,7 @@ import org.apache.tools.ant.gui.core.AntMenu; import org.apache.tools.ant.gui.core.AntModule; import org.apache.tools.ant.gui.core.AppContext; +import org.apache.tools.ant.gui.core.PropertiesManager; import org.apache.tools.ant.gui.event.BusFilter; import org.apache.tools.ant.gui.event.BusMember; import org.apache.tools.ant.gui.event.EventBus; @@ -84,8 +82,8 @@ /** * Adds a "Reopen" menu which contains a list of files which * have been opened and closed. - * - * @version $Revision$ + * + * @version $Revision$ * @author Nick Davis */ public class Reopener extends AntModule { @@ -96,30 +94,71 @@ private JMenu _menu = null; /** Used to format the time the file was closed */ private SimpleDateFormat _formatter - = new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss a zzz"); + = new SimpleDateFormat("yyyy.MM.dd G 'at' hh:mm:ss a zzz"); /** The title of our menu */ private String _menuName = null; /** The menu we put our menu after */ private String _insertAfterMenuName = null; /** The number of files to display */ private static final int NUMBER_MENU_ITEMS = 10; - - /** + + /** The string prefixed to every reopener filelist entry in our Properties */ + public static final String REOPENER_FILELIST_PROPERTY_PREFIX="Reopener.filelist."; + + /** Test if a key (i.e., a Properties file key in our persistent properties) + * is a key written/read by the Reopener as a filelist member. + * @param key A string used as a properties file key. + * @return true if indeed the string represents a Reopener filelist key. + */ + public static boolean isReopenerFilelistKey(String key) { + boolean result = false; + if (null != key) { + result = key.startsWith(REOPENER_FILELIST_PROPERTY_PREFIX); + } + return result; + } + + /** Make a file key intended to be stored in our persistent properties + * into a key written/read by the Reopener as a filelist member by prefixing + * it correctly. + * @param key A string used by the Reopener intended to be used as a properties file key. + * @return The same string prefixed with the Reopener filelist key. + */ + public static String makeReopenerFilelistKey(String protoKey) { + String result= REOPENER_FILELIST_PROPERTY_PREFIX + protoKey; + return result; + } + + /** Make a file key intended to be stored in our persistent properties + * into a the kind of key understood by the Reopener as a filelist member by + * stripping the prefix that identifies it as a Reopener key in the properties file. + * @param key A string used as a properties file key. + * @return The same string with the prefix for a Reopener filelist key stripped from it. + */ + public static String stripReopenerFilelistKey(String protoKey) { + String result = protoKey; + if (isReopenerFilelistKey(protoKey)) { + result = protoKey.substring(REOPENER_FILELIST_PROPERTY_PREFIX.length()); + } + return result; + } + + /** * Register our event listener - * + * * @param context Application context. */ public void contextualize(AppContext context) { setContext(context); _menuName = - context.getResources().getString(getClass(), "menuName"); + context.getResources().getString(getClass(), "menuName"); _insertAfterMenuName = - context.getResources().getString(getClass(), "insertAfterMenuName"); + context.getResources().getString(getClass(), "insertAfterMenuName"); context.getEventBus().addMember(EventBus.RESPONDING, new Handler()); } - + /** * Insert our menu into the main menu bar * @@ -132,7 +171,7 @@ // Load the list of files loadList(); - + // Loop throught the menus and look for the menu // we put our menu after. boolean breakOut = false; @@ -158,9 +197,9 @@ } updateMenu(); } - + /** - * + * */ protected void updateMenu() { @@ -182,7 +221,7 @@ map.put(new Long(date.getTime() * -1), key); } catch (Exception e) {} } - + // Remove all of our menu items. _menu.removeAll(); @@ -197,17 +236,17 @@ // Setup an event which will be sent when // the menu item is pressed. OpenRequestEvent event = new OpenRequestEvent( - getContext(), new File(name)); + getContext(), new File(name)); AntMenu subMenu = new AntMenu(getContext(), - event, name); + event, name); JMenuItem item = _menu.add(subMenu); item.setAccelerator( - KeyStroke.getKeyStroke("control " + count)); + KeyStroke.getKeyStroke("control " + count)); count++; } } - + /** * Remove the oldest entries from the list, * if the list is too large. @@ -226,7 +265,11 @@ // Remove any extra entries while (map.size() > NUMBER_MENU_ITEMS) { - map.remove(map.firstKey()); + Object key = map.firstKey(); + map.remove(key); // Remove it from our temp list used to reinstance _fileList + // .AND. remove it from our global properties (first converting key into form + // our persistent properties knows about!). + getContext().getPropertiesManager().remove(makeReopenerFilelistKey(key.toString())); } // Transfer the temp map to the real map. @@ -238,75 +281,75 @@ _fileList.put(value, key); } } - - /** - * @return the file used to save the list - */ - protected File getSaveFile() { - String home = System.getProperty("user.home"); - return new File(home + "/antidote.reopen.properties"); - } - + /** - * Saves the list + * Saves the list to the global PropertiesManager and causes a props file save. */ protected void saveList() { + Iterator i = _fileList.keySet().iterator(); + PropertiesManager pm = getContext().getPropertiesManager(); + while(i.hasNext()) { + String key = (String) i.next(); + String value = (String) _fileList.get(key); + pm.setProperty(makeReopenerFilelistKey(key), value); + } try { - File file = getSaveFile(); - if (!file.exists()) { - file.createNewFile(); - } - FileOutputStream out = new FileOutputStream(file); - _fileList.store(out, null); - } catch (IOException e) { - e.printStackTrace(); + getContext().saveProperties(); + } + catch (java.io.FileNotFoundException e) { + // Log error? Show dialog? Couldn't save. + } + catch (java.io.IOException e) { + // Log error? Show dialog? Couldn't save. } } - + /** - * Loads the list + * Refreshes the Properties used by the Reopened from the global PropertiesManager. */ protected void loadList() { - try { - File file = getSaveFile(); - if (file.exists()) - { - FileInputStream in = new FileInputStream(file); - _fileList.load(in); + _fileList.clear(); + PropertiesManager pm = getContext().getPropertiesManager(); + Iterator i = pm.keySet().iterator(); + String key = null; + String value = null; + while(i.hasNext()) { + key = (String) i.next(); + if (isReopenerFilelistKey(key)) { + value = (String) pm.getProperty(key); + _fileList.setProperty(stripReopenerFilelistKey(key), value); } - } catch (IOException e) { - e.printStackTrace(); } } /** Class for handling project events. */ private class Handler implements BusMember { private final Filter _filter = new Filter(); - - /** + + /** * Get the filter to that is used to determine if an event should * to to the member. - * + * * @return Filter to use. */ public BusFilter getBusFilter() { return _filter; } - /** + /** * Called when an event is to be posed to the member. - * + * * @param event Event to post. * @return true if event should be propogated, false if * it should be cancelled. */ public boolean eventPosted(EventObject event) { - + // We should only get project closed events - + // Find the name of the file which was just closed. ACSProjectElement project = - getContext().getSelectionManager().getSelectedProject(); + getContext().getSelectionManager().getSelectedProject(); URL url = project.getLocation(); if (url == null) { return true; @@ -319,20 +362,20 @@ // Add the file to the list _fileList.put(file, dateString); - + // Update the menu and save. updateMenu(); saveList(); return true; } } - + /** Class providing filtering for project events. */ private static class Filter implements BusFilter { - /** + /** * Determines if the given event should be accepted. - * + * * @param event Event to test. * @return True if event should be given to BusMember, false otherwise. */
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]