Search in sources :

Example 31 with OBlock

use of jmri.jmrit.logix.OBlock in project JMRI by JMRI.

the class CircuitBuilder method checkCircuits.

/**
     * ************** end closing frames *******************
     */
/**
     * Find the blocks with no icons and the blocks with icons that need
     * conversion Setup for main Frame - used in both initialization and close
     * of an editing frame Build Lists that are used to create menu items
     */
private void checkCircuits() {
    _portalIconMap.clear();
    _darkTrack.clear();
    _unconvertedTrack.clear();
    PortalManager portalMgr = InstanceManager.getDefault(jmri.jmrit.logix.PortalManager.class);
    Iterator<Positionable> it = _editor.getContents().iterator();
    while (it.hasNext()) {
        Positionable pos = it.next();
        //            if (log.isDebugEnabled()) log.debug("class: "+pos.getClass().getName());
        if (pos instanceof IndicatorTrack) {
            OBlock block = ((IndicatorTrack) pos).getOccBlock();
            ((IndicatorTrack) pos).removePath(EditCircuitPaths.TEST_PATH);
            if (block != null) {
                addIcon(block, pos);
            } else {
                _darkTrack.add(pos);
            }
        } else if (pos instanceof PortalIcon) {
            PortalIcon pIcon = (PortalIcon) pos;
            String name = pIcon.getName();
            Portal portal = portalMgr.getByUserName(name);
            if (portal == null) {
                log.error("No Portal for PortalIcon called \"" + name + "\". Discarding icon.");
                pIcon.remove();
            } else {
                PortalIcon pi = _portalIconMap.get(name);
                if (pi != null) {
                    log.error("Removing duplicate PortalIcon for Portal \"" + name + "\".");
                    pi.remove();
                }
                _portalIconMap.put(name, pIcon);
            }
        }
    }
    Iterator<Entry<OBlock, ArrayList<Positionable>>> iters = _circuitMap.entrySet().iterator();
    while (iters.hasNext()) {
        Entry<OBlock, ArrayList<Positionable>> entry = iters.next();
        Iterator<Positionable> iter = entry.getValue().iterator();
        while (iter.hasNext()) {
            Positionable pos = iter.next();
            if (isUnconvertedTrack(pos)) {
                if (!_unconvertedTrack.contains(pos)) {
                    _unconvertedTrack.add(pos);
                }
            }
        }
    }
    _bareBlock.clear();
    _convertBlock.clear();
    _badPortalIcon.clear();
    OBlockManager manager = InstanceManager.getDefault(jmri.jmrit.logix.OBlockManager.class);
    String[] sysNames = manager.getSystemNameArray();
    hasOBlocks = (sysNames.length > 0);
    for (int i = 0; i < sysNames.length; i++) {
        OBlock block = manager.getBySystemName(sysNames[i]);
        java.util.List<Portal> list = block.getPortals();
        if (list != null) {
            Iterator<Portal> iter = list.iterator();
            while (iter.hasNext()) {
                Portal portal = iter.next();
                // update circuitMap
                PortalIcon pi = _portalIconMap.get(portal.getName());
                if (pi != null) {
                    addIcon(block, pi);
                }
            }
        }
        java.util.List<Positionable> icons = _circuitMap.get(block);
        if (log.isDebugEnabled()) {
            log.debug("checkCircuits: block " + block.getDisplayName() + " has " + icons.size() + " icons.");
        }
        if (icons == null || icons.size() == 0) {
            _bareBlock.add(block);
        } else {
            _bareBlock.remove(block);
            for (int k = 0; k < icons.size(); k++) {
                Positionable pos = icons.get(k);
                if (!(pos instanceof IndicatorTrack) && !(pos instanceof PortalIcon)) {
                    if (!_convertBlock.contains(block)) {
                        _convertBlock.add(block);
                        break;
                    }
                }
            }
        }
    }
    List<NamedBean> list = portalMgr.getNamedBeanList();
    Iterator<NamedBean> iter = list.iterator();
    while (iter.hasNext()) {
        Portal portal = (Portal) iter.next();
        String name = portal.getName();
        PortalIcon pi = _portalIconMap.get(name);
        if (pi != null) {
            if (!checkPortalIcon(portal, pi)) {
                _badPortalIcon.put(name, portal);
            }
        } else {
            // no icon for this Portal
            _badPortalIcon.put(name, portal);
        }
    }
    makeToDoMenu();
}
Also used : NamedBean(jmri.NamedBean) PortalManager(jmri.jmrit.logix.PortalManager) ArrayList(java.util.ArrayList) OBlockManager(jmri.jmrit.logix.OBlockManager) Entry(java.util.Map.Entry) IndicatorTrack(jmri.jmrit.display.IndicatorTrack) Portal(jmri.jmrit.logix.Portal) Positionable(jmri.jmrit.display.Positionable) OBlock(jmri.jmrit.logix.OBlock)

Example 32 with OBlock

use of jmri.jmrit.logix.OBlock in project JMRI by JMRI.

the class CircuitBuilder method makeMenu.

/**
     * Makes menu for ControlPanelEditor Called by ControlPanelEditor at init
     * before contents have been loaded
     */
protected JMenu makeMenu() {
    if (_circuitMenu == null) {
        _circuitMenu = new JMenu(Bundle.getMessage("CircuitBuilder"));
        _circuitMap = new HashMap<OBlock, ArrayList<Positionable>>();
        OBlockManager manager = InstanceManager.getDefault(jmri.jmrit.logix.OBlockManager.class);
        String[] sysNames = manager.getSystemNameArray();
        for (int i = 0; i < sysNames.length; i++) {
            OBlock block = manager.getBySystemName(sysNames[i]);
            _circuitMap.put(block, new ArrayList<Positionable>());
        }
    }
    makeCircuitMenu();
    return _circuitMenu;
}
Also used : ArrayList(java.util.ArrayList) OBlockManager(jmri.jmrit.logix.OBlockManager) Positionable(jmri.jmrit.display.Positionable) OBlock(jmri.jmrit.logix.OBlock) JMenu(javax.swing.JMenu)

Example 33 with OBlock

use of jmri.jmrit.logix.OBlock in project JMRI by JMRI.

the class CircuitBuilder method okToAdd.

/**
     * Can this track icon be added to the circuit? N.B. Be sure Positionable
     * pos passes isTrack() call
     */
private boolean okToAdd(Positionable pos, OBlock editBlock) {
    if (pos instanceof IndicatorTrack) {
        OBlock block = ((IndicatorTrack) pos).getOccBlock();
        if (block != null) {
            if (!block.equals(editBlock)) {
                int result = JOptionPane.showConfirmDialog(_editor, java.text.MessageFormat.format(Bundle.getMessage("iconBlockConflict"), block.getDisplayName(), editBlock.getDisplayName()), Bundle.getMessage("whichCircuit"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                if (result == JOptionPane.YES_OPTION) {
                    // move icon from block to editBlock 
                    java.util.List<Positionable> ic = _circuitMap.get(block);
                    ic.remove(pos);
                    ((IndicatorTrack) pos).setOccBlockHandle(InstanceManager.getDefault(NamedBeanHandleManager.class).getNamedBeanHandle(editBlock.getSystemName(), editBlock));
                    return true;
                }
                return false;
            }
        }
    }
    return true;
}
Also used : IndicatorTrack(jmri.jmrit.display.IndicatorTrack) Positionable(jmri.jmrit.display.Positionable) OBlock(jmri.jmrit.logix.OBlock)

Example 34 with OBlock

use of jmri.jmrit.logix.OBlock in project JMRI by JMRI.

the class CircuitBuilder method checkPortalIcon.

// end checkCircuits
private boolean checkPortalIcon(Portal portal, PortalIcon icon) {
    OBlock a = portal.getToBlock();
    OBlock b = portal.getFromBlock();
    java.util.List<Positionable> aList = _circuitMap.get(a);
    java.util.List<Positionable> bList = _circuitMap.get(b);
    if (icon == null || aList == null || bList == null) {
        return false;
    }
    boolean ok = false;
    String status = icon.getStatus();
    icon.setStatus(PortalIcon.VISIBLE);
    Rectangle homeRect = new Rectangle();
    Iterator<Positionable> iter = aList.iterator();
    while (iter.hasNext()) {
        Positionable pos = iter.next();
        if (pos instanceof PortalIcon) {
            continue;
        }
        homeRect = pos.getBounds(homeRect);
        if (iconIntersectsRect(icon, homeRect)) {
            ok = true;
            break;
        }
    }
    if (!ok) {
        icon.setStatus(status);
        return false;
    }
    ok = false;
    iter = bList.iterator();
    while (iter.hasNext()) {
        Positionable pos = iter.next();
        if (pos instanceof PortalIcon) {
            continue;
        }
        homeRect = pos.getBounds(homeRect);
        if (iconIntersectsRect(icon, homeRect)) {
            ok = true;
            break;
        }
    }
    icon.setStatus(status);
    return ok;
}
Also used : Rectangle(java.awt.Rectangle) Positionable(jmri.jmrit.display.Positionable) OBlock(jmri.jmrit.logix.OBlock)

Example 35 with OBlock

use of jmri.jmrit.logix.OBlock in project JMRI by JMRI.

the class EditPortalFrame method checkPortalIconForUpdate.

/**
     * Called after click on portal icon moved recheck block connections
     */
protected boolean checkPortalIconForUpdate(PortalIcon icon, boolean moved) {
    Portal portal = icon.getPortal();
    if (portal == null) {
        icon.remove();
        log.error("Removed PortalIcon without Portal");
        return false;
    }
    String name = portal.getDisplayName();
    String msg = null;
    OBlock fromBlock = portal.getFromBlock();
    OBlock toBlock = portal.getToBlock();
    if (!_homeBlock.equals(fromBlock) && !_homeBlock.equals(toBlock)) {
        msg = Bundle.getMessage("iconNotOnBlock", _homeBlock.getDisplayName(), portal.getDescription());
        _adjacentBlock = null;
    }
    if (msg == null) {
        msg = iconIntersectsBlock(icon, _homeBlock);
    }
    if (log.isDebugEnabled()) {
        log.debug("checkPortalIconForUpdate: _homeBlock= " + _homeBlock.getDisplayName() + " icon On _homeBlock " + (msg == null) + ".  msg = " + msg);
    }
    if (msg == null) {
        if (_homeBlock.equals(fromBlock)) {
            _adjacentBlock = toBlock;
        } else {
            _adjacentBlock = fromBlock;
        }
        if (_adjacentBlock == null) {
            // maybe first time
            _adjacentBlock = findAdjacentBlock(icon);
            if (_homeBlock.equals(fromBlock)) {
                portal.setToBlock(_adjacentBlock, false);
            } else {
                portal.setFromBlock(_adjacentBlock, false);
            }
            _portalList.dataChange();
        }
        if (moved) {
            OBlock block = findAdjacentBlock(icon);
            if (log.isDebugEnabled()) {
                log.debug("Icon also on " + (block != null ? block.getDisplayName() : "null") + ", _adjacentBlock= " + (_adjacentBlock != null ? _adjacentBlock.getDisplayName() : "null"));
            }
            if (block != null && block.equals(_adjacentBlock)) {
                _portalList.setSelectedValue(portal, true);
                // no change  in connection
                return true;
            }
        }
        if (_adjacentBlock != null) {
            msg = iconIntersectsBlock(icon, _adjacentBlock);
            if (msg == null) {
                _portalList.setSelectedValue(portal, true);
                // no change  in connection                        
                return true;
            }
        }
        if (_adjacentBlock == null) {
            // icon not on Portal blocks Find a new _adjacentBlock
            _adjacentBlock = findAdjacentBlock(icon);
            if (_adjacentBlock != null) {
                if (log.isDebugEnabled()) {
                    log.debug("Current position: _homeBlock= " + _homeBlock.getDisplayName() + " and _adjacentBlock= " + _adjacentBlock.getDisplayName());
                }
                int result = JOptionPane.showConfirmDialog(this, Bundle.getMessage("repositionPortal", name, _homeBlock.getDisplayName(), _adjacentBlock.getDisplayName()), Bundle.getMessage("makePortal"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                if (result == JOptionPane.YES_OPTION) {
                    // Change portal position to join different pair of blocks
                    if (_homeBlock.equals(fromBlock)) {
                        portal.setToBlock(_adjacentBlock, true);
                    } else {
                        portal.setFromBlock(_adjacentBlock, true);
                    }
                }
                msg = null;
            } else {
                msg = Bundle.getMessage("iconNotOnAdjacent", icon.getNameString(), _homeBlock.getDisplayName());
            }
        }
    }
    if (msg != null) {
        JOptionPane.showMessageDialog(this, msg, Bundle.getMessage("makePortal"), JOptionPane.INFORMATION_MESSAGE);
        clearListSelection();
        return false;
    }
    _portalList.setSelectedValue(portal, true);
    _portalList.dataChange();
    return true;
}
Also used : Portal(jmri.jmrit.logix.Portal) OBlock(jmri.jmrit.logix.OBlock) Point(java.awt.Point)

Aggregations

OBlock (jmri.jmrit.logix.OBlock)40 Portal (jmri.jmrit.logix.Portal)13 OBlockManager (jmri.jmrit.logix.OBlockManager)10 Element (org.jdom2.Element)10 ArrayList (java.util.ArrayList)6 Positionable (jmri.jmrit.display.Positionable)6 NamedBean (jmri.NamedBean)5 Sensor (jmri.Sensor)5 Point (java.awt.Point)4 OPath (jmri.jmrit.logix.OPath)4 PropertyVetoException (java.beans.PropertyVetoException)3 Entry (java.util.Map.Entry)3 JMenu (javax.swing.JMenu)3 Reporter (jmri.Reporter)3 Attribute (org.jdom2.Attribute)3 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 ParseException (java.text.ParseException)2 Date (java.util.Date)2 JInternalFrame (javax.swing.JInternalFrame)2