The Data Module

To follow standard OO practices we will create a class to handle the communications with Relativity Server and the tables we are using. The benefit of this approach is that we can use our DataModule class regardless of whether we are creating an app that has a UI made with Swing or JavaFX, or we could instead take the class and use it in an Android project.

Creating the DataModule class

Create a new class called DataModule.java. In Eclipse select File->New->Class.

Adding a new class

This opens up a wizard page, add the name of the class

Wizard to define the new class.

Variables

Next define the variables we will be using in this tutorial.

private RemoteDataAdapter dataAdapter;

private DataTable tasksTable;
private DataTable prioritiesTable;

private DataTableModel tasksTableModel;
private DataTableModel prioritiesTableModel;

The RemoteDataAdapter handles the actual interactions with the server. Through it we can fill our DataTables, apply changes made to them back to the server, execute commands on the server and log in & out. All of the "action" methods have a synchronous and asynchronous version allowing you to work with the Data Abstract server in both a blocking and non blocking manner.

The DataTable class represent the data that has been retrieved from the server. It contains an array of the actual rows and maintains information about any changes to the data, indeed you can ask the class for a Collectionn of changed rows. Each row of data is an instance of the DataRow object. The fields in the row generally match those in the database themselves, however you can add fields that are only represented on the client side and are determined at runtime. These are calculated fields and lookup fields.

The DataTableModel class is a concrete implementation of Java's Swing class javax.swing.table.AbstractTableModel. It acts as the bridge between an instance of Java's JTable class and the DataTable class.

Methods

We are only going to add a few methods at this time, then over the course of the tutorial we will add others for logging in, loading the data, creating a new row, deleting a row and so on.

In the initializer add calls to initComponents which will initialize the RemoteDataAdapter object and set any configuration we need for it, and initTables where we will initialize the tables we are using.

public DataModule() {
    initComponents();
    initTables();
}

Create the initComponents method, it in we use the factory method create of RemoteDataAdapter to instantiate an instance of the class preconfigured. The argument to create is the URI of an instance of Relativity Server. In this case to an instance running on the local machine.

private void initComponents() {
    this.dataAdapter = RemoteDataAdapter.create(URI.create("http://localhost:7099/bin"));       
}

Create the initComponents method. Here we instantiate two DataTable classes, passing in the name of the table they will match to on the server. We then create a DataTableModel object which takes a DataTable as an argument. Then finally we add a listener for the TableChangedEvent event, which is sent when the data or structure of the table is changed.

private void initTables() {
    this.tasksTable = new DataTable("Tasks");
    this.tasksTableModel = new DataTableModel(this.tasksTable);
    this.tasksTable.addTableDataChangedListener(tasksTableModel);
    
    this.prioritiesTable = new DataTable("Priorities");
    this.prioritiesTableModel = new DataTableModel(this.prioritiesTable);
    this.prioritiesTable.addTableDataChangedListener(this.prioritiesTableModel);            
}

Finally add a helper method which returns the DataTableModel for the Tasks table, we need this to pass to the JTable

public DataTableModel getTasksDataTableModel() {
    return tasksTableModel;
}

On the next page we will build the user interface of the tutorial app.