1/*
2 * Copyright (C) 2010 Google Inc.
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 com.android.i18n.addressinput;
18
19import android.view.View;
20import android.widget.EditText;
21import android.widget.Spinner;
22
23import java.util.ArrayList;
24import java.util.List;
25
26/**
27 * Represents a component in the address widget UI. It could be either a text box (when there is no
28 * candidate) or a spinner.
29 */
30class AddressUiComponent {
31    // The label for the UI component
32    private String mFieldName;
33
34    // The type of the UI component
35    private UiComponent mUiType;
36
37    // The list of elements in the UI component
38    private List<RegionData> mCandidatesList = new ArrayList<RegionData>();
39
40    // The id of this UI component
41    private AddressField mId;
42
43    // The id of the parent UI component. When the parent UI component is updated, this UI
44    // component should be updated.
45    private AddressField mParentId;
46
47    // The View representing the UI component
48    private View mView;
49
50    /**
51     * Type of UI component. There are only EDIT (text-box) and SPINNER (drop-down) components.
52     */
53    enum UiComponent {
54        EDIT, SPINNER,
55    }
56
57    AddressUiComponent(AddressField id) {
58        mId = id;
59        // By default, an AddressUiComponent doesn't depend on anything else.
60        mParentId = null;
61        mUiType = UiComponent.EDIT;
62    }
63
64    /**
65     * Initializes the candidatesList, and set the uiType and parentId.
66     * @param candidatesList
67     */
68    void initializeCandidatesList(List<RegionData> candidatesList) {
69        mCandidatesList = candidatesList;
70        if (candidatesList.size() > 1) {
71            mUiType = UiComponent.SPINNER;
72            switch (mId) {
73                case DEPENDENT_LOCALITY:
74                    mParentId = AddressField.LOCALITY;
75                    break;
76                case LOCALITY:
77                    mParentId = AddressField.ADMIN_AREA;
78                    break;
79                case ADMIN_AREA:
80                    mParentId = AddressField.COUNTRY;
81                    break;
82                default:
83                    // Ignore.
84            }
85        }
86    }
87
88    /**
89     * Gets the value entered in the UI component.
90     */
91    String getValue() {
92        if (mView == null) {
93            return (mCandidatesList.size() == 0) ? "" : mCandidatesList.get(0).getDisplayName();
94        }
95        switch (mUiType) {
96            case SPINNER:
97                Object selectedItem = ((Spinner) mView).getSelectedItem();
98                if (selectedItem == null) {
99                    return "";
100                }
101                return selectedItem.toString();
102            case EDIT:
103                return ((EditText) mView).getText().toString();
104            default:
105                return "";
106        }
107    }
108
109    String getFieldName() {
110        return mFieldName;
111    }
112
113    void setFieldName(String fieldName) {
114        mFieldName = fieldName;
115    }
116
117    UiComponent getUiType() {
118        return mUiType;
119    }
120
121    void setUiType(UiComponent uiType) {
122        mUiType = uiType;
123    }
124
125    List<RegionData> getCandidatesList() {
126        return mCandidatesList;
127    }
128
129    void setCandidatesList(List<RegionData> candidatesList) {
130        mCandidatesList = candidatesList;
131    }
132
133    AddressField getId() {
134        return mId;
135    }
136
137    void setId(AddressField id) {
138        mId = id;
139    }
140
141    AddressField getParentId() {
142        return mParentId;
143    }
144
145    void setParentId(AddressField parentId) {
146        mParentId = parentId;
147    }
148
149    void setView(View view) {
150        mView = view;
151    }
152
153    View getView() {
154        return mView;
155    }
156}
157