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
17package jp.co.omronsoft.openwnn;
18
19import android.app.Dialog;
20import android.content.res.Resources;
21import android.content.SharedPreferences;
22import android.view.Gravity;
23import android.view.View;
24import android.view.ViewGroup;
25import android.view.ViewGroup.LayoutParams;
26import android.widget.HorizontalScrollView;
27import android.widget.LinearLayout;
28import android.widget.TextView;
29import android.util.TypedValue;
30
31import java.util.ArrayList;
32
33/**
34 * The interface of candidates view manager used by {@link OpenWnn}.
35 *
36 * @author Copyright (C) 2008-2011 OMRON SOFTWARE CO., LTD.  All Rights Reserved.
37 */
38public abstract class CandidatesViewManager {
39    /** Size of candidates view (normal) */
40    public static final int VIEW_TYPE_NORMAL = 0;
41    /** Size of candidates view (full) */
42    public static final int VIEW_TYPE_FULL   = 1;
43    /** Size of candidates view (close/non-display) */
44    public static final int VIEW_TYPE_CLOSE  = 2;
45
46    /**
47     * Attribute of a word (no attribute)
48     * @see jp.co.omronsoft.openwnn.WnnWord
49     */
50    public static final int ATTRIBUTE_NONE    = 0;
51    /**
52     * Attribute of a word (a candidate in the history list)
53     * @see jp.co.omronsoft.openwnn.WnnWord
54     */
55    public static final int ATTRIBUTE_HISTORY = 1;
56    /**
57     * Attribute of a word (the best candidate)
58     * @see jp.co.omronsoft.openwnn.WnnWord
59     */
60    public static final int ATTRIBUTE_BEST    = 2;
61    /**
62     * Attribute of a word (auto generated/not in the dictionary)
63     * @see jp.co.omronsoft.openwnn.WnnWord
64     */
65    public static final int ATTRIBUTE_AUTO_GENERATED  = 4;
66
67    /** The view of the LongPressDialog */
68    protected View mViewLongPressDialog = null;
69
70    /** Whether candidates long click enable */
71    protected Dialog mDialog = null;
72
73    /** The word pressed */
74    protected WnnWord mWord;
75
76    /**
77     * Initialize the candidates view.
78     *
79     * @param parent    The OpenWnn object
80     * @param width     The width of the display
81     * @param height    The height of the display
82     *
83     * @return The candidates view created in the initialize process; {@code null} if cannot create a candidates view.
84     */
85    public abstract View initView(OpenWnn parent, int width, int height);
86
87    /**
88     * Get the candidates view being used currently.
89     *
90     * @return The candidates view; {@code null} if no candidates view is used currently.
91     */
92    public abstract View getCurrentView();
93
94    /**
95     * Set the candidates view type.
96     *
97     * @param type  The candidate view type,
98     * from {@link CandidatesViewManager#VIEW_TYPE_NORMAL} to
99     * {@link CandidatesViewManager#VIEW_TYPE_CLOSE}
100     */
101    public abstract void setViewType(int type);
102
103    /**
104     * Get the candidates view type.
105     *
106     * @return      The view type,
107     * from {@link CandidatesViewManager#VIEW_TYPE_NORMAL} to
108     * {@link CandidatesViewManager#VIEW_TYPE_CLOSE}
109     */
110    public abstract int getViewType();
111
112    /**
113     * Display candidates.
114     *
115     * @param converter  The {@link WnnEngine} from which {@link CandidatesViewManager} gets the candidates
116     *
117     * @see jp.co.omronsoft.openwnn.WnnEngine#getNextCandidate
118     */
119    public abstract void displayCandidates(WnnEngine converter);
120
121    /**
122     * Clear and hide the candidates view.
123     */
124    public abstract void clearCandidates();
125
126    /**
127     * Replace the preferences in the candidates view.
128     *
129     * @param pref    The preferences
130     */
131    public abstract void setPreferences(SharedPreferences pref);
132
133    /**
134     * KeyEvent action for soft key board.
135     *
136     * @param key    Key event
137     */
138    public abstract void processMoveKeyEvent(int key);
139
140    /**
141     * Get candidate is focused now.
142     *
143     * @return the Candidate is focused of a flag.
144     */
145    public abstract boolean isFocusCandidate();
146
147    /**
148     * Select candidate that has focus.
149     */
150    public abstract void selectFocusCandidate();
151
152    /**
153     * MSG_SET_CANDIDATES removeMessages.
154     */
155    public abstract void setCandidateMsgRemove();
156
157    /**
158     * Display Dialog.
159     *
160     * @param view  View,
161     * @param word  Display word,
162     */
163    protected void displayDialog(View view, final WnnWord word) {
164        if ((view instanceof CandidateTextView) && (null != mViewLongPressDialog)) {
165            closeDialog();
166            mDialog = new Dialog(view.getContext(), R.style.Dialog);
167
168            TextView text = (TextView)mViewLongPressDialog.findViewById(R.id.candidate_longpress_dialog_text);
169            text.setText(word.candidate);
170
171            mDialog.setContentView(mViewLongPressDialog);
172            ((CandidateTextView) view).displayCandidateDialog(mDialog);
173        }
174    }
175
176    /**
177     * Close Dialog.
178     *
179     */
180    public void closeDialog() {
181        if (null != mDialog) {
182            mDialog.dismiss();
183            mDialog = null;
184            if (null != mViewLongPressDialog) {
185                ViewGroup parent = (ViewGroup)mViewLongPressDialog.getParent();
186                if (null != parent) {
187                    parent.removeView(mViewLongPressDialog);
188                }
189            }
190        }
191    }
192}
193