1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.text.method;
18
19import com.android.internal.R;
20
21import android.app.Dialog;
22import android.content.Context;
23import android.os.Bundle;
24import android.text.*;
25import android.view.LayoutInflater;
26import android.view.View.OnClickListener;
27import android.view.View;
28import android.view.ViewGroup;
29import android.view.Window;
30import android.view.WindowManager;
31import android.widget.AdapterView.OnItemClickListener;
32import android.widget.AdapterView;
33import android.widget.BaseAdapter;
34import android.widget.Button;
35import android.widget.GridView;
36
37/**
38 * Dialog for choosing accented characters related to a base character.
39 */
40public class CharacterPickerDialog extends Dialog
41        implements OnItemClickListener, OnClickListener {
42    private View mView;
43    private Editable mText;
44    private String mOptions;
45    private boolean mInsert;
46    private LayoutInflater mInflater;
47    private Button mCancelButton;
48
49    /**
50     * Creates a new CharacterPickerDialog that presents the specified
51     * <code>options</code> for insertion or replacement (depending on
52     * the sense of <code>insert</code>) into <code>text</code>.
53     */
54    public CharacterPickerDialog(Context context, View view,
55                                 Editable text, String options,
56                                 boolean insert) {
57        super(context, com.android.internal.R.style.Theme_Panel);
58
59        mView = view;
60        mText = text;
61        mOptions = options;
62        mInsert = insert;
63        mInflater = LayoutInflater.from(context);
64    }
65
66    @Override
67    protected void onCreate(Bundle savedInstanceState) {
68        super.onCreate(savedInstanceState);
69
70        WindowManager.LayoutParams params = getWindow().getAttributes();
71        params.token = mView.getApplicationWindowToken();
72        params.type = params.TYPE_APPLICATION_ATTACHED_DIALOG;
73        params.flags = params.flags | Window.FEATURE_NO_TITLE;
74
75        setContentView(R.layout.character_picker);
76
77        GridView grid = (GridView) findViewById(R.id.characterPicker);
78        grid.setAdapter(new OptionsAdapter(getContext()));
79        grid.setOnItemClickListener(this);
80
81        mCancelButton = (Button) findViewById(R.id.cancel);
82        mCancelButton.setOnClickListener(this);
83    }
84
85    /**
86     * Handles clicks on the character buttons.
87     */
88    public void onItemClick(AdapterView parent, View view, int position, long id) {
89        String result = String.valueOf(mOptions.charAt(position));
90        replaceCharacterAndClose(result);
91    }
92
93    private void replaceCharacterAndClose(CharSequence replace) {
94        int selEnd = Selection.getSelectionEnd(mText);
95        if (mInsert || selEnd == 0) {
96            mText.insert(selEnd, replace);
97        } else {
98            mText.replace(selEnd - 1, selEnd, replace);
99        }
100
101        dismiss();
102    }
103
104    /**
105     * Handles clicks on the Cancel button.
106     */
107    public void onClick(View v) {
108        if (v == mCancelButton) {
109            dismiss();
110        } else if (v instanceof Button) {
111            CharSequence result = ((Button) v).getText();
112            replaceCharacterAndClose(result);
113        }
114    }
115
116    private class OptionsAdapter extends BaseAdapter {
117
118        public OptionsAdapter(Context context) {
119            super();
120        }
121
122        public View getView(int position, View convertView, ViewGroup parent) {
123            Button b = (Button)
124                mInflater.inflate(R.layout.character_picker_button, null);
125            b.setText(String.valueOf(mOptions.charAt(position)));
126            b.setOnClickListener(CharacterPickerDialog.this);
127            return b;
128        }
129
130        public final int getCount() {
131            return mOptions.length();
132        }
133
134        public final Object getItem(int position) {
135            return String.valueOf(mOptions.charAt(position));
136        }
137
138        public final long getItemId(int position) {
139            return position;
140        }
141    }
142}
143