1/*
2 * Copyright (C) 2008-2009 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.latin;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.MotionEvent;
22import android.view.View;
23import android.view.View.OnTouchListener;
24import android.widget.LinearLayout;
25
26public class CandidateViewContainer extends LinearLayout implements OnTouchListener {
27
28    private View mButtonLeft;
29    private View mButtonRight;
30    private View mButtonLeftLayout;
31    private View mButtonRightLayout;
32    private CandidateView mCandidates;
33
34    public CandidateViewContainer(Context screen, AttributeSet attrs) {
35        super(screen, attrs);
36    }
37
38    public void initViews() {
39        if (mCandidates == null) {
40            mButtonLeftLayout = findViewById(R.id.candidate_left_parent);
41            mButtonLeft = findViewById(R.id.candidate_left);
42            if (mButtonLeft != null) {
43                mButtonLeft.setOnTouchListener(this);
44            }
45            mButtonRightLayout = findViewById(R.id.candidate_right_parent);
46            mButtonRight = findViewById(R.id.candidate_right);
47            if (mButtonRight != null) {
48                mButtonRight.setOnTouchListener(this);
49            }
50            mCandidates = (CandidateView) findViewById(R.id.candidates);
51        }
52    }
53
54    @Override
55    public void requestLayout() {
56        if (mCandidates != null) {
57            int availableWidth = mCandidates.getWidth();
58            int neededWidth = mCandidates.computeHorizontalScrollRange();
59            int x = mCandidates.getScrollX();
60            boolean leftVisible = x > 0;
61            boolean rightVisible = x + availableWidth < neededWidth;
62            if (mButtonLeftLayout != null) {
63                mButtonLeftLayout.setVisibility(leftVisible ? VISIBLE : GONE);
64            }
65            if (mButtonRightLayout != null) {
66                mButtonRightLayout.setVisibility(rightVisible ? VISIBLE : GONE);
67            }
68        }
69        super.requestLayout();
70    }
71
72    public boolean onTouch(View v, MotionEvent event) {
73        if (event.getAction() == MotionEvent.ACTION_DOWN) {
74            if (v == mButtonRight) {
75                mCandidates.scrollNext();
76            } else if (v == mButtonLeft) {
77                mCandidates.scrollPrev();
78            }
79        }
80        return false;
81    }
82
83}
84