Package DisplayProject.controls

Source Code of DisplayProject.controls.ToggleField

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

import java.awt.Dimension;
import java.awt.Insets;

import javax.swing.Action;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.JCheckBox;

import Framework.CloneHelper;
/**
* The TogglField class is a subclass of JCheckBox. Its purpose is to provide a type compatability with the original forte components.
*
*/
@SuppressWarnings("serial")
public class ToggleField extends JCheckBox {

  private boolean editable = true;
 
    // TF:20/8/07:Altered the margins to be more like Forte in terms of spacing of the components
    public ToggleField() {
        super();
        this.setupControl();
    }

    public ToggleField(Action a) {
        super(a);
        this.setupControl();
    }

    public ToggleField(Icon icon, boolean selected) {
        super(icon, selected);
        this.setupControl();
    }

    public ToggleField(Icon icon) {
        super(icon);
        this.setupControl();
    }

    public ToggleField(String text, boolean selected) {
        super(text, selected);
        this.setupControl();
    }

    public ToggleField(String text, Icon icon, boolean selected) {
        super(text, icon, selected);
        this.setupControl();
    }

    public ToggleField(String text, Icon icon) {
        super(text, icon);
        this.setupControl();
    }

    public ToggleField(String text) {
        super(text);
        this.setupControl();
    }

    private void setupControl() {
      setBackground(null);
      this.setMargin(new Insets(0,0,0,0));
      // TF:15/12/2009:DET-141:Set this up to allow inheriting of popup menu
      this.setInheritsPopupMenu(true);
    }
   
    @Override
    public void setText(String text) {
        super.setText(text);
        Dimension pSize = getPreferredSize();
        Dimension size = getSize();
        boolean update = false;
        if (pSize.width > size.width){
            size.width = pSize.width;
            update = true;
        }
        if (pSize.height > size.height){
            size.height = pSize.height;
            update = true;
        }
        if (update){
            setSize(size);
            revalidate();
        }
    }

    @Override
    public Dimension getMinimumSize() {
        return getPreferredSize();
    }

    // TF:07/11/2009:DET-129:This code introduces a bug if the toggle field has focus, the window is
    // left and then re-entered. This will simulate a click on the toggle field which is not desired.
    // I can't find a test case that breaks without this code here, so I removed this code.
    //PM:16/4/08 added to handle the traversal by mouse click
//    @Override
//    protected void processFocusEvent(FocusEvent e) {
//      super.processFocusEvent(e);
//      boolean inArrayField = (getClientProperty("ArrayField") != null);
//      int id = e.getID();
//      if ((id == FocusEvent.FOCUS_GAINED) &&
//          (ForteKeyboardFocusManager.getTraversalReason() == Constants.FC_MOUSECLICK) &&
//          !inArrayField){
//        doClick();
//      }
//    }

    /**
     * Set this component to be editable or not. An editable toggle field allows its value to be
     * changed whereas a non-editable one does not allow it's value to be changed by the user, but
     * can still be changed programmatically. This is needed so that view-only components will still
     * fire mouse events like click and child click, whereas simply disabling the compoinent would
     * not allow these events to be fired.
     * @param editable
     */
    public void setEditable(boolean editable) {
      if (this.editable != editable) {
        this.editable = editable;
        // Invoke setModel so we set the model to be the correct sort of model
        if (editable) {
          ButtonModel model = getModel();
          if (model instanceof ReadOnlyButtonModel) {
            this.setModel(((ReadOnlyButtonModel)model).getOriginalModel());
          }
        }
        else {
          ButtonModel model = getModel();
          if (!(model instanceof ReadOnlyButtonModel)) {
            this.setModel(new ReadOnlyButtonModel(this, model));
          }
        }
        this.firePropertyChange("editable", !editable, editable);
      }
  }
   
    /**
     * Return true if the the component's value can be changed by the user.
     * @return
     */
    public boolean isEditable() {
    return editable;
  }

    /**
     * Set the model. This will also set the editable property of the control.
     */
    @Override
    public void setModel(ButtonModel newModel) {
      if (newModel instanceof ReadOnlyButtonModel) {
        editable = false;
      }
      else {
        editable = true;
      }
    super.setModel(newModel);
    }
   
    public ToggleField cloneComponent(){
        ToggleField clone = new ToggleField();
        CloneHelper.cloneComponent(this, clone, new String[]{"UI", // class javax.swing.plaf.LabelUI
                "UIClassID", // class java.lang.String
                "accessibleContext", // class javax.accessibility.AccessibleContext
                "actionMap", // class javax.swing.ActionMap
                "alignmentX", // float
                "alignmentY", // float
                "ancestorListeners", // class [Ljavax.swing.event.AncestorListener;
                //"appData", // class java.lang.Object
                "autoscrolls", // boolean
                //"background", // class java.awt.Color
                "border", // interface javax.swing.border.Border
                //"component", // null
                "componentCount", // int
                //"componentPopupMenu", // class javax.swing.JPopupMenu
                "components", // class [Ljava.awt.Component;
                "containerListeners", // class [Ljava.awt.event.ContainerListener;
                "debugGraphicsOptions", // int
                //"disabledIcon", // interface javax.swing.Icon
                //"displayedMnemonic", // int
                //"displayedMnemonicIndex", // int
                "doubleBuffered", // boolean
                //"enabled", // boolean
                "focusCycleRoot", // boolean
                "focusTraversalKeys", // null
                "focusTraversalPolicy", // class java.awt.FocusTraversalPolicy
                "focusTraversalPolicyProvider", // boolean
                "focusTraversalPolicySet", // boolean
                //"focusable", // boolean
                //"font", // class java.awt.Font
                //"foreground", // class java.awt.Color
                "graphics", // class java.awt.Graphics
                //"height", // int
                //"horizontalAlignment", // int
                //"horizontalTextPosition", // int
                //"icon", // interface javax.swing.Icon
                //"iconTextGap", // int
                //"inheritsPopupMenu", // boolean
                "inputMap", // null
                "inputVerifier", // class javax.swing.InputVerifier
                "insets", // class java.awt.Insets
                "labelFor", // class java.awt.Component
                "layout", // interface java.awt.LayoutManager
                "managingFocus", // boolean
                //"maximumSize", // class java.awt.Dimension
                //"minimumSize", // class java.awt.Dimension
                "name", // class java.lang.String
                "nextFocusableComponent", // class java.awt.Component
                //"opaque", // boolean
                "optimizedDrawingEnabled", // boolean
                "paintingTile", // boolean
                "preferredSize", // class java.awt.Dimension
                "registeredKeyStrokes", // class [Ljavax.swing.KeyStroke;
                "requestFocusEnabled", // boolean
                "rootPane", // class javax.swing.JRootPane
                //"selected", // boolean
                //"text", // class java.lang.String
                //"toolTipText", // class java.lang.String
                "topLevelAncestor", // class java.awt.Container
                "transferHandler", // class javax.swing.TransferHandler
                //"transparent", // boolean
                "validateRoot", // boolean
                "verifyInputWhenFocusTarget", // boolean
                //"verticalAlignment", // int
                //"verticalTextPosition", // int
                "vetoableChangeListeners", // class [Ljava.beans.VetoableChangeListener;
                //"visible", // boolean
                "visibleRect", // class java.awt.Rectangle
                //"width", // int
                //"x", // int
                //"y" // int
                });
        return clone;
    }
}
TOP

Related Classes of DisplayProject.controls.ToggleField

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.