001package jmri.jmrit.display.controlPanelEditor.shape;
002
003import java.awt.Font;
004import java.awt.Graphics;
005import java.awt.Graphics2D;
006
007import javax.swing.JPopupMenu;
008
009import jmri.jmrit.display.Editor;
010import jmri.jmrit.logix.OBlock;
011import jmri.util.swing.JmriMouseEvent;
012
013public class LocoLabel extends PositionableRoundRect {
014
015    private OBlock _block;
016
017    public LocoLabel(Editor editor) {
018        super(editor);
019        setEditable(false);
020    }
021
022    public void setBlock(OBlock b) {
023        _block = b;
024        invalidateShape();
025    }
026
027    @Override
028    public boolean showPopUp(JPopupMenu popup) {
029        setRotateMenu(popup);
030        getEditor().setRemoveMenu(this, popup);
031        return true;
032    }
033
034    @Override
035    public void paint(Graphics g) {
036        super.paint(g);
037        if (_block == null) {
038            return;
039        }
040        String trainName = (String) _block.getValue();
041        if (trainName == null) {
042            return;
043        }
044        Font font = _block.getMarkerFont();
045        if (font == null) {
046            font = getFont();
047        }
048        g.setFont(font);
049        int textWidth = getFontMetrics(font).stringWidth(trainName);
050        int textHeight = getFontMetrics(font).getHeight();
051        int hOffset = Math.max((maxWidth() - textWidth) / 2, 0);
052        int vOffset = Math.max((maxHeight() - textHeight) / 2, 0) + getFontMetrics(font).getAscent();
053        g.setColor(_block.getMarkerForeground());
054        g.drawString(trainName, hOffset, vOffset);
055    }
056
057    @Override
058    protected void paintHandles(Graphics2D g2d) {
059    }
060
061    @Override
062    protected boolean doHandleMove(JmriMouseEvent event) {
063        return false;
064    }
065
066    @Override
067    public boolean storeItem() {
068        return false;
069    }
070}