/*
* File: textArea.java
* This file is part of Tico, an application
* to create and perform interactive communication boards to be
* used by people with severe motor disabilities.
*
* Authors: Eduardo Ferrer
*
* Date: September-2012
*
* Company: Universidad de Zaragoza, CPS, DIIS
*
* License:
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package android.TICO;
import android.TICO.Views.RectangleView;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.widget.HorizontalScrollView;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Defines a textArea
and its attributes.
*
* @author Eduardo Ferrer
* @version 1.0 Sep, 2012
*/
public class textArea {
/**
* Defines a textArea
and its attributes.
*
* @author Eduardo Ferrer
* @version 1.0 Sep, 2012
*/
private RectangleView rv;
private TextView tv;
private String id,text;
private boolean canSendText=true;
/**
* Creates a new textArea
.
* @param marginLeft The number marginLeft
of pixels of the left margin
* @param marginTop The number marginTop
of pixels of the top margin
* @param height The width
in pixels of the textArea
* @param width The height
in pixels of the textArea
* @param frameSize The size
of the frame in pixels of the textArea
* @param id The id
identificator of the textArea
* @param context The context
that contains the textArea
*/
public textArea(int marginLeft,int marginTop,int width,int height,int frameSize,String id,Context context){
this.id=id;
//ponemos el marco
rv=new RectangleView(marginLeft,marginTop,marginLeft+width,marginTop+height,context);
rv.setBorderWidth(frameSize);
//ponemos el texto
tv=new TextView(context);
RelativeLayout.LayoutParams ptext = new RelativeLayout.LayoutParams(width-2*frameSize,height-2*frameSize);
ptext.setMargins(marginLeft+frameSize, marginTop+frameSize,0, 0);
tv.setLayoutParams(ptext);
}
/**
* Set the text size of the textArea.
* @param textSize The desired size
of the text in the cell
*/
public void setTextSize(float textSize){
tv.setTextSize(textSize);
}
/**
* Set the color of the text
* @param color The integer that defines the desired color for the text
*/
public void setTextColor(int textColor){
tv.setTextColor(textColor);
}
/**
* Set the text of the text area
* @param text
*/
public void setText(String text){
tv.setText(text);
this.text=text;
}
/**
* Set the font and style of the text
*
* @param typeface The font
of the text
* @param textStyle int
whith the text style
*/
public void setTypeface(Typeface typeface,int textStyle){
tv.setTypeface(typeface,textStyle);
}
/**
* Set the alignment of the text inside the text Area
* @param ha the horizontal alignment. 0
center, 2
left, 4
right. Otherwise center.
* @param va the vertical alignment. 0
center, 1
top, 3
bottom. Otherwise center.
*/
public void alignText(int ha,int va){
//alineamos el texto
switch(ha){
case 0:
ha=Gravity.CENTER_HORIZONTAL;
break;
case 2:
ha=Gravity.LEFT;
break;
case 4:
ha=Gravity.RIGHT;
break;
default:
ha=Gravity.CENTER_HORIZONTAL;
break;
}
switch(va){
case 0:
va=Gravity.CENTER_VERTICAL;
break;
case 1:
va=Gravity.TOP;
break;
case 3:
va=Gravity.BOTTOM;
break;
default:
va=Gravity.CENTER_VERTICAL;
break;
}
tv.setGravity(va+ha);
}
/**
* Set the frame color of the text area
* @param color The color
of the text area frame
*/
public void setFrameColor(int frameColor){
rv.setColor(frameColor);
}
/**
* Set the border line with of the textArea
* @param frameSize border with in pixels
*/
public void setBorderWidth(int frameSize){
rv.setBorderWidth(frameSize);
}
/**
* Set the background color and the gradient
* @param backgroundColor The color
of the background
* @param gradient The gradient color
of the background
*/
public void setBackgroundColor(int backgroundColor,int gradient){
if(gradient==Color.TRANSPARENT){ //color simple de fondo
rv.setBackgroundColor(backgroundColor);
}else{ //color con gradiente de fondo
int Colors[]=new int[2];
Colors[0]=backgroundColor;
Colors[1]=gradient;
GradientDrawable g = new GradientDrawable(GradientDrawable.Orientation.TL_BR, Colors); //Colors is an array with 2 heximal values as int.
rv.setBackgroundDrawable(g);
}
}
/**
* Add the textArea to a RelativeLayout
* @param lay Relative layout that will contain the textArea
*/
public void addTo(RelativeLayout lay){
lay.addView(rv);
lay.addView(tv);
}
/**
* Change temporally the text shown in the text area. While a sent text is shown we ignore the other sending text until the original text is restored
* @param newtext the new text to show.
* @param time Time to restore the original text of the text area.
*/
public void sendText(final String newtext,int time){
//if(tv.getText().equals(text)){
// if(!tv.getText().equals(newtext)){
synchronized(this){
if(canSendText){
canSendText=false;
}else{
return;
}
}
tv.setText(newtext);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//pendingThreads--;
//if(tv.getText().equals(newtext) && pendingThreads==0){
tv.setText(text);
//}
canSendText=true;
}
}, time);
//}
//}
}
/**
* Return the identifier of this textArea. Is used to select the text area to send a text.
* @return the identifier of the textArea
*/
public String getId(){
return id;
}
}