Search in sources :

Example 1 with ReactionArrow

use of com.actelion.research.chem.reaction.ReactionArrow in project openchemlib by Actelion.

the class JDrawArea method cleanupMultiFragmentCoordinates.

private void cleanupMultiFragmentCoordinates(Graphics g, ExtendedDepictor depictor, boolean selectedOnly) {
    if (selectedOnly && mUpdateMode == UPDATE_INVENT_COORDS) {
        int[] fragmentAtom = new int[mFragment.length];
        for (int atom = 0; atom < mMol.getAllAtoms(); atom++) {
            int fragment = mFragmentNo[atom];
            mFragment[fragment].setAtomMarker(fragmentAtom[fragment], !mMol.isSelectedAtom(atom));
            fragmentAtom[fragment]++;
        }
    }
    Rectangle2D.Double[] boundingRect = new Rectangle2D.Double[mFragment.length];
    // float fragmentWidth = 0.0f;
    for (int fragment = 0; fragment < mFragment.length; fragment++) {
        if (mUpdateMode == UPDATE_INVENT_COORDS) {
            new CoordinateInventor(selectedOnly ? CoordinateInventor.MODE_KEEP_MARKED_ATOM_COORDS : 0).invent(mFragment[fragment]);
            mFragment[fragment].setStereoBondsFromParity();
        }
        Depictor d = new Depictor(mFragment[fragment]);
        d.updateCoords(g, null, AbstractDepictor.cModeInflateToMaxAVBL);
        boundingRect[fragment] = d.getBoundingRect();
    // fragmentWidth += boundingRect[fragment].width;
    }
    double spacing = FRAGMENT_CLEANUP_DISTANCE * AbstractDepictor.cOptAvBondLen;
    double avbl = mMol.getAverageBondLength();
    double arrowWidth = ((mMode & MODE_REACTION) == 0) ? 0f : (mUpdateMode == UPDATE_SCALE_COORDS_USE_FRAGMENTS) ? DEFAULT_ARROW_LENGTH * getWidth() : ((ReactionArrow) mDrawingObjectList.get(0)).getLength() * AbstractDepictor.cOptAvBondLen / avbl;
    double rawX = 0.5 * spacing;
    for (int fragment = 0; fragment <= mFragment.length; fragment++) {
        if ((mMode & MODE_REACTION) != 0 && fragment == mReactantCount) {
            ((ReactionArrow) mDrawingObjectList.get(0)).setCoordinates(rawX - spacing / 2, getHeight() / 2, rawX - spacing / 2 + arrowWidth, getHeight() / 2);
            rawX += arrowWidth;
        }
        if (fragment == mFragment.length) {
            break;
        }
        double dx = rawX - boundingRect[fragment].x;
        double dy = 0.5 * (getHeight() - boundingRect[fragment].height) - boundingRect[fragment].y;
        mFragment[fragment].translateCoords(dx, dy);
        rawX += spacing + boundingRect[fragment].width;
    }
    depictor.updateCoords(g, new Rectangle2D.Double(0, 0, getWidth(), getHeight()), maxUpdateMode());
    int[] fragmentAtom = new int[mFragment.length];
    for (int atom = 0; atom < mMol.getAllAtoms(); atom++) {
        int fragment = mFragmentNo[atom];
        mMol.setAtomX(atom, mFragment[fragment].getAtomX(fragmentAtom[fragment]));
        mMol.setAtomY(atom, mFragment[fragment].getAtomY(fragmentAtom[fragment]));
        fragmentAtom[fragment]++;
    }
    mMol.setStereoBondsFromParity();
}
Also used : CoordinateInventor(com.actelion.research.chem.coords.CoordinateInventor) Rectangle2D(java.awt.geom.Rectangle2D) ReactionArrow(com.actelion.research.chem.reaction.ReactionArrow)

Example 2 with ReactionArrow

use of com.actelion.research.chem.reaction.ReactionArrow in project openchemlib by Actelion.

the class JDrawArea method copy.

private void copy() {
    boolean isReaction = ((mMode & MODE_REACTION) != 0);
    boolean selectionFound = false;
    boolean isBothSideSelection = false;
    boolean isOnProductSide = false;
    ReactionArrow arrow = null;
    for (int atom = 0; atom < mMol.getAllAtoms(); atom++) {
        if (mMol.isSelectedAtom(atom)) {
            if (!selectionFound) {
                selectionFound = true;
                if (!isReaction) {
                    break;
                }
                arrow = (ReactionArrow) mDrawingObjectList.get(0);
                isOnProductSide = arrow.isOnProductSide(mMol.getAtomX(atom), mMol.getAtomY(atom));
            } else {
                if (isOnProductSide != arrow.isOnProductSide(mMol.getAtomX(atom), mMol.getAtomY(atom))) {
                    isBothSideSelection = true;
                    break;
                }
            }
        }
    }
    if (isReaction) {
        if (isBothSideSelection) {
            copyReaction(true);
        } else if (selectionFound) {
            copyMolecule(true);
        } else {
            copyReaction(false);
        }
    } else {
        copyMolecule(selectionFound);
    }
}
Also used : ReactionArrow(com.actelion.research.chem.reaction.ReactionArrow)

Example 3 with ReactionArrow

use of com.actelion.research.chem.reaction.ReactionArrow in project openchemlib by Actelion.

the class JDrawArea method paintComponent.

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
    Dimension theSize = getSize();
    if (mSize == null || mSize.width != theSize.width || mSize.height != theSize.height) {
        mSize = theSize;
        if (mUpdateMode < UPDATE_CHECK_COORDS) {
            mUpdateMode = UPDATE_CHECK_COORDS;
        }
    }
    Color background = UIManager.getColor("TextArea.background");
    Color foreground = UIManager.getColor("TextArea.foreground");
    g.setColor(background);
    g.fillRect(0, 0, theSize.width, theSize.height);
    if ((mMode & MODE_REACTION) != 0 && mDrawingObjectList.size() == 0) {
        float mx = 0.5f * (float) theSize.width;
        float my = 0.5f * (float) theSize.height;
        float dx = 0.5f * DEFAULT_ARROW_LENGTH * (float) theSize.width;
        ReactionArrow arrow = new ReactionArrow();
        arrow.setCoordinates(mx - dx, my, mx + dx, my);
        arrow.setDeletable(false);
        mDrawingObjectList.add(arrow);
    }
    boolean isScaledView = false;
    if (mUpdateMode != UPDATE_NONE) {
        if ((mMode & MODE_MULTIPLE_FRAGMENTS) != 0 && mUpdateMode != UPDATE_SCALE_COORDS_USE_FRAGMENTS) {
            analyzeFragmentMembership();
        }
        mDepictor = ((mMode & MODE_REACTION) != 0) ? new ExtendedDepictor(new Reaction(mFragment, mReactantCount), mDrawingObjectList, false, USE_GRAPHICS_2D) : ((mMode & MODE_MARKUSH_STRUCTURE) != 0) ? new ExtendedDepictor(mFragment, mReactantCount, mDrawingObjectList, USE_GRAPHICS_2D) : ((mMode & MODE_MULTIPLE_FRAGMENTS) != 0) ? new ExtendedDepictor(mFragment, mDrawingObjectList, USE_GRAPHICS_2D) : new ExtendedDepictor(mMol, mDrawingObjectList, USE_GRAPHICS_2D);
        mDepictor.setForegroundColor(foreground, background);
        mDepictor.setFragmentNoColor(((mMode & MODE_MULTIPLE_FRAGMENTS) == 0) ? null : LookAndFeelHelper.isDarkLookAndFeel() ? ColorHelper.brighter(background, 0.85f) : ColorHelper.darker(background, 0.85f));
        mDepictor.setDisplayMode(mDisplayMode | AbstractDepictor.cDModeHiliteAllQueryFeatures | ((mCurrentTool == JDrawToolbar.cToolMapper) ? AbstractDepictor.cDModeShowMapping | AbstractDepictor.cDModeSuppressCIPParity : 0));
        if ((mMode & (MODE_REACTION | MODE_MARKUSH_STRUCTURE | MODE_MULTIPLE_FRAGMENTS)) == 0) {
            mDepictor.getMoleculeDepictor(0).setAtomText(mAtomText);
        }
        switch(mUpdateMode) {
            case UPDATE_INVENT_COORDS:
            case UPDATE_SCALE_COORDS:
            case UPDATE_SCALE_COORDS_USE_FRAGMENTS:
                cleanupCoordinates(g, mDepictor);
                break;
            case UPDATE_CHECK_COORDS:
                DepictorTransformation t1 = mDepictor.updateCoords(g, new Rectangle2D.Double(0, 0, theSize.width, theSize.height), 0);
                if (t1 != null && (mMode & MODE_MULTIPLE_FRAGMENTS) != 0) {
                    // in fragment mode depictor transforms mFragment[] rather than mMol
                    t1.applyTo(mMol);
                }
                break;
            case UPDATE_CHECK_VIEW:
                DepictorTransformation t2 = mDepictor.validateView(g, new Rectangle2D.Double(0, 0, theSize.width, theSize.height), 0);
                isScaledView = (t2 != null && !t2.isVoidTransformation());
                break;
        }
        mUpdateMode = UPDATE_NONE;
    }
    if (mDepictor != null) {
        mDepictor.paintFragmentNumbers(g);
    }
    // don't hilite anything when the view is scaled and object coords don't reflect screen coords
    if (!isScaledView) {
        drawHiliting(g);
    }
    if (mDepictor != null) {
        mDepictor.paintStructures(g);
        mDepictor.paintDrawingObjects(g);
    }
    if (mCurrentHiliteAtom != -1 && mAtomKeyStrokeBuffer.length() != 0) {
        int x = (int) mMol.getAtomX(mCurrentHiliteAtom);
        int y = (int) mMol.getAtomY(mCurrentHiliteAtom);
        String s = mAtomKeyStrokeBuffer.toString();
        int validity = getAtomKeyStrokeValidity(s);
        g.setColor((validity == KEY_IS_ATOM_LABEL) ? foreground : (validity == KEY_IS_SUBSTITUENT) ? Color.BLUE : (validity == KEY_IS_VALID_START) ? Color.GRAY : Color.RED);
        if (validity == KEY_IS_INVALID)
            s = s + "<unknown>";
        g.setFont(g.getFont().deriveFont(0, 24));
        g.drawString(s, x, y);
    }
    g.setColor(foreground);
    switch(mPendingRequest) {
        case cRequestNewBond:
            int x1, y1, x2, y2, xdiff, ydiff;
            x1 = (int) mX1;
            y1 = (int) mY1;
            if (mCurrentHiliteAtom == -1 || mCurrentHiliteAtom == mAtom1) {
                x2 = (int) mX2;
                y2 = (int) mY2;
            } else {
                x2 = (int) mMol.getAtomX(mCurrentHiliteAtom);
                y2 = (int) mMol.getAtomY(mCurrentHiliteAtom);
            }
            switch(mCurrentTool) {
                case JDrawToolbar.cToolStdBond:
                    g.drawLine(x1, y1, x2, y2);
                    break;
                case JDrawToolbar.cToolUpBond:
                    int[] x = new int[3];
                    int[] y = new int[3];
                    xdiff = (y1 - y2) / 9;
                    ydiff = (x2 - x1) / 9;
                    x[0] = x1;
                    y[0] = y1;
                    x[1] = x2 + xdiff;
                    y[1] = y2 + ydiff;
                    x[2] = x2 - xdiff;
                    y[2] = y2 - ydiff;
                    g.fillPolygon(x, y, 3);
                    break;
                case JDrawToolbar.cToolDownBond:
                    int xx1, xx2, yy1, yy2;
                    xdiff = x2 - x1;
                    ydiff = y2 - y1;
                    for (int i = 2; i < 17; i += 2) {
                        xx1 = x1 + i * xdiff / 17 - i * ydiff / 128;
                        yy1 = y1 + i * ydiff / 17 + i * xdiff / 128;
                        xx2 = x1 + i * xdiff / 17 + i * ydiff / 128;
                        yy2 = y1 + i * ydiff / 17 - i * xdiff / 128;
                        g.drawLine(xx1, yy1, xx2, yy2);
                    }
                    break;
            }
            break;
        case cRequestNewChain:
            if (mChainAtoms > 0) {
                g.drawLine((int) mX1, (int) mY1, (int) mChainAtomX[0], (int) mChainAtomY[0]);
            }
            if (mChainAtoms > 1) {
                for (int i = 1; i < mChainAtoms; i++) {
                    g.drawLine((int) mChainAtomX[i - 1], (int) mChainAtomY[i - 1], (int) mChainAtomX[i], (int) mChainAtomY[i]);
                }
            }
            break;
        case cRequestLassoSelect:
            g.setColor(lassoColor());
            g.drawPolygon(mLassoRegion);
            g.setColor(foreground);
            break;
        case cRequestSelectRect:
            int x = (mX1 < mX2) ? (int) mX1 : (int) mX2;
            int y = (mY1 < mY2) ? (int) mY1 : (int) mY2;
            int w = (int) Math.abs(mX2 - mX1);
            int h = (int) Math.abs(mY2 - mY1);
            g.setColor(lassoColor());
            g.drawRect(x, y, w, h);
            g.setColor(foreground);
            break;
        case cRequestMapAtoms:
            x1 = (int) mX1;
            y1 = (int) mY1;
            if (mCurrentHiliteAtom == -1 || mCurrentHiliteAtom == mAtom1) {
                x2 = (int) mX2;
                y2 = (int) mY2;
            } else {
                x2 = (int) mMol.getAtomX(mCurrentHiliteAtom);
                y2 = (int) mMol.getAtomY(mCurrentHiliteAtom);
            }
            g.setColor(mapToolColor());
            g.drawLine(x1, y1, x2, y2);
            g.setColor(foreground);
            break;
    }
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) ReactionArrow(com.actelion.research.chem.reaction.ReactionArrow) Reaction(com.actelion.research.chem.reaction.Reaction)

Example 4 with ReactionArrow

use of com.actelion.research.chem.reaction.ReactionArrow in project openchemlib by Actelion.

the class JDrawArea method sortFragmentsByPosition.

private void sortFragmentsByPosition(int[] fragmentNo, int fragments) {
    int[][] fragmentDescriptor = new int[fragments][((mMode & (MODE_REACTION | MODE_MARKUSH_STRUCTURE)) != 0) ? 2 : 1];
    for (int fragment = 0; fragment < fragments; fragment++) {
        fragmentDescriptor[fragment][0] = fragment;
    }
    Point[] fragmentCOG = calculateFragmentCenterOfGravity(fragmentNo, fragments);
    if ((mMode & MODE_REACTION) != 0) {
        mReactantCount = 0;
        ReactionArrow arrow = ((mMode & MODE_REACTION) != 0) ? (ReactionArrow) mDrawingObjectList.get(0) : null;
        for (int fragment = 0; fragment < fragments; fragment++) {
            fragmentDescriptor[fragment][1] = (arrow.isOnProductSide(fragmentCOG[fragment].x, fragmentCOG[fragment].y)) ? 1 : 0;
            if (fragmentDescriptor[fragment][1] == 0) {
                mReactantCount++;
            }
        }
    } else if ((mMode & MODE_MARKUSH_STRUCTURE) != 0) {
        mReactantCount = fragments;
        for (int atom = 0; atom < mMol.getAllAtoms(); atom++) {
            if (mMol.getAtomicNo(atom) == 0 && fragmentDescriptor[fragmentNo[atom]][1] == 0) {
                fragmentDescriptor[fragmentNo[atom]][1] = 1;
                mReactantCount--;
            }
        }
    }
    final Point[] cog = fragmentCOG;
    Arrays.sort(fragmentDescriptor, new Comparator<int[]>() {

        public int compare(int[] fragmentDescriptor1, int[] fragmentDescriptor2) {
            if ((mMode & (MODE_REACTION | MODE_MARKUSH_STRUCTURE)) != 0) {
                if (fragmentDescriptor1[1] != fragmentDescriptor2[1]) {
                    return (fragmentDescriptor1[1] == 0) ? -1 : 1;
                }
            }
            return (cog[fragmentDescriptor1[0]].x + cog[fragmentDescriptor1[0]].y < cog[fragmentDescriptor2[0]].x + cog[fragmentDescriptor2[0]].y) ? -1 : 1;
        }
    });
    int[] newFragmentIndex = new int[fragments];
    Point[] centerOfGravity = new Point[fragments];
    for (int fragment = 0; fragment < fragments; fragment++) {
        int oldIndex = ((int[]) fragmentDescriptor[fragment])[0];
        newFragmentIndex[oldIndex] = fragment;
        centerOfGravity[fragment] = fragmentCOG[oldIndex];
    }
    fragmentCOG = centerOfGravity;
    for (int atom1 = 0; atom1 < mMol.getAllAtoms(); atom1++) {
        fragmentNo[atom1] = newFragmentIndex[fragmentNo[atom1]];
    }
}
Also used : ReactionArrow(com.actelion.research.chem.reaction.ReactionArrow)

Example 5 with ReactionArrow

use of com.actelion.research.chem.reaction.ReactionArrow in project openchemlib by Actelion.

the class DrawingObjectFactory method createObject.

public static AbstractDrawingObject createObject(String descriptor) {
    final String START = AbstractDrawingObject.DESCRIPTOR_START + AbstractDrawingObject.DESCRIPTOR_TYPE;
    if (!descriptor.startsWith(START) || !descriptor.endsWith(AbstractDrawingObject.DESCRIPTOR_END))
        return null;
    int index = descriptor.indexOf('\"', START.length());
    if (index == -1)
        return null;
    final String type = descriptor.substring(START.length(), index);
    final String detail = descriptor.substring(START.length() + type.length() + 1, descriptor.length() - AbstractDrawingObject.DESCRIPTOR_END.length());
    if (type.equals(ReactionArrow.TYPE_STRING))
        return new ReactionArrow(detail);
    if (type.equals(TextDrawingObject.TYPE_STRING))
        return new TextDrawingObject(detail);
    return null;
}
Also used : ReactionArrow(com.actelion.research.chem.reaction.ReactionArrow)

Aggregations

ReactionArrow (com.actelion.research.chem.reaction.ReactionArrow)6 Rectangle2D (java.awt.geom.Rectangle2D)2 CoordinateInventor (com.actelion.research.chem.coords.CoordinateInventor)1 Reaction (com.actelion.research.chem.reaction.Reaction)1