Make a listBox with table titles instead of the flexTable demo.
[online-glom:gwt-glom.git] / src / org / glom / web / server / LibGlomServiceImpl.java
1 package org.glom.web.server;
2
3 import java.io.File;
4
5 import org.glom.libglom.Document;
6 import org.glom.libglom.Glom;
7 import org.glom.libglom.StringVector;
8 import org.glom.web.client.GlomDocument;
9 import org.glom.web.client.GlomTable;
10 import org.glom.web.client.LibGlomService;
11
12 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
13
14 @SuppressWarnings("serial")
15 public class LibGlomServiceImpl extends RemoteServiceServlet implements
16                 LibGlomService {
17         private Document document;
18
19         public LibGlomServiceImpl() {
20                 Glom.libglom_init();
21                 // FIXME Need to call Glom.libglom_deinit()
22                 document = new Document();
23                 document.set_file_uri("file://" + Glom.GLOM_EXAMPLE_FILE_DIR +  File.separator + "example_music_collection.glom");
24                 int error = 0;
25                 @SuppressWarnings("unused")
26                 boolean retval = document.load(error);
27                 // FIXME handle error condition
28         }
29
30         // FIXME I think Swig is generating long on 64-bit machines and int on 32-bit machines - need to keep this constant
31         // From http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
32         public static int safeLongToInt(long l) {
33             if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
34                 throw new IllegalArgumentException
35                     (l + " cannot be cast to int without changing its value.");
36             }
37             return (int) l;
38         }
39
40         @Override
41         public GlomDocument getGlomDocument() {
42                 GlomDocument glomDocument = new GlomDocument();
43
44                 // set visable title
45                 glomDocument.setTitle(document.get_database_title());
46
47                 // set array of GlomTables and the default table index
48                 StringVector tableNames = document.get_table_names();
49                 GlomTable[] tables = new GlomTable[safeLongToInt(tableNames.size())];
50                 for (int i = 0; i < tableNames.size(); i++) {
51                         String tableName = tableNames.get(i);
52                         GlomTable glomTable = new GlomTable();
53                         glomTable.setName(tableName);
54                         glomTable.setTitle(document.get_table_title(tableName));
55                         tables[i] = glomTable;
56                         if (tableName.equals(document.get_default_table())) {
57                                 glomDocument.setDefaultTable(i);
58                         }
59                 }
60                 glomDocument.setTableNames(tables);
61
62                 return glomDocument;
63
64         }
65
66 }