001package jmri.jmrit.display;
002
003import javax.annotation.Nonnull;
004import javax.swing.JPopupMenu;
005
006import jmri.JmriException;
007import jmri.jmrit.catalog.NamedIcon;
008import jmri.util.swing.JmriMouseEvent;
009
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * LinkingLabel is a PositionableLabel that opens a link to another window or
015 * URL when clicked
016 *
017 * @author Bob Jacobsen Copyright (c) 2013
018 */
019public class LinkingLabel extends PositionableLabel implements LinkingObject {
020
021    public LinkingLabel(@Nonnull String s, @Nonnull Editor editor, @Nonnull String url) {
022        super(s, editor);
023        this.url = url;
024        setPopupUtility(new PositionablePopupUtil(this, this));
025    }
026
027    public LinkingLabel(NamedIcon s, @Nonnull Editor editor, @Nonnull String url) {
028        super(s, editor);
029        this.url = url;
030        setPopupUtility(new PositionablePopupUtil(this, this));
031    }
032
033    @Override
034    public Positionable deepClone() {
035        PositionableLabel pos;
036        if (_icon) {
037            NamedIcon icon = new NamedIcon((NamedIcon) getIcon());
038            pos = new LinkingLabel(icon, _editor, url);
039        } else {
040            pos = new LinkingLabel(_unRotatedText, _editor, url);
041        }
042        return finishClone(pos);
043    }
044
045    protected Positionable finishClone(LinkingLabel pos) {
046        return super.finishClone(pos);
047    }
048
049    String url;
050
051    @Override
052    public String getURL() {
053        return url;
054    }
055
056    @Override
057    public void setULRL(String u) {
058        url = u;
059    }
060
061    @Override
062    public boolean setLinkMenu(JPopupMenu popup) {
063        popup.add(CoordinateEdit.getLinkEditAction(this, "EditLink"));
064        return true;
065    }
066
067    // overide where used - e.g. momentary
068//    public void doMousePressed(JmriMouseEvent event) {}
069//    public void doMouseReleased(JmriMouseEvent event) {}
070    @Override
071    public void doMouseClicked(JmriMouseEvent event) {
072        log.debug("click to {}", url);
073        try {
074            if (url.startsWith("frame:")) {
075                // locate JmriJFrame and push to front
076                String frame = url.substring(6);
077                final jmri.util.JmriJFrame jframe = jmri.util.JmriJFrame.getFrame(frame);
078                if (jframe != null) {  //ignore if jframe not found
079                    java.awt.EventQueue.invokeLater(() -> {
080                        //if frame was minimized, restore
081                        if (jframe.getExtendedState() == java.awt.Frame.ICONIFIED) {
082                            jframe.setExtendedState(java.awt.Frame.NORMAL);
083                        }
084                        //bring the frame to the foreground
085                        jframe.toFront();
086                        jframe.repaint();
087                    });
088                } else {
089                    log.error("Frame '{}' not found, cannot link to it.", frame);
090                }
091            } else if (url.length() > 0) {
092                jmri.util.HelpUtil.openWebPage(url);
093            }
094        } catch (JmriException t) {
095            log.error("Error handling link", t);
096        }
097        super.doMouseClicked(event);
098    }
099
100    private final static Logger log = LoggerFactory.getLogger(LinkingLabel.class);
101
102}