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

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSeparator;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import org.jdesktop.swingx.JXStatusBar;
import org.jdesktop.swingx.plaf.basic.BasicStatusBarUI;

import de.javasoft.swing.JYDockingPort;
import de.javasoft.swing.JYDockingView;
import de.javasoft.swing.jydocking.DockingManager;
import de.javasoft.swing.plaf.jydocking.DefaultCloseAction;
import de.javasoft.swing.plaf.jydocking.DefaultMinimizeAction;

/**
 * Demonstrates how to use a statusbar together with the JYDocking framework.
 */
@SuppressWarnings("serial")
public class DockingStatusBar extends JFrame
{
//@highlight
  private JProgressBar progressBar = new JProgressBar(0, 20);
//@/highlight
  
  public DockingStatusBar()
  {
    super("StatusBar Docking Demo");
    //use statusbar style if available
    progressBar.setName("StatusProgressBar");
//@highlight
    add(createContentPane());
    JXStatusBar sBar = createStatusBar();
    getContentPane().add(sBar, BorderLayout.SOUTH);
//@/highlight    
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setSize(640, 480);
    setLocationRelativeTo(null);        //center
    setVisible(true);
    
//@highlight
    //start a new thread as worker to simulate activity
    new Thread()
    {
      private int i = 0;
      public void run()
      {
        while(i++ < progressBar.getMaximum())
        {  
          //we are not in the EDT - but changes should only be made from within the EDT (EventDispachThread) 
          EventQueue.invokeLater(new Runnable()
          {    
            public void run()
            {
              progressBar.setValue(i);
            }
          });

          //simulate some work 
          try{sleep(250);}catch(InterruptedException e){};          
        }  
        EventQueue.invokeLater(new Runnable()
        {    
          public void run()
          {
            progressBar.setValue(0);
          }
        });
      }
    }.start();
//@/highlight
  }

  /**
   * Create a simple statusbar.
   */
  private JXStatusBar createStatusBar()
  {
    JXStatusBar statusBar = new JXStatusBar();
    //do not add separators automatically
    statusBar.putClientProperty(BasicStatusBarUI.AUTO_ADD_SEPARATOR, Boolean.FALSE);
    statusBar.add(new JLabel("Ready"));
    //use dummy label to fill empty space
    statusBar.add(new JLabel(""), JXStatusBar.Constraint.ResizeBehavior.FILL);
    //create constraint to add space of 5 at components left and right side 
    JXStatusBar.Constraint ct = new JXStatusBar.Constraint(new Insets(0, 5, 0, 5));
    statusBar.add(new JSeparator(JSeparator.VERTICAL), ct);
    statusBar.add(new JLabel("INS"), ct);
    statusBar.add(new JSeparator(JSeparator.VERTICAL), ct);
    statusBar.add(new JLabel("Load"), ct);
    statusBar.add(progressBar);
//@highlight
    //also add the statusbar to the rootPane because some themes have to paint to the frame border
    getRootPane().putClientProperty("Synthetica.statusBar", statusBar);    
//@/highlight
    return statusBar;
  }

  /**
   * Create content pane with three different views.
   */
  private JPanel createContentPane()
  {
    //A view name (id) should be unique within your application
    JYDockingView mainView = createMainView("mainViewName", "MainTitle", "MainTabText");
    JYDockingView explorerView = createView("explorerViewName", "ExplorerTitle", "ExplorerTabText");
    JYDockingView infoView = createView("infoViewName", "InfoTitle", "InfoTabText");

    JYDockingPort viewport = new JYDockingPort();
    viewport.dock(mainView);
    
    mainView.dock(explorerView, DockingManager.WEST_REGION, .3f);
    mainView.dock(infoView, DockingManager.SOUTH_REGION, .3f);
    
    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 name, String title, String tabText)
  {
    JYDockingView view = createView(name, title, tabText);
    view.setTerritoryBlocked(DockingManager.CENTER_REGION, true);
    return view;
  }

  /**
   * Used to create additional views.
   */
  public static JYDockingView createView(String name, String title, String tabText)
  {
    JYDockingView view = new JYDockingView(name, 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);
  }

  /**
   * 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 DockingStatusBar();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }
}
