OSDN Git Service

https://osdn.net/cvs/view/gokigen/MeMoMa/ から コピーしてくる。(+API27でビルドできるよう、少し調整。)
[gokigen/MeMoMa.git] / app / src / main / java / jp / sourceforge / gokigen / memoma / ItemSelectionDialog.java
1 package jp.sourceforge.gokigen.memoma;
2
3 import android.app.AlertDialog;
4 import android.app.Dialog;
5 import android.content.Context;
6 import android.content.DialogInterface;
7
8
9 /**
10  *   アイテムを選択するダイアログを準備する
11  * 
12  * @author MRSa
13  *
14  */
15 public class ItemSelectionDialog
16 {
17         private Context context = null;
18         private ISelectionItemReceiver resultReceiver = null;
19         private ISelectionItemHolder dataHolder = null;
20         private String  title = "";
21
22         public ItemSelectionDialog(Context arg)
23         {
24                 context = arg;
25         }
26
27         /**
28          *  クラスの準備
29          * @param receiver
30          * @param initialMessage
31          */
32         public void prepare(ISelectionItemReceiver receiver, ISelectionItemHolder holder,  String titleMessage)
33         {
34                 title = titleMessage;
35                 resultReceiver = receiver;
36                 dataHolder = holder;
37         }
38         
39         /**
40      *   確認ダイアログを応答する
41      * @return
42      */
43     public Dialog getDialog()
44     {
45         AlertDialog.Builder builder = new AlertDialog.Builder(context);
46         
47         // 表示するデータ(ダイアログタイトル、メッセージ)を準備する
48         if (title != null)
49         {
50             builder.setTitle(title);
51         }
52         builder.setCancelable(false);
53         if (dataHolder != null)
54         {
55                 if (dataHolder.isMultipleSelection() == false)
56                 {
57                 builder.setItems(dataHolder.getItems(), new DialogInterface.OnClickListener()
58                 {
59                         public void onClick(DialogInterface dialog, int id)
60                         {
61                            boolean ret = false;
62                            if (resultReceiver != null)
63                            {
64                                resultReceiver.itemSelected(id, dataHolder.getItem(id));
65                            }
66                        if (ret == true)
67                        {
68                            dialog.dismiss();
69                        }
70                        else
71                        {
72                            dialog.cancel();
73                        }
74                        System.gc();
75                                 
76                         }
77                 }); 
78                 }
79             else
80             {
81                 /**  複数選択の選択肢を準備する **/
82                 builder.setMultiChoiceItems(dataHolder.getItems(), dataHolder.getSelectionStatus(), new DialogInterface.OnMultiChoiceClickListener()
83                 {
84                         public void onClick(DialogInterface dialog, int which, boolean isChecked)
85                         {
86                             if (resultReceiver != null)
87                             {
88                                 resultReceiver.itemSelected(which, dataHolder.getItem(which));
89                             }                           
90                         }
91                 });
92
93                 /**  複数選択時には、OKボタンを押したときに選択を確定させる。 **/
94                 builder.setPositiveButton(context.getString(R.string.confirmYes), new DialogInterface.OnClickListener()
95                 {
96                      public void onClick(DialogInterface dialog, int id)
97                      {
98                            boolean ret = false;
99                            if (resultReceiver != null)
100                            {
101                                resultReceiver.itemSelectedMulti(dataHolder.getItems(), dataHolder.getSelectionStatus());
102                            }
103                          if (ret == true)
104                          {
105                            dialog.dismiss();
106                          }
107                          else
108                          {
109                              dialog.cancel();
110                          }
111                          System.gc();
112                      }
113                  });
114             }
115         }
116
117 //        builder.setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, OnMultiChoiceClickListener listener)
118 /**
119 **/
120         builder.setNegativeButton(context.getString(R.string.confirmNo), new DialogInterface.OnClickListener()
121         {
122                    public void onClick(DialogInterface dialog, int id)
123                    {
124                            boolean ret = false;
125                            if (resultReceiver != null)
126                            {
127                                resultReceiver.canceledSelection();
128                            }
129                        if (ret == true)
130                        {
131                            dialog.dismiss();
132                        }
133                        else
134                        {
135                            dialog.cancel();
136                        }
137                        System.gc();
138                    }
139         });
140         return (builder.create());      
141     }
142
143     public interface ISelectionItemHolder
144     {
145         public abstract boolean isMultipleSelection();
146
147         public abstract String[] getItems();
148         public abstract String  getItem(int index);
149
150         /** 複数選択時に使用する **/
151         public abstract boolean[] getSelectionStatus();
152         public abstract void setSelectionStatus(int index, boolean isSelected);
153     };
154     
155     public interface ISelectionItemReceiver
156     {
157         public abstract void itemSelected(int index, String itemValue);
158         public abstract void itemSelectedMulti(String[] items, boolean[] status);
159         public abstract void canceledSelection();
160     }
161 }