TKTextField.java :  » Content-Management-System » webman » com » teamkonzept » field » Java Open Source

Java Open Source » Content Management System » webman 
webman » com » teamkonzept » field » TKTextField.java
package com.teamkonzept.field;

import com.teamkonzept.lib.*;
import com.teamkonzept.publishing.markups.*;
import com.teamkonzept.web.*;
import com.teamkonzept.field.db.*;
import com.teamkonzept.international.LanguageManager;
import org.w3c.dom.*;

/**
 * The text field control.
 *
 * @author $Author: uli $
 * @version $Revision: 1.27 $
 */
public class TKTextField
  extends TKAtomField
{
  // $Id: TKTextField.java,v 1.27 2002/02/27 11:07:04 uli Exp $

  /**
   * The class identifier.
   */
  public static final String CLASS_ID = "TEXT";
  public static final String COLS_KEY = "COLS";
  public static final String ROWS_KEY = "ROWS";
  public static final String MAX_SIZE_KEY = "MAX_SIZE";

  /**
   * The column field size.
   */
  public static final int COLUMN_FIELD_SIZE = 3;

  /**
   * The length field size.
   */
  public static final int LENGTH_FIELD_SIZE = 6;

  protected int rows = 0;

  protected int cols = 0;

  protected int maxSize = 0;

  public TKTextField() {};

  public TKTextField( String name, int rows, int cols, int maxSize )
  {
    this( name, rows, cols, maxSize, null );
  }

  public TKTextField( String name, int rows, int cols, int maxSize, String showName )
  {
    initTextField( CLASS_ID, name, rows, cols, maxSize, showName );
  }

  public void initTextField( String type, String name, int rows, int cols, int maxSize, String showName )
  {
    initAtomField( type, name, showName );
    this.rows = rows;
    this.cols = cols;
    this.maxSize = maxSize;
  }

  public void init( String fieldType, Object initData )
    throws
      TKUnregisteredClassException,
      ClassNotFoundException,
      InstantiationException,
      IllegalAccessException
  {
    super.init( fieldType, initData );
    TKHashtable data = (TKHashtable) initData;
    rows = Integer.parseInt( (String) data.get(ROWS_KEY) );
    cols = Integer.parseInt( (String) data.get(COLS_KEY) );

        String value = (String) data.get(MAX_SIZE_KEY);
    this.maxSize = value != null && value.length() > 0
               ? Integer.parseInt(value)
               : 0;
  }

  /**
   * Methode zur Definition eines Textfeldes
   */
  public TKFieldGroup getDefGroup(TKFieldSwitch allSwitch, TKFieldSwitchList allSwitchList) {

    TKBaseField [] textArray = {
      new TKInputField( TKTextField.NAME_KEY, TKInputField.SMALL_DEFAULT_SIZE, TKInputField.SMALL_DEFAULT_LENGTH, LanguageManager.getText(LANGUAGE_CONTEXT, "INPUT_NAME"), TKInputField.CHECK_STRING),
      new TKInputField( TKTextField.SHOW_NAME_KEY, TKInputField.LARGE_DEFAULT_SIZE, TKInputField.LARGE_DEFAULT_LENGTH, LanguageManager.getText(LANGUAGE_CONTEXT, "INPUT_SHOWNAME"), TKInputField.CHECK_STRING),
      new TKInputField( TKTextField.COLS_KEY, COLUMN_FIELD_SIZE, COLUMN_FIELD_SIZE, LanguageManager.getText(LANGUAGE_CONTEXT, "TEXTFIELD_WIDTH"), TKInputField.CHECK_INTEGER),
      new TKInputField( TKTextField.ROWS_KEY, COLUMN_FIELD_SIZE, COLUMN_FIELD_SIZE, LanguageManager.getText(LANGUAGE_CONTEXT, "TEXTFIELD_HEIGHT"), TKInputField.CHECK_INTEGER),
      new TKInputField( MAX_SIZE_KEY, LENGTH_FIELD_SIZE, LENGTH_FIELD_SIZE, LanguageManager.getText(LANGUAGE_CONTEXT, "INPUT_MAXLENGTH"), TKInputField.CHECK_OPTIONAL_INTEGER)

    };
    TKFieldGroup textGroup =
      new TKFieldGroup( TKTextField.CLASS_ID, new TKVector( textArray ), LanguageManager.getText(LANGUAGE_CONTEXT, TKTextField.CLASS_ID) );
    return textGroup;
  }

  public Object toData ()
  {
    TKHashtable result = (TKHashtable) super.toData();

    result.put(ROWS_KEY, String.valueOf(rows));
    result.put(COLS_KEY, String.valueOf(cols));

    if (maxSize > 0)
    {
      result.put(MAX_SIZE_KEY, String.valueOf(maxSize));
    }

    return result;
  }

  public void fillIntoTemplate (TKHTMLTemplate t, Object value, String prefix)
  {
    super.fillIntoTemplate(t, value, prefix);

    t.set(ROWS_KEY, String.valueOf(rows));
    t.set(COLS_KEY, String.valueOf(cols));

    if (maxSize > 0)
    {
      t.set(MAX_SIZE_KEY, String.valueOf(maxSize));
    }
  }

  public void fillAttributesIntoNode(Element node, Object data) throws DOMException
  {
    super.fillAttributesIntoNode(node, data);
    node.setAttribute( ROWS_KEY, String.valueOf( rows ) );
    node.setAttribute( COLS_KEY, String.valueOf( cols ) );
    node.setAttribute( MAX_SIZE_KEY,String.valueOf( maxSize));
  }

  public int realInsertIntoDB( TKFormDBData db, int formId )
  {
    if( super.realInsertIntoDB( db, formId ) == -1 ) return -1;
    insertNewFieldAttribute( db, formId, ROWS_KEY, 0, String.valueOf( rows ) );
    insertNewFieldAttribute( db, formId, COLS_KEY, 0, String.valueOf( cols ) );
    insertNewFieldAttribute( db, formId, MAX_SIZE_KEY, 0, String.valueOf( maxSize));
    return fieldId;
  }

  public void initFromDB( String classId, TKFormDBData db, TKVector otherFields )
    throws
      TKUnregisteredClassException,
      ClassNotFoundException,
      InstantiationException,
      IllegalAccessException
  {
    super.initFromDB( classId, db, otherFields );
    rows = Integer.parseInt( getFieldAttribute( db, ROWS_KEY, 0 ) );
    cols = Integer.parseInt( getFieldAttribute( db, COLS_KEY, 0 ) );
    if (hasFieldAttribute( db, MAX_SIZE_KEY, 0 ))
      maxSize = Integer.parseInt( getFieldAttribute( db, MAX_SIZE_KEY, 0) );
  }

  public Object compileData (String prefix, TKHashtable data, TKHashtable context)
  {
    Object result = super.compileData(prefix, data, context);
    Object[] args = new Object[1];
    args[0] = fieldName;
    String text = (String) data.get(prefix+fieldName);

    // Laenge ueberpruefen

    if (maxSize > 0)
    {
      if (text.length() > maxSize && context != null)
      {
        TKHashtable table = new TKHashtable ();
        table.put ("DIAG",LanguageManager.getText(LANGUAGE_CONTEXT, "TEXTFIELD_TOO_LONG", args));
        table.put ("PATH",prefix+fieldName);
        addToContext (table,DIAGS_KEY,context);
      }
    }
    return result;
  }

  public Object compileData( String prefix, TKMarkupNode data, TKHashtable context )
  {
    TKXmlMarkup markup = data == null ? null : (TKXmlMarkup) data.markup;
    if (markup == null) {  return null;}

    if (!markup.name.equals (getName())) {

      return null;
    }

    TKXmlTree tree = (TKXmlTree) data.tree;
    if (tree == null) {  return null;}

    String val = tree.getSingleText ();

    if (val == null) return getDefault();
    else return val.trim();
  }

  /**
   * Checks wether this object and the specified object
   * may be treated as equal.
   *
   * @param object the object to checked for equality.
   * @return <CODE>true</CODE> if this object and the
   * specified object may be treated as equal, otherwise
   * <CODE>false</CODE>.
   */
  public boolean equals (Object object)
  {
    if (! super.equals(object))
    {
      return false;
    }

    TKTextField field = (TKTextField) object;

    return (this.rows == field.rows) &&
         (this.cols == field.cols) &&
         (this.maxSize == field.maxSize);
  }

  /**
   * Returns the hash code for this object.
   *
   * @return the hash code for this object.
   */
  public int hashCode ()
  {
    // Implementation for JTest only ;-(
    return super.hashCode();
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.