1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.chrome.browser.infobar;
7 import android.content.Context;
8 import android.graphics.Color;
9 import android.text.SpannableString;
10 import android.text.TextUtils;
11 import android.text.style.ForegroundColorSpan;
12 import android.view.LayoutInflater;
13 import android.view.View;
14 import android.view.ViewGroup;
15 import android.widget.AdapterView;
16 import android.widget.ArrayAdapter;
17 import android.widget.FrameLayout;
18 import android.widget.Spinner;
19 import android.widget.TextView;
21 import org.chromium.chrome.R;
23 import java.util.ArrayList;
24 import java.util.List;
27 * Language panel shown in the translate infobar.
29 public class TranslateLanguagePanel
30 implements TranslateSubPanel, AdapterView.OnItemSelectedListener {
32 private static final int LANGUAGE_TYPE_SOURCE = 0;
33 private static final int LANGUAGE_TYPE_TARGET = 1;
36 private Spinner mSourceSpinner;
37 private Spinner mTargetSpinner;
39 // Items that are not interacted with.
40 // Provided by the caller, the new languages will be set here if the user
42 private final TranslateOptions mOptions;
44 // This object will be used to keep the state for the time the
45 // panel is opened it can be totally discarded in the end if the user
47 private final TranslateOptions mSessionOptions;
49 private LanguageArrayAdapter mSourceAdapter;
50 private LanguageArrayAdapter mTargetAdapter;
52 private final SubPanelListener mListener;
55 * Display language drop downs so they can be picked as source or
56 * target for a translation.
58 * @param listener triggered when the panel is closed
59 * @param options will be modified with the new languages selected.
61 public TranslateLanguagePanel(SubPanelListener listener, TranslateOptions options) {
64 mSessionOptions = new TranslateOptions(mOptions);
68 public void createContent(Context context, InfoBarLayout layout) {
69 mSourceSpinner = null;
70 mTargetSpinner = null;
72 String changeLanguage = context.getString(R.string.translate_infobar_change_languages);
73 TextView panelMessage = (TextView) layout.findViewById(R.id.infobar_message);
74 panelMessage.setText(changeLanguage);
76 // Set up the spinners.
77 createSpinners(context);
78 layout.addGroup(mSourceSpinner, mTargetSpinner);
80 // Set up the buttons.
81 layout.addButtons(context.getString(R.string.translate_button_done),
82 context.getString(R.string.cancel));
86 public void onButtonClicked(boolean primary) {
88 mOptions.setSourceLanguage(mSessionOptions.sourceLanguageIndex());
89 mOptions.setTargetLanguage(mSessionOptions.targetLanguageIndex());
91 mListener.onPanelClosed(InfoBar.ACTION_TYPE_NONE);
94 private void createSpinners(Context context) {
95 mSourceAdapter = new LanguageArrayAdapter(context, R.layout.translate_spinner,
96 LANGUAGE_TYPE_SOURCE);
97 mTargetAdapter = new LanguageArrayAdapter(context, R.layout.translate_spinner,
98 LANGUAGE_TYPE_TARGET);
100 // Determine how wide each spinner needs to be to avoid truncating its children.
101 mSourceAdapter.addAll(createSpinnerLanguages(-1));
102 mTargetAdapter.addAll(createSpinnerLanguages(-1));
103 mSourceAdapter.measureWidthRequiredForView();
104 mTargetAdapter.measureWidthRequiredForView();
106 // Create the spinners.
107 mSourceSpinner = new Spinner(context);
108 mTargetSpinner = new Spinner(context);
109 mSourceSpinner.setOnItemSelectedListener(this);
110 mTargetSpinner.setOnItemSelectedListener(this);
111 mSourceSpinner.setAdapter(mSourceAdapter);
112 mTargetSpinner.setAdapter(mTargetAdapter);
116 private void reloadSpinners() {
117 mSourceAdapter.clear();
118 mTargetAdapter.clear();
120 int sourceAvoidLanguage = mSessionOptions.targetLanguageIndex();
121 int targetAvoidLanguage = mSessionOptions.sourceLanguageIndex();
122 mSourceAdapter.addAll(createSpinnerLanguages(sourceAvoidLanguage));
123 mTargetAdapter.addAll(createSpinnerLanguages(targetAvoidLanguage));
125 int originalSourceSelection = mSourceSpinner.getSelectedItemPosition();
126 int newSourceSelection = getSelectionPosition(LANGUAGE_TYPE_SOURCE);
127 if (originalSourceSelection != newSourceSelection)
128 mSourceSpinner.setSelection(newSourceSelection);
130 int originalTargetSelection = mTargetSpinner.getSelectedItemPosition();
131 int newTargetSelection = getSelectionPosition(LANGUAGE_TYPE_TARGET);
132 if (originalTargetSelection != newTargetSelection)
133 mTargetSpinner.setSelection(newTargetSelection);
136 private int getSelectionPosition(int languageType) {
137 int position = languageType == LANGUAGE_TYPE_SOURCE ? mSessionOptions.sourceLanguageIndex()
138 : mSessionOptions.targetLanguageIndex();
140 // Since the source and target languages cannot appear in both spinners, the index for the
141 // source language can be off by one if comes after the target language alphabetically (and
143 int opposite = languageType == LANGUAGE_TYPE_SOURCE ? mSessionOptions.targetLanguageIndex()
144 : mSessionOptions.sourceLanguageIndex();
145 if (opposite < position) position -= 1;
151 public void onItemSelected(AdapterView<?> adapter, View view, int position, long id) {
152 Spinner spinner = (Spinner) adapter;
153 int newId = ((SpinnerLanguageElement) spinner.getSelectedItem()).getLanguageId();
154 if (spinner == mSourceSpinner) {
155 mSessionOptions.setSourceLanguage(newId);
157 mSessionOptions.setTargetLanguage(newId);
163 public void onNothingSelected(AdapterView<?> adapter) {
167 * Determines what languages will be shown in the Spinner.
168 * @param avoidLanguage Index of the language to avoid. Use -1 to display all languages.
170 private ArrayList<SpinnerLanguageElement> createSpinnerLanguages(int avoidLanguage) {
171 ArrayList<SpinnerLanguageElement> result = new ArrayList<SpinnerLanguageElement>();
172 List<String> languages = mSessionOptions.allLanguages();
173 for (int i = 0; i < languages.size(); ++i) {
174 if (i != avoidLanguage) {
175 result.add(new SpinnerLanguageElement(languages.get(i), i));
182 * The drop down view displayed to show the currently selected value.
184 private static class LanguageArrayAdapter extends ArrayAdapter<SpinnerLanguageElement> {
185 private final SpannableString mTextTemplate;
186 private int mMinimumWidth;
188 public LanguageArrayAdapter(Context context, int textViewResourceId,
190 super(context, textViewResourceId);
192 // Get the string that we will display inside the Spinner, indicating whether the
193 // spinner is used for the source or target language.
194 String textTemplate = languageType == LANGUAGE_TYPE_SOURCE
195 ? context.getString(R.string.translate_options_source_hint)
196 : context.getString(R.string.translate_options_target_hint);
197 mTextTemplate = new SpannableString(textTemplate);
198 mTextTemplate.setSpan(
199 new ForegroundColorSpan(Color.GRAY), 0, textTemplate.length(), 0);
202 /** Measures how large the view needs to be to avoid truncating its children. */
203 public void measureWidthRequiredForView() {
206 final int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
208 FrameLayout layout = new FrameLayout(getContext());
209 TextView estimator = (TextView) LayoutInflater.from(getContext()).inflate(
210 R.layout.infobar_text, null);
211 layout.addView(estimator);
212 for (int i = 0; i < getCount(); ++i) {
213 estimator.setText(getStringForLanguage(i));
214 estimator.measure(spec, spec);
215 mMinimumWidth = Math.max(mMinimumWidth, estimator.getMeasuredWidth());
220 public View getDropDownView(int position, View convertView, ViewGroup parent) {
222 if (!(convertView instanceof TextView)) {
223 result = (TextView) LayoutInflater.from(getContext()).inflate(
224 R.layout.infobar_spinner_item, null);
226 result = (TextView) convertView;
229 String language = getItem(position).toString();
230 result.setText(language);
235 public View getView(int position, View convertView, ViewGroup parent) {
237 if (!(convertView instanceof TextView)) {
238 result = (TextView) LayoutInflater.from(getContext()).inflate(
239 R.layout.infobar_text, null);
241 result = (TextView) convertView;
243 result.setEllipsize(TextUtils.TruncateAt.END);
244 result.setMaxLines(1);
245 result.setText(getStringForLanguage(position));
246 result.setMinWidth(mMinimumWidth);
250 private CharSequence getStringForLanguage(int position) {
251 // The spinners prepend a string to show if they're for the source or target language.
252 String language = getItem(position).toString();
253 SpannableString lang = new SpannableString(language);
254 lang.setSpan(new ForegroundColorSpan(Color.BLACK), 0, lang.length(), 0);
255 return TextUtils.expandTemplate(mTextTemplate, lang);
260 * The element that goes inside the spinner.
262 private static class SpinnerLanguageElement {
263 private final String mLanguageName;
264 private final int mLanguageId;
266 public SpinnerLanguageElement(String languageName, int languageId) {
267 mLanguageName = languageName;
268 mLanguageId = languageId;
271 public int getLanguageId() {
276 * This is the text displayed in the spinner element so make sure no debug information
280 public String toString() {
281 return mLanguageName;