You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ps...@apache.org on 2003/05/08 06:00:18 UTC

cvs commit: jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw/prefs default.properties SettingsManager.java SaveSettingsEvent.java LoadSettingsEvent.java

psmith      2003/05/07 21:00:12

  Modified:    src/java/org/apache/log4j/chainsaw LogUI.java
               src/java/org/apache/log4j/chainsaw/prefs
                        SettingsManager.java SaveSettingsEvent.java
                        LoadSettingsEvent.java
  Added:       src/java/org/apache/log4j/chainsaw/prefs default.properties
  Log:
  initial implementation of Load/Save settings, the main window now
  configures itself to the previous location/size on startup.
  
  Revision  Changes    Path
  1.38      +46 -5     jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw/LogUI.java
  
  Index: LogUI.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw/LogUI.java,v
  retrieving revision 1.37
  retrieving revision 1.38
  diff -u -r1.37 -r1.38
  --- LogUI.java	8 May 2003 00:54:49 -0000	1.37
  +++ LogUI.java	8 May 2003 04:00:11 -0000	1.38
  @@ -114,6 +114,10 @@
   import org.apache.log4j.LogManager;
   import org.apache.log4j.UtilLoggingLevel;
   import org.apache.log4j.chainsaw.icons.ChainsawIcons;
  +import org.apache.log4j.chainsaw.prefs.LoadSettingsEvent;
  +import org.apache.log4j.chainsaw.prefs.SaveSettingsEvent;
  +import org.apache.log4j.chainsaw.prefs.SettingsListener;
  +import org.apache.log4j.chainsaw.prefs.SettingsManager;
   
   
   /**
  @@ -126,7 +130,11 @@
    * @author Paul Smith <ps...@apache.org>
    *
    */
  -public class LogUI extends JFrame implements ChainsawViewer {
  +public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
  +  private static final String MAIN_WINDOW_HEIGHT = "main.window.height";
  +  private static final String MAIN_WINDOW_WIDTH = "main.window.width";
  +  private static final String MAIN_WINDOW_Y = "main.window.y";
  +  private static final String MAIN_WINDOW_X = "main.window.x";
     ChainsawTabbledPane tabbedPane;
     JToolBar toolbar;
     private final Map tableModelMap = new HashMap();
  @@ -141,6 +149,7 @@
     private final Map scrollMap = new HashMap();
     ChainsawAppenderHandler handler;
     ChainsawToolBarAndMenus tbms;
  +  
   
     /**
      * Constructor which builds up all the visual elements of the frame
  @@ -179,14 +188,35 @@
     }
   
     /**
  +   * Given the load event, configures the size/location of the main window
  +   * etc etc.
  +   */
  +  public void loadSettings(LoadSettingsEvent event) {
  +	setLocation(event.asInt(LogUI.MAIN_WINDOW_X),event.asInt(LogUI.MAIN_WINDOW_Y));
  +	setSize(event.asInt(LogUI.MAIN_WINDOW_WIDTH),event.asInt(LogUI.MAIN_WINDOW_HEIGHT));
  +  }
  +
  +  /**
  +   * Ensures the location/size of the main window is stored with the settings
  +   */
  +  public void saveSettings(SaveSettingsEvent event) {
  +	event.saveSetting(LogUI.MAIN_WINDOW_X, (int)getLocation().getX());
  +	event.saveSetting(LogUI.MAIN_WINDOW_Y, (int)getLocation().getY());
  +	
  +	event.saveSetting(LogUI.MAIN_WINDOW_WIDTH, getWidth());
  +	event.saveSetting(LogUI.MAIN_WINDOW_HEIGHT, getHeight());
  +  }
  +	
  +	
  +  /**
      * Activates itself as a viewer by configuring Size, and location of
      * itself, and configures the default Tabbed Pane elements with the correct
      * layout, table columns, and sets itself viewable.
      */
     public void activateViewer() {
  +  	
  +  	
       initGUI();
  -    setSize(new Dimension(500, 500));
  -    setLocation(500, 150);
   
       names.add(ChainsawConstants.LOGGER_COL_NAME);
       names.add(ChainsawConstants.TIMESTAMP_COL_NAME);
  @@ -260,12 +290,21 @@
       getContentPane().add(toolbar, BorderLayout.NORTH);
       getContentPane().add(panePanel, BorderLayout.CENTER);
   
  +
  +	final SettingsManager sm = SettingsManager.getInstance();
       addWindowListener(
         new WindowAdapter() {
           public void windowClosing(WindowEvent event) {
  +        	sm.saveSettings();
             System.exit(0);
           }
         });
  +    
  +    pack();
  +	
  +	sm.addSettingsListener(this);
  +	sm.loadSettings();
  +
       setVisible(true);
     }
   
  @@ -391,7 +430,9 @@
   
   				remoteHost = properties.substring(rhposition, rhlength);
   			}
  -			ident.append(remoteHost);
  +			if(remoteHost!=null) {
  +				ident.append(remoteHost);
  +			}
       }
   
       if (ident.length() == 0) {
  @@ -889,7 +930,7 @@
         final JPanel statusLabelPanel = new JPanel();
         statusLabelPanel.setLayout(new BorderLayout());
         final JLabel statusPaneLabel = new JLabel();
  -      statusLabelPanel.setBorder(BorderFactory.createEtchedBorder());
  +      statusLabelPanel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
         statusPaneLabel.setHorizontalAlignment(JLabel.LEFT);
         statusLabelPanel.add(statusPaneLabel, BorderLayout.CENTER);
         eventsAndStatusPanel.add(statusLabelPanel, BorderLayout.NORTH);
  
  
  
  1.2       +171 -19   jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw/prefs/SettingsManager.java
  
  Index: SettingsManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw/prefs/SettingsManager.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SettingsManager.java	8 May 2003 00:02:38 -0000	1.1
  +++ SettingsManager.java	8 May 2003 04:00:12 -0000	1.2
  @@ -48,31 +48,183 @@
    */
   package org.apache.log4j.chainsaw.prefs;
   
  +import java.io.BufferedInputStream;
  +import java.io.BufferedOutputStream;
  +import java.io.File;
  +import java.io.FileInputStream;
  +import java.io.FileOutputStream;
  +import java.io.IOException;
  +import java.io.InputStream;
  +import java.io.OutputStream;
  +import java.util.EventListener;
  +import java.util.Properties;
  +
  +import javax.swing.event.EventListenerList;
  +
   /**
  + * SettingManager allows components to register interest in Saving/Loading
  + * of general application preferences/settings.
  + * 
    * @author Paul Smith <ps...@apache.org>
    *
    */
   public final class SettingsManager {
   
  -	private static final SettingsManager instance = new SettingsManager();
  +  private static final SettingsManager instance = new SettingsManager();
  +
  +  /**
  +   * Returns the singleton instance of the SettingsManager
  +   * @return
  +   */
  +  public static final SettingsManager getInstance() {
  +    return instance;
  +  }
  +
  +  /**
  +   * Registers the listener with the manager
  +   * @param listener
  +   */
  +  public void addSettingsListener(SettingsListener listener) {
  +    listenerList.add(SettingsListener.class, listener);
  +  }
  +
  +	/**
  +	 * Requests that the settings be loaded, all listeners will be notified of
  +	 * this call, and configure themselves according to the values found in the
  +	 * loaded settings
  +	 *
  +	 */
  +  public void loadSettings() {
  +    EventListener[] listeners =
  +      listenerList.getListeners(SettingsListener.class);
  +    LoadSettingsEvent event = null;
  +    for (int i = 0; i < listeners.length; i++) {
  +      SettingsListener settingsListener = (SettingsListener) listeners[i];
  +      if (event == null) {
  +        Properties loadedProperties = loadCurrentUserProperties();
  +        loadedProperties.list(System.out);
  +        event = new LoadSettingsEvent(this, loadedProperties);
  +      }
  +      settingsListener.loadSettings(event);
  +    }
  +  }
  +
  +  /**
  +   * Creates a SaveSettingsEvent and calls all the SettingsListeners
  +   * to populate the properties with configuration information
  +   *
  +   */
  +  public void saveSettings() {
  +    EventListener[] listeners =
  +      listenerList.getListeners(SettingsListener.class);
  +    SaveSettingsEvent event = null;
  +    for (int i = 0; i < listeners.length; i++) {
  +      SettingsListener settingsListener = (SettingsListener) listeners[i];
  +      if (event == null) {
  +        event = new SaveSettingsEvent(this);
  +      }
  +      settingsListener.saveSettings(event);
  +    }
  +
  +    /**
  +     * Ok, note we ensure we have a .chainsaw directory in the users
  +     * home folder, and create a chainsaw.settings.properties file..
  +     */
  +    File settingsDir = getSettingsDirectory();
  +
  +    if (!settingsDir.exists()) {
  +      settingsDir.mkdir();
  +    }
  +    
  +    OutputStream os = null;
  +    try {
  +      os =
  +        new BufferedOutputStream(
  +          new FileOutputStream(new File(settingsDir, SETTINGS_FILE_NAME)));
  +      event.getProperties().store(os, HEADER);
  +    } catch (Exception e) {
  +      e.printStackTrace();
  +    } finally {
  +      if (os != null) {
  +        try {
  +          os.close();
  +        } catch (IOException e1) {
  +          e1.printStackTrace();
  +        }
  +      }
  +    }
  +  }
  +
  +  private static final String SETTINGS_FILE_NAME =
  +    "chainsaw.settings.properties";
  +
  +  private File getSettingsDirectory() {
  +    return new File(System.getProperty("user.home"), ".chainsaw");
  +  }
  +
  +  /**
  +   * Returns the current Properties settings for this user
  +   * by merging the default Properties with the ones we find in their directory.
  +   * 
  +   * @return
  +   */
  +  private Properties loadCurrentUserProperties() {
  +    Properties properties = new Properties(defaultProperties);
  +    InputStream is = null;
  +    try {
  +      is =
  +        new BufferedInputStream(
  +          new FileInputStream(
  +            new File(getSettingsDirectory(), SETTINGS_FILE_NAME)));
  +      Properties toLoad = new Properties();
  +      toLoad.load(is);
  +      properties.putAll(toLoad);
  +    } catch (Exception e) {
  +      e.printStackTrace();
  +    } finally {
  +      if (is != null) {
  +        try {
  +          is.close();
  +        } catch (IOException e1) {
  +          e1.printStackTrace();
  +        }
  +      }
  +    }
  +    return properties;
  +  }
  +
  +  /**
  +   * Initialises the SettingsManager by loading the default Properties from
  +   * a resource
  +   *
  +   */
  +  private SettingsManager() {
  +    //	load the default properties as a Resource
  +    InputStream is = null;
  +    try {
  +      is =
  +        this
  +          .getClass()
  +          .getClassLoader()
  +          .getResource("org/apache/log4j/chainsaw/prefs/default.properties")
  +          .openStream();
  +      defaultProperties.load(is);
   
  +      //      defaultProperties.list(System.out);
  +      is.close();
  +    } catch (IOException e) {
  +      e.printStackTrace();
  +    } finally {
  +      if (is != null) {
  +        try {
  +          is.close();
  +        } catch (Exception e) {
  +        }
  +      }
  +    }
  +  }
   
  -	public static final SettingsManager getInstance() {
  -		return instance;
  -	}
  -	
  -	public void addSettingsListener(SettingsListener listener) {
  -		throw new UnsupportedOperationException();
  -	}
  -  
  -	public void loadSettings() { 
  -		throw new UnsupportedOperationException();
  -	}
  -	public void saveSettings() { 
  -		throw new UnsupportedOperationException();
  -	}
  -	
  -	private SettingsManager() {
  -		
  -	}
  +  private static final String HEADER = "Chainsaws Settings Files";
  +  private EventListenerList listenerList = new EventListenerList();
  +  private Properties defaultProperties = new Properties();
   }
  
  
  
  1.2       +20 -1     jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw/prefs/SaveSettingsEvent.java
  
  Index: SaveSettingsEvent.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw/prefs/SaveSettingsEvent.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SaveSettingsEvent.java	8 May 2003 00:02:38 -0000	1.1
  +++ SaveSettingsEvent.java	8 May 2003 04:00:12 -0000	1.2
  @@ -48,6 +48,8 @@
    */
   package org.apache.log4j.chainsaw.prefs;
   
  +import java.util.Properties;
  +
   /**
    * @author Paul Smith <ps...@apache.org>
    *
  @@ -58,7 +60,24 @@
   		super(source);
   	}
   	
  +	public void saveSetting(String key, int value) {
  +		saveSetting(key, "" + value);
  +	}
  +
  +	public void saveSetting(String key, double value) {
  +		saveSetting(key, "" + value);
  +	}
  +
  +	public void saveSetting(String key, Object value) {
  +		saveSetting(key, value.toString());
  +	}
  +
   	public void saveSetting(String key, String value) {
  -		throw new UnsupportedOperationException();
  +		properties.put(key, value);
  +	}
  +	
  +	Properties getProperties() {
  +		return properties;
   	}
  +	private Properties properties = new Properties();
   }
  
  
  
  1.2       +23 -10    jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw/prefs/LoadSettingsEvent.java
  
  Index: LoadSettingsEvent.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw/prefs/LoadSettingsEvent.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- LoadSettingsEvent.java	8 May 2003 00:02:38 -0000	1.1
  +++ LoadSettingsEvent.java	8 May 2003 04:00:12 -0000	1.2
  @@ -48,21 +48,34 @@
    */
   package org.apache.log4j.chainsaw.prefs;
   
  +import java.util.Properties;
  +
   /**
    * @author Paul Smith <ps...@apache.org>
    *
    */
   public class LoadSettingsEvent extends SettingsEvent {
   
  +  LoadSettingsEvent(Object source, Properties properties) {
  +    super(source);
  +    this.properties = properties;
  +  }
  +
  +  public String getSetting(String key) {
  +    return properties.getProperty(key);
  +  }
  +  public int asInt(String key) {
  +    String val = getSetting(key);
  +    try {
  +      return Integer.parseInt(val);
  +    } catch (NumberFormatException e) {
  +      e.printStackTrace();
  +      throw new RuntimeException(
  +        "An error occurred retrieving the Integer value of the setting '"
  +          + key
  +          + "'");
  +      }
   
  -	LoadSettingsEvent(Object source) {
  -		super(source);
  -	}
  -	
  -	public String getSetting(String key) {
  -		throw new UnsupportedOperationException();
  -	}
  -	public long asLong(String key) {
  -		throw new UnsupportedOperationException();
  -	}
  +  }
  +  private final Properties properties;
   }
  
  
  
  1.1                  jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw/prefs/default.properties
  
  Index: default.properties
  ===================================================================
  # ====================================
  # The Default Settings for Chainsaw
  # ====================================
  
  # These next settings define the location and dimenions of the main
  # window when it is first realized on startup
  main.window.x=50
  main.window.y=50
  main.window.width=640
  main.window.height=480
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org