OSDN Git Service

60dec1302c19ed2cb7e7a8e30d345438a65e4967
[gokigen/MeMoMa.git] / app / src / main / java / jp / sourceforge / gokigen / memoma / ConfirmationDialog.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 import android.view.LayoutInflater;
8 import android.view.View;
9 import android.widget.TextView;
10
11 /**
12  *   はい か いいえ を入力するダイアログを準備する
13  * 
14  * @author MRSa
15  *
16  */
17 public class ConfirmationDialog
18 {
19         private Context context = null;
20         private IResultReceiver resultReceiver = null;
21     private String  message = "";
22         private String  title = "";
23         private int    icon = 0;
24
25         public ConfirmationDialog(Context arg)
26         {
27                 context = arg;
28         }
29
30         /**
31          *  クラスの準備
32          * @param receiver
33          * @param initialMessage
34          */
35         public void prepare(IResultReceiver receiver, int titleIcon, String titleMessage, String confirmMessage)
36         {
37                 if (receiver != null)
38                 {
39                         resultReceiver = receiver;
40                 }
41                 icon = titleIcon;
42                 title = titleMessage;
43         message = confirmMessage;               
44         }
45
46     /**
47      *   確認ダイアログを応答する
48      * @return
49      */
50     public Dialog getDialog()
51     {
52         LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
53         final View layout = inflater.inflate(R.layout.confirmationdialog, null);
54
55         AlertDialog.Builder builder = new AlertDialog.Builder(context);
56         final TextView  textView = (TextView)  layout.findViewById(R.id.confirm_message);
57
58         // 表示するデータ(アイコン、ダイアログタイトル、メッセージ)を準備する
59         if (icon != 0)
60         {
61             builder.setIcon(icon);
62         }
63         if (title != null)
64         {
65             builder.setTitle(title);
66         }
67         if (message != null)
68         {
69                 textView.setText(message);
70         }
71         builder.setView(layout);
72         builder.setCancelable(false);
73         builder.setPositiveButton(context.getString(R.string.confirmYes), new DialogInterface.OnClickListener()
74               {
75                    public void onClick(DialogInterface dialog, int id)
76                    {
77                            boolean ret = false;
78                            if (resultReceiver != null)
79                            {
80                                resultReceiver.acceptConfirmation();
81                            }
82                        if (ret == true)
83                        {
84                            dialog.dismiss();
85                        }
86                        else
87                        {
88                            dialog.cancel();
89                        }
90                        System.gc();
91                    }
92                });
93         builder.setNegativeButton(context.getString(R.string.confirmNo), new DialogInterface.OnClickListener()
94                {
95                    public void onClick(DialogInterface dialog, int id)
96                    {
97                            boolean ret = false;
98                            if (resultReceiver != null)
99                            {
100                                resultReceiver.rejectConfirmation();
101                            }
102                        if (ret == true)
103                        {
104                            dialog.dismiss();
105                        }
106                        else
107                        {
108                            dialog.cancel();
109                        }
110                        System.gc();
111                    }
112                });
113         return (builder.create());      
114     }
115
116     public interface IResultReceiver
117     {
118         public abstract void acceptConfirmation();
119         public abstract void rejectConfirmation();
120     }
121 }