// Copyright 1996, Marimba Inc. All Rights Reserved.


// @(#)TextWidget.java, 1.10, 12/04/96





package marimba.gui;





import java.awt.*;





import marimba.persist.*;





/**


 * A generic text widget.


 *


 * @author	Arthur van Hoff


 * @version 	1.10, 12/04/96


 */


public abstract class TextWidget extends Widget {


    /**


     * A textWidget can be editable or not editable.


     * This means that the content can be changed or not.


     * @see #isEditable


     * @see #setEditable


     */


    public boolean editable;





    /**


     * Determine which characters are part of a word.


     */


    public static boolean isWord(char ch) {


	return ((ch >= 'a') && (ch <= 'z')) ||


	       ((ch >= 'A') && (ch <= 'Z')) ||


	       ((ch >= '0') && (ch <= '9')) ||


		(ch == '_') || (ch == '$');


    }





    /**


     * Get the properties of this widget.


     */


    public void getProperties(PropertyList list) {


	super.getProperties(list);


	list.setBoolean("disabled", disabled, false);


	list.setBoolean("editable", editable, false);


    }





    /**


     * Set the properties of this widget.


     */


    public void setProperties(PropertyList list) {


	super.setProperties(list);


	disabled = list.getBoolean("disabled", false);


	editable = list.getBoolean("editable", false);


    }





    /**


     * Get the value of the text.


     */


    public String getStringValue() {


	return getText();


    }





    /**


     * Get the value of the text.


     */


    public Object getValue() {


	return getText();


    }





    /**


     * Set the value of the text.


     */


    public void setValue(Object value) {


	if (value instanceof String) {


	    setText((String)value);


	} else if (value == null) {


	    setText("");


	} else {


	    setText(String.valueOf(value));


	}


    }





    /**


     * Check if this text is editable.


     * @see #editable


     */


    public boolean isEditable() {


	return editable;


    }





    /**


     * Make this text editable.


     * @see #editable


     */


    public void setEditable(boolean editable) {


	if (this.editable != editable) {


	    this.editable = editable;


	    repaint();


	}


    }





    /**


     * Get the length of the text.


     */


    public int getLength() {


	return getText().length();


    }





    /**


     * Return the start of the selection.


     */


    public int getSelStart() {


	return 0;


    }





    /**


     * Return the end of the selection.


     */


    public int getSelEnd() {


	return getLength();


    }





    /**


     * Get the selected part of the text.


     */


    public String getSelected() {


	return getText().substring(getSelStart(), getSelEnd());


    }





    /**


     * Clear the text.


     */


    public void clear() {


	setText(null);


    }





    /**


     * Clear the current selection.


     */


    public void delete() {


    }





    /**


     * Insert a character. This wil first clear the selecton, before


     * inserting the character.


     */


    public void insert(char ch) {


	char str[] = {ch};


	insert(new String(str));


    }





    /**


     * Insert a string at the current cursor position.


     */


    public void insert(String str) {


    }





    /**


     * Append to the end of text.


     */


    public void append(char ch) {


	select(getLength());


	insert(ch);


    }





    /**


     * Append to the end of text.


     */


    public void append(String str) {


	select(getLength());


	insert(str);


    }





    /**


     * This widget is interested in getting the character focus.


     */


    public boolean focusInterest() {


	return editable && !disabled;


    }





    /**


     * Select the entire text.


     */


    public void selectAll() {


	select(0, getLength());


    }





    /**


     * Select a postition.


     */


    public void select(int pos) {


	select(pos, pos, 0);


    }





    /**


     * Select a portion of the text.


     */


    public void select(int selStart, int selEnd) {


	select(selStart, selEnd, 0);


    }





    /**


     * Select a portion of the text. Depth controls character (1),


     * word (2), and line selection (3).


     */


    public void select(int selStart, int selEnd, int depth) {


    }





    /**


     * Focus on a character position.


     */


    public void focus(int pos) {


    }





    /**


     * Checks whether the given event is within the text area of


     * this text widget. Used in handleEvent to determine whether


     * the mouse is in the text area so the cursor should be changed.


     */


    protected boolean eventInText(Event evt) {


	return (evt.target == this && evt.x > 0 && evt.x < width && evt.y > 0 && evt.y < height);


    }





    /**


     * Change the cursor when necessary.


     */


    public boolean handleEvent(Event evt) {


	switch (evt.id) {


	    case Event.MOUSE_MOVE:


		if (editable && !disabled) {


		    if (eventInText(evt)) {


			setCursor(Frame.TEXT_CURSOR);


		    } else {


			resetCursor();


		    }


		}


		return true;





	    case Event.MOUSE_ENTER:


		if (editable && !disabled && eventInText(evt)) {


		    setCursor(Frame.TEXT_CURSOR);


		}


		break;





	    case Event.MOUSE_EXIT:


		if (editable && !disabled && evt.target == this) {


		    resetCursor();


		}


		break;


	}


	return super.handleEvent(evt);


    }


}


