001package jmri.jmrit.jython;
002
003import java.awt.Container;
004import javax.swing.JPanel;
005import javax.swing.JPopupMenu;
006import org.jdom2.Element;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * A Jynstrument is a Jython script and associated other resources that can
012 * decorate a Java class.
013 *
014 * @see JynstrumentFactory
015 * @author Lionel Jeanson Copyright 2009
016 * @since 2.7.8
017 */
018public abstract class Jynstrument extends JPanel {
019
020    private Object mContext;  // Object being extended
021    private String jythonFile;  // Name of the Jython file being run
022    private String jynstrumentFolder; // Folder where the script seats (to retrieve resources)
023    private String className; // name of the JYnstrument class
024    private JPopupMenu myPopUpMenu; // a popup menu
025
026    /**
027     * Access to the context object to which this Jynstrument was attached when
028     * it was created.
029     *
030     * @return the context object
031     */
032    public Object getContext() {
033        return mContext;
034    }
035
036    public void setContext(Object context) {
037        mContext = context;
038    }
039
040    public String getJythonFile() {
041        return jythonFile;
042    }
043
044    public void setJythonFile(String jythonFile) {
045        this.jythonFile = jythonFile;
046    }
047
048    /**
049     * Get the folder containing the defining Jython script.
050     *
051     * @return the parent folder of the script
052     */
053    public String getFolder() {
054        return jynstrumentFolder;
055    }
056
057    public void setFolder(String jynstrumentFolder) {
058        this.jynstrumentFolder = jynstrumentFolder;
059    }
060
061    public String getClassName() {
062        return className;
063    }
064
065    public void setClassName(String className) {
066        this.className = className;
067    }
068
069    public void exit() {
070        Container cnt = getParent();
071        log.debug("getParent() is {}", cnt);
072        if (cnt != null) {
073            cnt.remove(this);
074            cnt.repaint();
075        }
076        quit();
077        setPopUpMenu(null);
078    }
079
080    public boolean validateContext() {
081        if (getExpectedContextClassName() == null || mContext == null) {
082            return false;
083        }
084        try {
085            return (Class.forName(getExpectedContextClassName()).isAssignableFrom(mContext.getClass()));
086        } catch (ClassNotFoundException e) {
087            log.error("Class {} not found.", getExpectedContextClassName(), e);
088        }
089        return false;
090    }
091
092    public abstract String getExpectedContextClassName();
093
094    public abstract void init();
095
096    protected abstract void quit();
097
098    private final static Logger log = LoggerFactory.getLogger(Jynstrument.class);
099
100    public JPopupMenu getPopUpMenu() {
101        return myPopUpMenu;
102    }
103
104    public void setPopUpMenu(JPopupMenu myPopUpMenu) {
105        this.myPopUpMenu = myPopUpMenu;
106    }
107
108    public void setXml(Element e) {
109        // do nothing by default
110    }
111
112    public Element getXml() {
113        return null;
114    }
115}