1 package org.coolreader.crengine;
2 
3 import org.coolreader.R;
4 
5 import android.text.method.DigitsKeyListener;
6 import android.view.LayoutInflater;
7 import android.view.ViewGroup;
8 import android.widget.EditText;
9 import android.widget.SeekBar;
10 import android.widget.SeekBar.OnSeekBarChangeListener;
11 import android.widget.TextView;
12 
13 public class InputDialog extends BaseDialog {
14 
15 	public interface InputHandler {
validate( String s )16 		boolean validate( String s ) throws Exception;
onOk( String s )17 		void onOk( String s ) throws Exception;
onCancel()18 		void onCancel();
19 	};
20 
21 	private InputHandler handler;
22 	private EditText input;
23 	int minValue;
24 	int maxValue;
InputDialog( BaseActivity activity, final String title, final String prompt, boolean isNumberEdit, int minValue, int maxValue, int currentValue, final InputHandler handler )25 	public InputDialog( BaseActivity activity, final String title, final String prompt, boolean isNumberEdit, int minValue, int maxValue, int currentValue, final InputHandler handler )
26 	{
27 		super(activity, title, true, false);
28 		this.handler = handler;
29 		this.minValue = minValue;
30 		this.maxValue = maxValue;
31         LayoutInflater mInflater = LayoutInflater.from(getContext());
32         ViewGroup layout = (ViewGroup)mInflater.inflate(R.layout.line_edit_dlg, null);
33         input = (EditText)layout.findViewById(R.id.input_field);
34         TextView promptView = (TextView)layout.findViewById(R.id.lbl_prompt);
35         if (promptView != null) {
36         	promptView.setText(prompt);
37         }
38         SeekBar seekBar = layout.findViewById(R.id.goto_position_seek_bar);
39         if (seekBar != null) {
40         	seekBar.setMax(maxValue - minValue);
41         	seekBar.setProgress(currentValue);
42         	seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
43 				@Override
44 				public void onStopTrackingTouch(SeekBar seekBar) {
45 				}
46 
47 				@Override
48 				public void onStartTrackingTouch(SeekBar seekBar) {
49 				}
50 
51 				@Override
52 				public void onProgressChanged(SeekBar seekBar, int progress,
53 						boolean fromUser) {
54 					if (fromUser) {
55 						String value = String.valueOf(progress + InputDialog.this.minValue);
56 						try {
57 							if (handler.validate(value))
58 								input.setText(value);
59 						} catch (Exception e) {
60 							// ignore
61 						}
62 					}
63 				}
64 			});
65         }
66         //input = new EditText(getContext());
67         if ( isNumberEdit )
68         	input.setKeyListener(DigitsKeyListener.getInstance("0123456789."));
69 //	        input.getText().setFilters(new InputFilter[] {
70 //	        	new DigitsKeyListener()
71 //	        });
72         setView(layout);
73 	}
74 	@Override
onNegativeButtonClick()75 	protected void onNegativeButtonClick() {
76         cancel();
77         handler.onCancel();
78 	}
79 	@Override
onPositiveButtonClick()80 	protected void onPositiveButtonClick() {
81         String value = input.getText().toString().trim();
82         try {
83         	if ( handler.validate(value) )
84         		handler.onOk(value);
85         	else
86         		handler.onCancel();
87         } catch ( Exception e ) {
88         	handler.onCancel();
89         }
90         cancel();
91 	}
92 }
93