www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.dwt - TableEditorexample-Seg Fault

from here I drew an example and connverted to DWT
http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/TableEditorexample.htm

It causes a seg falt at or around the comment and print lines and is related to
the switch statement but it is not obvious yet which line is causing the fault
as it happens whether mouse or key is used to traverse to the new cell.

Here is the code anyway 

module tableeditor;
/*
 * TableEditor example snippet: edit a cell in a table (in place, fancy)
 * 
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */

import dwt.DWT;
import dwt.custom.TableEditor;
import dwt.graphics.Point;
import dwt.graphics.Rectangle;
import dwt.layout.FillLayout;
import dwt.widgets.Display;
import dwt.widgets.Event;
import dwt.widgets.Listener;
import dwt.widgets.Shell;
import dwt.widgets.Table;
import dwt.widgets.TableColumn;
import dwt.widgets.TableItem;
import dwt.widgets.Text;

import tango.io.Stdout;
import tango.util.Convert; 

static void main(char[][] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Table table = new Table(shell, DWT.BORDER | DWT.MULTI);
    table.setLinesVisible(true);
    for (int i = 0; i < 3; i++) {
      TableColumn column = new TableColumn(table, DWT.NONE);
      column.setWidth(100);
    }
// Originally this was :-
/* 
  for (int i=0; i<3; i++) {
    TableItem item = new TableItem (table, SWT.NONE);
    item.setText(new String [] {"" + i, "" + i , "" + i});
  }
*/
   for (int i = 0; i < 3; i++) {
      TableItem item = new TableItem(table, DWT.NONE);
      char[][] x = [ to!(char[])(i), to!(char[])(i), to!(char[])(i) ];
      item.setText(x);
    }
    final TableEditor editor = new TableEditor(table);
    editor.horizontalAlignment = DWT.LEFT;
    editor.grabHorizontal = true;
    table.addListener(DWT.MouseDown, new class Listener {
      void handleEvent(Event event) {
Stdout.print("Event is  ",event).newline; 
       Rectangle clientArea = table.getClientArea();
        Point pt = new Point(event.x, event.y);
        int index = table.getTopIndex();
        while (index < table.getItemCount()) {
          bool visible = false;
          final TableItem item = table.getItem(index);
          for (int i = 0; i < table.getColumnCount(); i++) {
            Rectangle rect = item.getBounds(i);
            if (rect.contains(pt)) {
              final int column = i;
              final Text text = new Text(table, DWT.NONE);
              Listener textListener = new class Listener {
                void handleEvent( Event e) {
Stdout.print("e is ",e).newline;
Stdout.print("e.type is  ",e.type).newline;
                switch (e.type) {
                  case DWT.FocusOut:// value is 16
Stdout.print(column,text.getText()).newline;
                    item.setText(column, text.getText());
                    text.dispose();
                    break;
                  case DWT.Traverse:  //value is 31
                    switch (e.detail) {
                    case DWT.TRAVERSE_RETURN:
Stdout.print(" Column , text is  ",column,text.getText()).newline;
                      item.setText(column, text.getText());
                    //FALL THROUGH
                    case DWT.TRAVERSE_ESCAPE:
                      text.dispose();
                      e.doit = false;
                    }
                    break;
                  }
                }
              };
              text.addListener(DWT.FocusOut, textListener);
              text.addListener(DWT.Traverse, textListener);
              editor.setEditor(text, item, i);
              text.setText(item.getText(i));
              text.selectAll();
              text.setFocus();
Stdout.print("text is  ",text).newline;
              return;
            }
            if (!visible && rect.intersects(clientArea)) {
              visible = true;
            }
          }
          if (!visible)
            return;
          index++;
Stdout.print("index is  ",index).newline;
        }
      }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }



for conversion on the below I used that shown ,is there another way?
/* 
  for (int i=0; i<3; i++) {
    TableItem item = new TableItem (table, SWT.NONE);
    item.setText(new String [] {"" + i, "" + i , "" + i});
  }
*/
   for (int i = 0; i < 3; i++) {
      TableItem item = new TableItem(table, DWT.NONE);
      char[][] x = [ to!(char[])(i), to!(char[])(i), to!(char[])(i) ];
      item.setText(x);
    }
Jun 08 2008