1/*
2 * Copyright (C) 2008 The Android Open Source Project
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.calculator2;
18
19import android.view.LayoutInflater;
20import android.view.ViewGroup;
21import android.view.View;
22import android.content.Context;
23import android.widget.BaseAdapter;
24import android.widget.TextView;
25
26import java.util.Vector;
27
28import org.javia.arity.SyntaxException;
29
30class HistoryAdapter extends BaseAdapter {
31    private Vector<HistoryEntry> mEntries;
32    private LayoutInflater mInflater;
33    private Logic mEval;
34
35    HistoryAdapter(Context context, History history, Logic evaluator) {
36        mEntries = history.mEntries;
37        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
38        mEval = evaluator;
39    }
40
41    // @Override
42    public int getCount() {
43        return mEntries.size() - 1;
44    }
45
46    // @Override
47    public Object getItem(int position) {
48        return mEntries.elementAt(position);
49    }
50
51    // @Override
52    public long getItemId(int position) {
53        return position;
54    }
55
56    @Override
57    public boolean hasStableIds() {
58        return true;
59    }
60
61    // @Override
62    public View getView(int position, View convertView, ViewGroup parent) {
63        View view;
64        if (convertView == null) {
65            view = mInflater.inflate(R.layout.history_item, parent, false);
66        } else {
67            view = convertView;
68        }
69
70        TextView expr   = (TextView) view.findViewById(R.id.historyExpr);
71        TextView result = (TextView) view.findViewById(R.id.historyResult);
72
73        HistoryEntry entry = mEntries.elementAt(position);
74        String base = entry.getBase();
75        expr.setText(entry.getBase());
76
77        try {
78            String res = mEval.evaluate(base);
79            result.setText("= " + res);
80        } catch (SyntaxException e) {
81            result.setText("");
82        }
83
84        return view;
85    }
86}
87
88