<commit_before>/*
 * Copyright (C) 2009 JavaRosa
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

//conditional include for polish?

package org.javarosa.formmanager.view.chatterbox;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import org.javarosa.core.api.Constants;
import org.javarosa.core.model.FormIndex;
import org.javarosa.core.model.GroupDef;
import org.javarosa.core.model.IFormElement;
import org.javarosa.core.model.data.IAnswerData;
import org.javarosa.core.model.data.helper.InvalidDataException;
import org.javarosa.core.model.data.helper.Selection;
import org.javarosa.core.reference.InvalidReferenceException;
import org.javarosa.core.reference.Reference;
import org.javarosa.core.reference.ReferenceManager;
import org.javarosa.core.services.Logger;
import org.javarosa.core.services.UnavailableServiceException;
import org.javarosa.core.services.locale.Localization;
import org.javarosa.core.util.NoLocalizedTextException;
import org.javarosa.form.api.FormEntryCaption;
import org.javarosa.form.api.FormEntryController;
import org.javarosa.form.api.FormEntryModel;
import org.javarosa.formmanager.api.FormMultimediaController;
import org.javarosa.formmanager.api.JrFormEntryController;
import org.javarosa.formmanager.api.JrFormEntryModel;
import org.javarosa.formmanager.api.transitions.FormEntryTransitions;
import org.javarosa.formmanager.properties.FormManagerProperties;
import org.javarosa.formmanager.utility.SortedIndexSet;
import org.javarosa.formmanager.view.IFormEntryView;
import org.javarosa.formmanager.view.chatterbox.widget.ChatterboxWidget;
import org.javarosa.formmanager.view.chatterbox.widget.ChatterboxWidgetFactory;
import org.javarosa.formmanager.view.widgets.CollapsedWidget;
import org.javarosa.formmanager.view.widgets.GeoPointWidget;
import org.javarosa.formmanager.view.widgets.WidgetFactory;
import org.javarosa.j2me.log.CrashHandler;
import org.javarosa.j2me.log.HandledPCommandListener;
import org.javarosa.j2me.log.HandledThread;
import org.javarosa.j2me.view.J2MEDisplay;
import org.javarosa.utilities.media.MediaUtils;

import de.enough.polish.ui.Command;
import de.enough.polish.ui.Container;
import de.enough.polish.ui.Displayable;
import de.enough.polish.ui.FramedForm;
import de.enough.polish.ui.Item;
import de.enough.polish.ui.StringItem;
import de.enough.polish.ui.UiAccess;


public class Chatterbox extends FramedForm implements HandledPCommandListener, IFormEntryView{
	
	private boolean USE_HASH_FOR_AUDIO = true;
	private static int POUND_KEYCODE = Canvas.KEY_POUND;
    private static final String PROMPT_REQUIRED_QUESTION = Localization.get("view.sending.RequiredQuestion");

	private static final String PROMPT_DEFAULT_CONSTRAINT_VIOL = Localization.get("form.entry.constraint.msg");
	
    public static int KEY_CENTER_LETS_HOPE = -5;
    
    public static final int UIHACK_SELECT_PRESS = 1;
	
    public static final int Q_NORMAL = 0;
    public static final int Q_REPEAT_JUNCTURE = 1;
    public static final int Q_REPEAT_DELETE = 2;
    
	private JrFormEntryController controller;
    private JrFormEntryModel model;
    
    private ChatterboxWidgetFactory widgetFactory;
    private boolean multiLingual;
    private SortedIndexSet questionIndexes;
    private FormIndex activeQuestionIndex;
    
    /** The active question's index when a key is pressed */
    private FormIndex indexWhenKeyPressed = null;
    
    private boolean activeIsInterstitial = false; //true if the question corresponding to activeQuestionIndex is a 'create new repeat' question
    private boolean deleteInterstitial = false;
    private Vector<FormIndex> uncommittedRepeats = new Vector<FormIndex>();
    //active repeat for deleting? //TODO figure this out
    
    //TODO get the progress bar working
    
    //GUI elements
    private Command backCommand;
    private Command exitNoSaveCommand;
    private Command exitSaveCommand;
    private Command saveCommand;
    private Command languageSubMenu;
    private Command[] languageCommands;
    private String[] localeCommandMap;
    //private Command deleteRepeatCommand; //TODO do something with this
    private Gauge progressBar;
        
    public Chatterbox (String formTitle, JrFormEntryController controller) {
        //#style framedForm
    	super(formTitle);
    	this.controller = controller;
    	this.model = controller.getModel();
    	
    	if (model.isReadOnlyMode()) {
    		//#style ReviewFramedForm
    		UiAccess.setStyle(this);
    	}
    	
    	//22 Jan, 2009 - <EMAIL>
    	//This constructor code supresses the ability to scroll the chatterbox by dragging the mouse
    	//pointer. This "Feature" was causing tons of problems for our nurses. Let me know if anyone else
    	//wants to make this a customizable inclusion.
    	this.container = new Container(false) {
    		protected boolean handlePointerScrollReleased(int relX, int relY) {
    			return false;
    		}
    	};

    	multiLingual = (model.getForm().getLocalizer() != null);
    	questionIndexes = new SortedIndexSet();
    	activeQuestionIndex = FormIndex.createBeginningOfFormIndex(); //null is not allowed
    	
    	//#if device.identifier == Sony-Ericsson/P1i
    	KEY_CENTER_LETS_HOPE = 13;
    	//#endif
    	
    	//#if device.identifier == Sony-Ericsson/K610i
    	POUND_KEYCODE = Canvas.KEY_STAR;
    	//#endif
    	
    	if(FormManagerProperties.EXTRA_KEY_AUDIO_PLAYBACK.equals(controller.getExtraKeyMode())){
    		USE_HASH_FOR_AUDIO = true;
    	}else{
    		USE_HASH_FOR_AUDIO = false;
    	}
    }
    

    public void destroy () {
    	for (int i = 0; i < size(); i++) {
    		//As long as everything is collapsed, we should clear all relevant resources.
    		ChatterboxWidget cw = ((ChatterboxWidget)get(i));
    		cw.setViewState(ChatterboxWidget.VIEW_COLLAPSED);    		
    	}
    }
    
    public void show () {
    	//Pre-set the screen for the main container. Otherwise commands for items won't 
    	//show up correctly until this is shown. 
    	UiAccess.setItemScreen(this.container, this);
    	initGUI();
    	J2MEDisplay.setView(this);
    }
    
    public void show (FormIndex index) {
    	J2MEDisplay.setView(this);
    }
    
    private void initGUI () {
    	setUpCommands();
    	initProgressBar();
    	<commit_msg>when destroying form view, only clear resources, don't also collapse. Fixes a weird visual artifact.<commit_after>/*
 * Copyright (C) 2009 JavaRosa
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

//conditional include for polish?

package org.javarosa.formmanager.view.chatterbox;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import org.javarosa.core.api.Constants;
import org.javarosa.core.model.FormIndex;
import org.javarosa.core.model.GroupDef;
import org.javarosa.core.model.IFormElement;
import org.javarosa.core.model.data.IAnswerData;
import org.javarosa.core.model.data.helper.InvalidDataException;
import org.javarosa.core.model.data.helper.Selection;
import org.javarosa.core.reference.InvalidReferenceException;
import org.javarosa.core.reference.Reference;
import org.javarosa.core.reference.ReferenceManager;
import org.javarosa.core.services.Logger;
import org.javarosa.core.services.UnavailableServiceException;
import org.javarosa.core.services.locale.Localization;
import org.javarosa.core.util.NoLocalizedTextException;
import org.javarosa.form.api.FormEntryCaption;
import org.javarosa.form.api.FormEntryController;
import org.javarosa.form.api.FormEntryModel;
import org.javarosa.formmanager.api.FormMultimediaController;
import org.javarosa.formmanager.api.JrFormEntryController;
import org.javarosa.formmanager.api.JrFormEntryModel;
import org.javarosa.formmanager.api.transitions.FormEntryTransitions;
import org.javarosa.formmanager.properties.FormManagerProperties;
import org.javarosa.formmanager.utility.SortedIndexSet;
import org.javarosa.formmanager.view.IFormEntryView;
import org.javarosa.formmanager.view.chatterbox.widget.ChatterboxWidget;
import org.javarosa.formmanager.view.chatterbox.widget.ChatterboxWidgetFactory;
import org.javarosa.formmanager.view.widgets.CollapsedWidget;
import org.javarosa.formmanager.view.widgets.GeoPointWidget;
import org.javarosa.formmanager.view.widgets.WidgetFactory;
import org.javarosa.j2me.log.CrashHandler;
import org.javarosa.j2me.log.HandledPCommandListener;
import org.javarosa.j2me.log.HandledThread;
import org.javarosa.j2me.view.J2MEDisplay;
import org.javarosa.utilities.media.MediaUtils;

import de.enough.polish.ui.Command;
import de.enough.polish.ui.Container;
import de.enough.polish.ui.Displayable;
import de.enough.polish.ui.FramedForm;
import de.enough.polish.ui.Item;
import de.enough.polish.ui.StringItem;
import de.enough.polish.ui.UiAccess;


public class Chatterbox extends FramedForm implements HandledPCommandListener, IFormEntryView{
	
	private boolean USE_HASH_FOR_AUDIO = true;
	private static int POUND_KEYCODE = Canvas.KEY_POUND;
    private static final String PROMPT_REQUIRED_QUESTION = Localization.get("view.sending.RequiredQuestion");

	private static final String PROMPT_DEFAULT_CONSTRAINT_VIOL = Localization.get("form.entry.constraint.msg");
	
    public static int KEY_CENTER_LETS_HOPE = -5;
    
    public static final int UIHACK_SELECT_PRESS = 1;
	
    public static final int Q_NORMAL = 0;
    public static final int Q_REPEAT_JUNCTURE = 1;
    public static final int Q_REPEAT_DELETE = 2;
    
	private JrFormEntryController controller;
    private JrFormEntryModel model;
    
    private ChatterboxWidgetFactory widgetFactory;
    private boolean multiLingual;
    private SortedIndexSet questionIndexes;
    private FormIndex activeQuestionIndex;
    
    /** The active question's index when a key is pressed */
    private FormIndex indexWhenKeyPressed = null;
    
    private boolean activeIsInterstitial = false; //true if the question corresponding to activeQuestionIndex is a 'create new repeat' question
    private boolean deleteInterstitial = false;
    private Vector<FormIndex> uncommittedRepeats = new Vector<FormIndex>();
    //active repeat for deleting? //TODO figure this out
    
    //TODO get the progress bar working
    
    //GUI elements
    private Command backCommand;
    private Command exitNoSaveCommand;
    private Command exitSaveCommand;
    private Command saveCommand;
    private Command languageSubMenu;
    private Command[] languageCommands;
    private String[] localeCommandMap;
    //private Command deleteRepeatCommand; //TODO do something with this
    private Gauge progressBar;
        
    public Chatterbox (String formTitle, JrFormEntryController controller) {
        //#style framedForm
    	super(formTitle);
    	this.controller = controller;
    	this.model = controller.getModel();
    	
    	if (model.isReadOnlyMode()) {
    		//#style ReviewFramedForm
    		UiAccess.setStyle(this);
    	}
    	
    	//22 Jan, 2009 - <EMAIL>
    	//This constructor code supresses the ability to scroll the chatterbox by dragging the mouse
    	//pointer. This "Feature" was causing tons of problems for our nurses. Let me know if anyone else
    	//wants to make this a customizable inclusion.
    	this.container = new Container(false) {
    		protected boolean handlePointerScrollReleased(int relX, int relY) {
    			return false;
    		}
    	};

    	multiLingual = (model.getForm().getLocalizer() != null);
    	questionIndexes = new SortedIndexSet();
    	activeQuestionIndex = FormIndex.createBeginningOfFormIndex(); //null is not allowed
    	
    	//#if device.identifier == Sony-Ericsson/P1i
    	KEY_CENTER_LETS_HOPE = 13;
    	//#endif
    	
    	//#if device.identifier == Sony-Ericsson/K610i
    	POUND_KEYCODE = Canvas.KEY_STAR;
    	//#endif
    	
    	if(FormManagerProperties.EXTRA_KEY_AUDIO_PLAYBACK.equals(controller.getExtraKeyMode())){
    		USE_HASH_FOR_AUDIO = true;
    	}else{
    		USE_HASH_FOR_AUDIO = false;
    	}
    }
    

    public void destroy () {
    	for (int i = 0; i < size(); i++) {
    		//As long as everything is collapsed, we should clear all relevant resources.
    		ChatterboxWidget cw = ((ChatterboxWidget)get(i));
    		cw.releaseResources();
    	}
    }
    
    public void show () {
    	//Pre-set the screen for the main container. Otherwise commands for items won't 
    	//show up correctly until this is shown. 
    	UiAccess.setItemScreen(this.container, this);
    	initGUI();
    	J2MEDisplay.setView(this);
    }
    
    public void show (FormIndex index) {
    	J2MEDisplay.setView(this);
    }
    
    private void initGUI () {
    	setUpCommands();
    	initProgressBar();
    	
{'fraction_non_alphanumeric': 0.08585282373074729, 'fraction_numerical': 0.004706217912150599, 'mean_word_length': 3.9593352192362095, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4629714', 'n_tokens_mistral': 4370, 'n_tokens_neox': 4252, 'n_words': 1042}