/**
  * This will activate <b>f</b> moving it to the front. It will set the current active frame's (if
  * any) <code>IS_SELECTED_PROPERTY</code> to <code>false</code>. There can be only one active
  * frame across all Layers.
  *
  * @param f the <code>JInternalFrame</code> to be activated
  */
 public void activateFrame(JInternalFrame f) {
   Container p = f.getParent();
   Component[] c;
   JDesktopPane d = f.getDesktopPane();
   JInternalFrame currentlyActiveFrame = (d == null) ? null : d.getSelectedFrame();
   // fix for bug: 4162443
   if (p == null) {
     // If the frame is not in parent, its icon maybe, check it
     p = f.getDesktopIcon().getParent();
     if (p == null) return;
   }
   // we only need to keep track of the currentActive InternalFrame, if any
   if (currentlyActiveFrame == null) {
     if (d != null) {
       d.setSelectedFrame(f);
     }
   } else if (currentlyActiveFrame != f) {
     // if not the same frame as the current active
     // we deactivate the current
     if (currentlyActiveFrame.isSelected()) {
       try {
         currentlyActiveFrame.setSelected(false);
       } catch (PropertyVetoException e2) {
       }
     }
     if (d != null) {
       d.setSelectedFrame(f);
     }
   }
   f.moveToFront();
 }
 /**
  * Removes the desktopIcon from its parent and adds its frame to the parent.
  *
  * @param f the <code>JInternalFrame</code> to be de-iconified
  */
 public void deiconifyFrame(JInternalFrame f) {
   JInternalFrame.JDesktopIcon desktopIcon = f.getDesktopIcon();
   Container c = desktopIcon.getParent();
   JDesktopPane d = f.getDesktopPane();
   if (c != null && d != null) {
     c.add(f);
     // If the frame is to be restored to a maximized state make
     // sure it still fills the whole desktop.
     if (f.isMaximum()) {
       Rectangle desktopBounds = c.getBounds();
       if (f.getWidth() != desktopBounds.width || f.getHeight() != desktopBounds.height) {
         setBoundsForFrame(f, 0, 0, desktopBounds.width, desktopBounds.height);
       }
     }
     removeIconFor(f);
     if (f.isSelected()) {
       f.moveToFront();
       f.restoreSubcomponentFocus();
     } else {
       try {
         f.setSelected(true);
       } catch (PropertyVetoException e2) {
       }
     }
   }
 }
 /** Convenience method to remove the desktopIcon of <b>f</b> is necessary. */
 protected void removeIconFor(JInternalFrame f) {
   JInternalFrame.JDesktopIcon di = f.getDesktopIcon();
   Container c = di.getParent();
   if (c != null) {
     c.remove(di);
     c.repaint(di.getX(), di.getY(), di.getWidth(), di.getHeight());
   }
 }
 @RunsInCurrentThread
 private static Triple<Boolean, Container, Point> deiconifyInfo(JInternalFrame internalFrame) {
   boolean deiconified = !isIconified(internalFrame);
   if (deiconified) return new Triple<Boolean, Container, Point>(true, null, null);
   JDesktopIcon desktopIcon = internalFrame.getDesktopIcon();
   return new Triple<Boolean, Container, Point>(
       deiconified, desktopIcon, iconifyLocationOf(desktopIcon));
 }
  /**
   * Removes the frame from its parent and adds its <code>desktopIcon</code> to the parent.
   *
   * @param f the <code>JInternalFrame</code> to be iconified
   */
  public void iconifyFrame(JInternalFrame f) {
    JInternalFrame.JDesktopIcon desktopIcon;
    Container c = f.getParent();
    JDesktopPane d = f.getDesktopPane();
    boolean findNext = f.isSelected();
    desktopIcon = f.getDesktopIcon();
    if (!wasIcon(f)) {
      Rectangle r = getBoundsForIconOf(f);
      desktopIcon.setBounds(r.x, r.y, r.width, r.height);
      setWasIcon(f, Boolean.TRUE);
    }

    if (c == null || d == null) {
      return;
    }

    if (c instanceof JLayeredPane) {
      JLayeredPane lp = (JLayeredPane) c;
      int layer = lp.getLayer(f);
      lp.putLayer(desktopIcon, layer);
    }

    // If we are maximized we already have the normal bounds recorded
    // don't try to re-record them, otherwise we incorrectly set the
    // normal bounds to maximized state.
    if (!f.isMaximum()) {
      f.setNormalBounds(f.getBounds());
    }
    d.setComponentOrderCheckingEnabled(false);
    c.remove(f);
    c.add(desktopIcon);
    d.setComponentOrderCheckingEnabled(true);
    c.repaint(f.getX(), f.getY(), f.getWidth(), f.getHeight());
    if (findNext) {
      if (d.selectFrame(true) == null) {
        // The icon is the last frame.
        f.restoreSubcomponentFocus();
      }
    }
  }
 /**
  * Normally this method will not be called. If it is, it try to determine the appropriate parent
  * from the desktopIcon of the frame. Will remove the desktopIcon from its parent if it
  * successfully adds the frame.
  */
 public void openFrame(JInternalFrame f) {
   if (f.getDesktopIcon().getParent() != null) {
     f.getDesktopIcon().getParent().add(f);
     removeIconFor(f);
   }
 }
  /** The iconifyFrame() code calls this to determine the proper bounds for the desktopIcon. */
  protected Rectangle getBoundsForIconOf(JInternalFrame f) {
    //
    // Get the icon for this internal frame and its preferred size
    //

    JInternalFrame.JDesktopIcon icon = f.getDesktopIcon();
    Dimension prefSize = icon.getPreferredSize();
    //
    // Get the parent bounds and child components.
    //

    Container c = f.getParent();
    if (c == null) {
      c = f.getDesktopIcon().getParent();
    }

    if (c == null) {
      /* the frame has not yet been added to the parent; how about (0,0) ?*/
      return new Rectangle(0, 0, prefSize.width, prefSize.height);
    }

    Rectangle parentBounds = c.getBounds();
    Component[] components = c.getComponents();

    //
    // Iterate through valid default icon locations and return the
    // first one that does not intersect any other icons.
    //

    Rectangle availableRectangle = null;
    JInternalFrame.JDesktopIcon currentIcon = null;

    int x = 0;
    int y = parentBounds.height - prefSize.height;
    int w = prefSize.width;
    int h = prefSize.height;

    boolean found = false;

    while (!found) {

      availableRectangle = new Rectangle(x, y, w, h);

      found = true;

      for (int i = 0; i < components.length; i++) {

        //
        // Get the icon for this component
        //

        if (components[i] instanceof JInternalFrame) {
          currentIcon = ((JInternalFrame) components[i]).getDesktopIcon();
        } else if (components[i] instanceof JInternalFrame.JDesktopIcon) {
          currentIcon = (JInternalFrame.JDesktopIcon) components[i];
        } else
          /* found a child that's neither an internal frame nor
          an icon. I don't believe this should happen, but at
          present it does and causes a null pointer exception.
          Even when that gets fixed, this code protects against
          the npe. hania */
          continue;

        //
        // If this icon intersects the current location, get next location.
        //

        if (!currentIcon.equals(icon)) {
          if (availableRectangle.intersects(currentIcon.getBounds())) {
            found = false;
            break;
          }
        }
      }

      if (currentIcon == null)
        /* didn't find any useful children above. This probably shouldn't
        happen, but this check protects against an npe if it ever does
        (and it's happening now) */
        return availableRectangle;

      x += currentIcon.getBounds().width;

      if (x + w > parentBounds.width) {
        x = 0;
        y -= h;
      }
    }

    return (availableRectangle);
  }
 @RunsInCurrentThread
 private static Point findIconifyLocation(JInternalFrame internalFrame) {
   return iconifyLocationOf(internalFrame.getDesktopIcon());
 }
 @RunsInCurrentThread
 private static Pair<Container, Point> findMaximizeLocation(JInternalFrame internalFrame) {
   Container clickTarget = internalFrame.isIcon() ? internalFrame.getDesktopIcon() : internalFrame;
   Point location = maximizeLocationOf(clickTarget);
   return new Pair<Container, Point>(clickTarget, location);
 }