<commit_before>package org.dimagi.chatscreen;

import java.util.Vector;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

import org.dimagi.entity.Question;
import org.dimagi.utils.StringUtils;
import org.dimagi.utils.ViewUtils;
import org.dimagi.view.Component;
import org.dimagi.view.Widget;
import org.dimagi.view.widget.ChoiceList;
import org.dimagi.view.widget.Textbox;

/**
 * The Frame component is the basic element of the Chat Screen
 * interface. It displays a Question in the optimal fashion
 * based on its text and widget type.
 * 
 * A Frame can be considered the UI equivilant of a Question object.
 * 
 * @author ctsims
 * @date Aug-08-2007
 *
 */
public class Frame extends Component {
	
	private Question _question;
	
	private boolean _isActiveFrame = true;
	
	private String _text = "";
	
	private int _labelWidth;
	
	private int _xBufferSize;
	
	private int _yBufferSize;
	
	private Widget _theWidget;
	
	/**
	 * Creates a new Frame for the given Question.
	 * @param theQuestion The question that this frame will represent.	
	 */
	public Frame(Question theQuestion) {
		_question = theQuestion;
		setText();
		setupWidget();
	}
	
	/**
	 * Sets the drawing mode of the frame to either large or small
	 * @param small True if the frame should be drawn in its small form, false otherwise.
	 */
	public void setActiveFrame(boolean isActiveFrame) {
		_isActiveFrame = isActiveFrame;
		setText();
		if(isActiveFrame) {
			setBackgroundColor(ViewUtils.WHITE);
			_theWidget.setActiveWidget(true);
		} else {
			setBackgroundColor(ViewUtils.LIGHT_GREY);
			_theWidget.setActiveWidget(false);
		}
		sizeFrame();
	}
	
	/**
	 * Sets the text of the widget to the proper field of the Question
	 */
	private void setText() {
		if(_isActiveFrame) {
			_text = _question.getLongText();
		}
		else {
			_text = _question.getShortText();
		}
	}
	
	/**
	 * Chooses the Frame's widget, based on what input type the 
	 * question requires.
	 *
	 */
	private void setupWidget() {
		switch(_question.getWidgetType()){
		case(Constants.SINGLE_CHOICE):
			ChoiceList newWidget = new ChoiceList();
			newWidget.setChoiceType(ChoiceList.SINGLE);
			
			for(int i =0 ; i < _question.getInternalArray().length ; ++i) {
				newWidget.addChoice(_question.getInternalArray()[i]);
			}
			
			_theWidget = newWidget;
			break;
		case(Constants.MULTIPLE_CHOICE):
			ChoiceList aWidget = new ChoiceList();
			aWidget.setChoiceType(ChoiceList.MULTI);
			
			for(int i =0 ; i < _question.getInternalArray().length ; ++i) {
				aWidget.addChoice(_question.getInternalArray()[i]);
			}
			
			_theWidget = aWidget;
			break;
		case ( Constants.TEXTBOX):
			_theWidget = new Textbox();
			break;
		}
		_theWidget.setLabelPosition(_question.getLabelPosition());
		this.add(_theWidget);
	}
	
	/**
	 * Lays out the internal elements of the form to optimal sizes. Also sets the height 
	 * of the frame to the correct size, allowing both the widget and the Label to be 
	 * seen in full.
	 */
	public void sizeFrame() {
		Font theFont = Font.getDefaultFont();
		_xBufferSize = this.getWidth()/10;
		_yBufferSize = _xBufferSize/2;
		
		if(!_isActiveFrame) {
			_labelWidth = this.getWidth() - _xBufferSize;
		}
		else {
			if ( _theWidget.getLabelPosition() == Constants.LABEL_TOP ) {
				_labelWidth = this.getWidth();
			} else {
				_labelWidth = this.getWidth()/3 - _xBufferSize;
			}
		}
		
		Vector splitStrings = StringUtils.splitStringByWords(_text, _labelWidth, theFont);
		
		int numLines = splitStrings.size();
		
		int labelHeight = (theFont.getHeight() * numLines) + _yBufferSize;

		_theWidget.sizeWidget();
			
		if ( _theWidget.getLabelPosition() == Constants.LABEL_TOP ) {
			_theWidget.setWidth(this.getWidth() - _xBufferSize);
			_theWidget.setX(_xBufferSize/2);
			_theWidget.setY(labelHeight);
		} else {
			_theWidget.setWidth(this.getWidth() - _labelWidth - _xBufferSize); 
			_theWidget.setX(getWidth() - _theWidget.getWidth()); 
	        _theWidget.setY(0); 
	    }
			
		if(_theWidget.getHeight() < labelHeight || !_isActiveFrame) {
			this.setHeight(labelHeight + _yBufferSize);	
			_theWidget.setHeight(labelHeight + _yBufferSize);
		}
		else {
			if ( _theWidget.getLabelPosition() == Constants.LABEL_TOP)
				this.setHeight(_theWidget.getHeight() + labelHeight + _yBufferSize);
			else
				this.setHeight(_theWidget.getHeight()); 
		}
	}
	
	/**
	 * Manually splits the string that will be displayed, and draws it to the
	 * proper place in the frame
	 * @param g the graphic canvas
	 */
	public void drawInternal(Graphics g) {

		// draw border
		g.setColor(ViewUtils.BLACK);
		g.drawRect(0, 0, this.getWidth(), this.getHeight());
		
		// draw string
		Font theFont = g.getFont();
		if ( _theWidget.getLabelPosition() == Constants.LABEL_TOP ) {
			g.drawString(_text,
					     _xBufferSize/2,
 		                _yBufferSize/2,
					    Graphics.TOP|Graphics.LEFT);	
		} else {
			Vector splitStrings;
			splitStrings = StringUtils.splitStringByWords(_text, _labelWidth, theFont);
			for(int i = 0; i < splitStrings.size(); ++i) {
				String stringPiece = (String)splitStrings.elementAt(i);
				g.drawString(stringPiece,
						     _xBufferSize/2 ,
							_yBufferSize/2 + theFont.getHeight()*(i),
							Graphics.TOP|Graphics.LEFT);
			}
		}
	}
}
<commit_msg>[r63] [trunk/MobileMRSNoPolish] Fix word wrapping on Palm<commit_after>package org.dimagi.chatscreen;

import java.util.Vector;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

import org.dimagi.entity.Question;
import org.dimagi.utils.StringUtils;
import org.dimagi.utils.ViewUtils;
import org.dimagi.view.Component;
import org.dimagi.view.Widget;
import org.dimagi.view.widget.ChoiceList;
import org.dimagi.view.widget.Textbox;

/**
 * The Frame component is the basic element of the Chat Screen
 * interface. It displays a Question in the optimal fashion
 * based on its text and widget type.
 * 
 * A Frame can be considered the UI equivilant of a Question object.
 * 
 * @author ctsims
 * @date Aug-08-2007
 *
 */
public class Frame extends Component {
	
	private Question _question;
	
	private boolean _isActiveFrame = true;
	
	private String _text = "";
	
	private int _labelWidth;
	
	private int _xBufferSize;
	
	private int _yBufferSize;
	
	private Widget _theWidget;
	
	/**
	 * Creates a new Frame for the given Question.
	 * @param theQuestion The question that this frame will represent.	
	 */
	public Frame(Question theQuestion) {
		_question = theQuestion;
		setText();
		setupWidget();
	}
	
	/**
	 * Sets the drawing mode of the frame to either large or small
	 * @param small True if the frame should be drawn in its small form, false otherwise.
	 */
	public void setActiveFrame(boolean isActiveFrame) {
		_isActiveFrame = isActiveFrame;
		setText();
		if(isActiveFrame) {
			setBackgroundColor(ViewUtils.WHITE);
			_theWidget.setActiveWidget(true);
		} else {
			setBackgroundColor(ViewUtils.LIGHT_GREY);
			_theWidget.setActiveWidget(false);
		}
		sizeFrame();
	}
	
	/**
	 * Sets the text of the widget to the proper field of the Question
	 */
	private void setText() {
		if(_isActiveFrame) {
			_text = _question.getLongText();
		}
		else {
			_text = _question.getShortText();
		}
	}
	
	/**
	 * Chooses the Frame's widget, based on what input type the 
	 * question requires.
	 *
	 */
	private void setupWidget() {
		switch(_question.getWidgetType()){
		case(Constants.SINGLE_CHOICE):
			ChoiceList newWidget = new ChoiceList();
			newWidget.setChoiceType(ChoiceList.SINGLE);
			
			for(int i =0 ; i < _question.getInternalArray().length ; ++i) {
				newWidget.addChoice(_question.getInternalArray()[i]);
			}
			
			_theWidget = newWidget;
			break;
		case(Constants.MULTIPLE_CHOICE):
			ChoiceList aWidget = new ChoiceList();
			aWidget.setChoiceType(ChoiceList.MULTI);
			
			for(int i =0 ; i < _question.getInternalArray().length ; ++i) {
				aWidget.addChoice(_question.getInternalArray()[i]);
			}
			
			_theWidget = aWidget;
			break;
		case ( Constants.TEXTBOX):
			_theWidget = new Textbox();
			break;
		}
		_theWidget.setLabelPosition(_question.getLabelPosition());
		this.add(_theWidget);
	}
	
	/**
	 * Lays out the internal elements of the form to optimal sizes. Also sets the height 
	 * of the frame to the correct size, allowing both the widget and the Label to be 
	 * seen in full.
	 */
	public void sizeFrame() {
		Font theFont = Font.getDefaultFont();
		_xBufferSize = this.getWidth()/10;
		_yBufferSize = _xBufferSize/2;
		
		if(!_isActiveFrame) {
			_labelWidth = this.getWidth() - _xBufferSize;
		}
		else {
			if ( _theWidget.getLabelPosition() == Constants.LABEL_TOP ) {
				_labelWidth = this.getWidth();
			} else {
				_labelWidth = this.getWidth()/3 - _xBufferSize;
			}
		}
		
		Vector splitStrings = StringUtils.splitStringByWords(_text, _labelWidth, theFont);
		
		int numLines = splitStrings.size();
		
		int labelHeight = (theFont.getHeight() * numLines) + _yBufferSize;

		_theWidget.sizeWidget();
			
		if ( _theWidget.getLabelPosition() == Constants.LABEL_TOP ) {
			_theWidget.setWidth(this.getWidth() - _xBufferSize);
			_theWidget.setX(_xBufferSize/2);
			_theWidget.setY(labelHeight);
		} else {
			_theWidget.setWidth(this.getWidth() - _labelWidth - _xBufferSize); 
			_theWidget.setX(getWidth() - _theWidget.getWidth()); 
	        _theWidget.setY(0); 
	    }
			
		if(_theWidget.getHeight() < labelHeight || !_isActiveFrame) {
			this.setHeight(labelHeight + _yBufferSize);	
			_theWidget.setHeight(labelHeight + _yBufferSize);
		}
		else {
			if ( _theWidget.getLabelPosition() == Constants.LABEL_TOP)
				this.setHeight(_theWidget.getHeight() + labelHeight + _yBufferSize);
			else
				this.setHeight(_theWidget.getHeight()); 
		}
	}
	
	/**
	 * Manually splits the string that will be displayed, and draws it to the
	 * proper place in the frame
	 * @param g the graphic canvas
	 */
	public void drawInternal(Graphics g) {
		
		Vector splitStrings;
		
		// draw border
		g.setColor(ViewUtils.BLACK);
		g.drawRect(0, 0, this.getWidth(), this.getHeight());
		
		// draw string
		Font theFont = g.getFont();
		splitStrings = StringUtils.splitStringByWords(_text, _labelWidth, theFont);
		for(int i = 0; i < splitStrings.size(); ++i) {
			String stringPiece = (String)splitStrings.elementAt(i);
			g.drawString(stringPiece,
						_xBufferSize/2,
 	                	_yBufferSize/2 + theFont.getHeight()*(i),
 	                	Graphics.TOP|Graphics.LEFT);
		}
	}
}

{'fraction_non_alphanumeric': 0.10793621531938427, 'fraction_numerical': 0.0038713245460411097, 'mean_word_length': 3.031958379784467, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4995051', 'n_tokens_mistral': 3934, 'n_tokens_neox': 3387, 'n_words': 806}