Search in sources :

Example 1 with PositionableLabel

use of jmri.jmrit.display.PositionableLabel in project JMRI by JMRI.

the class PositionableLabelXml method loadCommonAttributes.

public void loadCommonAttributes(Positionable l, int defaultLevel, Element element) {
    Attribute a = element.getAttribute("forcecontroloff");
    if ((a != null) && a.getValue().equals("true")) {
        l.setControlling(false);
    } else {
        l.setControlling(true);
    }
    // find coordinates
    int x = 0;
    int y = 0;
    try {
        x = element.getAttribute("x").getIntValue();
        y = element.getAttribute("y").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert positional attribute");
    }
    l.setLocation(x, y);
    // find display level
    int level = defaultLevel;
    try {
        level = element.getAttribute("level").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.warn("Could not parse level attribute!");
    } catch (NullPointerException e) {
    // considered normal if the attribute not present
    }
    l.setDisplayLevel(level);
    a = element.getAttribute("hidden");
    if ((a != null) && a.getValue().equals("yes")) {
        l.setHidden(true);
        l.setVisible(false);
    }
    a = element.getAttribute("positionable");
    if ((a != null) && a.getValue().equals("true")) {
        l.setPositionable(true);
    } else {
        l.setPositionable(false);
    }
    a = element.getAttribute("showtooltip");
    if ((a != null) && a.getValue().equals("true")) {
        l.setShowTooltip(true);
    } else {
        l.setShowTooltip(false);
    }
    a = element.getAttribute("editable");
    if ((a != null) && a.getValue().equals("true")) {
        l.setEditable(true);
    } else {
        l.setEditable(false);
    }
    a = element.getAttribute("degrees");
    if (a != null && l instanceof PositionableLabel) {
        try {
            int deg = a.getIntValue();
            ((PositionableLabel) l).setDegrees(deg);
        } catch (org.jdom2.DataConversionException dce) {
        }
    }
    Element elem = element.getChild("tooltip");
    if (elem == null) {
        // pre JMRI 3.5.2
        elem = element.getChild("toolTip");
    }
    if (elem != null) {
        ToolTip tip = l.getTooltip();
        if (tip != null) {
            tip.setText(elem.getText());
        }
    }
}
Also used : ToolTip(jmri.jmrit.display.ToolTip) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) PositionableLabel(jmri.jmrit.display.PositionableLabel) DataConversionException(org.jdom2.DataConversionException)

Example 2 with PositionableLabel

use of jmri.jmrit.display.PositionableLabel 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 3 with PositionableLabel

use of jmri.jmrit.display.PositionableLabel in project JMRI by JMRI.

the class DecoratorPanel method updateSamples.

private void updateSamples() {
    if (_previewPanel == null) {
        return;
    }
    int mar = _util.getMargin();
    int bor = _util.getBorderSize();
    Border outlineBorder;
    if (bor == 0) {
        outlineBorder = BorderFactory.createEmptyBorder(0, 0, 0, 0);
    } else {
        outlineBorder = new LineBorder(_util.getBorderColor(), bor);
    }
    Font font = _util.getFont();
    int just = _util.getJustification();
    Iterator<PositionableLabel> it = _sample.values().iterator();
    while (it.hasNext()) {
        PositionableLabel sam = it.next();
        PositionablePopupUtil util = sam.getPopupUtility();
        sam.setFont(font);
        util.setFixedWidth(_util.getFixedWidth());
        util.setFixedHeight(_util.getFixedHeight());
        util.setMargin(mar);
        util.setBorderSize(bor);
        Border borderMargin;
        if (sam.isOpaque()) {
            borderMargin = new LineBorder(sam.getBackground(), mar);
        } else {
            borderMargin = BorderFactory.createEmptyBorder(mar, mar, mar, mar);
        }
        sam.setBorder(new CompoundBorder(outlineBorder, borderMargin));
        switch(just) {
            case PositionablePopupUtil.LEFT:
                sam.setHorizontalAlignment(JLabel.LEFT);
                break;
            case PositionablePopupUtil.RIGHT:
                sam.setHorizontalAlignment(JLabel.RIGHT);
                break;
            default:
                sam.setHorizontalAlignment(JLabel.CENTER);
        }
        sam.updateSize();
        sam.setPreferredSize(sam.getSize());
        sam.repaint();
    }
    if (_dialog != null) {
        _dialog.pack();
    }
}
Also used : PositionablePopupUtil(jmri.jmrit.display.PositionablePopupUtil) LineBorder(javax.swing.border.LineBorder) PositionableLabel(jmri.jmrit.display.PositionableLabel) CompoundBorder(javax.swing.border.CompoundBorder) LineBorder(javax.swing.border.LineBorder) Border(javax.swing.border.Border) CompoundBorder(javax.swing.border.CompoundBorder) Font(java.awt.Font)

Example 4 with PositionableLabel

use of jmri.jmrit.display.PositionableLabel in project JMRI by JMRI.

the class DecoratorPanel method setAttributes.

public void setAttributes(Positionable pos) {
    if (pos instanceof SensorIcon && !((SensorIcon) pos).isIcon()) {
        SensorIcon icon = (SensorIcon) pos;
        PositionableLabel sample = _sample.get("Active");
        if (sample.isOpaque()) {
            icon.setBackgroundActive(sample.getBackground());
        } else {
            icon.setBackgroundActive(null);
        }
        icon.setTextActive(sample.getForeground());
        icon.setActiveText(sample.getText());
        sample = _sample.get("InActive");
        icon.setInactiveText(sample.getText());
        if (sample.isOpaque()) {
            icon.setBackgroundInActive(sample.getBackground());
        } else {
            icon.setBackgroundInActive(null);
        }
        icon.setTextInActive(sample.getForeground());
        sample = _sample.get("Unknown");
        icon.setUnknownText(sample.getText());
        if (sample.isOpaque()) {
            icon.setBackgroundUnknown(sample.getBackground());
        } else {
            icon.setBackgroundUnknown(null);
        }
        icon.setTextUnknown(sample.getForeground());
        sample = _sample.get("Inconsistent");
        icon.setInconsistentText(sample.getText());
        if (sample.isOpaque()) {
            icon.setBackgroundInconsistent(sample.getBackground());
        } else {
            icon.setBackgroundInconsistent(null);
        }
        icon.setTextInconsistent(sample.getForeground());
    } else {
        PositionableLabel sample = _sample.get("Text");
        pos.setForeground(sample.getForeground());
        if (pos instanceof PositionableLabel && !(pos instanceof jmri.jmrit.display.MemoryIcon)) {
            ((PositionableLabel) pos).setText(sample.getText());
        }
        if (sample.isOpaque()) {
            pos.setBackground(sample.getBackground());
        } else {
            pos.setBackground(null);
        }
        _util.setHasBackground(sample.isOpaque());
    }
}
Also used : SensorIcon(jmri.jmrit.display.SensorIcon) PositionableLabel(jmri.jmrit.display.PositionableLabel)

Example 5 with PositionableLabel

use of jmri.jmrit.display.PositionableLabel in project JMRI by JMRI.

the class NamedIcon method rotate.

/**
     * Rotate from anchor point (upper left corner) and shift into place.
     *
     * @param degree the distance to rotate
     * @param comp   containing component
     */
public void rotate(int degree, Component comp) {
    setImage(mDefaultImage);
    if (Math.abs(_scale - 1.0) > .00001) {
        int w = (int) Math.ceil(_scale * getIconWidth());
        int h = (int) Math.ceil(_scale * getIconHeight());
        transformImage(w, h, _transformS, comp);
    }
    mRotation = 0;
    degree = degree % 360;
    _deg = degree;
    if (degree == 0) {
        return;
    }
    double rad = Math.toRadians(degree);
    double w = getIconWidth();
    double h = getIconHeight();
    int width = (int) Math.ceil(Math.abs(h * Math.sin(rad)) + Math.abs(w * Math.cos(rad)));
    int heigth = (int) Math.ceil(Math.abs(h * Math.cos(rad)) + Math.abs(w * Math.sin(rad)));
    AffineTransform t;
    if (0 <= degree && degree < 90 || -360 < degree && degree <= -270) {
        t = AffineTransform.getTranslateInstance(h * Math.sin(rad), 0.0);
    } else if (90 <= degree && degree < 180 || -270 < degree && degree <= -180) {
        t = AffineTransform.getTranslateInstance(h * Math.sin(rad) - w * Math.cos(rad), -h * Math.cos(rad));
    } else if (180 <= degree && degree < 270 || -180 < degree && degree <= -90) {
        t = AffineTransform.getTranslateInstance(-w * Math.cos(rad), -w * Math.sin(rad) - h * Math.cos(rad));
    } else /*if (270<=degree && degree<360)*/
    {
        t = AffineTransform.getTranslateInstance(0.0, -w * Math.sin(rad));
    }
    AffineTransform r = AffineTransform.getRotateInstance(rad);
    t.concatenate(r);
    transformImage(width, heigth, t, comp);
    if (comp instanceof PositionableLabel) {
        ((PositionableLabel) comp).setDegrees(degree);
    }
}
Also used : PositionableLabel(jmri.jmrit.display.PositionableLabel) AffineTransform(java.awt.geom.AffineTransform)

Aggregations

PositionableLabel (jmri.jmrit.display.PositionableLabel)9 NamedIcon (jmri.jmrit.catalog.NamedIcon)3 SensorIcon (jmri.jmrit.display.SensorIcon)3 Element (org.jdom2.Element)3 Positionable (jmri.jmrit.display.Positionable)2 PositionablePopupUtil (jmri.jmrit.display.PositionablePopupUtil)2 Attribute (org.jdom2.Attribute)2 DataConversionException (org.jdom2.DataConversionException)2 Color (java.awt.Color)1 Font (java.awt.Font)1 Point (java.awt.Point)1 DataFlavor (java.awt.datatransfer.DataFlavor)1 Transferable (java.awt.datatransfer.Transferable)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 AffineTransform (java.awt.geom.AffineTransform)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 JPanel (javax.swing.JPanel)1 JPopupMenu (javax.swing.JPopupMenu)1