GCで表を描く

Graphic Contextで表を描くクラスの例です。最低限の機能しか持ってませんが、セルの右寄せ、センター配置ができます。

GCTableTest.java

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class GCTableTest {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setSize(700, 500);
        shell.setLayout(new FillLayout());

        final GCTable gt = new GCTable();
        gt.setColumnWidth(new int[]{10, 20, 30});
        gt.add("000").add("111").add("222");
        gt.add(new LCell("Left")).add(new RCell("Right")).add(new CCell("Center"));
        gt.add(new LCell("Left")).add(new RCell("Right")).add(new CCell("Center"));
        gt.add(new LCell("Left")).add(new RCell("Right")).add(new CCell("Center"));
        gt.add(new LCell("Left")).add(new RCell("Right")).add(new CCell("Center"));
        gt.add("1").add("2").add("3");
        gt.add("4").add("5").add("6");
        gt.add("7").add("8").add("9");

        Canvas c = new Canvas(shell, SWT.NONE);
        c.addPaintListener(new PaintListener(){
            public void paintControl(PaintEvent e) {
                gt.draw(e.gc, 10, 10);
            }
        });
        
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}


GCTable.java

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.swt.graphics.GC;

public class GCTable {
    
    private char marginChar = '.';
    private char widthChar = 'A';
    private List cells = new LinkedList();
    private int[] columnWidth;
    
    public GCTable add(String s) {
        cells.add(new LCell(s));
        return this;
    }

    public GCTable add(Cell cell) {
        cells.add(cell);
        return this;
    }

    public void draw(GC gc, int x, int y) {
        int margin = gc.getCharWidth(marginChar);
        int cx = x;
        int cy = y;
        int index = 0;
        int width = getTableWidth() * gc.getCharWidth(widthChar);
        
        Iterator it = cells.iterator();
        while (it.hasNext()) {
            int col = index % columnWidth.length;
            
            Cell c = (Cell)it.next();
            int cw = columnWidth[col] * gc.getCharWidth(widthChar);
            c.draw(gc, cx + margin, cy, cw - margin * 2);
            cx += cw;
            
            // line feed
            if (col == columnWidth.length - 1) {
                cx = x;
                gc.drawLine(cx, cy, cx + width, cy);
                cy += gc.getFontMetrics().getHeight();
            }
            
            index++;
        }
        gc.drawLine(cx, cy, cx + width, cy);

        // draw vertical line
        gc.drawLine(x, y, x, cy);
        for (int i = 0; i < columnWidth.length; i++) {
            cx += columnWidth[i] * gc.getCharWidth(widthChar);
            gc.drawLine(cx, y, cx, cy);
        }
    }
    
    public int getTableWidth() {
        int w = 0;
        for (int i = 0; i < columnWidth.length; i++) {
            w += columnWidth[i];
        }
        return w;
    }
    
    public int getWidht(GC gc) {
        return getTableWidth() * gc.getCharWidth(widthChar);
    }
    
    public int getHeight(GC gc) {
        return cells.size() / columnWidth.length * gc.getFontMetrics().getHeight();
    }

    public void setColumnWidth(int[] columnWidth) {
        this.columnWidth = columnWidth;
    }
    
    public void setMarginChar(char marginChar) {
        this.marginChar = marginChar;
    }
    
    public void setWidthChar(char widthChar) {
        this.widthChar = widthChar;
    }
}


Cell.java

import org.eclipse.swt.graphics.GC;

public abstract class Cell {
    private String text;
    
    public Cell(String s) {
        text = s;
    }
    
    public String getText() {
        return text;
    }

    public abstract void draw(GC gc, int x, int y, int w);
}


LCell.java

import org.eclipse.swt.graphics.GC;

public class LCell extends Cell {
    public LCell(String s) {
        super(s);
    }

    public void draw(GC gc, int x, int y, int w) {
        gc.drawText(getText(), x, y);
    }
}


CCell.java

import org.eclipse.swt.graphics.GC;

public class CCell extends Cell {
    public CCell(String s) {
        super(s);
    }

    public void draw(GC gc, int x, int y, int w) {
        gc.drawText(getText(), x + (w - gc.textExtent(getText()).x) / 2, y);
    }
}


RCell.java

import org.eclipse.swt.graphics.GC;

public class RCell extends Cell {
    
    public RCell(String s) {
        super(s);
    }

    public void draw(GC gc, int x, int y, int w) {
        gc.drawText(getText(), x + w - gc.textExtent(getText()).x, y);
    }
}