comparison src/com/five_ten_sg/connectbot/util/StringPickerDialog.java @ 223:61ed3984fc1d

proper labels on the soft 24 function keypad
author Carl Byington <carl@five-ten-sg.com>
date Tue, 08 Jul 2014 17:20:07 -0700
parents
children 071eccdff8ea
comparison
equal deleted inserted replaced
219:c438c2ff0052 223:61ed3984fc1d
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 com.five_ten_sg.connectbot.util;
18
19 import com.five_ten_sg.connectbot.R;
20
21 import android.app.Dialog;
22 import android.content.Context;
23 import android.os.Bundle;
24 import android.text.*;
25 import android.view.LayoutInflater;
26 import android.view.View.OnClickListener;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.view.Window;
30 import android.view.WindowManager;
31 import android.widget.AdapterView.OnItemClickListener;
32 import android.widget.AdapterView;
33 import android.widget.BaseAdapter;
34 import android.widget.Button;
35 import android.widget.GridView;
36
37 /**
38 * Dialog for choosing accented characters related to a base character.
39 */
40 public class StringPickerDialog 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 StringPickerDialog 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 StringPickerDialog(Context context, View view,
55 Editable text, String []options,
56 boolean insert) {
57 //super(context, com.android.internal.R.style.Theme_Panel);
58
59 //Resources res = Resources.getSystem();
60 //int id = res.getIdentifier("Theme_Panel", "style", "android");
61 //int id = android.R.style.Theme_Panel;
62 super(context, android.R.style.Theme_Panel);
63 mView = view;
64 mText = text;
65 mOptions = options;
66 mInsert = insert;
67 mInflater = LayoutInflater.from(context);
68 }
69
70 @Override
71 protected void onCreate(Bundle savedInstanceState) {
72 super.onCreate(savedInstanceState);
73
74 WindowManager.LayoutParams params = getWindow().getAttributes();
75 params.token = mView.getApplicationWindowToken();
76 params.type = params.TYPE_APPLICATION_ATTACHED_DIALOG;
77 params.flags = params.flags | Window.FEATURE_NO_TITLE;
78
79 setContentView(R.layout.string_picker);
80
81 GridView grid = (GridView) findViewById(R.id.stringPicker);
82 grid.setAdapter(new OptionsAdapter(getContext()));
83 grid.setOnItemClickListener(this);
84
85 mCancelButton = (Button) findViewById(R.id.cancel);
86 mCancelButton.setOnClickListener(this);
87 }
88
89 /**
90 * Handles clicks on the character buttons.
91 */
92 public void onItemClick(AdapterView parent, View view, int position, long id) {
93 String result = mOptions[position];
94 replaceCharacterAndClose(result);
95 }
96
97 private void replaceCharacterAndClose(CharSequence replace) {
98 int selEnd = Selection.getSelectionEnd(mText);
99 if (mInsert || selEnd == 0) {
100 mText.insert(selEnd, replace);
101 } else {
102 mText.replace(selEnd - 1, selEnd, replace);
103 }
104
105 dismiss();
106 }
107
108 /**
109 * Handles clicks on the Cancel button.
110 */
111 public void onClick(View v) {
112 if (v == mCancelButton) {
113 dismiss();
114 } else if (v instanceof Button) {
115 CharSequence result = ((Button) v).getText();
116 replaceCharacterAndClose(result);
117 }
118 }
119
120 private class OptionsAdapter extends BaseAdapter {
121
122 public OptionsAdapter(Context context) {
123 super();
124 }
125
126 public View getView(int position, View convertView, ViewGroup parent) {
127 Button b = (Button)
128 mInflater.inflate(R.layout.string_picker_button, null);
129 b.setText(mOptions[position]);
130 b.setOnClickListener(StringPickerDialog.this);
131 return b;
132 }
133
134 public final int getCount() {
135 return mOptions.length;
136 }
137
138 public final Object getItem(int position) {
139 return mOptions[position];
140 }
141
142 public final long getItemId(int position) {
143 return position;
144 }
145 }
146 }