comparison app/src/main/java/com/five_ten_sg/connectbot/util/StringPickerDialog.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/com/five_ten_sg/connectbot/util/StringPickerDialog.java@071eccdff8ea
children
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
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 //Resources res = Resources.getSystem();
59 //int id = res.getIdentifier("Theme_Panel", "style", "android");
60 //int id = android.R.style.Theme_Panel;
61 super(context, android.R.style.Theme_Panel);
62 mView = view;
63 mText = text;
64 mOptions = options;
65 mInsert = insert;
66 mInflater = LayoutInflater.from(context);
67 }
68
69 @Override
70 protected void onCreate(Bundle savedInstanceState) {
71 super.onCreate(savedInstanceState);
72 WindowManager.LayoutParams params = getWindow().getAttributes();
73 params.token = mView.getApplicationWindowToken();
74 params.type = params.TYPE_APPLICATION_ATTACHED_DIALOG;
75 params.flags = params.flags | Window.FEATURE_NO_TITLE;
76 setContentView(R.layout.string_picker);
77 GridView grid = (GridView) findViewById(R.id.stringPicker);
78 grid.setAdapter(new OptionsAdapter(getContext()));
79 grid.setOnItemClickListener(this);
80 mCancelButton = (Button) findViewById(R.id.cancel);
81 mCancelButton.setOnClickListener(this);
82 }
83
84 /**
85 * Handles clicks on the character buttons.
86 */
87 public void onItemClick(AdapterView parent, View view, int position, long id) {
88 String result = mOptions[position];
89 replaceCharacterAndClose(result);
90 }
91
92 private void replaceCharacterAndClose(CharSequence replace) {
93 int selEnd = Selection.getSelectionEnd(mText);
94
95 if (mInsert || selEnd == 0) {
96 mText.insert(selEnd, replace);
97 }
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 }
112 else if (v instanceof Button) {
113 CharSequence result = ((Button) v).getText();
114 replaceCharacterAndClose(result);
115 }
116 }
117
118 private class OptionsAdapter extends BaseAdapter {
119
120 public OptionsAdapter(Context context) {
121 super();
122 }
123
124 public View getView(int position, View convertView, ViewGroup parent) {
125 Button b = (Button)
126 mInflater.inflate(R.layout.string_picker_button, null);
127 b.setText(mOptions[position]);
128 b.setOnClickListener(StringPickerDialog.this);
129 return b;
130 }
131
132 public final int getCount() {
133 return mOptions.length;
134 }
135
136 public final Object getItem(int position) {
137 return mOptions[position];
138 }
139
140 public final long getItemId(int position) {
141 return position;
142 }
143 }
144 }