/*
    VLDocking Framework 2.1
    Copyright VLSOLUTIONS, 2004-2006

    email : info@vlsolutions.com
------------------------------------------------------------------------
This software is distributed under the CeCILL license, a GNU GPL-compatible
license adapted to french law.
French and English license headers are provided at the begining of
the source files of this software application.
------------------------------------------------------------------------
LICENCE CeCILL (FRENCH VERSION).
------------------------------------------------------------------------
Ce logiciel est un programme informatique servant à améliorer les interfaces
homme-machine d'applications Java basées sur Swing, en leur apportant un
ensemble de fonctions relatives au dockage des composants.

Ce logiciel est régi par la licence CeCILL soumise au droit français et
respectant les principes de diffusion des logiciels libres. Vous pouvez
utiliser, modifier et/ou redistribuer ce programme sous les conditions
de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
sur le site "http://www.cecill.info".

En contrepartie de l'accessibilité au code source et des droits de copie,
de modification et de redistribution accordés par cette licence, il n'est
offert aux utilisateurs qu'une garantie limitée.  Pour les mêmes raisons,
seule une responsabilité restreinte pèse sur l'auteur du programme,  le
titulaire des droits patrimoniaux et les concédants successifs.

A cet égard  l'attention de l'utilisateur est attirée sur les risques
associés au chargement,  à l'utilisation,  à la modification et/ou au
développement et à la reproduction du logiciel par l'utilisateur étant
donné sa spécificité de logiciel libre, qui peut le rendre complexe à
manipuler et qui le réserve donc à des développeurs et des professionnels
avertis possédant  des  connaissances  informatiques approfondies.  Les
utilisateurs sont donc invités à charger  et  tester  l'adéquation  du
logiciel à leurs besoins dans des conditions permettant d'assurer la
sécurité de leurs systèmes et ou de leurs données et, plus généralement,
à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.

Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
pris connaissance de la licence CeCILL, et que vous en avez accepté les
termes.

------------------------------------------------------------------------
CeCILL License (ENGLISH VERSION)
------------------------------------------------------------------------

This software is a computer program whose purpose is to enhance Human-Computer
Interfaces written in Java with the Swing framework, providing them a set of
functions related to component docking.

This software is governed by the CeCILL  license under French law and
abiding by the rules of distribution of free software.  You can  use,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".

As a counterpart to the access to the source code and  rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty  and the software's author,  the holder of the
economic rights,  and the successive licensors  have only  limited
liability.

In this respect, the user's attention is drawn to the risks associated
with loading,  using,  modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean  that it is complicated to manipulate,  and  that  also
therefore means  that it is reserved for developers  and  experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and,  more generally, to use and operate it in the
same conditions as regards security.

The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.

*/


package com.vlsolutions.swing.docking.ui;

import com.vlsolutions.swing.docking.DockView;
import com.vlsolutions.swing.docking.ShadowBorder;
import com.vlsolutions.swing.docking.SplitContainer;
import com.vlsolutions.swing.docking.TabbedDockableContainer;
import java.awt.Color;
import java.awt.Container;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.PanelUI;

/** This is the UI delegate for the DockView class.
 *
 * <p> Default behaviour in to install a shadow border on the dockview, 
 * but this can be replaced by overriding the 3 install<i>XXX</i>DockableBorder methods.
 *
 * @author Lilian Chamontin, VLSolutions
 *
 * @since 2.0
 */
public class DockViewUI extends PanelUI {
  
  /** Ancestor listener used to install different borders depending on the usage 
   * of the dock view (docked, maximized, tabbed).
   */
  protected ViewAncestorListener ancestorListener = new ViewAncestorListener();
  
  private static DockViewUI instance = new DockViewUI();

  
  public DockViewUI() {    
  }
  
  
  public static ComponentUI createUI(JComponent c) {
      return instance;
  }

  public void installUI(JComponent c) {
    super.installUI(c);
    c.addAncestorListener(ancestorListener);
  }
  
  public void uninstallUI(JComponent c){
    super.uninstallUI(c);
    c.removeAncestorListener(ancestorListener);
  }


  /** Installs a border when the DockView target is a SingleDockableContainer 
   * (not nested in a tabbed container )
   *
   *<p> default is a shadowed border.
   */
  protected void installSingleDockableBorder(DockView v){
    Border b = UIManager.getBorder("DockView.singleDockableBorder");
    v.setBorder(b);
    
  }

  /** Installs a border when the DockView target is included in a TabbedDockableContainer.
   * 
   *<p> Default is a shadow border without top and left shadows
   *
   */
  protected void installTabbedDockableBorder(DockView v){
    Border b = UIManager.getBorder("DockView.tabbedDockableBorder");
    v.setBorder(b);

  }

  /** Installs a border when the DockView target is unique on the desktop (mamimized, or alone)
   * 
   *<p> Default is a shadow border without top and left shadows
   *
   */
  protected void installMaximizedDockableBorder(DockView v){
    Border b = UIManager.getBorder("DockView.maximizedDockableBorder");
    v.setBorder(b); 
  }

  
  class ViewAncestorListener implements AncestorListener {
      public void ancestorAdded(AncestorEvent ancestorEvent) {
        DockView v = (DockView)ancestorEvent.getComponent();
        Container parent = v.getParent();
        if (parent instanceof TabbedDockableContainer){
          installTabbedDockableBorder(v);
        } else if (parent instanceof SplitContainer){
          installSingleDockableBorder(v);
        } else {
          installMaximizedDockableBorder(v);
        }
      }
      public void ancestorMoved(AncestorEvent ancestorEvent) {  }
      public void ancestorRemoved(AncestorEvent ancestorEvent) { }
  }


  
}
