Package DisplayProject.actions

Source Code of DisplayProject.actions.Caption

/*
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 DisplayProject.actions;

import java.awt.Component;
import java.awt.Font;

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

import DisplayProject.GridField;
import DisplayProject.GridTitledBorder;
import DisplayProject.RadioList;
import DisplayProject.controls.Panel;
import Framework.TextData;
/**
* Provides caption features on a Panel or GridField or RadioList
*
*/
public class Caption extends PendingAction {

  private TextData caption;
  private Caption(Component pComponent, TextData value) {
    super(pComponent);
    this.caption = value;
  }
  private Caption(Component pComponent, String value) {
    super(pComponent);
    this.caption = new TextData(value);
  }
  public void performAction() {
    // TF:26/06/2008:Made this class handle RadioLists
    if (this._component instanceof RadioList) {
      RadioList rl = (RadioList)this._component;
      rl.setCaption(this.caption);

      // CraigM:26/08/2008 - If we add or remove a caption, this affects the size of the radio list
      rl.updateSize();
    }
    else if (this._component != null) {
      // CraigM:23/07/2008 - Changed to JComponent as a JScrollPane can have captions too
      JComponent panel = (JComponent)this._component;
      panel.putClientProperty("qq_caption", this.caption.toString());
      if (panel.getParent() instanceof JTabbedPane){
        JTabbedPane tp = (JTabbedPane)panel.getParent();
        int index = tp.indexOfComponent(panel);
        tp.setTitleAt(index, caption.toString());
      }
      else if (panel instanceof GridField) {
        ((GridField)panel).setCaption(caption);
      }
      else if (panel instanceof Panel) {
        ((Panel)panel).setCaption(caption);
      }
      else {
        Border border = panel.getBorder();
        if (border != null) {
          if (border instanceof TitledBorder) {
            TitledBorder titledBorder = (TitledBorder)border;
            if (caption != null){
              titledBorder.setTitle(caption.toString());
              Font titleFont = (Font)panel.getClientProperty("qq_CaptionFont");
              if (titleFont != null){
                titledBorder.setTitleFont(titleFont);
              }

            }
            else {
              titledBorder.setTitle("");
            }
          } else {
            if (caption != null){
              panel.setBorder(setCaptionInBorder(border, caption.toString()));
              // TF:24/04/2009:Removed these lines as the border is not a TitledBorder
              // Font titleFont = (Font)panel.getClientProperty("qq_CaptionFont");
              // if (titleFont != null){
              //  ((TitledBorder)border).setTitleFont(titleFont);
              // }
            }
          }
        } else {
          if ((caption != null) && (!(caption.length()==0))) {//PM:7 oct. 2008:performance improvement
            // CraigM:26/08/2008 - Fixed to set the border variable
            border = new GridTitledBorder(caption.toString());
            // panel.setBorder(BorderFactory.createTitledBorder(caption.toString()));
            panel.setBorder(border);
            Font titleFont = (Font)panel.getClientProperty("qq_CaptionFont");
            if (titleFont != null) {
              ((TitledBorder)border).setTitleFont(titleFont);
            }
          }
          else {
            panel.setBorder(null);
          }
        }
      }
    }
  }
 
  private Border setCaptionInBorder(Border border, String caption){
    //        return BorderFactory.createTitledBorder(border, caption, TitledBorder.LEFT, TitledBorder.TOP);
    return new GridTitledBorder(border, caption, TitledBorder.LEFT, TitledBorder.TOP);
  }
 
  public static void set(JComponent comp, String value){
    ActionMgr.addAction(new Caption(comp, value));
  }
 
  public static void set(JComponent comp, TextData value){
    ActionMgr.addAction(new Caption(comp, value));
  }
 
  public static TextData get(JComponent comp){
    Caption action = ActionMgr.getAction(comp, Caption.class);
    if (action != null ) {
      return action.caption;
    }
    else if (comp instanceof RadioList) {
      // TF:26/06/2008:Added support for RadioLists
      return ((RadioList)comp).getCaption();
    }
    JPanel panel = (JPanel)comp;
    if (panel.getParent() instanceof JTabbedPane) {
      JTabbedPane tp = (JTabbedPane)panel.getParent();
      int index = tp.indexOfComponent(panel);
      return index >=  0 ? new TextData(tp.getTitleAt(index)) : new TextData("");
    }
    else {
      Border border =  panel.getBorder();
      if (border instanceof TitledBorder) {
        return new TextData(((TitledBorder)border).getTitle());
      } else {
        return null;
      }
    }
  }
}
TOP

Related Classes of DisplayProject.actions.Caption

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.