Package DisplayProject.actions

Source Code of DisplayProject.actions.TabPages

/*
Copyright (c) 2003-2008 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package DisplayProject.actions;

import java.awt.Component;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import DisplayProject.Array_Of_Panel;
import DisplayProject.GridField;
import DisplayProject.controls.Panel;
import DisplayProject.controls.TabFolder;
/**
* this pending action implements the adding, removing and locating of tab pages on the EDT
* <b>
* PM:01/8/07 Changed The page type from JPanel to Panel
* PM:14/09/2008:implemented replace page
*/
public class TabPages extends PendingAction {
    @SuppressWarnings("unchecked")
  private Array_Of_Panel pages;
    private JPanel page;
    private String caption;
    private Icon icon;
    private int action = 0;
    private int index = -99;
    public static final int ADD = 1;
    public static final int DELETE = 2;
    public static final int TOP_PAGE = 3;
    public static final int REPLACE = 4;//PM:14/09/2008:implemented replace page

    @SuppressWarnings("unchecked")
  public TabPages(JTabbedPane tabFolder, Array_Of_Panel pages) {
        super(tabFolder);
        this.pages = pages;
    }

    public TabPages(JTabbedPane tabFolder, int index, int action) {
        super(tabFolder);
        this.page = null;
        this.action = action;
        this.index = index - 1;
    }

    public TabPages(JTabbedPane tabFolder, Panel page, int action) {
        super(tabFolder);
        this.page = page;
        this.action = action;
    }

    public TabPages(JTabbedPane tabFolder, Panel page, int index, int action) {
        super(tabFolder);
        this.page = page;
        this.index = index - 1;
        this.action = action;
    }

    public TabPages(JTabbedPane tabFolder, Panel page, int action, String caption) {
        super(tabFolder);
        this.page = page;
        this.action = action;
        this.caption = caption;
    }

    public TabPages(JTabbedPane tabFolder, GridField page, int action, String caption) {
        super(tabFolder);
        this.page = page;
        this.action = action;
        this.caption = caption;
    }
    public TabPages(JTabbedPane tabFolder, Panel page, int action, String caption, Icon icon) {
        this(tabFolder, page, action, caption);
        this.icon = icon;
    }

    @SuppressWarnings("unchecked")
  public static void setPages(JTabbedPane comp, Array_Of_Panel value) {
        ActionMgr.addAction(new TabPages(comp, value));
    }

    @SuppressWarnings("unchecked")
  public static Array_Of_Panel<Panel> getPages(JTabbedPane comp) {
        Array_Of_Panel pages = new Array_Of_Panel(JPanel.class);
        TabPages action = (TabPages) ActionMgr.getAction(comp, TabPages.class);
        int numberOfTabs = comp.getTabCount();
        if (action != null)
            pages = action.pages;
        else {
          if (comp instanceof TabFolder){ //PM:14/01/2009:corrected the for hidden pages
            pages = ((TabFolder)comp).getPages();
          } else {
            for (int i = 0; i < numberOfTabs; i++) {
              Component tmpPanel = comp.getComponentAt(i);
              if (tmpPanel instanceof JPanel)
                pages.add(tmpPanel);
            }
          }
        }
        return pages;
    }

    public static void setTopPage(JTabbedPane tabFolder, Panel page) {
        ActionMgr.addAction(new TabPages(tabFolder, page, TabPages.TOP_PAGE));
    }
  //PM:14/09/2008:implemented replace page
    public static void replacePage(JTabbedPane tabFolder, Panel page, int index) {
        ActionMgr.addAction(new TabPages(tabFolder, page, index, TabPages.REPLACE));
    }
    public static void addPage(JTabbedPane tabFolder, Panel page) {
        ActionMgr.addAction(new TabPages(tabFolder, page, TabPages.ADD));
    }

    /**
     * Insert a tab at a particular index.
     *
     * CraigM:28/04/2008.
     *
     * @param tabFolder
     * @param page
     * @param index
     */
    public static void addPage(JTabbedPane tabFolder, Panel page, int index) {
        ActionMgr.addAction(new TabPages(tabFolder, page, index, TabPages.ADD));
    }

    public static void addPage(JTabbedPane tabFolder, Panel page, String caption) {
        ActionMgr.addAction(new TabPages(tabFolder, page, TabPages.ADD, caption));
    }

    public static void addPage(JTabbedPane tabFolder, GridField page, String caption) {
        ActionMgr.addAction(new TabPages(tabFolder, page, TabPages.ADD, caption));
    }
    public static void addPage(JComponent tabFolder, Panel page) {
        ActionMgr.addAction(new TabPages((JTabbedPane) tabFolder, page, TabPages.ADD));
    }

    public static void deletePage(JTabbedPane tabFolder, Panel page) {
        ActionMgr.addAction(new TabPages(tabFolder, page, TabPages.DELETE));
    }

    public static void deletePage(JTabbedPane tabFolder, int page) {
        ActionMgr.addAction(new TabPages(tabFolder, page, TabPages.DELETE));
    }

    public static void deletePage(JComponent tabFolder, Panel page) {
        ActionMgr.addAction(new TabPages((JTabbedPane) tabFolder, page, TabPages.DELETE));
    }

    public static void addPage(JTabbedPane tabFolder, Panel page, String caption, Icon icon) {
        ActionMgr.addAction(new TabPages(tabFolder, page, TabPages.ADD, caption, icon));
    }

  public void performAction() {
        if (pages != null) {
          //PM:14/01/2009:moved code to TabFolder
          TabFolder tabPane = (TabFolder)_component;
          tabPane.setPages(pages);
        } else {
            // add or delete a page
            switch (action) {
            case TabPages.ADD:
              _addPage();
                break;

            case TabPages.DELETE:
              _removePage();
                break;

            case TabPages.TOP_PAGE:
                if (this.page != null)
                    ((JTabbedPane) this._component).setSelectedComponent(this.page);
                else if (this.index >= 0)
                    ((JTabbedPane) this._component).setSelectedIndex(this.index);
                break;
                //PM:14/09/2008:implemented replace page
            case TabPages.REPLACE:
              JPanel page = this.page;
                this.page = null;
                _removePage();
                this.page = page;
                _addPage();
                break;
            default:

            }
        }
    }
    private void _removePage(){
        if (this.page != null)
            ((JTabbedPane) this._component).remove(this.page);
        else if (this.index >= 0)
            ((JTabbedPane) this._component).remove(this.index);
        // TF:16/8/07:Force the tab folder to re-layout again
        ((JTabbedPane)_component).setSize(1,1);
        ((JTabbedPane)_component).setMinimumSize(null);
        ((JTabbedPane)_component).setPreferredSize(null);
        ((JComponent)_component).doLayout();
    }

    private void _addPage(){
        if (this.caption == null) {
            if (Caption.get(this.page) != null)
                this.caption = Caption.get(this.page).toString();
        }
        // TF:13/8/07:Panels don't have borders in tab panes
        Border b = page.getBorder();
        if (b instanceof TitledBorder) {
            ((TitledBorder)b).setBorder(new EmptyBorder(0,0,0,0));
            // PM:30 Oct 2008: remove the title string
            ((TitledBorder)b).setTitle(null);
        }
        else {
            page.setBorder(null);
        }

        // CraigM:28/04/2008:Cater for indexes
        if (this.index >=0) {
            ((JTabbedPane) this._component).insertTab(this.caption, this.icon, this.page, "", this.index);
        }
        else {
            ((JTabbedPane) this._component).addTab(this.caption, this.icon, this.page);
        }
    }

    /**
     * Return the current top page of the JTabbed Pane
     * @param comp
     * @return
     */
    public static Panel getTopPage(JTabbedPane comp){
        // TF:14/8/07: Many actions can affect the top page, for example, setting the pages, the Index value, adding pages,
        // setting the top page, removing pages, etc. For this reason we have to process the actions first then return the real value
        ActionMgr.processGUIActions();
        return (Panel)comp.getSelectedComponent();
    }
}
TOP

Related Classes of DisplayProject.actions.TabPages

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.