Example #1
0
 public static boolean getBoolean(PrefType name, Context context) {
   SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
   return settings.getBoolean(name.toString(), false);
 }
Example #2
0
 public static String getString(PrefType name, Context context) {
   SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
   return settings.getString(name.toString(), null);
 }
Example #3
0
 public static double getDouble(PrefType name, Context context) {
   SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
   return settings.getFloat(name.toString(), 0f);
 }
Example #4
0
 public static int getInt(PrefType name, Context context) {
   SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
   return settings.getInt(name.toString(), 0);
 }
Example #5
0
 public static void add(PrefType name, boolean value, Context context) {
   SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
   SharedPreferences.Editor editor = settings.edit();
   editor.putBoolean(name.toString(), value);
   editor.commit();
 }
Example #6
0
  public File browse(
      Component parent,
      BrowseType browseType,
      FileType fileType,
      PrefType prefType,
      boolean dirsOnly,
      String startingDir) {
    init(parent);

    try {
      prefs.sync();
    } catch (BackingStoreException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    File selFile = null;
    if (startingDir != null) {
      File f = new File(startingDir);

      if (f.isFile()) //
      startingDir = f.getParent();
      else {
        if (!f.exists()) f.mkdir();

        if (dirsOnly) { // make parent dir the starting dir, if it exists
          startingDir = f.getParent();
          selFile = f;
        }
      }
    }

    String lastDir =
        startingDir != null && startingDir.length() != 0
            ? startingDir
            : prefs.get("file." + browseType.name() + "." + prefType.name(), null);
    if (lastDir == null) {
      if (prefType == PrefType.FontLoad) {
        lastDir = getBestFontPath();
      } else {
        lastDir = System.getProperty("user.home");
      }
    }

    File lastDirFile = new File(lastDir);
    String lastDirFileName = "";
    if (lastDirFile.exists() && lastDirFile.isFile()) lastDirFileName = lastDirFile.getName();

    File file = null;

    if (useJFileChooser) {
      jFileChooser.setFileFilter(fileType.swingFilter);

      jFileChooser.setCurrentDirectory(new File(lastDir));

      if (selFile != null) jFileChooser.setSelectedFile(selFile);

      jFileChooser.setFileSelectionMode(
          dirsOnly ? JFileChooser.DIRECTORIES_ONLY : JFileChooser.FILES_ONLY);
      int a =
          browseType == BrowseType.Open
              ? jFileChooser.showOpenDialog(parent)
              : jFileChooser.showSaveDialog(parent);
      if (a == JFileChooser.APPROVE_OPTION) {
        prefs.put(
            "file." + browseType.name(), jFileChooser.getCurrentDirectory().getAbsolutePath());
        file = jFileChooser.getSelectedFile();
      }
    } else {
      System.setProperty("apple.awt.fileDialogForDirectories", Boolean.toString(dirsOnly));

      fileDialog.setAlwaysOnTop(true);
      fileDialog.setFilenameFilter(fileType.awtFilter);
      fileDialog.setDirectory(lastDir);
      fileDialog.setFile(lastDirFileName);
      fileDialog.setMode(browseType == BrowseType.Open ? FileDialog.LOAD : FileDialog.SAVE);
      fileDialog.setVisible(true);
      String res = fileDialog.getFile();
      if (res != null) {
        File ret = new File(fileDialog.getDirectory(), res);
        File dir = ret;
        if (dir.getParentFile() != null && !dir.isDirectory()) dir = dir.getParentFile();
        prefs.put("file." + browseType.name(), dir.getAbsolutePath());
        file = ret;
      }
    }

    if (file != null) {
      try {
        prefs.flush();
      } catch (BackingStoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    return file;
  }