123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- package de.tudarmstadt.informatik.hostage.ui.dialog;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.app.DialogFragment;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import java.util.ArrayList;
- import de.tudarmstadt.informatik.hostage.R;
- import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
- //import android.content.Intent;
- /**
- * Created by Julien on 16.02.14.
- */
- @SuppressLint("ValidFragment")
- public class ChecklistDialog extends DialogFragment {
-
- private ArrayList<Integer> mSelectedItems;
- private ArrayList<String> itemTitles;
- private ChecklistDialogListener mListener;
-
- private int selectedIndex;
- private boolean isMultipleChoice;
-
- public String title;
-
- /**
- * The ChecklistDialogListener will be called if the user clicked a "cancel" or "ok" button.
- * */
- @SuppressLint("ValidFragment")
- public interface ChecklistDialogListener {
- /**
- * Called if the user tapped "ok"
- * @param dialog {@link ChecklistDialog ChecklistDialog}
- */
- public void onDialogPositiveClick(ChecklistDialog dialog);
- /**
- * Called if the user tapped "cancel".
- * @param dialog {@link ChecklistDialog ChecklistDialog}
- */
- public void onDialogNegativeClick(ChecklistDialog dialog);
- }
- /**
- * Returns the dialog title
- * @return title String
- */
- public String getTitle(){
- return this.title;
- }
- /**
- * Returns true if the checklist dialog is a multiple choice dialog.
- * @return boolean isMultipleChoice
- * */
- public boolean isMultipleChoice(){
- return this.isMultipleChoice;
- }
- /*CONSTRUCTOR*/
- /**
- * The Constructor Method
- * @param title String
- * @param itemTitles ArrayList<String> item titles list
- * @param selected boolean[] an array of bools descriping the position of all the selected titles.
- * @param isMultipleChoice boolean isMultipleChoice
- * @param listener ChecklistDialogListener an user "event" listener
- *
- * */
- public ChecklistDialog(String title, ArrayList<String> itemTitles, boolean[] selected, boolean isMultipleChoice , ChecklistDialogListener listener){
- mListener = listener;
- this.mSelectedItems = new ArrayList<Integer>();
-
- this.isMultipleChoice = isMultipleChoice;
- this.title = title;
- this.itemTitles = itemTitles;
-
-
- boolean[] selectedArray = new boolean[this.itemTitles.size()];
- if(this.isMultipleChoice){
- for(int i = 0; i < this.itemTitles.size(); i++){
- boolean isSelected = selected[i];
- selectedArray[i] = isSelected;
- if(isSelected) this.mSelectedItems.add(i);
- }
- } else {
- for(int i = 0; i < this.itemTitles.size(); i++){
- boolean isSelected = selected[i];
- selectedArray[i] = isSelected;
- if(isSelected) this.selectedIndex = i;
- }
- }
- }
-
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
-
- if(this.mListener == null)
- try {
-
- if (activity.getClass().equals(MainActivity.class)){
- mListener = (ChecklistDialogListener) (((MainActivity)activity).getDisplayedFragment());
- } else {
- mListener = (ChecklistDialogListener) activity;
- }
- } catch (ClassCastException e) {
- throw new ClassCastException(activity.toString()
- + " must implement ChecklistDialogListener");
- }
- }
- /**
- * Return the selected titles.
- * @return ArrayList<String>
- */
- public ArrayList<String> getSelectedItemTitles(){
- ArrayList<String> list = new ArrayList<String>();
- if (this.mSelectedItems == null){
- return list;
- }
- for(Integer i : this.mSelectedItems){
- list.add(this.itemTitles.get(i.intValue()));
- }
- if (this.mSelectedItems.size() == 0 && !this.isMultipleChoice){
- list.add(this.itemTitles.get(this.selectedIndex));
- }
- return list;
- }
-
- @Override
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
-
- CharSequence[] titles = this.itemTitles.toArray(new CharSequence[this.itemTitles.size()]);
-
- boolean[] selectedArray = new boolean[this.itemTitles.size()];
- for(Integer selection : this.mSelectedItems){
- selectedArray[selection.intValue()] = true;
- }
- if(this.isMultipleChoice){
- builder.setTitle(title).setMultiChoiceItems(titles, selectedArray,
- new DialogInterface.OnMultiChoiceClickListener() {
- public void onClick(DialogInterface dialog, int which,
- boolean isChecked) {
- if (isChecked) {
- mSelectedItems.add(which);
- } else if (mSelectedItems.contains(which)) {
- mSelectedItems.remove(Integer.valueOf(which));
- }
- }
- }).setPositiveButton(R.string.button_title_apply, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- mListener.onDialogPositiveClick(ChecklistDialog.this);
- }
- })
- .setNegativeButton(R.string.button_title_cancel, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- mListener.onDialogNegativeClick(ChecklistDialog.this);
- }
- });
-
- } else {
-
- builder.setTitle(title).setSingleChoiceItems(titles, this.selectedIndex,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- mSelectedItems.clear();
- mSelectedItems.add(id);
- }
- })
- .setPositiveButton(R.string.button_title_apply, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- mListener.onDialogPositiveClick(ChecklistDialog.this);
- }
- })
- .setNegativeButton(R.string.button_title_cancel, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- mListener.onDialogNegativeClick(ChecklistDialog.this);
- }
- });
- }
-
- return builder.create();
- }
- }
|