Package net.helipilot50.stocktrade.displayproject.controls

Source Code of net.helipilot50.stocktrade.displayproject.controls.Panel

/*
Copyright (c) 2003-2009 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 net.helipilot50.stocktrade.displayproject.controls;

import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Hashtable;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import net.helipilot50.stocktrade.displayproject.CloneableComponent;
import net.helipilot50.stocktrade.displayproject.CompoundField;
import net.helipilot50.stocktrade.displayproject.Constants;
import net.helipilot50.stocktrade.displayproject.GridCell;
import net.helipilot50.stocktrade.displayproject.GridField;
import net.helipilot50.stocktrade.displayproject.GridTitledBorder;
import net.helipilot50.stocktrade.displayproject.StatusTextListener;
import net.helipilot50.stocktrade.displayproject.WindowFormLayout;
import net.helipilot50.stocktrade.displayproject.actions.CaptionFont;
import net.helipilot50.stocktrade.displayproject.actions.Column;
import net.helipilot50.stocktrade.displayproject.actions.Parent;
import net.helipilot50.stocktrade.displayproject.actions.Row;
import net.helipilot50.stocktrade.displayproject.events.ClientEventManager;
import net.helipilot50.stocktrade.displayproject.factory.CompoundFieldFactory;
import net.helipilot50.stocktrade.displayproject.swingdisplayer.SwingDisplayer;
import net.helipilot50.stocktrade.displayproject.table.ArrayFieldCellHelper;
import net.helipilot50.stocktrade.framework.CloneHelper;
import net.helipilot50.stocktrade.framework.EventManager;
import net.helipilot50.stocktrade.framework.ForteKeyboardFocusManager;
import net.helipilot50.stocktrade.framework.ParameterHolder_integer;
import net.helipilot50.stocktrade.framework.TextData;

import org.apache.log4j.Logger;

/**
* This is an implementation of the Forte Panel class. It is identical to the
* JPanel and is included to provide type compatability
*
*  PM:5/8/07 remove toString()
*  TF:16/8/07 Major overhaul to make compatible with Forte
*  TF:2/10/07 Added the ignoreInvisibleChildren attribute
*/
@SuppressWarnings("serial")
public class Panel extends JPanel implements CompoundField, CloneableComponent {
    protected Font captionFont;
    protected TextData caption;
    /*
     * these values are used to specify the explicit
     * height and width. The value of -1 indicates that
     * the values have not been set. The values will only
     * be used by the layout manager if the height policy
     * of explicit is set
     */
    protected int minHeight = -1;
    protected int minWidth = -1;

    /**
     * If ignoreInisibleChildren is false, invisible children partake in the sizing calculations
     */
    protected boolean ignoreInvisibleChildren = true;

    protected Insets insets = null;

    public Panel() {
        super();
        setupControl();
    }

    public Panel(boolean isDoubleBuffered) {
        super(isDoubleBuffered);
        setupControl();
    }

    public Panel(LayoutManager layout, boolean isDoubleBuffered) {
        super(layout, isDoubleBuffered);
        setupControl();
    }

    public Panel(LayoutManager layout) {
        super(layout);
        setupControl();
    }

    private void setupControl() {
      // TF:26/07/2008:Set the default colours of the panel to inherit (null)
      this.setForeground(null);
      this.setBackground(null);
      this.setOpaque(true);
      // TF:13/11/2009:DET-130:Added status line listener
      this.addMouseListener(StatusTextListener.sharedInstance());
      // TF:11/12/2009:DET-141:Allowed this control to inherit its popup menus
      this.setInheritsPopupMenu(true);
      // TF:04/03/2010:Panels default their height and width policies to SP_EXPLICIT, not SP_NATURAL
      GridCell.get(this).setWidthPolicy(GridCell.SP_EXPLICIT);
      GridCell.get(this).setHeightPolicy(GridCell.SP_EXPLICIT);
    }

    public void setInsets(Insets pInsets) {
        this.insets = pInsets;
        this.setOpaque(true);
    }

    /**
     * Return the insets for this panel. No child should encroach on these insets
     */
    @Override
    public Insets getInsets() {
        if (this.insets != null) {
            return this.insets;
        }
        else {
            // Need to return the super insets incase there is a border on this panel with a title
            return super.getInsets();
        }
    }

    /**
     * If the height and width policy are Natural use the margin
     * property to set the margin between the panel boundaries and
     * the panel's child widgets. This value is currently in pixels.
     */
    protected int margin = 0;

    public void setMargin(int margin) {
        this.margin = margin;
        // Force a re-layout
        this.revalidate();
    }

    public int getMargin() {
        return margin;
    }

    public int getMinHeight() {
        return minHeight;
    }

    public void setMinHeight(int minHeight) {
        this.minHeight = minHeight;
    }

    public int getMinWidth() {
        return minWidth;
    }

    public void setMinWidth(int minWidth) {
        this.minWidth = minWidth;
    }

    public TextData getCaption() {
        if (caption == null) {
            Border border = getBorder();
            if ((border != null) && (border instanceof TitledBorder)) {
                String title = ((TitledBorder) border).getTitle();
                caption = (title == null) ? null : new TextData(title);
            }
        }
        return caption;
    }

    public void setCaption(TextData pCaption) {
        caption = pCaption;
        Border border = getBorder();
        if (border != null) {
            if (border instanceof TitledBorder) {
                if (pCaption != null) {
                    ((TitledBorder) border).setTitle(pCaption.toString());
                } else {
                    ((TitledBorder) border).setTitle("");
                }
            } else {
                if (pCaption != null) {
                    // TitledBorder tb = BorderFactory.createTitledBorder(border, caption.toString(), TitledBorder.LEFT, TitledBorder.TOP);
                    TitledBorder tb = new GridTitledBorder(border, caption.toString(), TitledBorder.LEFT, TitledBorder.TOP);
                    if (this.captionFont != null) {
                        tb.setTitleFont(this.captionFont);
                    }
                    setBorder(tb);
                }
            }
        } else {
          GridTitledBorder gridBorder;
            if (pCaption != null) {
              gridBorder = new GridTitledBorder(pCaption.toString());
            } else {
              gridBorder = new GridTitledBorder("");
            }
            if (this.captionFont != null) {
              gridBorder.setTitleFont(this.captionFont);
            }
            setBorder(gridBorder);
        }
    }

    /**
     * The CaptionFont attribute specifies the font used for the grid field�s caption as set with the Caption attribute
     */
    public Font getCaptionFont() {
        return CaptionFont.get(this);
    }

    /**
     * The CaptionFont attribute specifies the font used for the grid field�s caption as set with the Caption attribute
     * @param font
     */
    public void setCaptionFont(Font font) {
        this.captionFont = font;
        Border border = getBorder();
        if (border != null) {
            if (border instanceof TitledBorder) {
                ((TitledBorder) border).setTitleFont(font);
            }
        }
        // Force a re-layout
        this.revalidate();
    }
   
    /**
     * Set the font for this Panel. This simply sets the caption font
     */
    @Override
    public void setFont(Font font) {
      setCaptionFont(font);
    }

    public Panel cloneComponent(CompoundField newParent){
        Panel clone = CompoundFieldFactory.newPanel();
        CloneHelper.cloneComponent(this, clone, new String[]{"parent"});
        // Set the parent prior to setting the array part information so the
        // binding can map across properly
        if (newParent != null) {
          Row.set(clone, Row.get(this));
          Column.set(clone, Column.get(this));
          Parent.set(clone, newParent);
        }
        // Set up the binding information for this component. This must be done prior
        // to setting up the kids
        ArrayFieldCellHelper.cloneComponentArrayParts(this, clone);
        for (Component kid : getComponents()){
          JComponent clonedKid;
          // For grid fields and panels, we need to ensure that we call the overload that
          // allows the parent to be set PRIOR to calling cloneComponentArrayParts. The
          // problem is that this method will scan up the parent list looking for a valid
          // binding manager and if we don't have one then the binding will die a horrible
          // death. Hence, the only compound field that can be used to contain other widgets
          // within a grid field inside an array field are a grid field or a panel (currently)
          if (kid instanceof GridField) {
            clonedKid = ((GridField)kid).cloneComponent(clone);
          }
          else if (kid instanceof Panel) {
            clonedKid = ((Panel)kid).cloneComponent(clone);
          }
          else {
            clonedKid = (JComponent)CloneHelper.clone(kid, true);
                Parent.set(clonedKid, clone);
          }

            // Now that the kids are mapped to the new parent, we must add in their binding
            // information if necessary
            ArrayFieldCellHelper.cloneComponentArrayParts((JComponent)kid, clonedKid);
           
            // Set the virtual location client property
            clonedKid.putClientProperty("virtualX", ((JComponent)kid).getClientProperty("virtualX"));
            clonedKid.putClientProperty("virtualY", ((JComponent)kid).getClientProperty("virtualY"));
        }
        return clone;
    }
   
    public Panel cloneComponent() {
      return this.cloneComponent(null);
    }
   
    @Override
    protected void addImpl(Component comp, Object constraints, int index) {
        // Compensate for the insets of the top left corner\
      // TF:06/11/2008:Removed this insets as we now store the virtual location within the panel
//        Insets i = this.getInsets();
//        if (i.top > 0 || i.left > 0) {
//            comp.setLocation(comp.getX() + i.left, comp.getY() + i.top);
//        }

        if (comp instanceof JComponent) {
            // If this object already has constraints, merge them against the new ones
//            constraints = LayoutManagerHelper.mergeConstraints((JComponent)comp, constraints);
        }
       
        super.addImpl(comp, constraints, index);

        // TF:22/8/07:Moved this here from CompoundFieldFactory
        comp.addFocusListener(new FocusListener() {
            public void focusGained(FocusEvent e) {
                EventManager.startEventChain();
                int reason = ForteKeyboardFocusManager.getTraversalReason();
                if (reason != Constants.FC_SUPRESS) {
                    Hashtable<String, Object> params = new Hashtable<String, Object>();
                    params.put("reason", new ParameterHolder_integer(reason));
                    ClientEventManager.postEvent( Panel.this, "AfterFocusGain", params );
                }
                EventManager.endEventChain();
            }

            public void focusLost(FocusEvent e) {
                EventManager.startEventChain();
                int reason = ForteKeyboardFocusManager.getTraversalReason();
                if (reason != Constants.FC_SUPRESS) {
                    Hashtable<String, Object> params = new Hashtable<String, Object>();
                    params.put("reason", new ParameterHolder_integer(reason));
                    ClientEventManager.postEvent( Panel.this, "BeforeFocusLoss", params );
                }
                EventManager.endEventChain();
            }
        });
        // TF:02/03/2010:When adding children into the panel, we can resize the panel
//        if (GridCell.get(this).getWidthPolicy() == Constants.SP_NATURAL || GridCell.get(this).getHeightPolicy() == Constants.SP_NATURAL) {
//          virtualLocationUpdatesEnabled = false;
//          int minX = Integer.MAX_VALUE;
//          int minY = Integer.MIN_VALUE;
//          for (Component child : this.getComponents()) {
//           
//          }
//          virtualLocationUpdatesEnabled = true;
//        }
    }

//    private boolean virtualLocationUpdatesEnabled = true;
//    private final Point virtualLocation = new Point(0, 0);
//    public Point getVirtualLocation() {
//      return virtualLocation;
//    }
//    public void enableVirtualPointUpdates(boolean enable) {
//      this.virtualLocationUpdatesEnabled = enable;
//    }
//    @Override
//    public void setBounds(int x, int y, int width, int height) {
//      if (virtualLocationUpdatesEnabled) {
//        this.virtualLocation.x = x;
//        this.virtualLocation.y = y;
//      }
//      super.setBounds(x, y, width, height);
//    }
   
    /**
     * Set whether the layout manager is to ignore invisible children in the layout or not
     * @param pIgnoreInvisibleChildren
     */
    public void setIgnoreInvisibleChildren(boolean pIgnoreInvisibleChildren) {
        boolean oldValue = ignoreInvisibleChildren;
        this.ignoreInvisibleChildren = pIgnoreInvisibleChildren;
        if (oldValue != pIgnoreInvisibleChildren) {
            firePropertyChange("ignoreInvisibleChildren", oldValue, pIgnoreInvisibleChildren);
            this.invalidate();
            this.validate();
        }
    }

    /**
     * Return whether the layout manager is to ignore invisible children in the layout or not
     */
    public boolean getIgnoreInvisibleChildren() {
        return ignoreInvisibleChildren;
    }

    /**
     * This logger is used to be able to disable the Ctrl-Shift-F1 functionality in production
     */
    private static final Logger DebugGUIPanelLogger = Logger.getLogger("gui.display");
    /**
     * When the developer hits Ctrl-Shift-F1, Swing dumps the contents of a window to the log by
     * default, allowing easier debugging. Overriding this method and using a SwingDisplayer
     * enhances this functionality greatly. NB: This functionality should be disabled before going
     * into production!
     */
    @Override
    public void list(java.io.PrintStream out, int indent) {
      if (DebugGUIPanelLogger.isDebugEnabled()) {
          if (this.getLayout() instanceof WindowFormLayout) {
              new SwingDisplayer(getTopLevelAncestor());
          }
//          super.list(out, indent);
      }
    }

    @Override
    public Container getParent() {
      Container parent = super.getParent();
      if (parent == null){
         parent = (Container)getClientProperty("qq_Tab_Parent");
      }
      return parent;
    }
}
TOP

Related Classes of net.helipilot50.stocktrade.displayproject.controls.Panel

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.