YtemProxyOJ.java :  » Image » objectj » oj » processor » state » proxy » Java Open Source

Java Open Source » Image » objectj 
objectj » oj » processor » state » proxy » YtemProxyOJ.java
/*
 * ObjectProxyOJ.java
 */
package oj.processor.state.proxy;

import oj.OJ;
import oj.project.CellOJ;
import oj.project.LocationOJ;
import oj.project.YtemDefOJ;
import oj.project.YtemOJ;

public class YtemProxyOJ {

    private int ytemDefType;
    private int maxCloneCount;
    private int pointCount;
    private YtemOJ ytem;
    private CellOJ cell;
    private String ytemDefName;
    private boolean closed;

    public YtemProxyOJ(CellOJ cell, String ytemDefName, int stackIndex) {
        this.cell = cell;
        this.closed = false;
        this.ytemDefName = ytemDefName;
        this.ytemDefType = OJ.getData().getYtemDefs().getYtemDefByName(ytemDefName).getYtemType();
        this.maxCloneCount = OJ.getData().getYtemDefs().getYtemDefByName(ytemDefName).getCloneMax();

        if (cell.getCloneCount(ytemDefName) < maxCloneCount) {
            createClone(cell, ytemDefName, stackIndex);
        }
    }

    public YtemProxyOJ(CellOJ cell, YtemOJ ytem) {
        if (ytem != null) {
            ytem.setOpen(false);
        }

        this.cell = cell;
        this.closed = false;
        this.ytem = ytem;
        this.ytemDefName = ytem.getYtemDef();
        this.pointCount = ytem.getLocationsCount();
        this.ytemDefType = OJ.getData().getYtemDefs().getYtemDefByName(ytemDefName).getYtemType();
        this.maxCloneCount = OJ.getData().getYtemDefs().getYtemDefByName(ytemDefName).getCloneMax();

        ytem.setOpen(true);
    }

    public boolean isSameYtem(YtemOJ ytem) {
        return ((this.ytem != null) && (this.ytem.equals(ytem)));
    }

    public boolean addLocation(int stackIndex, double x, double y, double z) {
        if (ytem == null) {
            if (cell.getCloneCount(ytemDefName) < maxCloneCount) {
                createClone(cell, ytemDefName, stackIndex);
            } else {
                return false;
            }
        }
        if (!addLocationToClone(x, y, z)) {

            if ((OJ.getData().getYtemDefs().isComposite()) && (cell.getCloneCount(ytemDefName) < maxCloneCount)) {
                createClone(cell, ytemDefName, stackIndex);
                addLocationToClone(x, y, z);
                return true;
            }
            return false;
        }
        return true;
    }

    public void close() {
        if (ytem != null) {
            ytem.setOpen(false);
        }

        switch (ytemDefType) {
            case YtemDefOJ.YTEM_TYPE_LINE:
            case YtemDefOJ.YTEM_TYPE_SEGLINE:
            case YtemDefOJ.YTEM_TYPE_POLYGON:
                if (pointCount < 2) {
                    cell.removeYtem(ytem);
                }
                break;
            case YtemDefOJ.YTEM_TYPE_ANGLE:
                if (pointCount < 3) {
                    cell.removeYtem(ytem);
                }
                break;
            default:
                if (pointCount < 1) {
                    cell.removeYtem(ytem);
                }
        }

        closed = true;
        ytem = null;
    }

    public boolean deleteLastLocation() {
        if ((ytem != null) && (ytem.getLocationsCount() > 0)) {
            pointCount -= 1;
            OJ.getDataProcessor().removeLocation(ytem, ytem.getLocationsCount() - 1);
            if (ytem.getLocationsCount() == 0) {
                close();
            }
            return true;
        } else {
            return false;
        }
    }

    public boolean isClosed() {
        if (closed) {
            return true;
        }
        switch (ytemDefType) {
            case YtemDefOJ.YTEM_TYPE_POINT:
                if (pointCount == 1) {
                    if (ytem != null) {
                        ytem.setOpen(false);
                    }
                    return true;
                }
                break;
            case YtemDefOJ.YTEM_TYPE_LINE:
                if (pointCount == 2) {
                    if (ytem != null) {
                        ytem.setOpen(false);
                    }
                    return true;
                }
                break;
            case YtemDefOJ.YTEM_TYPE_ANGLE:
                if (pointCount == 3) {
                    if (ytem != null) {
                        ytem.setOpen(false);
                    }
                    return true;
                }
                break;
        }
        return false;
    }

    private boolean addLocationToClone(double x, double y, double z) {
        if (!isClosed()) {
            pointCount += 1;
            OJ.getDataProcessor().addLocation(ytem, new LocationOJ(x, y, z));
            return true;
        } else {
            return false;
        }
    }

    private void createClone(CellOJ cell, String objectDefName, int stackIndex) {
        if (ytem != null) {
            ytem.setOpen(false);
        }
        ytem = OJ.getDataProcessor().createNewYtem(objectDefName);
        ytem.setStackIndex(stackIndex);
        ytem.setOpen(true);

        OJ.getDataProcessor().addYtem(cell, ytem);
        pointCount = 0;
        closed = false;

    }

    public int getLocationsCount() {
        if (ytem != null) {
            return ytem.getLocationsCount();
        }
        return 0;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.