<commit_before>package edu.brandeis.cs.steele.wn.browser;

import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.prefs.*;

/**
 * Save window positions and all other persistent user preferences.
 * Doesn't use Properties (files), uses Preferences - no files or maps to manage.
 * TODO remove static methods -- go through getInstance()
 * TODO check if importPreferences() clobbers saved preferences - seems it would have to
 * TODO put WNHOME/WNSEARCHDIR per-OS defaults (same as those from .jnlp file) into preferences
 *      and reflect them into system properties
 * @author http://www.oreilly.com/catalog/swinghks/
 */
class PreferencesManager implements AWTEventListener {
  // import/export preferences
  // good article
  // http://blogs.sun.com/CoreJavaTechTips/entry/the_preferences_api

  private static Preferences prefs = Preferences.userNodeForPackage(PreferencesManager.class);
  private static PreferencesManager saver;

  static {
    // on class load, register self as saver
    //XXX Toolkit.getDefaultToolkit().addAWTEventListener(
    //XXX    PreferencesManager.getInstance(), AWTEvent.WINDOW_EVENT_MASK);

    //XXX for debugging
    //try {
    //  //prefs.clear();
    //  //prefs.removeNode();
    //  prefs = Preferences.userNodeForPackage(PreferencesManager.class);
    //} catch(BackingStoreException bse) {
    //  bse.printStackTrace();
    //}
    //TODO need way to do this in GUI since it clobbers user actions
    //loadDefaults();
  };

  static synchronized void loadDefaults() {
    // FIXME currently clobbers all settings
    // TODO
    // see if the defaults have been loaded,
    //   if not, load them
    // * Don't clobber user preferences
    // Parallel/isomorphic tree for user preferences and defaults
    // - like the package structure of src/java/ and test/
    // - user
    // - default
    // Add a revision number or something to determine the defaults tree version
    // Preferences.userNodeForPackage(Class<?> c)
    //
    // TODO how to represent search history ?
    // Note: when encoding values, there is a maximum size:
    // Preferences.MAX_VALUE_LENGTH characters

    final InputStream is = PreferencesManager.class.getResourceAsStream("defaults.xml");
    try {
      Preferences.importPreferences(is);
      is.close();
    } catch(InvalidPreferencesFormatException ipfe) {
      throw new RuntimeException(ipfe);
    } catch(IOException ioe) {
      throw new RuntimeException(ioe);
    }
  }

  /** Utility enum to change the Swing LookAndFeel */
  enum LookAndFeel {
    System {
      public void set() {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(Exception e) {
          java.lang.System.err.println("Error setting LAF "+this+" " + e);
        }
      }
    },
    CrossPlatform {
      public void set() {
        try {
          UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch(Exception e) {
          java.lang.System.err.println("Error setting LAF "+this+" " + e);
        }
      }
    },
    GTK {
      public void set() {
        try {
          UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
        } catch(Exception e) {
          java.lang.System.err.println("Error setting LAF "+this+" " + e);
        }
      }
    },
    MOTIF {
      public void set() {
        try {
          UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
        } catch(Exception e) {
          java.lang.System.err.println("Error setting LAF "+this+" " + e);
        }
      }
    }
    ;
    /**
     * Activate this LookAndFeel
     */
    public abstract void set();
  } // end enum LookAndFeel

  static void setLookAndFeel() {

    // set manually
    //LookAndFeel.GTK.set();
    //LookAndFeel.System.set();
    // even OS X LAF has bugs
    //LookAndFeel.CrossPlatform.set();

    //System.getProperty("os.name").toLowerCase().startsWith("mac os x");

    //for(final UIManager.LookAndFeelInfo lafInfo : UIManager.getInstalledLookAndFeels()) {
    //  System.err.println("lafInfo: "+lafInfo);
    //}

    // ideal behavior:
    // if not Linux (OS X or Windows)
    //   use System
    // else
    //   use CrossPlatform
  
    //TODO loadDefaults();
    final String defaultLnFName = "System";
    final String lnfName = prefs.get("lookAndFeel", defaultLnFName);
    LookAndFeel.valueOf(lnfName).set();
  }

  public static PreferencesManager getInstance() {
    if (saver == null) {
      saver = new PreferencesManager();<commit_msg>commented hacks - exploring LookAndFeel bugs<commit_after>package edu.brandeis.cs.steele.wn.browser;

import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.prefs.*;

/**
 * Save window positions and all other persistent user preferences.
 * Doesn't use Properties (files), uses Preferences - no files or maps to manage.
 * TODO remove static methods -- go through getInstance()
 * TODO check if importPreferences() clobbers saved preferences - seems it would have to
 * TODO put WNHOME/WNSEARCHDIR per-OS defaults (same as those from .jnlp file) into preferences
 *      and reflect them into system properties
 * @author http://www.oreilly.com/catalog/swinghks/
 */
class PreferencesManager implements AWTEventListener {
  // import/export preferences
  // good article
  // http://blogs.sun.com/CoreJavaTechTips/entry/the_preferences_api

  private static Preferences prefs = Preferences.userNodeForPackage(PreferencesManager.class);
  private static PreferencesManager saver;

  static {
    // on class load, register self as saver
    //XXX Toolkit.getDefaultToolkit().addAWTEventListener(
    //XXX    PreferencesManager.getInstance(), AWTEvent.WINDOW_EVENT_MASK);

    //XXX for debugging
    //try {
    //  //prefs.clear();
    //  //prefs.removeNode();
    //  prefs = Preferences.userNodeForPackage(PreferencesManager.class);
    //} catch(BackingStoreException bse) {
    //  bse.printStackTrace();
    //}
    //TODO need way to do this in GUI since it clobbers user actions
    //loadDefaults();
  };

  static synchronized void loadDefaults() {
    // FIXME currently clobbers all settings
    // TODO
    // see if the defaults have been loaded,
    //   if not, load them
    // * Don't clobber user preferences
    // Parallel/isomorphic tree for user preferences and defaults
    // - like the package structure of src/java/ and test/
    // - user
    // - default
    // Add a revision number or something to determine the defaults tree version
    // Preferences.userNodeForPackage(Class<?> c)
    //
    // TODO how to represent search history ?
    // Note: when encoding values, there is a maximum size:
    // Preferences.MAX_VALUE_LENGTH characters

    final InputStream is = PreferencesManager.class.getResourceAsStream("defaults.xml");
    try {
      Preferences.importPreferences(is);
      is.close();
    } catch(InvalidPreferencesFormatException ipfe) {
      throw new RuntimeException(ipfe);
    } catch(IOException ioe) {
      throw new RuntimeException(ioe);
    }
  }

  /** Utility enum to change the Swing LookAndFeel */
  enum LookAndFeel {
    System {
      public void set() {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(Exception e) {
          java.lang.System.err.println("Error setting LAF "+this+" " + e);
        }
      }
    },
    CrossPlatform {
      public void set() {
        try {
          UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch(Exception e) {
          java.lang.System.err.println("Error setting LAF "+this+" " + e);
        }
      }
    },
    GTK {
      public void set() {
        try {
          UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
        } catch(Exception e) {
          java.lang.System.err.println("Error setting LAF "+this+" " + e);
        }
      }
    },
    MOTIF {
      public void set() {
        try {
          UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
        } catch(Exception e) {
          java.lang.System.err.println("Error setting LAF "+this+" " + e);
        }
      }
    }
    ;
    /**
     * Activate this LookAndFeel
     */
    public abstract void set();
  } // end enum LookAndFeel

  static void setLookAndFeel() {

    // set manually
    //LookAndFeel.GTK.set();
    //LookAndFeel.System.set();
    // even OS X LAF has bugs
    //LookAndFeel.CrossPlatform.set();

    //System.getProperty("os.name").toLowerCase().startsWith("mac os x");

    //for(final UIManager.LookAndFeelInfo lafInfo : UIManager.getInstalledLookAndFeels()) {
    //  System.err.println("lafInfo: "+lafInfo);
    //}

    // ideal behavior:
    // if not Linux (OS X or Windows)
    //   use System
    // else
    //   use CrossPlatform
  
    //TODO loadDefaults();
    final String defaultLnFName = "System";
    //final String defaultLnFName = "CrossPlatform";
    String lnfName = prefs.get("lookAndFeel", defaultLnFName);
    //lnfName = "GTK";
    //lnfName = "CrossPlatform";
    LookAndFeel.valueOf(lnfName).set();
  }

  public static PreferencesManager getInstance() {
    if (saver == null) {
      saver = new PreferencesManager();
{'fraction_non_alphanumeric': 0.11700085324232082, 'fraction_numerical': 0.0, 'mean_word_length': 2.599616122840691, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '680481', 'n_tokens_mistral': 2791, 'n_tokens_neox': 2782, 'n_words': 708}