Debian Codegraph @

core/java/android/text/method/CharacterPickerDialog.java

raw symbols SCIP
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
17 package android.text.method;
18
19 import android.app.Dialog;
20 import android.content.Context;
21 import android.os.Bundle;
22 import android.text.Editable;
23 import android.text.Selection;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.View.OnClickListener;
27 import android.view.ViewGroup;
28 import android.view.Window;
29 import android.view.WindowManager;
30 import android.widget.AdapterView;
31 import android.widget.AdapterView.OnItemClickListener;
32 import android.widget.BaseAdapter;
33 import android.widget.Button;
34 import android.widget.GridView;
35
36 import com.android.internal.R;
37
38 /**
39 * Dialog for choosing accented characters related to a base character.
40 */
41 public class CharacterPickerDialog extends Dialog
42 implements OnItemClickListener, OnClickListener {
43 private View mView;
44 private Editable mText;
45 private String mOptions;
46 private boolean mInsert;
47 private LayoutInflater mInflater;
48 private Button mCancelButton;
49
50 /**
51 * Creates a new CharacterPickerDialog that presents the specified
52 * <code>options</code> for insertion or replacement (depending on
53 * the sense of <code>insert</code>) into <code>text</code>.
54 */
55 public CharacterPickerDialog(Context context, View view,
56 Editable text, String options,
57 boolean insert) {
58 super(context, com.android.internal.R.style.Theme_Panel);
59
60 mView = view;
61 mText = text;
62 mOptions = options;
63 mInsert = insert;
64 mInflater = LayoutInflater.from(context);
65 }
66
67 @Override
68 protected void onCreate(Bundle savedInstanceState) {
69 super.onCreate(savedInstanceState);
70
71 WindowManager.LayoutParams params = getWindow().getAttributes();
72 params.token = mView.getApplicationWindowToken();
73 params.type = params.TYPE_APPLICATION_ATTACHED_DIALOG;
74 params.flags = params.flags | Window.FEATURE_NO_TITLE;
75
76 setContentView(R.layout.character_picker);
77
78 GridView grid = (GridView) findViewById(R.id.characterPicker);
79 grid.setAdapter(new OptionsAdapter(getContext()));
80 grid.setOnItemClickListener(this);
81
82 mCancelButton = (Button) findViewById(R.id.cancel);
83 mCancelButton.setOnClickListener(this);
84 }
85
86 /**
87 * Handles clicks on the character buttons.
88 */
89 public void onItemClick(AdapterView parent, View view, int position, long id) {
90 String result = String.valueOf(mOptions.charAt(position));
91 replaceCharacterAndClose(result);
92 }
93
94 private void replaceCharacterAndClose(CharSequence replace) {
95 int selEnd = Selection.getSelectionEnd(mText);
96 if (mInsert || selEnd == 0) {
97 mText.insert(selEnd, replace);
98 } else {
99 mText.replace(selEnd - 1, selEnd, replace);
100 }
101
102 dismiss();
103 }
104
105 /**
106 * Handles clicks on the Cancel button.
107 */
108 public void onClick(View v) {
109 if (v == mCancelButton) {
110 dismiss();
111 } else if (v instanceof Button) {
112 CharSequence result = ((Button) v).getText();
113 replaceCharacterAndClose(result);
114 }
115 }
116
117 private class OptionsAdapter extends BaseAdapter {
118
119 public OptionsAdapter(Context context) {
120 super();
121 }
122
123 public View getView(int position, View convertView, ViewGroup parent) {
124 Button b = (Button)
125 mInflater.inflate(R.layout.character_picker_button, null);
126 b.setText(String.valueOf(mOptions.charAt(position)));
127 b.setOnClickListener(CharacterPickerDialog.this);
128 return b;
129 }
130
131 public final int getCount() {
132 return mOptions.length();
133 }
134
135 public final Object getItem(int position) {
136 return String.valueOf(mOptions.charAt(position));
137 }
138
139 public final long getItemId(int position) {
140 return position;
141 }
142 }
143 }