(mensagem de log vazia)
@@ -0,0 +1,163 @@ | ||
1 | +/* | |
2 | + * Copyright (C) 2011 kiyoshi.motoki | |
3 | + * | |
4 | + * This program is free software: you can redistribute it and/or modify | |
5 | + * it under the terms of the GNU General Public License as published by | |
6 | + * the Free Software Foundation, either version 3 of the License, or | |
7 | + * any later version. | |
8 | + * | |
9 | + * This program is distributed in the hope that it will be useful, | |
10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | + * GNU General Public License for more details. | |
13 | + * | |
14 | + * You should have received a copy of the GNU General Public License | |
15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | + */ | |
17 | +package jp.ne.motoki.android.fuelchecker; | |
18 | + | |
19 | +import java.util.Date; | |
20 | + | |
21 | +import android.app.AlertDialog; | |
22 | +import android.content.Context; | |
23 | +import android.content.DialogInterface; | |
24 | +import android.text.format.DateFormat; | |
25 | +import android.view.LayoutInflater; | |
26 | +import android.view.View; | |
27 | +import android.widget.TextView; | |
28 | + | |
29 | +/** | |
30 | + * @author kiyoshi.motoki | |
31 | + * | |
32 | + */ | |
33 | +public class ConfirmDialog extends AlertDialog { | |
34 | + | |
35 | + /** | |
36 | + * @param context | |
37 | + */ | |
38 | + public ConfirmDialog(Context context) { | |
39 | + super(context); | |
40 | + // TODO Auto-generated constructor stub | |
41 | + } | |
42 | + | |
43 | + /** | |
44 | + * @param context | |
45 | + * @param theme | |
46 | + */ | |
47 | + public ConfirmDialog(Context context, int theme) { | |
48 | + super(context, theme); | |
49 | + // TODO Auto-generated constructor stub | |
50 | + } | |
51 | + | |
52 | + /** | |
53 | + * @param context | |
54 | + * @param cancelable | |
55 | + * @param cancelListener | |
56 | + */ | |
57 | + public ConfirmDialog(Context context, boolean cancelable, | |
58 | + OnCancelListener cancelListener) { | |
59 | + super(context, cancelable, cancelListener); | |
60 | + // TODO Auto-generated constructor stub | |
61 | + } | |
62 | + | |
63 | + public static class Builder { | |
64 | + | |
65 | + private static final String NUMERIC_FORMAT = "% 3.1f"; | |
66 | + | |
67 | + private java.text.DateFormat dateFormatter = null; | |
68 | + private java.text.DateFormat timeFormatter = null; | |
69 | + | |
70 | + private final Context context; | |
71 | + private CharSequence header = null; | |
72 | + private FuelData data = null; | |
73 | + private CharSequence positiveText = null; | |
74 | + private DialogInterface.OnClickListener positiveListener = null; | |
75 | + private CharSequence negativeText = null; | |
76 | + private DialogInterface.OnClickListener negativeListener = null; | |
77 | + | |
78 | + public Builder(Context context) { | |
79 | + if (context == null) { | |
80 | + throw new NullPointerException("context == null"); | |
81 | + } | |
82 | + this.context = context; | |
83 | + } | |
84 | + | |
85 | + public Builder setMessageHeader(int textId) { | |
86 | + return setMessageHeader(context.getText(textId)); | |
87 | + } | |
88 | + | |
89 | + public Builder setMessageHeader(CharSequence header) { | |
90 | + this.header = header; | |
91 | + return this; | |
92 | + } | |
93 | + | |
94 | + public Builder setFuelData(FuelData data) { | |
95 | + if (data == null) { | |
96 | + throw new NullPointerException("data == null"); | |
97 | + } | |
98 | + this.data = data; | |
99 | + return this; | |
100 | + } | |
101 | + | |
102 | + public Builder setPositiveButton(int textId, DialogInterface.OnClickListener listener) { | |
103 | + return setPositiveButton(context.getText(textId), listener); | |
104 | + } | |
105 | + | |
106 | + public Builder setPositiveButton( | |
107 | + CharSequence text, DialogInterface.OnClickListener listener) { | |
108 | + positiveText = text; | |
109 | + positiveListener = listener; | |
110 | + return this; | |
111 | + } | |
112 | + | |
113 | + public Builder setNegativeButton( | |
114 | + int textId, DialogInterface.OnClickListener listener) { | |
115 | + return setNegativeButton(context.getText(textId), listener); | |
116 | + } | |
117 | + | |
118 | + public Builder setNegativeButton( | |
119 | + CharSequence text, DialogInterface.OnClickListener listener) { | |
120 | + negativeText = text; | |
121 | + negativeListener = listener; | |
122 | + return this; | |
123 | + } | |
124 | + | |
125 | + public ConfirmDialog show() { | |
126 | + ConfirmDialog confirmDialog = new ConfirmDialog(context); | |
127 | + confirmDialog.setTitle(R.string.title_confirm_dialog); | |
128 | + LayoutInflater factory = LayoutInflater.from(context); | |
129 | + View view = factory.inflate(R.layout.confirm_dialog, null); | |
130 | + | |
131 | + dateFormatter = DateFormat.getMediumDateFormat(context); | |
132 | + timeFormatter = DateFormat.getTimeFormat(context); | |
133 | + Date datetime = data.getDatetime().getTime(); | |
134 | + | |
135 | + TextView headerView = (TextView) view.findViewById(R.id.header); | |
136 | + headerView.setText(header); | |
137 | + TextView dateView = (TextView) view.findViewById(R.id.date); | |
138 | + dateView.setText(dateFormatter.format(datetime)); | |
139 | + TextView timeView = (TextView) view.findViewById(R.id.time); | |
140 | + timeView.setText(timeFormatter.format(datetime)); | |
141 | + TextView quantityView = | |
142 | + (TextView) view.findViewById(R.id.quantity); | |
143 | + quantityView.setText( | |
144 | + String.format(NUMERIC_FORMAT, data.getQuantity())); | |
145 | + TextView distanceView = | |
146 | + (TextView) view.findViewById(R.id.distance); | |
147 | + distanceView.setText( | |
148 | + String.format(NUMERIC_FORMAT, data.getDistance())); | |
149 | + confirmDialog.setView(view); | |
150 | + | |
151 | + confirmDialog.setButton( | |
152 | + DialogInterface.BUTTON_POSITIVE, | |
153 | + positiveText, | |
154 | + positiveListener); | |
155 | + confirmDialog.setButton( | |
156 | + DialogInterface.BUTTON_NEGATIVE, | |
157 | + negativeText, | |
158 | + negativeListener); | |
159 | + confirmDialog.show(); | |
160 | + return confirmDialog; | |
161 | + } | |
162 | + } | |
163 | +} |