package de.javasoft.synthetica.democenter.examples.jydocking;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import de.javasoft.swing.JYDockingPort;
import de.javasoft.swing.JYDockingView;
import de.javasoft.swing.jydocking.DockingManager;
import de.javasoft.swing.jydocking.IPerspectiveFactory;
import de.javasoft.swing.jydocking.IPerspectiveManager;
import de.javasoft.swing.jydocking.Perspective;
import de.javasoft.swing.jydocking.PerspectiveManager;
import de.javasoft.swing.plaf.jydocking.DefaultCloseAction;
import de.javasoft.swing.plaf.jydocking.DefaultMinimizeAction;

/**
 * Basic Docking application with two different perspectives   
 * demonstrates Perspective usage.
 */
@SuppressWarnings("serial")
public class SimplePerspective extends JFrame
{
//@highlight
  //unique IDs for views and perspectives
  public static final String MAIN_VIEW = "main-view SimplePerspective";
  public static final String EXPLORER_VIEW = "explorer-view SimplePerspective";
  public static final String INFO_VIEW = "info-view SimplePerspective";
  public static final String P1 = "Perspective1 SimplePerspective";
  public static final String P2 = "Perspective2 SimplePerspective";
//@/highlight
  
  public SimplePerspective()
  {
    super("Simple Perspective Demo");
    add(createContentPane());

//@highlight
    //install perspective factory
    IPerspectiveManager pm = DockingManager.getPerspectiveManager();
    pm.setFactory(new MyPerspectiveFactory());
    PerspectiveManager.setCurrentPerspectiveID(P1, true);
    pm.restorePerspectives(this, false);
//@/highlight

    //clean up resources, recommended when Docking framework is used 
    //for a separate window just like the examples in the DemoCenter
    addWindowListener(new WindowAdapter()
    {
      @Override
      public void windowClosed(WindowEvent evt)
      {
        DockingManager.removePerspective(P1);
        DockingManager.removePerspective(P2);
        DockingManager.unregisterDockable(MAIN_VIEW);
        DockingManager.unregisterDockable(EXPLORER_VIEW);
        DockingManager.unregisterDockable(INFO_VIEW);
      }
    });
    
    addMenu();
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setSize(640, 480);
    setLocationRelativeTo(null);        //center
    setVisible(true);
  }

  private void addMenu()
  {
    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    JRadioButton b1 = new JRadioButton(new AbstractAction(P1){
      public void actionPerformed(ActionEvent e)
      {
//@highlight
        //load perspective - keep modifications made by user
        PerspectiveManager.loadPerspective(P1, SimplePerspective.this, false);        
//@/highlight
      }});
    b1.setOpaque(false);
    b1.setSelected(true);
 
    JRadioButton b2 = new JRadioButton(new AbstractAction(P2){
      public void actionPerformed(ActionEvent e)
      {
//@highlight
        //load perspective - reset modifications made by user
        PerspectiveManager.loadPerspective(P2, SimplePerspective.this, true);
//@/highlight
      }});
    b2.setOpaque(false);    
    
    ButtonGroup bg = new ButtonGroup();
    bg.add(b1);
    bg.add(b2);
    menuBar.add(b1);
    menuBar.add(b2);
  }
  
  /**
   * Create content pane with Viewport
   */
  private JPanel createContentPane()
  {
    JYDockingPort viewport = new JYDockingPort();
    JPanel p = new JPanel(new BorderLayout(0, 0));
    p.setBorder(new EmptyBorder(5, 5, 5, 5));
    p.add(viewport, BorderLayout.CENTER);
    return p;
  }

  /**
   * Create the main view - with blocked center region.
   */
  public static JYDockingView createMainView(String id, String title, String tabText)
  {
    JYDockingView view = createView(id, title, tabText);
    view.setTerritoryBlocked(DockingManager.CENTER_REGION, true);
    return view;
  }

  /**
   * Create additional views.
   */
  public static JYDockingView createView(String id, String title, String tabText)
  {
    JYDockingView view = new JYDockingView(id, title, tabText);
    initView(view);
    return view;
  }

  /**
   * Initialize specified view.
   */
  private static void initView(JYDockingView view)
  {
    view.addAction(new DefaultMinimizeAction(view));
    view.addAction(new DefaultCloseAction(view));

    JPanel p = new JPanel(new BorderLayout());
    view.setContentPane(p);
  }
    
//@highlight
  private static class MyPerspectiveFactory implements IPerspectiveFactory
  {
    public MyPerspectiveFactory()
    {
      //with creating views they are registered to the DockingManager
      //you can use DockingManager#getDockable(String id) to get the created view by its ID     
      createMainView(MAIN_VIEW, "MainTitle", "MainTabText"); 
      createView(EXPLORER_VIEW, "ExplorerTitle", "ExplorerTabText"); 
      createView(INFO_VIEW, "InfoTitle", "InfoTabText");
    }
    
    //PerspectiveFactory implementation
    public Perspective getPerspective(String id)
    {
      //each ID has to be unique within your application
      if (P1.equals(id))
        return createPerspective1(P1);
      if (P2.equals(id))
        return createPerspective2(P2);
      return null;
    }

    private Perspective createPerspective1(String id)
    {
      Perspective perspective = new Perspective(id, "MyPerspective Name 1");
      JYDockingView mainView = (JYDockingView)DockingManager.getDockable(MAIN_VIEW);
      perspective.add(mainView);
      perspective.add(DockingManager.getDockable(EXPLORER_VIEW), mainView, DockingManager.WEST_REGION, .3f);
      perspective.add(DockingManager.getDockable(INFO_VIEW), mainView, DockingManager.EAST_REGION, .3f);
      return perspective;
    }

    private Perspective createPerspective2(String id)
    {
      Perspective perspective = new Perspective(id, "MyPerspective Name 2");
      JYDockingView mainView = (JYDockingView)DockingManager.getDockable(MAIN_VIEW);
      perspective.add(mainView);
      perspective.add(DockingManager.getDockable(EXPLORER_VIEW), mainView, DockingManager.SOUTH_REGION, .5f);
      perspective.add(DockingManager.getDockable(INFO_VIEW), mainView, DockingManager.EAST_REGION, .3f);
      return perspective;
    }
  }
//@/highlight

  /**
   * Static main method for application startup. 
   */
  public static void main(String[] args)
  {
    EventQueue.invokeLater(new Runnable()
    {
      public void run()
      {
        try
        {
          UIManager.setLookAndFeel("de.javasoft.plaf.synthetica.SyntheticaStandardLookAndFeel");
          new SimplePerspective();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }  
  
}
