Search in sources :

Example 61 with SpeciesPattern

use of org.vcell.model.rbm.SpeciesPattern in project vcell by virtualcell.

the class ReactionRuleEditorPropertiesPanel method manageComponentPatternFromShape.

public void manageComponentPatternFromShape(final Object selectedObject, PointLocationInShapeContext locationContext, final ReactionRulePropertiesTreeModel treeModel, ShowWhat showWhat, boolean bIsReactant) {
    popupFromShapeMenu.removeAll();
    final MolecularComponentPattern mcp = (MolecularComponentPattern) selectedObject;
    final MolecularComponent mc = mcp.getMolecularComponent();
    boolean anyStateProhibited = false;
    boolean explicitStateProhibited = false;
    boolean existBondProhibited = false;
    boolean noneBondProhibited = false;
    boolean possibleBondProhibited = false;
    boolean specifiedBondProhibited = false;
    if (!bIsReactant) {
        // product has restrictions for states and bonds, depending on reactant
        BondType reactantComponentBondType = reactionRule.getReactantComponentBondType(mcp);
        if (reactantComponentBondType != null && reactantComponentBondType == BondType.Exists) {
            // has external		+
            // existBondProhibited = true;
            noneBondProhibited = true;
            possibleBondProhibited = true;
            specifiedBondProhibited = true;
        } else if (reactantComponentBondType != null && reactantComponentBondType == BondType.None) {
            // is unbound		-
            existBondProhibited = true;
            // noneBondProhibited = true;
            possibleBondProhibited = true;
        // specifiedBondProhibited = true;
        } else if (reactantComponentBondType != null && reactantComponentBondType == BondType.Possible) {
            // may be bound		?
            existBondProhibited = true;
            noneBondProhibited = true;
            // possibleBondProhibited = true;
            specifiedBondProhibited = true;
        } else if (reactantComponentBondType != null && reactantComponentBondType == BondType.Specified) {
            existBondProhibited = true;
            // noneBondProhibited = true;
            possibleBondProhibited = true;
        // specifiedBondProhibited = true;
        }
        // if it's null nothing is prohibited
        ComponentStatePattern reactantComponentStatePattern = reactionRule.getReactantComponentState(mcp);
        if (reactantComponentStatePattern != null && reactantComponentStatePattern.isAny()) {
            explicitStateProhibited = true;
        } else if (reactantComponentStatePattern != null && !reactantComponentStatePattern.isAny()) {
            anyStateProhibited = true;
        }
    // if reactantComponentStatePattern is null nothing is prohibited, we may not have a matching reactant for this product
    }
    // ------------------------------------------------------------------- State
    if (showWhat == ShowWhat.ShowState && mc.getComponentStateDefinitions().size() != 0) {
        String prefix = "State:  ";
        String csdCurrentName = "";
        final Map<String, String> itemMap = new LinkedHashMap<String, String>();
        if (mcp.getComponentStatePattern() == null || mcp.getComponentStatePattern().isAny()) {
            csdCurrentName = "<html>" + prefix + "<b>" + ComponentStatePattern.strAny + "</b></html>";
        } else {
            csdCurrentName = "<html>" + prefix + ComponentStatePattern.strAny + "</html>";
        }
        itemMap.put(csdCurrentName, ComponentStatePattern.strAny);
        for (final ComponentStateDefinition csd : mc.getComponentStateDefinitions()) {
            csdCurrentName = "";
            if (mcp.getComponentStatePattern() != null && !mcp.getComponentStatePattern().isAny()) {
                ComponentStateDefinition csdCurrent = mcp.getComponentStatePattern().getComponentStateDefinition();
                csdCurrentName = csdCurrent.getName();
            }
            String name = csd.getName();
            if (name.equals(csdCurrentName)) {
                // currently selected menu item is shown in bold
                name = "<html>" + prefix + "<b>" + name + "</b></html>";
            } else {
                name = "<html>" + prefix + name + "</html>";
            }
            itemMap.put(name, csd.getName());
        }
        for (String key : itemMap.keySet()) {
            JMenuItem menuItem = new JMenuItem(key);
            if (!bIsReactant) {
                String name = itemMap.get(key);
                if (name.equals(ComponentStatePattern.strAny) && anyStateProhibited) {
                    menuItem.setEnabled(false);
                } else if (!name.equals(ComponentStatePattern.strAny) && explicitStateProhibited) {
                    menuItem.setEnabled(false);
                }
            }
            popupFromShapeMenu.add(menuItem);
            menuItem.setIcon(VCellIcons.rbmComponentStateIcon);
            menuItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    String key = e.getActionCommand();
                    String name = itemMap.get(key);
                    ComponentStatePattern csp = new ComponentStatePattern();
                    if (!name.equals(ComponentStatePattern.strAny)) {
                        ComponentStateDefinition csd = mcp.getMolecularComponent().getComponentStateDefinition(name);
                        if (csd == null) {
                            throw new RuntimeException("Missing ComponentStateDefinition " + name + " for Component " + mcp.getMolecularComponent().getName());
                        }
                        csp = new ComponentStatePattern(csd);
                    }
                    mcp.setComponentStatePattern(csp);
                    if (bIsReactant) {
                        reflectStateToProduct(mcp, csp);
                        productTreeModel.populateTree();
                    }
                    treeModel.populateTree();
                    shapePanel.repaint();
                }
            });
        }
    }
    if (showWhat == ShowWhat.ShowState) {
        return;
    }
    // ------------------------------------------------------------------------------------------- Bonds
    final MolecularTypePattern mtp = locationContext.getMolecularTypePattern();
    final SpeciesPattern sp = locationContext.getSpeciesPattern();
    JMenu editBondMenu = new JMenu();
    final String specifiedString = mcp.getBondType() == BondType.Specified ? "<html><b>" + "Site bond specified" + "</b></html>" : "<html>" + "Site bond specified" + "</html>";
    editBondMenu.setText(specifiedString);
    editBondMenu.setToolTipText("Specified");
    editBondMenu.removeAll();
    editBondMenu.setEnabled(!specifiedBondProhibited);
    final Map<String, Bond> itemMap = new LinkedHashMap<String, Bond>();
    String noneString = mcp.getBondType() == BondType.None ? "<html><b>" + "Site is unbound" + "</b></html>" : "<html>" + "Site is unbound" + "</html>";
    // Site is bound
    String existsString = mcp.getBondType() == BondType.Exists ? "<html><b>" + "Site has external bond" + "</b></html>" : "<html>" + "Site has external bond" + "</html>";
    String possibleString = mcp.getBondType() == BondType.Possible ? "<html><b>" + "Site may be bound" + "</b></html>" : "<html>" + "Site may be bound" + "</html>";
    itemMap.put(noneString, null);
    itemMap.put(existsString, null);
    itemMap.put(possibleString, null);
    if (mtp != null && sp != null && !specifiedBondProhibited) {
        List<Bond> bondPartnerChoices = sp.getAllBondPartnerChoices(mtp, mc);
        for (Bond b : bondPartnerChoices) {
            // if(b.equals(mcp.getBond())) {
            // continue;	// if the mcp has a bond already we don't offer it
            // }
            int index = 0;
            if (mcp.getBondType() == BondType.Specified) {
                index = mcp.getBondId();
            } else {
                index = sp.nextBondId();
            }
            itemMap.put(b.toHtmlStringLong(sp, mtp, mc, index), b);
        // itemMap.put(b.toHtmlStringLong(sp, index), b);
        }
    }
    int index = 0;
    Graphics gc = shapePanel.getGraphics();
    for (String name : itemMap.keySet()) {
        JMenuItem menuItem = new JMenuItem(name);
        if (!bIsReactant) {
            if (name.equals(noneString) && noneBondProhibited) {
                menuItem.setEnabled(false);
            } else if (name.equals(existsString) && existBondProhibited) {
                menuItem.setEnabled(false);
            } else if (name.equals(possibleString) && possibleBondProhibited) {
                menuItem.setEnabled(false);
            } else if (!name.equals(noneString) && !name.equals(existsString) && !name.equals(possibleString) && specifiedBondProhibited) {
                menuItem.setEnabled(false);
            }
        }
        if (index == 0) {
            menuItem.setIcon(VCellIcons.rbmBondNoneIcon);
            menuItem.setToolTipText("None");
            popupFromShapeMenu.add(menuItem);
        } else if (index == 1) {
            menuItem.setIcon(VCellIcons.rbmBondExistsIcon);
            menuItem.setToolTipText("Exists");
            popupFromShapeMenu.add(menuItem);
        } else if (index == 2) {
            menuItem.setIcon(VCellIcons.rbmBondPossibleIcon);
            menuItem.setToolTipText("Possible");
            popupFromShapeMenu.add(menuItem);
        } else if (index > 2) {
            // we skip None, Exists, Possible
            Bond b = itemMap.get(name);
            // clone of the sp, with only the bond of interest
            SpeciesPattern spBond = new SpeciesPattern(bioModel.getModel(), sp);
            spBond.resetBonds();
            spBond.resetStates();
            MolecularTypePattern mtpFrom = spBond.getMolecularTypePattern(mtp.getMolecularType().getName(), mtp.getIndex());
            MolecularComponentPattern mcpFrom = mtpFrom.getMolecularComponentPattern(mc);
            MolecularTypePattern mtpTo = spBond.getMolecularTypePattern(b.molecularTypePattern.getMolecularType().getName(), b.molecularTypePattern.getIndex());
            MolecularComponentPattern mcpTo = mtpTo.getMolecularComponentPattern(b.molecularComponentPattern.getMolecularComponent());
            spBond.setBond(mtpTo, mcpTo, mtpFrom, mcpFrom);
            Icon icon = new SpeciesPatternSmallShape(3, 4, spBond, gc, reactionRule, false, issueManager);
            ((SpeciesPatternSmallShape) icon).setDisplayRequirements(DisplayRequirements.highlightBonds);
            menuItem.setIcon(icon);
            editBondMenu.add(menuItem);
        }
        menuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String name = e.getActionCommand();
                BondType btBefore = mcp.getBondType();
                if (name.equals(noneString)) {
                    if (btBefore == BondType.Specified) {
                        // specified -> not specified
                        // change the partner to possible
                        mcp.getBond().molecularComponentPattern.setBondType(BondType.Possible);
                        mcp.getBond().molecularComponentPattern.setBond(null);
                    }
                    mcp.setBondType(BondType.None);
                    mcp.setBond(null);
                } else if (name.equals(existsString)) {
                    if (btBefore == BondType.Specified) {
                        // specified -> exists
                        // change the partner to possible
                        mcp.getBond().molecularComponentPattern.setBondType(BondType.Possible);
                        mcp.getBond().molecularComponentPattern.setBond(null);
                    }
                    mcp.setBondType(BondType.Exists);
                    mcp.setBond(null);
                } else if (name.equals(possibleString)) {
                    if (btBefore == BondType.Specified) {
                        // specified -> possible
                        // change the partner to possible
                        mcp.getBond().molecularComponentPattern.setBondType(BondType.Possible);
                        mcp.getBond().molecularComponentPattern.setBond(null);
                    }
                    mcp.setBondType(BondType.Possible);
                    mcp.setBond(null);
                } else {
                    if (btBefore != BondType.Specified) {
                        // if we go from a non-specified to a specified we need to find the next available
                        // bond id, so that we can choose the color for displaying the bond
                        // a bad bond id, like -1, will crash badly when trying to choose the color
                        int bondId = sp.nextBondId();
                        mcp.setBondId(bondId);
                    } else {
                        // specified -> specified
                        // change the old partner to possible, continue using the bond id
                        mcp.getBond().molecularComponentPattern.setBondType(BondType.Possible);
                        mcp.getBond().molecularComponentPattern.setBond(null);
                    }
                    mcp.setBondType(BondType.Specified);
                    Bond b = itemMap.get(name);
                    mcp.setBond(b);
                    mcp.getBond().molecularComponentPattern.setBondId(mcp.getBondId());
                    sp.resolveBonds();
                }
                // when the tree will be gone
                if (bIsReactant) {
                    reflectBondToProduct(mcp);
                    productTreeModel.populateTree();
                }
                treeModel.populateTree();
                shapePanel.repaint();
            }
        });
        index++;
    }
    popupFromShapeMenu.add(editBondMenu);
}
Also used : BondType(org.vcell.model.rbm.MolecularComponentPattern.BondType) MolecularComponentPattern(org.vcell.model.rbm.MolecularComponentPattern) SpeciesPatternSmallShape(cbit.vcell.graph.SpeciesPatternSmallShape) ActionEvent(java.awt.event.ActionEvent) ComponentStatePattern(org.vcell.model.rbm.ComponentStatePattern) SpeciesPattern(org.vcell.model.rbm.SpeciesPattern) Point(java.awt.Point) LinkedHashMap(java.util.LinkedHashMap) ComponentStateDefinition(org.vcell.model.rbm.ComponentStateDefinition) Graphics(java.awt.Graphics) ActionListener(java.awt.event.ActionListener) MolecularComponent(org.vcell.model.rbm.MolecularComponent) Icon(javax.swing.Icon) ZoomShapeIcon(cbit.vcell.graph.gui.ZoomShapeIcon) JMenuItem(javax.swing.JMenuItem) MolecularTypePattern(org.vcell.model.rbm.MolecularTypePattern) Bond(org.vcell.model.rbm.SpeciesPattern.Bond) JMenu(javax.swing.JMenu)

Example 62 with SpeciesPattern

use of org.vcell.model.rbm.SpeciesPattern in project vcell by virtualcell.

the class ReactionRuleEditorPropertiesPanel method showPopupMenu.

private void showPopupMenu(MouseEvent e, PointLocationInShapeContext locationContext) {
    if (popupFromShapeMenu == null) {
        popupFromShapeMenu = new JPopupMenu();
    }
    if (popupFromShapeMenu.isShowing()) {
        return;
    }
    popupFromShapeMenu.removeAll();
    Point mousePoint = e.getPoint();
    final Object deepestShape = locationContext.getDeepestShape();
    final RbmObject selectedObject;
    if (deepestShape == null) {
        selectedObject = null;
        // when cursor is outside any species pattern we offer to add a new one
        System.out.println("outside");
    // popupFromShapeMenu.add(getAddSpeciesPatternFromShapeMenuItem());
    } else if (deepestShape instanceof ComponentStateLargeShape) {
        System.out.println("inside state");
        if (((ComponentStateLargeShape) deepestShape).isHighlighted()) {
            selectedObject = ((ComponentStateLargeShape) deepestShape).getComponentStatePattern();
        } else {
            return;
        }
    } else if (deepestShape instanceof MolecularComponentLargeShape) {
        System.out.println("inside component");
        if (((MolecularComponentLargeShape) deepestShape).isHighlighted()) {
            selectedObject = ((MolecularComponentLargeShape) deepestShape).getMolecularComponentPattern();
        } else {
            return;
        }
    } else if (deepestShape instanceof MolecularTypeLargeShape) {
        System.out.println("inside molecule");
        if (((MolecularTypeLargeShape) deepestShape).isHighlighted()) {
            selectedObject = ((MolecularTypeLargeShape) deepestShape).getMolecularTypePattern();
        } else {
            return;
        }
    } else if (deepestShape instanceof SpeciesPatternLargeShape) {
        System.out.println("inside species pattern");
        if (((SpeciesPatternLargeShape) deepestShape).isHighlighted()) {
            selectedObject = ((SpeciesPatternLargeShape) deepestShape).getSpeciesPattern();
        } else {
            return;
        }
    } else if (deepestShape instanceof ReactionRulePatternLargeShape) {
        System.out.println("inside reactant line or products line");
        if (((ReactionRulePatternLargeShape) deepestShape).isHighlighted()) {
            selectedObject = ((ReactionRulePatternLargeShape) deepestShape).getReactionRule();
        } else {
            return;
        }
    } else {
        selectedObject = null;
        System.out.println("inside something else?");
        return;
    }
    boolean bReactantsZone = false;
    int xExtent = SpeciesPatternLargeShape.calculateXExtent(shapePanel);
    Rectangle2D reactantRectangle = new Rectangle2D.Double(xOffsetInitial - xExtent, yOffsetReactantInitial - 3, 3000, 80 - 2 + GraphConstants.ReactionRuleDisplay_ReservedSpaceForNameOnYAxis);
    Rectangle2D productRectangle = new Rectangle2D.Double(xOffsetInitial - xExtent, yOffsetProductInitial - 3, 3000, 80 - 2 + GraphConstants.ReactionRuleDisplay_ReservedSpaceForNameOnYAxis);
    if (locationContext.isInside(reactantRectangle)) {
        // clicked inside the reactant rectangle (above yOffsetProductInitial)
        bReactantsZone = true;
    } else if (locationContext.isInside(productRectangle)) {
        // clicked inside the product rectangle (below yOffsetProductInitial)
        bReactantsZone = false;
    } else {
        return;
    }
    // -------------------------------- reactant zone --------------------------------------------------------
    if (bReactantsZone) {
        if (selectedObject == null) {
            return;
        } else if (selectedObject instanceof ReactionRule) {
            // add reactant pattern
            JMenuItem addMenuItem = new JMenuItem("Add Reactant");
            addMenuItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    reactionRule.addReactant(new ReactantPattern(new SpeciesPattern(), reactionRule.getStructure()));
                    shapePanel.repaint();
                }
            });
            popupFromShapeMenu.add(addMenuItem);
        } else if (selectedObject instanceof SpeciesPattern) {
            // delete (pattern) / specify molecule
            final SpeciesPattern sp = (SpeciesPattern) selectedObject;
            JMenuItem deleteMenuItem = new JMenuItem("Delete");
            deleteMenuItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    for (ReactantPattern rp : reactionRule.getReactantPatterns()) {
                        if (rp.getSpeciesPattern() == sp) {
                            reactionRule.removeReactant(rp);
                            Structure st = rp.getStructure();
                            if (reactionRule.getReactantPatterns().isEmpty()) {
                                reactionRule.addReactant(new ReactantPattern(new SpeciesPattern(), st));
                                shapePanel.repaint();
                            }
                        }
                    }
                }
            });
            popupFromShapeMenu.add(deleteMenuItem);
            JMenu addMenuItem = new JMenu(VCellErrorMessages.SpecifyMolecularTypes);
            popupFromShapeMenu.add(addMenuItem);
            addMenuItem.removeAll();
            for (final MolecularType mt : bioModel.getModel().getRbmModelContainer().getMolecularTypeList()) {
                JMenuItem menuItem = new JMenuItem(mt.getName());
                Graphics gc = shapePanel.getGraphics();
                Icon icon = new MolecularTypeSmallShape(1, 4, mt, null, gc, mt, null, issueManager);
                menuItem.setIcon(icon);
                addMenuItem.add(menuItem);
                menuItem.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        MolecularTypePattern molecularTypePattern = new MolecularTypePattern(mt);
                        for (MolecularComponentPattern mcp : molecularTypePattern.getComponentPatternList()) {
                            mcp.setBondType(BondType.Possible);
                        }
                        sp.addMolecularTypePattern(molecularTypePattern);
                        shapePanel.repaint();
                    }
                });
            }
            JMenu compartmentMenuItem = new JMenu("Specify structure");
            popupFromShapeMenu.add(compartmentMenuItem);
            if (sp.getMolecularTypePatterns().isEmpty()) {
                compartmentMenuItem.setEnabled(false);
            }
            compartmentMenuItem.removeAll();
            for (final Structure struct : bioModel.getModel().getStructures()) {
                JMenuItem menuItem = new JMenuItem(struct.getName());
                compartmentMenuItem.add(menuItem);
                for (MolecularTypePattern mtp : sp.getMolecularTypePatterns()) {
                    MolecularType mt = mtp.getMolecularType();
                    if (mt.isAnchorAll()) {
                        // no restrictions (no anchor exclusion) for this molecular type
                        continue;
                    }
                    if (!mt.getAnchors().contains(struct)) {
                        // sp can't be in this struct if any of its molecules is excluded (not anchored)
                        menuItem.setEnabled(false);
                        break;
                    }
                }
                menuItem.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        String nameStruct = e.getActionCommand();
                        Structure struct = bioModel.getModel().getStructure(nameStruct);
                        ReactantPattern rp = reactionRule.getReactantPattern(sp);
                        rp.setStructure(struct);
                        productTreeModel.populateTree();
                        shapePanel.repaint();
                    }
                });
            }
        } else if (selectedObject instanceof MolecularTypePattern) {
            // move left / right / delete molecule / reassign match
            MolecularTypePattern mtp = (MolecularTypePattern) selectedObject;
            int numMtp = locationContext.sps.getSpeciesPattern().getMolecularTypePatterns().size();
            String moveRightMenuText = "Move <b>" + "right" + "</b>";
            moveRightMenuText = "<html>" + moveRightMenuText + "</html>";
            JMenuItem moveRightMenuItem = new JMenuItem(moveRightMenuText);
            Icon icon = VCellIcons.moveRightIcon;
            moveRightMenuItem.setIcon(icon);
            moveRightMenuItem.setEnabled(numMtp < 2 ? false : true);
            moveRightMenuItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    MolecularTypePattern from = (MolecularTypePattern) selectedObject;
                    SpeciesPattern sp = locationContext.sps.getSpeciesPattern();
                    sp.shiftRight(from);
                    reactantTreeModel.populateTree();
                    productTreeModel.populateTree();
                    shapePanel.repaint();
                }
            });
            popupFromShapeMenu.add(moveRightMenuItem);
            String moveLeftMenuText = "Move <b>" + "left" + "</b>";
            moveLeftMenuText = "<html>" + moveLeftMenuText + "</html>";
            JMenuItem moveLeftMenuItem = new JMenuItem(moveLeftMenuText);
            icon = VCellIcons.moveLeftIcon;
            moveLeftMenuItem.setIcon(icon);
            moveLeftMenuItem.setEnabled(numMtp < 2 ? false : true);
            moveLeftMenuItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    MolecularTypePattern from = (MolecularTypePattern) selectedObject;
                    SpeciesPattern sp = locationContext.sps.getSpeciesPattern();
                    sp.shiftLeft(from);
                    reactantTreeModel.populateTree();
                    productTreeModel.populateTree();
                    shapePanel.repaint();
                }
            });
            popupFromShapeMenu.add(moveLeftMenuItem);
            popupFromShapeMenu.add(new JSeparator());
            String deleteMenuText = "Delete <b>" + mtp.getMolecularType().getName() + "</b>";
            deleteMenuText = "<html>" + deleteMenuText + "</html>";
            JMenuItem deleteMenuItem = new JMenuItem(deleteMenuText);
            deleteMenuItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    MolecularTypePattern mtp = (MolecularTypePattern) selectedObject;
                    SpeciesPattern sp = locationContext.sps.getSpeciesPattern();
                    sp.removeMolecularTypePattern(mtp);
                    shapePanel.repaint();
                }
            });
            popupFromShapeMenu.add(deleteMenuItem);
            if (mtp.hasExplicitParticipantMatch()) {
                String newKey = mtp.getParticipantMatchLabel();
                List<String> keyCandidates = new ArrayList<String>();
                List<MolecularTypePattern> mtpReactantList = reactionRule.populateMaps(mtp.getMolecularType(), ReactionRuleParticipantType.Reactant);
                List<MolecularTypePattern> mtpProductList = reactionRule.populateMaps(mtp.getMolecularType(), ReactionRuleParticipantType.Product);
                for (MolecularTypePattern mtpCandidate : mtpReactantList) {
                    // we can look for indexes in any list, we should find the same
                    if (mtpCandidate.hasExplicitParticipantMatch() && !mtpCandidate.getParticipantMatchLabel().equals(newKey)) {
                        keyCandidates.add(mtpCandidate.getParticipantMatchLabel());
                    }
                }
                if (!keyCandidates.isEmpty()) {
                    JMenu reassignMatchMenuItem = new JMenu();
                    reassignMatchMenuItem.setText("Reassign match to");
                    for (int i = 0; i < keyCandidates.size(); i++) {
                        JMenuItem menuItem = new JMenuItem(keyCandidates.get(i));
                        reassignMatchMenuItem.add(menuItem);
                        menuItem.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent e) {
                                String oldKey = e.getActionCommand();
                                MolecularTypePattern orphanReactant = reactionRule.findMatch(oldKey, mtpReactantList);
                                mtp.setParticipantMatchLabel(oldKey);
                                orphanReactant.setParticipantMatchLabel(newKey);
                                // TODO: replace the populate tree with reactantPatternShapeList.update() and productPatternShapeList.update()
                                // when the tree will be gone
                                reactantTreeModel.populateTree();
                                productTreeModel.populateTree();
                                shapePanel.repaint();
                                SwingUtilities.invokeLater(new Runnable() {

                                    public void run() {
                                        reactantShape.flash(oldKey);
                                        productShape.flash(oldKey);
                                    }
                                });
                            }
                        });
                    }
                    popupFromShapeMenu.add(reassignMatchMenuItem);
                }
            }
        } else if (selectedObject instanceof MolecularComponentPattern) {
            // edit bond / edit state
            manageComponentPatternFromShape(selectedObject, locationContext, reactantTreeModel, ShowWhat.ShowBond, bReactantsZone);
        } else if (selectedObject instanceof ComponentStatePattern) {
            // edit state
            MolecularComponentPattern mcp = ((ComponentStateLargeShape) deepestShape).getMolecularComponentPattern();
            manageComponentPatternFromShape(mcp, locationContext, reactantTreeModel, ShowWhat.ShowState, bReactantsZone);
        }
    // ---------------------------------------- product zone ---------------------------------------------
    } else if (!bReactantsZone) {
        if (selectedObject == null) {
            return;
        } else if (selectedObject instanceof ReactionRule) {
            // add product pattern
            JMenuItem addMenuItem = new JMenuItem("Add Product");
            addMenuItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    reactionRule.addProduct(new ProductPattern(new SpeciesPattern(), reactionRule.getStructure()));
                    shapePanel.repaint();
                }
            });
            popupFromShapeMenu.add(addMenuItem);
        } else if (selectedObject instanceof SpeciesPattern) {
            // delete (pattern) / specify molecule
            final SpeciesPattern sp = (SpeciesPattern) selectedObject;
            JMenuItem deleteMenuItem = new JMenuItem("Delete");
            deleteMenuItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    for (ProductPattern pp : reactionRule.getProductPatterns()) {
                        if (pp.getSpeciesPattern() == sp) {
                            reactionRule.removeProduct(pp);
                            Structure st = pp.getStructure();
                            if (reactionRule.getProductPatterns().isEmpty()) {
                                reactionRule.addProduct(new ProductPattern(new SpeciesPattern(), st));
                                shapePanel.repaint();
                            }
                        }
                    }
                }
            });
            popupFromShapeMenu.add(deleteMenuItem);
            JMenu addMenuItem = new JMenu(VCellErrorMessages.SpecifyMolecularTypes);
            popupFromShapeMenu.add(addMenuItem);
            addMenuItem.removeAll();
            for (final MolecularType mt : bioModel.getModel().getRbmModelContainer().getMolecularTypeList()) {
                JMenuItem menuItem = new JMenuItem(mt.getName());
                Graphics gc = shapePanel.getGraphics();
                Icon icon = new MolecularTypeSmallShape(1, 4, mt, null, gc, mt, null, issueManager);
                menuItem.setIcon(icon);
                addMenuItem.add(menuItem);
                menuItem.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        MolecularTypePattern molecularTypePattern = new MolecularTypePattern(mt);
                        for (MolecularComponentPattern mcp : molecularTypePattern.getComponentPatternList()) {
                            mcp.setBondType(BondType.Possible);
                        }
                        sp.addMolecularTypePattern(molecularTypePattern);
                        shapePanel.repaint();
                    }
                });
            }
            // specify structure
            JMenu compartmentMenuItem = new JMenu("Specify structure");
            popupFromShapeMenu.add(compartmentMenuItem);
            compartmentMenuItem.removeAll();
            if (sp.getMolecularTypePatterns().isEmpty()) {
                compartmentMenuItem.setEnabled(false);
            }
            for (final Structure struct : bioModel.getModel().getStructures()) {
                JMenuItem menuItem = new JMenuItem(struct.getName());
                compartmentMenuItem.add(menuItem);
                for (MolecularTypePattern mtp : sp.getMolecularTypePatterns()) {
                    MolecularType mt = mtp.getMolecularType();
                    if (mt.isAnchorAll()) {
                        // no restrictions for this molecular type
                        continue;
                    }
                    if (!mt.getAnchors().contains(struct)) {
                        // sp can't be in this struct if any of its molecules is excluded (not anchored)
                        menuItem.setEnabled(false);
                        break;
                    }
                }
                menuItem.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        String nameStruct = e.getActionCommand();
                        Structure struct = bioModel.getModel().getStructure(nameStruct);
                        ProductPattern pp = reactionRule.getProductPattern(sp);
                        pp.setStructure(struct);
                        productTreeModel.populateTree();
                        shapePanel.repaint();
                    }
                });
            }
        } else if (selectedObject instanceof MolecularTypePattern) {
            // move left / right / delete molecule / reassign match
            MolecularTypePattern mtp = (MolecularTypePattern) selectedObject;
            int numMtp = locationContext.sps.getSpeciesPattern().getMolecularTypePatterns().size();
            String moveRightMenuText = "Move <b>" + "right" + "</b>";
            moveRightMenuText = "<html>" + moveRightMenuText + "</html>";
            JMenuItem moveRightMenuItem = new JMenuItem(moveRightMenuText);
            Icon icon = VCellIcons.moveRightIcon;
            moveRightMenuItem.setIcon(icon);
            moveRightMenuItem.setEnabled(numMtp < 2 ? false : true);
            moveRightMenuItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    MolecularTypePattern from = (MolecularTypePattern) selectedObject;
                    SpeciesPattern sp = locationContext.sps.getSpeciesPattern();
                    sp.shiftRight(from);
                    reactantTreeModel.populateTree();
                    productTreeModel.populateTree();
                    shapePanel.repaint();
                }
            });
            popupFromShapeMenu.add(moveRightMenuItem);
            String moveLeftMenuText = "Move <b>" + "left" + "</b>";
            moveLeftMenuText = "<html>" + moveLeftMenuText + "</html>";
            JMenuItem moveLeftMenuItem = new JMenuItem(moveLeftMenuText);
            icon = VCellIcons.moveLeftIcon;
            moveLeftMenuItem.setIcon(icon);
            moveLeftMenuItem.setEnabled(numMtp < 2 ? false : true);
            moveLeftMenuItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    MolecularTypePattern from = (MolecularTypePattern) selectedObject;
                    SpeciesPattern sp = locationContext.sps.getSpeciesPattern();
                    sp.shiftLeft(from);
                    reactantTreeModel.populateTree();
                    productTreeModel.populateTree();
                    shapePanel.repaint();
                }
            });
            popupFromShapeMenu.add(moveLeftMenuItem);
            popupFromShapeMenu.add(new JSeparator());
            String deleteMenuText = "Delete <b>" + mtp.getMolecularType().getName() + "</b>";
            deleteMenuText = "<html>" + deleteMenuText + "</html>";
            JMenuItem deleteMenuItem = new JMenuItem(deleteMenuText);
            deleteMenuItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    MolecularTypePattern mtp = (MolecularTypePattern) selectedObject;
                    SpeciesPattern sp = locationContext.sps.getSpeciesPattern();
                    sp.removeMolecularTypePattern(mtp);
                    shapePanel.repaint();
                }
            });
            popupFromShapeMenu.add(deleteMenuItem);
            if (mtp.hasExplicitParticipantMatch()) {
                String newKey = mtp.getParticipantMatchLabel();
                List<String> keyCandidates = new ArrayList<String>();
                List<MolecularTypePattern> mtpReactantList = reactionRule.populateMaps(mtp.getMolecularType(), ReactionRuleParticipantType.Reactant);
                List<MolecularTypePattern> mtpProductList = reactionRule.populateMaps(mtp.getMolecularType(), ReactionRuleParticipantType.Product);
                for (MolecularTypePattern mtpCandidate : mtpReactantList) {
                    // we can look for indexes in any list, we should find the same
                    if (mtpCandidate.hasExplicitParticipantMatch() && !mtpCandidate.getParticipantMatchLabel().equals(newKey)) {
                        keyCandidates.add(mtpCandidate.getParticipantMatchLabel());
                    }
                }
                if (!keyCandidates.isEmpty()) {
                    JMenu reassignMatchMenuItem = new JMenu();
                    reassignMatchMenuItem.setText("Reassign match to");
                    for (int i = 0; i < keyCandidates.size(); i++) {
                        JMenuItem menuItem = new JMenuItem(keyCandidates.get(i));
                        reassignMatchMenuItem.add(menuItem);
                        menuItem.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent e) {
                                String oldKey = e.getActionCommand();
                                MolecularTypePattern orphanProduct = reactionRule.findMatch(oldKey, mtpProductList);
                                mtp.setParticipantMatchLabel(oldKey);
                                orphanProduct.setParticipantMatchLabel(newKey);
                                // TODO: replace the populate tree with reactantPatternShapeList.update() and productPatternShapeList.update()
                                // when the tree will be gone
                                reactantTreeModel.populateTree();
                                productTreeModel.populateTree();
                                shapePanel.repaint();
                                SwingUtilities.invokeLater(new Runnable() {

                                    public void run() {
                                        reactantShape.flash(oldKey);
                                        productShape.flash(oldKey);
                                    }
                                });
                            }
                        });
                    }
                    popupFromShapeMenu.add(reassignMatchMenuItem);
                }
            }
        } else if (selectedObject instanceof MolecularComponentPattern) {
            // edit bond / edit state
            manageComponentPatternFromShape(selectedObject, locationContext, productTreeModel, ShowWhat.ShowBond, bReactantsZone);
        } else if (selectedObject instanceof ComponentStatePattern) {
            // edit state
            MolecularComponentPattern mcp = ((ComponentStateLargeShape) deepestShape).getMolecularComponentPattern();
            manageComponentPatternFromShape(mcp, locationContext, productTreeModel, ShowWhat.ShowState, bReactantsZone);
        }
    }
    popupFromShapeMenu.show(e.getComponent(), mousePoint.x, mousePoint.y);
}
Also used : ActionEvent(java.awt.event.ActionEvent) ArrayList(java.util.ArrayList) SpeciesPatternLargeShape(cbit.vcell.graph.SpeciesPatternLargeShape) SpeciesPattern(org.vcell.model.rbm.SpeciesPattern) JSeparator(javax.swing.JSeparator) MolecularTypeLargeShape(cbit.vcell.graph.MolecularTypeLargeShape) List(java.util.List) ArrayList(java.util.ArrayList) JMenuItem(javax.swing.JMenuItem) Structure(cbit.vcell.model.Structure) MolecularComponentLargeShape(cbit.vcell.graph.MolecularComponentLargeShape) ReactantPattern(cbit.vcell.model.ReactantPattern) ReactionRule(cbit.vcell.model.ReactionRule) ProductPattern(cbit.vcell.model.ProductPattern) MolecularComponentPattern(org.vcell.model.rbm.MolecularComponentPattern) ComponentStatePattern(org.vcell.model.rbm.ComponentStatePattern) Rectangle2D(java.awt.geom.Rectangle2D) Point(java.awt.Point) JPopupMenu(javax.swing.JPopupMenu) Point(java.awt.Point) MolecularType(org.vcell.model.rbm.MolecularType) Graphics(java.awt.Graphics) MolecularTypeSmallShape(cbit.vcell.graph.MolecularTypeSmallShape) ActionListener(java.awt.event.ActionListener) ComponentStateLargeShape(cbit.vcell.graph.MolecularComponentLargeShape.ComponentStateLargeShape) RbmObject(org.vcell.model.rbm.RbmObject) RbmObject(org.vcell.model.rbm.RbmObject) Icon(javax.swing.Icon) ZoomShapeIcon(cbit.vcell.graph.gui.ZoomShapeIcon) MolecularTypePattern(org.vcell.model.rbm.MolecularTypePattern) ReactionRulePatternLargeShape(cbit.vcell.graph.ReactionRulePatternLargeShape) JMenu(javax.swing.JMenu)

Example 63 with SpeciesPattern

use of org.vcell.model.rbm.SpeciesPattern in project vcell by virtualcell.

the class ReactionRulePropertiesTreeModel method propertyChange.

public void propertyChange(PropertyChangeEvent evt) {
    if (evt.getPropertyName().equals(PropertyConstants.PROPERTY_NAME_NAME)) {
        nodeChanged(rootNode);
    } else if (evt.getPropertyName().equals("entityChange")) {
        nodeChanged(rootNode);
    } else if (evt.getSource() == reactionRule || evt.getSource() instanceof SpeciesPattern || evt.getSource() instanceof MolecularTypePattern) {
        populateTree();
        Object source = evt.getSource();
        if (source == reactionRule) {
            if (participantType == ReactionRuleParticipantType.Reactant && evt.getPropertyName().equals(ReactionRule.PROPERTY_NAME_REACTANT_PATTERNS)) {
                List<ReactantPattern> oldValue = (List<ReactantPattern>) evt.getOldValue();
                if (oldValue != null) {
                    for (ReactantPattern sp : oldValue) {
                        RbmUtils.removePropertyChangeListener(sp.getSpeciesPattern(), this);
                    }
                }
                List<ReactantPattern> newValue = (List<ReactantPattern>) evt.getNewValue();
                if (newValue != null) {
                    for (ReactantPattern sp : newValue) {
                        RbmUtils.addPropertyChangeListener(sp.getSpeciesPattern(), this);
                    }
                }
            } else if (participantType == ReactionRuleParticipantType.Product && evt.getPropertyName().equals(ReactionRule.PROPERTY_NAME_PRODUCT_PATTERNS)) {
                List<ProductPattern> oldValue = (List<ProductPattern>) evt.getOldValue();
                if (oldValue != null) {
                    for (ProductPattern sp : oldValue) {
                        RbmUtils.removePropertyChangeListener(sp.getSpeciesPattern(), this);
                    }
                }
                List<ProductPattern> newValue = (List<ProductPattern>) evt.getNewValue();
                if (newValue != null) {
                    for (ProductPattern sp : newValue) {
                        RbmUtils.addPropertyChangeListener(sp.getSpeciesPattern(), this);
                    }
                }
            }
        } else if (source instanceof SpeciesPattern) {
            if (evt.getPropertyName().equals(SpeciesPattern.PROPERTY_NAME_MOLECULAR_TYPE_PATTERNS)) {
                List<MolecularTypePattern> oldValue = (List<MolecularTypePattern>) evt.getOldValue();
                if (oldValue != null) {
                    for (MolecularTypePattern mtp : oldValue) {
                        RbmUtils.removePropertyChangeListener(mtp, this);
                    }
                }
                List<MolecularTypePattern> newValue = (List<MolecularTypePattern>) evt.getNewValue();
                if (newValue != null) {
                    for (MolecularTypePattern mtp : newValue) {
                        RbmUtils.addPropertyChangeListener(mtp, this);
                    }
                }
            }
        } else if (source instanceof MolecularTypePattern) {
            if (evt.getPropertyName().equals(MolecularTypePattern.PROPERTY_NAME_COMPONENT_PATTERN_LIST)) {
                List<MolecularComponentPattern> oldValue = (List<MolecularComponentPattern>) evt.getOldValue();
                if (oldValue != null) {
                    for (MolecularComponentPattern mcp : oldValue) {
                        RbmUtils.removePropertyChangeListener(mcp, this);
                    }
                }
                List<MolecularComponentPattern> newValue = (List<MolecularComponentPattern>) evt.getNewValue();
                if (newValue != null) {
                    for (MolecularComponentPattern mcp : newValue) {
                        RbmUtils.addPropertyChangeListener(mcp, this);
                    }
                }
            }
        } else if (source instanceof MolecularComponentPattern) {
            if (evt.getSource().equals(MolecularComponentPattern.PROPERTY_NAME_COMPONENT_STATE)) {
                ComponentStatePattern oldValue = (ComponentStatePattern) evt.getOldValue();
                if (oldValue != null) {
                    oldValue.removePropertyChangeListener(this);
                }
                ComponentStatePattern newValue = (ComponentStatePattern) evt.getNewValue();
                if (newValue != null) {
                    newValue.addPropertyChangeListener(this);
                }
            }
        }
    }
}
Also used : ProductPattern(cbit.vcell.model.ProductPattern) MolecularComponentPattern(org.vcell.model.rbm.MolecularComponentPattern) ComponentStatePattern(org.vcell.model.rbm.ComponentStatePattern) List(java.util.List) MolecularTypePattern(org.vcell.model.rbm.MolecularTypePattern) SpeciesPattern(org.vcell.model.rbm.SpeciesPattern) ReactantPattern(cbit.vcell.model.ReactantPattern)

Example 64 with SpeciesPattern

use of org.vcell.model.rbm.SpeciesPattern in project vcell by virtualcell.

the class SpeciesPropertiesTreeModel method setSpeciesContext.

public void setSpeciesContext(SpeciesContext newValue) {
    if (newValue == speciesContext) {
        return;
    }
    SpeciesContext oldValue = speciesContext;
    if (oldValue != null) {
        oldValue.removePropertyChangeListener(this);
        SpeciesPattern speciesPattern = oldValue.getSpeciesPattern();
        RbmUtils.removePropertyChangeListener(speciesPattern, this);
    }
    speciesContext = newValue;
    populateTree();
    if (newValue != null) {
        newValue.addPropertyChangeListener(this);
        SpeciesPattern speciesPattern = newValue.getSpeciesPattern();
        RbmUtils.addPropertyChangeListener(speciesPattern, this);
    }
}
Also used : SpeciesContext(cbit.vcell.model.SpeciesContext) SpeciesPattern(org.vcell.model.rbm.SpeciesPattern)

Example 65 with SpeciesPattern

use of org.vcell.model.rbm.SpeciesPattern in project vcell by virtualcell.

the class SpeciesPropertiesTreeModel method populateTree.

public void populateTree() {
    if (speciesContext == null || bioModel == null) {
        return;
    }
    rootNode.setUserObject(speciesContext);
    rootNode.removeAllChildren();
    int count = 0;
    if (speciesContext.getSpeciesPattern() == null) {
        nodeStructureChanged(rootNode);
        return;
    }
    SpeciesPattern sp = speciesContext.getSpeciesPattern();
    // BioModelNode spNode = new BioModelNode(new SpeciesPatternLocal(sp, ++count));
    for (MolecularTypePattern mtp : sp.getMolecularTypePatterns()) {
        BioModelNode node = createMolecularTypePatternNode(mtp);
        rootNode.add(node);
    // spNode.add(node);
    }
    // rootNode.add(spNode);
    nodeStructureChanged(rootNode);
    GuiUtils.treeExpandAllRows(ownerTree);
    // we fire a "dummy" event because the species properties panel and the bio model editor species table model
    // will repaint the shape and respectively the table row for any speciesContext property change event
    speciesContext.firePropertyChange("entityChange", null, "bbb");
}
Also used : BioModelNode(cbit.vcell.desktop.BioModelNode) MolecularTypePattern(org.vcell.model.rbm.MolecularTypePattern) SpeciesPattern(org.vcell.model.rbm.SpeciesPattern)

Aggregations

SpeciesPattern (org.vcell.model.rbm.SpeciesPattern)93 MolecularTypePattern (org.vcell.model.rbm.MolecularTypePattern)39 MolecularComponentPattern (org.vcell.model.rbm.MolecularComponentPattern)30 MolecularType (org.vcell.model.rbm.MolecularType)25 RbmObservable (cbit.vcell.model.RbmObservable)22 SpeciesContext (cbit.vcell.model.SpeciesContext)22 Structure (cbit.vcell.model.Structure)22 Point (java.awt.Point)18 ReactionRule (cbit.vcell.model.ReactionRule)16 ArrayList (java.util.ArrayList)16 ComponentStatePattern (org.vcell.model.rbm.ComponentStatePattern)16 Graphics (java.awt.Graphics)13 PropertyVetoException (java.beans.PropertyVetoException)13 SpeciesPatternLargeShape (cbit.vcell.graph.SpeciesPatternLargeShape)12 ProductPattern (cbit.vcell.model.ProductPattern)12 ReactantPattern (cbit.vcell.model.ReactantPattern)12 Dimension (java.awt.Dimension)12 ComponentStateDefinition (org.vcell.model.rbm.ComponentStateDefinition)12 Model (cbit.vcell.model.Model)11 ModelException (cbit.vcell.model.ModelException)11