ChecklistDialog.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package de.tudarmstadt.informatik.hostage.ui2.dialog;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.app.Dialog;
  6. import android.app.DialogFragment;
  7. import android.content.DialogInterface;
  8. import android.os.Bundle;
  9. import java.util.ArrayList;
  10. import de.tudarmstadt.informatik.hostage.R;
  11. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  12. //import android.content.Intent;
  13. /**
  14. * Created by Julien on 16.02.14.
  15. */
  16. @SuppressLint("ValidFragment")
  17. public class ChecklistDialog extends DialogFragment {
  18. private ArrayList<Integer> mSelectedItems;
  19. private ArrayList<String> itemTitles;
  20. private ChecklistDialogListener mListener;
  21. private int selectedIndex;
  22. private boolean isMultipleChoice;
  23. public String title;
  24. /**
  25. * The ChecklistDialogListener will be called if the user clicked a "cancel" or "ok" button.
  26. * */
  27. @SuppressLint("ValidFragment")
  28. public interface ChecklistDialogListener {
  29. /**
  30. * Called if the user tapped "ok"
  31. * @param dialog {@link ChecklistDialog ChecklistDialog}
  32. */
  33. public void onDialogPositiveClick(ChecklistDialog dialog);
  34. /**
  35. * Called if the user tapped "cancel".
  36. * @param dialog {@link ChecklistDialog ChecklistDialog}
  37. */
  38. public void onDialogNegativeClick(ChecklistDialog dialog);
  39. }
  40. /**
  41. * Returns the dialog title
  42. * @return title String
  43. */
  44. public String getTitle(){
  45. return this.title;
  46. }
  47. /**
  48. * Returns true if the checklist dialog is a multiple choice dialog.
  49. * @return boolean isMultipleChoice
  50. * */
  51. public boolean isMultipleChoice(){
  52. return this.isMultipleChoice;
  53. }
  54. /*CONSTRUCTOR*/
  55. /**
  56. * The Constructor Method
  57. * @param title String
  58. * @param itemTitles ArrayList<String> item titles list
  59. * @param selected boolean[] an array of bools descriping the position of all the selected titles.
  60. * @param isMultipleChoice boolean isMultipleChoice
  61. * @param listener ChecklistDialogListener an user "event" listener
  62. *
  63. * */
  64. public ChecklistDialog(String title, ArrayList<String> itemTitles, boolean[] selected, boolean isMultipleChoice , ChecklistDialogListener listener){
  65. mListener = listener;
  66. this.mSelectedItems = new ArrayList<Integer>();
  67. this.isMultipleChoice = isMultipleChoice;
  68. this.title = title;
  69. this.itemTitles = itemTitles;
  70. boolean[] selectedArray = new boolean[this.itemTitles.size()];
  71. if(this.isMultipleChoice){
  72. for(int i = 0; i < this.itemTitles.size(); i++){
  73. boolean isSelected = selected[i];
  74. selectedArray[i] = isSelected;
  75. if(isSelected) this.mSelectedItems.add(i);
  76. }
  77. } else {
  78. for(int i = 0; i < this.itemTitles.size(); i++){
  79. boolean isSelected = selected[i];
  80. selectedArray[i] = isSelected;
  81. if(isSelected) this.selectedIndex = i;
  82. }
  83. }
  84. }
  85. @Override
  86. public void onAttach(Activity activity) {
  87. super.onAttach(activity);
  88. if(this.mListener == null)
  89. try {
  90. if (activity.getClass().equals(MainActivity.class)){
  91. mListener = (ChecklistDialogListener) (((MainActivity)activity).getDisplayedFragment());
  92. } else {
  93. mListener = (ChecklistDialogListener) activity;
  94. }
  95. } catch (ClassCastException e) {
  96. throw new ClassCastException(activity.toString()
  97. + " must implement ChecklistDialogListener");
  98. }
  99. }
  100. /**
  101. * Return the selected titles.
  102. * @return ArrayList<String>
  103. */
  104. public ArrayList<String> getSelectedItemTitles(){
  105. ArrayList<String> list = new ArrayList<String>();
  106. if (this.mSelectedItems == null){
  107. return list;
  108. }
  109. for(Integer i : this.mSelectedItems){
  110. list.add(this.itemTitles.get(i.intValue()));
  111. }
  112. if (this.mSelectedItems.size() == 0 && !this.isMultipleChoice){
  113. list.add(this.itemTitles.get(this.selectedIndex));
  114. }
  115. return list;
  116. }
  117. @Override
  118. public Dialog onCreateDialog(Bundle savedInstanceState) {
  119. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  120. CharSequence[] titles = this.itemTitles.toArray(new CharSequence[this.itemTitles.size()]);
  121. boolean[] selectedArray = new boolean[this.itemTitles.size()];
  122. for(Integer selection : this.mSelectedItems){
  123. selectedArray[selection.intValue()] = true;
  124. }
  125. if(this.isMultipleChoice){
  126. builder.setTitle(title).setMultiChoiceItems(titles, selectedArray,
  127. new DialogInterface.OnMultiChoiceClickListener() {
  128. public void onClick(DialogInterface dialog, int which,
  129. boolean isChecked) {
  130. if (isChecked) {
  131. mSelectedItems.add(which);
  132. } else if (mSelectedItems.contains(which)) {
  133. mSelectedItems.remove(Integer.valueOf(which));
  134. }
  135. }
  136. }).setPositiveButton(R.string.button_title_apply, new DialogInterface.OnClickListener() {
  137. public void onClick(DialogInterface dialog, int id) {
  138. mListener.onDialogPositiveClick(ChecklistDialog.this);
  139. }
  140. })
  141. .setNegativeButton(R.string.button_title_cancel, new DialogInterface.OnClickListener() {
  142. public void onClick(DialogInterface dialog, int id) {
  143. mListener.onDialogNegativeClick(ChecklistDialog.this);
  144. }
  145. });
  146. } else {
  147. builder.setTitle(title).setSingleChoiceItems(titles, this.selectedIndex,
  148. new DialogInterface.OnClickListener() {
  149. public void onClick(DialogInterface dialog, int id) {
  150. mSelectedItems.clear();
  151. mSelectedItems.add(id);
  152. }
  153. })
  154. .setPositiveButton(R.string.button_title_apply, new DialogInterface.OnClickListener() {
  155. public void onClick(DialogInterface dialog, int id) {
  156. mListener.onDialogPositiveClick(ChecklistDialog.this);
  157. }
  158. })
  159. .setNegativeButton(R.string.button_title_cancel, new DialogInterface.OnClickListener() {
  160. public void onClick(DialogInterface dialog, int id) {
  161. mListener.onDialogNegativeClick(ChecklistDialog.this);
  162. }
  163. });
  164. }
  165. return builder.create();
  166. }
  167. }