Beispiel #1
0
  /**
   * Tries to load/restore the window state of the given window.
   *
   * @param aNamespace the namespace to use for the window state;
   * @param aProperties the properties to read from;
   * @param aWindow the window to load the state for.
   */
  public static void loadWindowState(final Preferences aProperties, final Window aWindow) {
    // Special case: for FileDialog/JFileChooser we also should restore the
    // properties...
    loadFileDialogState(aProperties, aWindow);

    try {
      final int xPos = aProperties.getInt("winXpos", -1);
      final int yPos = aProperties.getInt("winYpos", -1);
      if ((xPos >= 0) && (yPos >= 0)) {
        aWindow.setLocation(xPos, yPos);
      }
    } catch (NumberFormatException exception) {
      // Ignore...
    }

    if (isNonResizableWindow(aWindow)) {
      // In case the window cannot be resized, don't restore its width &
      // height...
      return;
    }

    try {
      final int width = aProperties.getInt("winWidth", -1);
      final int height = aProperties.getInt("winHeight", -1);
      if ((width >= 0) && (height >= 0)) {
        aWindow.setSize(width, height);
      }
    } catch (NumberFormatException exception) {
      // Ignore...
    }
  }
Beispiel #2
0
  /**
   * Saves the window state to the given properties map.
   *
   * @param aNamespace the namespace to use for the window state;
   * @param aProperties the properties to fill;
   * @param aWindow the window to save the state for.
   */
  public static void saveWindowState(final Preferences aProperties, final Window aWindow) {
    // Special case: for FileDialog/JFileChooser we also store the properties...
    saveFileDialogState(aProperties, aWindow);

    final Point location = aWindow.getLocation();
    aProperties.put("winXpos", Integer.toString(location.x));
    aProperties.put("winYpos", Integer.toString(location.y));

    if (isNonResizableWindow(aWindow)) {
      // In case the window cannot be resized, don't restore its width &
      // height...
      return;
    }

    final Dimension dims = aWindow.getSize();
    aProperties.put("winWidth", Integer.toString(dims.width));
    aProperties.put("winHeight", Integer.toString(dims.height));
  }
Beispiel #3
0
  /**
   * Checks whether the given window is either a FileDialog, or contains a JFileChooser component.
   * If so, its current directory is restored from the given properties.
   *
   * @param aNamespace the name space to use;
   * @param aProperties the properties to get the directory from;
   * @param aWindow the window to check for.
   */
  private static void saveFileDialogState(final Preferences aProperties, final Window aWindow) {
    final String propKey = "lastDirectory";

    if (aWindow instanceof FileDialog) {
      final String dir = ((FileDialog) aWindow).getDirectory();
      if (dir != null) {
        aProperties.put(propKey, dir);
      }
    } else if (aWindow instanceof JDialog) {
      final Container contentPane = ((JDialog) aWindow).getContentPane();
      final JFileChooser fileChooser =
          (JFileChooser) findComponent(contentPane, JFileChooser.class);
      if (fileChooser != null) {
        final File dir = fileChooser.getCurrentDirectory();
        if (dir != null) {
          aProperties.put(propKey, dir.getAbsolutePath());
        }
      }
    }
  }
Beispiel #4
0
  /**
   * Checks whether the given window is either a FileDialog, or contains a JFileChooser component.
   * If so, its current directory is stored in the given properties.
   *
   * @param aNamespace the name space to use;
   * @param aProperties the properties to store the found directory in;
   * @param aWindow the window to check for.
   */
  private static void loadFileDialogState(final Preferences aProperties, final Window aWindow) {
    final String propKey = "lastDirectory";

    if (aWindow instanceof FileDialog) {
      final String dir = aProperties.get(propKey, null);
      if (dir != null) {
        ((FileDialog) aWindow).setDirectory(dir);
      }
    } else if (aWindow instanceof JDialog) {
      final Container contentPane = ((JDialog) aWindow).getContentPane();
      final JFileChooser fileChooser =
          (JFileChooser) findComponent(contentPane, JFileChooser.class);
      if (fileChooser != null) {
        final String dir = aProperties.get(propKey, null);
        if (dir != null) {
          fileChooser.setCurrentDirectory(new File(dir));
        }
      }
    }
  }