Home | History | Annotate | Download | only in openwnn
      1 /*
      2  * Copyright (C) 2008-2012  OMRON SOFTWARE Co., Ltd.
      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 package jp.co.omronsoft.openwnn;
     17 
     18 import android.app.Dialog;
     19 import android.content.Context;
     20 import android.graphics.drawable.Drawable;
     21 import android.text.TextUtils;
     22 import android.text.TextPaint;
     23 import android.view.Gravity;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 import android.view.Window;
     27 import android.view.WindowManager;
     28 import android.widget.LinearLayout;
     29 import android.widget.TextView;
     30 
     31 /**
     32  * The default candidates view manager using {@link TextView}.
     33  *
     34  * @author Copyright (C) 2011 OMRON SOFTWARE CO., LTD.  All Rights Reserved.
     35  */
     36 public class CandidateTextView extends TextView {
     37 
     38     /** Width of fontsize change */
     39     private static final int WIDTH_SIZE[] = {0, 50, 100};
     40     /** Fontsize corresponding to width */
     41     private static final float CUSTOM_FONTSIZE[] = {20.0f, 17.0f, 15.0f};
     42     /** Width of fontsize change beginning */
     43     private static final int CHANGE_FONTSIZE_WIDTH = 120;
     44 
     45     /** Maximum width of candidate view */
     46     private int mMaxWidth;
     47     /** Width of fontsize change beginning */
     48     private int mChangeFontSize;
     49     /** Minimum width of candidate view */
     50     private int mCandidateMinimumWidth;
     51 
     52     /** Alert dialog */
     53     private Dialog mCandidateDialog = null;
     54 
     55    /**
     56     * Constructor
     57     * @param context    context
     58     */
     59     public CandidateTextView(Context context) {
     60         super(context);
     61         setSoundEffectsEnabled(false);
     62     }
     63 
     64    /**
     65     * Constructor
     66     * @param context    context
     67     * @param candidateMinimumHeight Minimum height of candidate view
     68     * @param candidateMinimumWidth  Minimum width of candidate view
     69     * @param maxWidth  Maximum width of candidate view
     70     */
     71     public CandidateTextView(Context context, int candidateMinimumHeight, int candidateMinimumWidth, int maxWidth) {
     72         super(context);
     73         setSoundEffectsEnabled(false);
     74         setTextColor(getResources().getColor(R.color.candidate_text_1line));
     75         setBackgroundResource(R.drawable.cand_back_1line);
     76         setGravity(Gravity.CENTER);
     77         setSingleLine();
     78         setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
     79                                                            ViewGroup.LayoutParams.MATCH_PARENT,
     80                                                            1.0f));
     81         setMinHeight(candidateMinimumHeight);
     82         setMinimumWidth(candidateMinimumWidth);
     83         mCandidateMinimumWidth = candidateMinimumWidth;
     84         mMaxWidth = maxWidth;
     85         mChangeFontSize = maxWidth - CHANGE_FONTSIZE_WIDTH;
     86     }
     87 
     88    /**
     89     * Textview is set to the best content for the display of candidate.
     90     * @param WnnWord    candidate
     91     * @param wordCount  candidate id
     92     * @param OnClickListener Operation when clicking
     93     * @param OnClickListener Operation when longclicking
     94     * @return Set completion textview
     95     */
     96     public CandidateTextView setCandidateTextView(WnnWord word, int wordCount,
     97                                                         OnClickListener candidateOnClick,
     98                                                         OnLongClickListener candidateOnLongClick) {
     99         setTextSize(CUSTOM_FONTSIZE[0]);
    100         setText(word.candidate);
    101         setId(wordCount);
    102         setVisibility(View.VISIBLE);
    103         setPressed(false);
    104         setWidth(0);
    105         setEllipsize(null);
    106         setOnClickListener(candidateOnClick);
    107         setOnLongClickListener(candidateOnLongClick);
    108         setCustomCandidate(word);
    109         return this;
    110     }
    111 
    112    /**
    113     * If the text view is set to the best width for the display,
    114     * and it is necessary, the character is shortened.
    115     * @param WnnWord candidate word
    116     * @return int    textview width
    117     */
    118     public int setCustomCandidate(WnnWord word) {
    119         TextPaint paint = getPaint();
    120         int width = (int)paint.measureText(word.candidate, 0, word.candidate.length());
    121         width += getPaddingLeft() + getPaddingRight();
    122 
    123         if (width > mCandidateMinimumWidth) {
    124             int i;
    125             for (i = 0; i < WIDTH_SIZE.length; i++) {
    126                 if (width > mChangeFontSize + WIDTH_SIZE[i]) {
    127                     setTextSize(CUSTOM_FONTSIZE[i]);
    128                 }
    129             }
    130 
    131             width = (int)paint.measureText(word.candidate, 0, word.candidate.length());
    132             width += getPaddingLeft() + getPaddingRight();
    133 
    134             if (width >= mMaxWidth) {
    135                 setWidth(mMaxWidth);
    136                 width = mMaxWidth;
    137                 setEllipsize(TextUtils.TruncateAt.START);
    138             } else {
    139                 setWidth(width);
    140             }
    141         } else {
    142             setWidth(mCandidateMinimumWidth);
    143             width = mCandidateMinimumWidth;
    144         }
    145         return width;
    146     }
    147 
    148     /** @see View#setBackgroundDrawable */
    149     @Override public void setBackgroundDrawable(Drawable d) {
    150         super.setBackgroundDrawable(d);
    151         setPadding(20, 0, 20, 0);
    152     }
    153 
    154     /**
    155      * Display Dialog.
    156      *
    157      * @param builder  The Dialog builder,
    158      */
    159     public void displayCandidateDialog(Dialog builder) {
    160         if (mCandidateDialog != null) {
    161             mCandidateDialog.dismiss();
    162             mCandidateDialog = null;
    163         }
    164         mCandidateDialog = builder;
    165         Window window = mCandidateDialog.getWindow();
    166         WindowManager.LayoutParams lp = window.getAttributes();
    167         lp.token = getWindowToken();
    168         lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
    169         window.setAttributes(lp);
    170         window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    171         mCandidateDialog.show();
    172     }
    173 
    174     /** @see android.view.View#onWindowVisibilityChanged */
    175     @Override protected void onWindowVisibilityChanged(int visibility) {
    176        super.onWindowVisibilityChanged(visibility);
    177        if ((visibility != VISIBLE) && (mCandidateDialog != null)) {
    178            mCandidateDialog.dismiss();
    179        }
    180     }
    181 }
    182