Package DisplayProject.actions

Source Code of DisplayProject.actions.TextValue

/*
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 javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.text.JTextComponent;

import DisplayProject.CharacterField;
import DisplayProject.DataField;
import DisplayProject.DropListModel;
import DisplayProject.ListField;
import DisplayProject.RadioList;
import DisplayProject.RadioListModel;
import DisplayProject.ScrollListModel;
import DisplayProject.UIutils;
import DisplayProject.controls.DropList;
import DisplayProject.controls.FillInField;
import DisplayProject.controls.MenuList;
import DisplayProject.controls.ScrollList;
import DisplayProject.controls.TextGraphic;
import Framework.DataValue;
import Framework.ErrorMgr;
import Framework.TextData;
import Framework.TextNullable;

/**
* This class replicates the TextValue attribute on many Widgets
*
*/
public class TextValue extends PendingAction {
  // TF:24 sept. 2008:Changed this class to return TextNullables, as some people cast to this class
  // Note that the interface does not change, just what is internally stored and returned
    private TextData value;
    public TextValue(Component pComponent, TextData pValue) {
        super(pComponent);
        this.value = pValue;
    }

    public TextData getValue() {
        return new TextData(this.value);
    }

    public void performAction() {
      if (this._component == null)
        return
      if (this._component instanceof JComboBox)
        ((DropListModel)((JComboBox)this._component).getModel()).setTextValue(value);
      else if (this._component instanceof RadioList)
        ((RadioListModel)((RadioList)this._component).getModel()).setTextValue(value);
      else if (this._component instanceof MenuList) {
        ((MenuList)this._component).setTextValue(value);
      }
      else if (this._component instanceof JCheckBox) {
        // TF:26/06/2008:Added check for text boxes
        ((JCheckBox)this._component).setText(TextData.valueOf(value));
      }
      else if (this._component instanceof JList)
        ((ScrollListModel)((JList)this._component).getModel()).setTextValue(value);
      else if (this._component instanceof DataField
        ((DataField)this._component).setTextValue(value);
      else if (this._component instanceof JTextArea) {
        ((JTextArea)this._component).setText(TextData.valueOf(value));
      }
      else if (this._component instanceof JLabel) {
        ((JLabel)this._component).setText(TextData.valueOf(value));
        UIutils.labelWidth((JLabel)this._component);
      } else if (this._component instanceof TextGraphic) {
        ((TextGraphic)this._component).setText(TextData.valueOf(value));
        UIutils.labelWidth((TextGraphic)this._component);
      }else if(this._component instanceof JMenuItem){
        ((JMenuItem)this._component).setText(TextData.valueOf(value));
      }else if(this._component instanceof JButton){
        String text = TextData.valueOf(value);
        // TF:26/06/2008:This code is mostly obsolete, as the layout managers should know how to re-draw
        // children that have resized. The code is also wrong: think of an Explicitly sized button that is
        // larger than it needs to be to display the new text. The old code would have shrunk this down.
//        Insets ins = ((JButton)this._component).getInsets();
//        FontMetrics fm = ((JButton)this._component).getFontMetrics(((JButton)this._component).getFont());
        ((JButton)this._component).setText(text);
//        int width = SwingUtilities.computeStringWidth(fm, text) + ins.left + ins.right;
//        int height = fm.getHeight() + ins.top + ins.bottom;
//        Dimension dim = new Dimension(width, height);
//        ((JButton)this._component).setMinimumSize(dim);
//        ((JButton)this._component).setSize(dim);
      }
      else {
        UnsupportedOperationException errorVar = new UnsupportedOperationException("setTextValue() is not implemented for Type [" + this._component.getClass().getSimpleName() +"]");
        ErrorMgr.addError(errorVar);
        throw errorVar;
      }
    }

    /**
     * Set the text value of the passed component to the passed value
     * @param comp the component whose text value is to be set
     * @param value the new value of the text
     */
    public static void set(JLabel comp, TextData value) {
      ActionMgr.addAction(new TextValue(comp, value));
    }

    /**
     * Set the text value of the passed component to the passed value
     * @param comp the component whose text value is to be set
     * @param value the new value of the text
     */
    public static void set(JLabel comp, String value) {
        ActionMgr.addAction(new TextValue(comp, new TextNullable(value)));
    }

    /**
     * Set the text value of the passed component to the passed value
     * @param comp the component whose text value is to be set
     * @param value the new value of the text
     */
    public static void set(JCheckBox comp, TextData value) {
      ActionMgr.addAction(new TextValue(comp, value));
    }

    /**
     * Set the text value of the passed component to the passed value
     * @param comp the component whose text value is to be set
     * @param value the new value of the text
     */
    public static void set(JCheckBox comp, String value) {
        ActionMgr.addAction(new TextValue(comp, new TextNullable(value)));
    }

    public static void setWithCharacterField(CharacterField comp, TextData value) {
      setWithCharacterField(comp, value.toString());
    }
    public static void setWithCharacterField(CharacterField comp, String value) {
        if (comp instanceof DataField)
            ActionMgr.addAction(new TextValue((DataField)comp, new TextNullable(value)));
        else if (comp instanceof JTextComponent)
            ActionMgr.addAction(new TextValue((JTextComponent)comp, new TextNullable(value)));
        else if (comp instanceof FillInField)
            ActionMgr.addAction(new TextValue((FillInField)comp, new TextNullable(value)));
    }

    /**
     * Set the text value of the passed component to the passed value
     * @param comp the component whose text value is to be set
     * @param value the new value of the text
     */
    public static void set(JButton comp, String value) {
        ActionMgr.addAction(new TextValue(comp, new TextNullable(value)));
    }

    /**
     * Set the text value of the passed component to the passed value
     * @param comp the component whose text value is to be set
     * @param value the new value of the text
     */
    public static void set(JButton comp, TextData value) {
        ActionMgr.addAction(new TextValue(comp, value));
    }

    public static void set(JMenuItem comp, TextData value){
        ActionMgr.addAction(new TextValue(comp,value));
    }

    public static void set(JMenuItem comp, String value) {
        ActionMgr.addAction(new TextValue(comp, new TextNullable(value)));
    }

    /**
     * Set the text value of the passed component to the passed value
     * @param comp the component whose text value is to be set
     * @param value the new value of the text
     */
    public static void set(JTextComponent comp, TextData value){
        ActionMgr.addAction(new TextValue(comp, value));
    }

    /**
     * Set the text value of the passed component to the passed value
     * @param comp the component whose text value is to be set
     * @param value the new value of the text
     */
    public static void set(JTextComponent comp, String value){
        ActionMgr.addAction(new TextValue(comp, new TextNullable(value)));
    }

    /**
     * Set the text value of the passed component to the passed value
     * @param comp the component whose text value is to be set
     * @param value the new value of the text
     */
    public static void set(DataField comp, String value){
        ActionMgr.addAction(new TextValue(comp, new TextNullable(value)));
    }

    /**
     * Set the text value of the passed component to the passed value
     * @param comp the component whose text value is to be set
     * @param value the new value of the text
     */
    public static void set(DataField comp, TextData value){
        ActionMgr.addAction(new TextValue(comp, value));
    }

    /**
     * Set the text value of the passed component to the passed value
     * @param comp the component whose text value is to be set
     * @param value the new value of the text
     */
    public static void set(DataField comp, DataValue value){
        ActionMgr.addAction(new TextValue(comp, value.getTextValue()));
    }

    /**
     * Set the text value of the passed component to the passed value
     * @param comp the component whose text value is to be set
     * @param value the new value of the text
     */
    public static void set(DataField comp, int value) {
        ActionMgr.addAction(new TextValue(comp, new TextNullable(Integer.toString(value))));
    }

    /**
     * Set the text value of the passed component to the passed value
     * @param comp the component whose text value is to be set
     * @param value the new value of the text
     */
    public static void set(DataField comp, double value) {
        ActionMgr.addAction(new TextValue(comp, new TextNullable(Double.toString(value))));
    }
    public static void set(ListField comp, String value){
        set(comp, new TextData(value));
    }
    public static void set(MenuList menuList, TextData textData) {
        ActionMgr.addAction(new TextValue(menuList, textData));
    }
    public static void set(ListField comp, TextData value){
        if (comp instanceof RadioList)
            ActionMgr.addAction(new TextValue((RadioList)comp, value));
        else if (comp instanceof DropList)
            ActionMgr.addAction(new TextValue((DropList)comp, value));
        else if (comp instanceof ScrollList)
            ActionMgr.addAction(new TextValue((ScrollList)comp, value));
        else if (comp instanceof FillInField)
            ActionMgr.addAction(new TextValue((FillInField)comp, value));
    }

    public static TextData get(JTextComponent comp){
        TextData value;
        TextValue action = ActionMgr.getAction(comp, TextValue.class);
        if (action != null) {
            value = ((TextValue)action).getValue();
            if (!(value instanceof TextNullable)) {
              value = new TextNullable(value);
            }
        } else {
          //PM:14/3/08 bound the text value
          value = TextData.bind(comp, "text");
        }
        return value;
    }

    public static TextData getWithCharacterField(CharacterField comp){
        TextData value = null;
        if (comp instanceof DataField){
            value = get((DataField)comp);
        } else if (comp instanceof JTextComponent){
          value = get((JTextComponent)comp);
        } else if (comp instanceof FillInField){
          value = get((ListField)comp);
        }
        return value;
    }

    public static TextData get(JLabel comp){
        TextData value;
        TextValue action = ActionMgr.getAction(comp, TextValue.class);
        if (action != null) {
            value = ((TextValue)action).getValue();
            if (!(value instanceof TextNullable)) {
              value = new TextNullable(value);
            }
        } else {
          //PM:14/3/08 bound the text value
          value = TextData.bind(comp, "text");
        }
        return value;
    }

    public static TextData get(JButton comp){
        TextData value;
        TextValue action = ActionMgr.getAction(comp, TextValue.class);
        if (action != null) {
            value = ((TextValue)action).getValue();
            if (!(value instanceof TextNullable)) {
              value = new TextNullable(value);
            }
        } else {
          //PM:14/3/08 bound the text value
          value = TextData.bind(comp, "text");
        }
        return value;
    }

    public static TextData get(JMenuItem comp){
        TextData value;
        TextValue action = ActionMgr.getAction(comp, TextValue.class);
        if (action != null) {
            value = ((TextValue)action).getValue();
            if (!(value instanceof TextNullable)) {
              value = new TextNullable(value);
            }
        } else {
          //PM:14/3/08 bound the text value
          value = TextData.bind(comp, "text");
        }
        return value;
    }

    public static TextData get(ListField comp){
      TextData value = null;
      TextValue action = ActionMgr.getAction((Component)comp, TextValue.class);
      if (action != null) {
        value = ((TextValue)action).getValue();
            if (!(value instanceof TextNullable)) {
              value = new TextNullable(value);
            }
      } else {
          //PM:14/3/08 bound the text value
        if (comp instanceof ScrollList){
                value = TextData.bind(comp, "text");

        } else if (comp instanceof RadioList) {
          value = TextData.bind(comp, "text");
        } else if (comp instanceof DropList) {
          value = TextData.bind(comp, "text");
        } else if (comp instanceof FillInField) {
          value = TextData.bind(comp, "editedValue");
        } else {
          UnsupportedOperationException errorVar = new UnsupportedOperationException("getTextValue() is not implemented for " + comp.getClass().getName());
          ErrorMgr.addError(errorVar);
          throw errorVar;
        }
      }
      return value;
    }

    public static TextData get(DataField comp){
      TextData value;
      TextValue action = ActionMgr.getAction(comp, TextValue.class);
      if (action != null) {
        value = ((TextValue)action).getValue();
            if (!(value instanceof TextNullable)) {
              value = new TextNullable(value);
            }
      } else {
          //PM:14/3/08 bound the text value
        value = TextData.bind(comp, "text");
      }
      return value;
    }

    public static TextData get(JCheckBox comp){
      TextData value;
      TextValue action = ActionMgr.getAction(comp, TextValue.class);
      if (action != null) {
        value = ((TextValue)action).getValue();
            if (!(value instanceof TextNullable)) {
              value = new TextNullable(value);
            }
      } else {
          //PM:14/3/08 bound the text value
        value = TextData.bind(comp, "text");
      }
      return value;
    }

    public static TextData get(MenuList menuList) {
      TextData value;
        TextValue action = ActionMgr.getAction(menuList, TextValue.class);
        if (action != null) {
            value = ((TextValue)action).getValue();
            if (!(value instanceof TextNullable)) {
              value = new TextNullable(value);
            }
        } else {
            value = new TextNullable(menuList.getTextValue());
        }
        return value;
    }
}
TOP

Related Classes of DisplayProject.actions.TextValue

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.