Class TableBinding

java.lang.Object
com.codename1.wordprocessing.TableBinding

public final class TableBinding extends Object

Keeps a table of a document and a live Sheet in step, so the table can be edited in a DataGrid with Excel formulas, sorting and a formula bar, and the document follows.

DocumentModel model = new DocumentModel(new Document());
try {
    model.setDocument(Document.read(new ByteArrayInputStream(("<table><tr><th>Region</th>"
            + "<th>Q1</th></tr><tr><td>North</td><td>120</td></tr></table>").getBytes()),
            Document.Format.HTML));
} catch (IOException e) {
    Log.e(e);
}
// wordprocessing has a Table, and so does com.codename1.ui.table
com.codename1.wordprocessing.Table table = null;
for (int i = 0; i < model.size(); i++) {
    if (model.getDocument().get(i) instanceof com.codename1.wordprocessing.Table) {
        table = (com.codename1.wordprocessing.Table) model.getDocument().get(i);
    }
}
TableBinding binding = new TableBinding(model, table.id);
Dialog d = new Dialog("Edit table", new BorderLayout());
d.add(BorderLayout.CENTER, binding.createGrid());
d.showPacked(BorderLayout.CENTER, true);
binding.dispose();

An edit in the sheet replaces the table in the model, as one undoable step per cell. An edit to the model -- typing in the table in a DocumentView, or an undo -- is copied into the sheet. Each cell keeps its paragraphs and formatting; only the text of a changed cell changes. Rows and columns added in the sheet are not copied back.

  • Constructor Details

    • TableBinding

      public TableBinding(DocumentModel model, String tableId)

      Binds a top-level table of a model to a new sheet.

      Parameters
      • model: the model

      • tableId: the id of a table in the model's document

  • Method Details

    • getSheet

      public Sheet getSheet()

      The sheet the table is bound to.

      Returns

      the sheet

    • getTableId

      public String getTableId()

      The id of the bound table.

      Returns

      the id

    • createGrid

      public DataGrid createGrid()

      A grid editing the sheet.

      Returns

      a new DataGrid over the sheet

    • dispose

      public void dispose()
      Stops following: the sheet keeps its cells, the model no longer changes with it.