/*
   *  Create using the specified focus policy
   */
  public TabFocusHandler(JTabbedPane tabbedPane, int focusPolicy) {
    if (focusPolicy != RESET_FOCUS && focusPolicy != RETAIN_FOCUS)
      throw new IllegalArgumentException("Invalid focus policy");

    this.tabbedPane = tabbedPane;
    this.focusPolicy = focusPolicy;

    //  Add listeners to manage a tab change

    tabbedPane.addChangeListener(this);
    KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    focusManager.addPropertyChangeListener("permanentFocusOwner", this);
  }
  /*
   * The method maps the list of the active windows to the window's AppContext,
   * then the method registers ActiveWindowListener, GuiDisposedListener listeners;
   * it executes the initilialization only once per AppContext.
   */
  @SuppressWarnings("unchecked")
  private static void initActiveWindowsTracking(Window w) {
    AppContext appContext = AppContext.getAppContext();
    synchronized (appContext) {
      List<WWindowPeer> l = (List<WWindowPeer>) appContext.get(ACTIVE_WINDOWS_KEY);
      if (l == null) {
        l = new LinkedList<WWindowPeer>();
        appContext.put(ACTIVE_WINDOWS_KEY, l);
        appContext.addPropertyChangeListener(AppContext.GUI_DISPOSED, guiDisposedListener);

        KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        kfm.addPropertyChangeListener("activeWindow", activeWindowListener);
      }
    }
  }