Data Editor

Each layer has an event handler (implementation of ILayerEventHandler). This class is responsible for handling events coming from mouse and keyboard. It has to convert mouse coordinates into model coordinates, update data and shape. To accomplish it we create class HorizonEventHandler which implements ILayerEventHandler.

We implement three methods: mouseReleased(MouseEvent e), mouseDragged (MouseEvent e) and mousePressed(MouseEvent e). The fragment below contains three of them.

EVENT HANDLING

@Override
    public void mouseReleased(MouseEvent e) {
        layer.commit(data);
    }
    @Override
    public void mouseDragged(MouseEvent e) {
        Point2D p = getModelPoint(e);
        ArrayList<Float> tmpData = new ArrayList<Float>();
        if(p.getX() >lastPoint.getX()){
            int start = (int)lastPoint.getX();
            int end = (int)p.getX();
            for(int x = start;x<= end;x++){
                double y = lastPoint.getY();
                PointData pd = layer.pickValue(x,y);
                if(pd == null)
                    break;
                data.add(pd);
                tmpData.add(pd.getValue(vertIndex));
                shape.update((int)lastPoint.getX(), (int)p.getX(), tmpData);
            }
        } else{
            int end = (int)lastPoint.getX();
            int start = (int)p.getX();
            for(int x = start;x<=end;x++){                
                double y = p.getY();
                PointData pd = layer.pickValue(x,y);
                if(pd == null)
                    break;
                data.add(pd);
                tmpData.add(pd.getValue(vertIndex));
                shape.update((int)p.getX(), (int)lastPoint.getX() , tmpData);
            }
        }
        lastPoint = p;
    }
    @Override
    public void mousePressed(MouseEvent e) {
        data.clear();
        lastPoint = getModelPoint(e);
        ArrayList<Float> tmpData = new ArrayList<Float>();
        PointData pd = layer.pickValue((int)lastPoint.getX(),lastPoint.getY());
        if(pd == null)
            return;
        data.add(pd);
        tmpData.add(pd.getValue(vertIndex));
        shape.update((int)lastPoint.getX(), (int)lastPoint.getX(), tmpData);
    }

mouseReleased is simplest, it just commits collected data into layer. mousePressed remembers points where event happens and updates shape. mouseDraged iterates from first point (saved by mousePressed) to last, collects data, updates shape.

Mapping view coordinates to model is done by getModelPoint method (with layer's help):

GETMODELPOINT

private Point2D getModelPoint(MouseEvent e){
        Point p = layer.getViewPosition();
        Point ep = e.getPoint();
        Point pp= new Point(ep.x+p.x, ep.y+p.y);
        return layer.deviceToModel(pp);
    }

Layer's method commit() updates horizon data:

COMMIT()

void commit(final List<PointData> data) {
        final IHorizonData hdata = (IHorizonData)getData();
        hdata.update(new Runnable() {
            public void run() {
                for(PointData pd:data){
                    hdata.addPointData(pd);
                }
            }
        });
    }

We finished data editor. HorizonLayer provides basic functionality now. But if you try to change selection in seismic layer you will see our layer ignores it. It still shows old data. The reason is we ignore events INTViewer sends when something important happens. In next section we will fix it: INTViewer events.