Search in sources :

Example 36 with NamedIcon

use of jmri.jmrit.catalog.NamedIcon in project JMRI by JMRI.

the class SignalHeadIconXml method store.

/**
     * Default implementation for storing the contents of a SignalHeadIcon
     *
     * @param o Object to store, of type SignalHeadIcon
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    SignalHeadIcon p = (SignalHeadIcon) o;
    if (!p.isActive()) {
        // if flagged as inactive, don't store
        return null;
    }
    Element element = new Element("signalheadicon");
    element.setAttribute("signalhead", "" + p.getNamedSignalHead().getName());
    storeCommonAttributes(p, element);
    element.setAttribute("clickmode", "" + p.getClickMode());
    element.setAttribute("litmode", "" + p.getLitMode());
    Element elem = new Element("icons");
    NamedIcon icon = p.getIcon(rbean.getString("SignalHeadStateHeld"));
    if (icon != null) {
        elem.addContent(storeIcon("held", icon));
    }
    icon = p.getIcon(rbean.getString("SignalHeadStateDark"));
    if (icon != null) {
        elem.addContent(storeIcon("dark", icon));
    }
    icon = p.getIcon(rbean.getString("SignalHeadStateRed"));
    if (icon != null) {
        elem.addContent(storeIcon("red", icon));
    }
    icon = p.getIcon(rbean.getString("SignalHeadStateYellow"));
    if (icon != null) {
        elem.addContent(storeIcon("yellow", icon));
    }
    icon = p.getIcon(rbean.getString("SignalHeadStateGreen"));
    if (icon != null) {
        elem.addContent(storeIcon("green", icon));
    }
    icon = p.getIcon(rbean.getString("SignalHeadStateLunar"));
    if (icon != null) {
        elem.addContent(storeIcon("lunar", icon));
    }
    icon = p.getIcon(rbean.getString("SignalHeadStateFlashingRed"));
    if (icon != null) {
        elem.addContent(storeIcon("flashred", icon));
    }
    icon = p.getIcon(rbean.getString("SignalHeadStateFlashingYellow"));
    if (icon != null) {
        elem.addContent(storeIcon("flashyellow", icon));
    }
    icon = p.getIcon(rbean.getString("SignalHeadStateFlashingGreen"));
    if (icon != null) {
        elem.addContent(storeIcon("flashgreen", icon));
    }
    icon = p.getIcon(rbean.getString("SignalHeadStateFlashingLunar"));
    if (icon != null) {
        elem.addContent(storeIcon("flashlunar", icon));
    }
    element.addContent(elem);
    elem = new Element("iconmaps");
    String family = p.getFamily();
    if (family != null) {
        elem.setAttribute("family", family);
    }
    element.addContent(elem);
    element.setAttribute("class", "jmri.jmrit.display.configurexml.SignalHeadIconXml");
    return element;
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) Element(org.jdom2.Element) SignalHeadIcon(jmri.jmrit.display.SignalHeadIcon)

Example 37 with NamedIcon

use of jmri.jmrit.catalog.NamedIcon in project JMRI by JMRI.

the class ControlPanelEditor method drop.

@SuppressWarnings("unchecked")
@Override
public void drop(DropTargetDropEvent evt) {
    try {
        //Point pt = evt.getLocation(); coords relative to entire window
        Point pt = _targetPanel.getMousePosition(true);
        Transferable tr = evt.getTransferable();
        if (log.isDebugEnabled()) {
            // avoid string building if not debug
            DataFlavor[] flavors = tr.getTransferDataFlavors();
            StringBuilder flavor = new StringBuilder();
            for (DataFlavor flavor1 : flavors) {
                flavor.append(flavor1.getRepresentationClass().getName()).append(", ");
            }
            log.debug("Editor Drop: flavor classes={}", flavor);
        }
        if (tr.isDataFlavorSupported(_positionableDataFlavor)) {
            Positionable item = (Positionable) tr.getTransferData(_positionableDataFlavor);
            if (item == null) {
                return;
            }
            item.setLocation(pt.x, pt.y);
            // now set display level in the pane.
            item.setDisplayLevel(item.getDisplayLevel());
            item.setEditor(this);
            putItem(item);
            item.updateSize();
            //if (_debug) log.debug("Drop positionable "+item.getNameString()+
            //                                    " as "+item.getClass().getName()+
            //                                    ", w= "+item.maxWidth()+", h= "+item.maxHeight());
            evt.dropComplete(true);
            return;
        } else if (tr.isDataFlavorSupported(_namedIconDataFlavor)) {
            NamedIcon newIcon = new NamedIcon((NamedIcon) tr.getTransferData(_namedIconDataFlavor));
            String url = newIcon.getURL();
            NamedIcon icon = NamedIcon.getIconByName(url);
            PositionableLabel ni = new PositionableLabel(icon, this);
            // infer a background icon from the size
            if (icon.getIconHeight() > 500 || icon.getIconWidth() > 600) {
                ni.setDisplayLevel(BKG);
            } else {
                ni.setDisplayLevel(ICONS);
            }
            ni.setLocation(pt.x, pt.y);
            ni.setEditor(this);
            putItem(ni);
            ni.updateSize();
            evt.dropComplete(true);
            return;
        } else if (tr.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String text = (String) tr.getTransferData(DataFlavor.stringFlavor);
            PositionableLabel l = new PositionableLabel(text, this);
            l.setSize(l.getPreferredSize().width, l.getPreferredSize().height);
            l.setDisplayLevel(LABELS);
            l.setLocation(pt.x, pt.y);
            l.setEditor(this);
            putItem(l);
            evt.dropComplete(true);
        } else if (tr.isDataFlavorSupported(_positionableListDataFlavor)) {
            List<Positionable> dragGroup = (List<Positionable>) tr.getTransferData(_positionableListDataFlavor);
            for (Positionable pos : dragGroup) {
                pos.setEditor(this);
                putItem(pos);
                pos.updateSize();
                log.debug("DnD Add {}", pos.getNameString());
            }
        } else {
            log.warn("Editor DropTargetListener  supported DataFlavors not avaialable at drop from " + tr.getClass().getName());
        }
    } catch (IOException ioe) {
        log.warn("Editor DropTarget caught IOException", ioe);
    } catch (UnsupportedFlavorException ufe) {
        log.warn("Editor DropTarget caught UnsupportedFlavorException", ufe);
    }
    log.debug("Editor DropTargetListener drop REJECTED!");
    evt.rejectDrop();
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) Transferable(java.awt.datatransfer.Transferable) PositionableLabel(jmri.jmrit.display.PositionableLabel) List(java.util.List) ArrayList(java.util.ArrayList) Point(java.awt.Point) Positionable(jmri.jmrit.display.Positionable) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 38 with NamedIcon

use of jmri.jmrit.catalog.NamedIcon in project JMRI by JMRI.

the class ClockItemPanel method addIconsToPanel.

@Override
protected void addIconsToPanel(HashMap<String, NamedIcon> iconMap) {
    _iconPanel = new JPanel();
    Iterator<Entry<String, NamedIcon>> it = iconMap.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, NamedIcon> entry = it.next();
        // make copy for possible reduction
        NamedIcon icon = new NamedIcon(entry.getValue());
        JPanel panel = new JPanel();
        String borderName = ItemPalette.convertText(entry.getKey());
        panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), borderName));
        try {
            JLabel label = new ClockDragJLabel(new DataFlavor(Editor.POSITIONABLE_FLAVOR));
            if (icon.getIconWidth() < 1 || icon.getIconHeight() < 1) {
                label.setText(Bundle.getMessage("invisibleIcon"));
                label.setForeground(Color.lightGray);
            } else {
                icon.reduceTo(100, 100, 0.2);
            }
            label.setIcon(icon);
            label.setName(borderName);
            panel.add(label);
        } catch (java.lang.ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        }
        _iconPanel.add(panel);
    }
    add(_iconPanel, 1);
}
Also used : JPanel(javax.swing.JPanel) Entry(java.util.Map.Entry) NamedIcon(jmri.jmrit.catalog.NamedIcon) JLabel(javax.swing.JLabel) DragJLabel(jmri.jmrit.catalog.DragJLabel) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 39 with NamedIcon

use of jmri.jmrit.catalog.NamedIcon in project JMRI by JMRI.

the class FamilyItemPanel method addIconsToPanel.

protected void addIconsToPanel(HashMap<String, NamedIcon> iconMap) {
    if (iconMap == null) {
        log.warn("iconMap is null for type " + _itemType + " family " + _family);
        return;
    }
    GridBagLayout gridbag = new GridBagLayout();
    _iconPanel.setLayout(gridbag);
    int numCol = 4;
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.NONE;
    c.anchor = GridBagConstraints.CENTER;
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.gridx = -1;
    c.gridy = 0;
    int cnt = iconMap.size();
    Iterator<Entry<String, NamedIcon>> it = iconMap.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, NamedIcon> entry = it.next();
        // make copy for possible reduction
        NamedIcon icon = new NamedIcon(entry.getValue());
        icon.reduceTo(100, 100, 0.2);
        JPanel panel = new JPanel(new FlowLayout());
        // I18N use existing NamedBeanBundle keys
        String borderName = getIconBorderName(entry.getKey());
        panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), borderName));
        JLabel image = new JLabel(icon);
        if (icon.getIconWidth() < 1 || icon.getIconHeight() < 1) {
            image.setText(Bundle.getMessage("invisibleIcon"));
            image.setForeground(Color.lightGray);
        }
        image.setToolTipText(icon.getName());
        panel.add(image);
        int width = getFontMetrics(getFont()).stringWidth(borderName);
        width = Math.max(100, Math.max(width, icon.getIconWidth()) + 10);
        panel.setPreferredSize(new java.awt.Dimension(width, panel.getPreferredSize().height));
        c.gridx += 1;
        if (c.gridx >= numCol) {
            //start next row
            c.gridy++;
            c.gridx = 0;
            if (cnt < numCol - 1) {
                // last row
                JPanel p = new JPanel(new FlowLayout());
                p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
                p.add(Box.createHorizontalStrut(100));
                gridbag.setConstraints(p, c);
                //if (log.isDebugEnabled()) log.debug("addIconsToPanel: gridx= "+c.gridx+" gridy= "+c.gridy);
                _iconPanel.add(p);
                c.gridx = 1;
            }
        }
        cnt--;
        gridbag.setConstraints(panel, c);
        _iconPanel.add(panel);
    }
}
Also used : JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) NamedIcon(jmri.jmrit.catalog.NamedIcon) FlowLayout(java.awt.FlowLayout) GridBagLayout(java.awt.GridBagLayout) BoxLayout(javax.swing.BoxLayout) JLabel(javax.swing.JLabel) Entry(java.util.Map.Entry)

Example 40 with NamedIcon

use of jmri.jmrit.catalog.NamedIcon in project JMRI by JMRI.

the class ItemPanel method checkIconMap.

protected static void checkIconMap(String type, HashMap<String, NamedIcon> map) {
    String[] names = getNames(type);
    for (int i = 0; i < names.length; i++) {
        if (map.get(names[i]) == null) {
            NamedIcon icon = new jmri.jmrit.catalog.NamedIcon(redX, redX);
            // store RedX as default icon if icon not set
            map.put(names[i], icon);
        }
    }
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon)

Aggregations

NamedIcon (jmri.jmrit.catalog.NamedIcon)128 Entry (java.util.Map.Entry)28 Element (org.jdom2.Element)27 HashMap (java.util.HashMap)23 JPanel (javax.swing.JPanel)13 Editor (jmri.jmrit.display.Editor)13 Attribute (org.jdom2.Attribute)13 ActionEvent (java.awt.event.ActionEvent)12 ActionListener (java.awt.event.ActionListener)12 JLabel (javax.swing.JLabel)11 JButton (javax.swing.JButton)10 DataFlavor (java.awt.datatransfer.DataFlavor)6 CatalogTreeNode (jmri.jmrit.catalog.CatalogTreeNode)6 DragJLabel (jmri.jmrit.catalog.DragJLabel)6 Sensor (jmri.Sensor)5 CatalogTreeLeaf (jmri.jmrit.catalog.CatalogTreeLeaf)5 RosterEntry (jmri.jmrit.roster.RosterEntry)5 Point (java.awt.Point)4 JComponent (javax.swing.JComponent)4 JDialog (javax.swing.JDialog)4