0

我是 RCP 开发的新手。

我想创建两个表,每个表包含不同的数据。来自两个表的数据具有 1 对 1、1 对多或多对 1 的关系。这可以通过在两个表之间绘制箭头来完成。例如,

         **Row 1**                  **Row 2**
           R1 V1                      R2 V1
           R1 V2                      R2 V2
           R1 V3                      R2 V3

我想绘制从 R1V1 到( R2V1 和 R2V3 )的箭头,反之亦然。如何以图形方式显示它。

我怎样才能找到由箭头组合的行。

任何帮助表示赞赏。

--- 曼达尔

4

3 回答 3

2

这是一个很难实现的组件,前段时间我为 Tibco Business Studio 做了一个。

您需要在两个表格之间放置一个画布来绘制链接。您可能有两个表的数据模型,您还需要第三个模型来存储链接并确保对该模型的任何修改都会触发 Canvas 的刷新。

接下来为两个表添加拖放支持,将表 1 中的项目拖放到表 2 中应该会在链接模型中创建一个新项目(从而触发 Canvas 刷新以绘制链接)。

实际上在正确的位置绘制链接你必须自己解决,但希望这能给你一些想法。

于 2012-09-24T10:33:25.693 回答
2

这是基于尼克提出的想法的代码。这只是为那些可能想知道从哪里开始实施如下所示的东西的人提供一个想法

在此处输入图像描述

这将让您单击左侧表格上的任何列,然后在鼠标移向右侧表格时画一条线,并在选择右侧表格上的列后立即锚定该线。它在一个链表中保持一个左表行和右表行之间的映射作为映射数据模型。

package sample;

import java.util.LinkedList;

import org.eclipse.draw2d.AutomaticRouter;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FreeformLayeredPane;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.PolylineDecoration;
import org.eclipse.draw2d.XYAnchor;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class GraphicTableMapper {
    private static Point sourcePosition;
    private static PathFigure currentPath;
    private static Figure bf;
    private static Canvas canvas;
    private static int sourceRow;
    private static int targetRow;
    private static LinkedList<RowMapper> rowmapList = new LinkedList<RowMapper>();

    public static void main(String[] args) {
        Display display = Display.getDefault();
        final Shell shell = new Shell(display);
        shell.setSize(550, 500);
        shell.setLayout(new GridLayout(3, false));
        final Table table = new Table(shell, SWT.MULTI | SWT.BORDER
                | SWT.FULL_SELECTION);
        table.setLinesVisible(true);
        table.setHeaderVisible(true);
        final String[] titles = { "Serial Number", "Whatever" };
        for (int i = 0; i < titles.length; i++) {
            TableColumn column = new TableColumn(table, SWT.NONE);
            column.setText(titles[i]);
        }
        int count = 100;// create 100 rows in table
        for (int i = 0; i < count; i++) {
            TableItem item = new TableItem(table, SWT.NONE);
            item.setText(0, "x");
            item.setText(1, "y");
            item.setText(2, "!");
            item.setText(3, "this stuff behaves the way I expect");
            item.setText(4, "almost everywhere");
            item.setText(5, "some.folder");
            item.setText(6, "line " + i + " in nowhere");
        }
        for (int i = 0; i < titles.length; i++) {
            table.getColumn(i).pack();
        }
        table.addListener(SWT.MouseDown, new Listener() {
            public void handleEvent(Event event) {
                Point pt = new Point(event.x, event.y);
                TableItem item = table.getItem(pt);
                if (item == null)
                    return;
                for (int i = 0; i < titles.length; i++) {
                    Rectangle rect = item.getBounds(i);
                    if (rect.contains(pt)) {
                        int index = table.indexOf(item);
                        System.out.println("Item " + index + "-" + i);
                        sourcePosition = pt;
                        sourceRow = index;
                        currentPath = new PathFigure();
                        currentPath.setSourceAnchor(new XYAnchor(
                                new org.eclipse.draw2d.geometry.Point(-10,
                                        event.y)));
                        currentPath
                                .setTargetAnchor(new XYAnchor(
                                        new org.eclipse.draw2d.geometry.Point(
                                                0, pt.y)));
                        bf.add(currentPath);
                    }
                }
            }
        });

        table.addMouseMoveListener(new MouseMoveListener() {
            public void mouseMove(MouseEvent arg0) {
                if (currentPath != null) {
                    ((XYAnchor) (currentPath.getTargetAnchor()))
                            .setLocation(new org.eclipse.draw2d.geometry.Point(
                                    0, arg0.y));
                }
            }
        });

        canvas = new Canvas(shell, SWT.None);       
        canvas.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_GREEN));
        LightweightSystem lws = new LightweightSystem(canvas);
        bf = new BaseFigure();
        lws.setContents(bf);

        canvas.addMouseMoveListener(new MouseMoveListener() {
            public void mouseMove(MouseEvent arg0) {
                if (currentPath != null) {
                    ((XYAnchor) (currentPath.getTargetAnchor()))
                            .setLocation(new org.eclipse.draw2d.geometry.Point(
                                    arg0.x > canvas.getSize().x - 5 ? canvas
                                            .getSize().x - 5 : arg0.x, arg0.y));
                }
            }
        });
        GridData data2 = new GridData();
        data2.verticalAlignment = SWT.TOP;
        data2.grabExcessHorizontalSpace = false;
        data2.grabExcessVerticalSpace = true;
        data2.horizontalIndent = -10;
        data2.widthHint = 200;
        data2.heightHint = 1000;
        canvas.setLayoutData(data2);
        final Table table2 = new Table(shell, SWT.MULTI | SWT.BORDER
                | SWT.FULL_SELECTION);
        table2.setLinesVisible(true);
        table2.setHeaderVisible(true);
        data2 = new GridData();
        data2.grabExcessHorizontalSpace = false;
        data2.horizontalIndent = -10;
        table2.setLayoutData(data2);
        final String[] titles2 = { "Serial Number", "Whatever" };
        for (int i = 0; i < titles.length; i++) {
            TableColumn column = new TableColumn(table2, SWT.NONE);
            column.setText(titles[i]);
            canvas.redraw();
        }
        table2.addMouseMoveListener(new MouseMoveListener() {
            public void mouseMove(MouseEvent event) {
                if (currentPath != null) {
                    Point pt = new Point(event.x, event.y);
                    TableItem item = table2.getItem(pt);
                    if (item == null)
                        return;
                    for (int i = 0; i < titles2.length; i++) {
                        Rectangle rect = item.getBounds(i);
                        if (rect.contains(pt)) {
                            ((XYAnchor) (currentPath.getTargetAnchor()))
                                    .setLocation(new org.eclipse.draw2d.geometry.Point(
                                            canvas.getSize().x - 5, event.y));
                        }
                    }
                }
            }
        });

        int count2 = 100;// create 100 rows in table 2
        for (int i = 0; i < count2; i++) {
            TableItem item = new TableItem(table2, SWT.NONE);
            item.setText(0, "x");
            item.setText(1, "y");
            item.setText(2, "!");
            item.setText(3, "this stuff behaves the way I expect");
            item.setText(4, "almost everywhere");
            item.setText(5, "some.folder");
            item.setText(6, "line " + i + " in nowhere");
        }
        for (int i = 0; i < titles.length; i++) {
            table2.getColumn(i).pack();
        }
        table2.addListener(SWT.MouseDown, new Listener() {
            public void handleEvent(Event event) {
                try {
                    Point pt = new Point(event.x, event.y);
                    TableItem item = table2.getItem(pt);
                    if (item == null)
                        return;
                    for (int i = 0; i < titles2.length; i++) {
                        Rectangle rect = item.getBounds(i);
                        if (rect.contains(pt)) {
                            int index = table2.indexOf(item);
                            targetRow = index;
                            System.out.println("Item " + index + "-" + i);
                            if (sourcePosition != null) {
                                add(event);
                            }
                        }
                    }
                } finally {
                    sourcePosition = null;
                    sourceRow = -1;
                    targetRow = -1;
                }
            }
        });
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }

    public static void add(Event event) {
        bf.remove(currentPath);
        PathFigure figure = new PathFigure();
        figure.setSourceAnchor(currentPath.getSourceAnchor());
        figure.setTargetAnchor(currentPath.getTargetAnchor());
        bf.add(figure);
        currentPath = null;
        RowMapper mapper = new RowMapper();
        mapper.sourceRow = sourceRow;
        mapper.targetRow = targetRow;
        if (!rowmapList.contains(mapper)) {
            rowmapList.add(mapper);
        }
    }


    class BaseFigure extends FreeformLayeredPane {
    public BaseFigure() {
        setLayoutManager(new FreeformLayout());
        setBorder(new MarginBorder(5));
        setBackgroundColor(ColorConstants.white);
        setOpaque(true);
    }
}

class PathFigure extends PolylineConnection {
    public PathFigure() {
        setTargetDecoration(new PolylineDecoration());
        setConnectionRouter(new AutomaticRouter() {
            @Override
            protected void handleCollision(PointList list, int index) {
            }
        });
    }
}

class RowMapper {
    int sourceRow;
    int targetRow;

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof RowMapper) {
            RowMapper mapper = (RowMapper) obj;
            return (sourceRow == mapper.sourceRow && targetRow == mapper.targetRow);
        }
        return false;
    }

}
于 2012-10-31T15:57:38.777 回答
0

我是否认为这个实现使用鼠标位置来绘制箭头?因此,如果您想保存/加载关系,则必须保存箭头的 x,y 位置,并且必须确保组件始终保持相同大小?

于 2012-11-21T09:28:43.353 回答