summaryrefslogtreecommitdiff
path: root/data/iphone-java/HelloJava.app/HelloJava.java
blob: 08f4c70db3b09f74ca080358e1c9ebd3ffb854e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import obc.*;
import joc.*;

import joc.Message;

import static joc.Pointer.*;
import static joc.Static.*;

import java.util.ArrayList;
import java.util.Date;

public class HelloJava
    extends UIApplication
{

private UIWindow window;

private static class Contact {
    public String first;
    public String last;

    public Contact(String first, String last) {
        this.first = first;
        this.last = last;
    }

    public String getName() {
        String name = first;
        if (last != null)
            name += " " + last;
        return name;
    }
}

public static class Section {
    public int row;
    public String title;

    public Section(int row, String title) {
        this.row = row;
        this.title = title;
    }
}

private ArrayList<Contact> contacts_;
private ArrayList<Section> sections_;

@Message int numberOfSectionsInSectionList$(UISectionList list) {
    return sections_.size();
}

@Message String sectionList$titleForSection$(UISectionList list, int section) {
    return sections_.get(section).title;
}

@Message int sectionList$rowForSection$(UISectionList list, int section) {
    return sections_.get(section).row;
}

@Message int numberOfRowsInTable$(UITable table) {
    return contacts_.size();
}

@Message UITableCell table$cellForRow$column$reusing$(UITable table, int row, UITableColumn col, UITableCell reusing) {
    Contact contact = contacts_.get(row);

    UIImageAndTextTableCell cell;
    if (reusing != null)
        cell = (UIImageAndTextTableCell) reusing;
    else
        cell = (UIImageAndTextTableCell) new UIImageAndTextTableCell().init();

    cell.setTitle$(contact.getName());
    return cell;
}

@Message public byte table$canSelectRow$(UITable table, int row) {
    return NO;
}

@Message public void applicationDidFinishLaunching$(Object unused)
    throws Exception
{
    System.out.println("work");
    contacts_ = new ArrayList<Contact>();
    sections_ = new ArrayList<Section>();

    SQLite.Database ab = new SQLite.Database();
    ab.open(userHomeDirectory().toString() + "/Library/AddressBook/AddressBook.sqlitedb", 0666); try {
        SQLite.Stmt st = ab.prepare("select first, last from ABPerson where first is not null order by first"); try {
            while (st.step())
                contacts_.add(new Contact(
                    st.column_string(0),
                    st.column_string(1)
                ));
        } finally { st.close(); }
    } finally { ab.close(); }

    CGRect outer = UIHardware.$fullScreenApplicationContentRect();
    window = new UIWindow().initWithContentRect$(outer);

    window.orderFront$(this);
    window.makeKey$(this);
    window._setHidden$(NO);

    CGRect inner = window.bounds();
    CGSize navsize = UINavigationBar.$defaultSize();
    CGRect navrect = new CGRect(0, 0, inner.size.width, navsize.height);

    UIView view = new UIView().initWithFrame$(inner);
    window.setContentView$(view);

    UINavigationBar navbar = new UINavigationBar().initWithFrame$(navrect);
    view.addSubview$(navbar);

    navbar.setBarStyle$(1);

    UINavigationItem navitem = new UINavigationItem().initWithTitle$("Contact List");
    navbar.pushNavigationItem$(navitem);

    char letter = 0;
    for (int i = 0; i != contacts_.size(); ++i) {
        String name = contacts_.get(i).getName();
        char now = name.charAt(0);
        if (letter != now) {
            letter = now;
            sections_.add(new Section(
                i,
                new String(new char[] {now})
            ));
        }
    }

    CGRect lower = new CGRect(0, navsize.height, inner.size.width, inner.size.height - navsize.height);
    UISectionList list = new UISectionList().initWithFrame$(lower);
    view.addSubview$(list);

    UITableColumn col = new UITableColumn().initWithTitle$identifier$width$("Name", "name", 320);

    UITable table = (UITable) list.table();
    table.setSeparatorStyle$(1);
    table.addTableColumn$(col);
    table.setReusesTableCells$(YES);

    list.setDataSource$(this);
    list.reloadData();

    /* // XXX: this doesn't work the same on 2.0 and I don't want to think about porting it right now
    AVController controller = new AVController().init();
    CharSequence wavfile = (CharSequence) ((NSBundle) NSBundle.mainBundle()).pathForResource$ofType$("start", "wav");
    AVItem wavitem = new AVItem().initWithPath$error$(wavfile, null);
    wavitem.setVolume$(100);
    controller.setCurrentItem$(wavitem);
    controller.setCurrentTime$(0);
    controller.play$(null);*/
}

}