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 try { 077 quit(); 078 } catch (RuntimeException e) { 079 // catch any error in order to avoid breaking JMRI exit flow 080 log.error("While quiting Jynstrument.",e); 081 } 082 setPopUpMenu(null); 083 } 084 085 public boolean validateContext() { 086 if (getExpectedContextClassName() == null || mContext == null) { 087 return false; 088 } 089 try { 090 return (Class.forName(getExpectedContextClassName()).isAssignableFrom(mContext.getClass())); 091 } catch (ClassNotFoundException e) { 092 log.error("Class {} not found.", getExpectedContextClassName(), e); 093 } 094 return false; 095 } 096 097 public abstract String getExpectedContextClassName(); 098 099 public abstract void init(); 100 101 protected abstract void quit(); 102 103 private final static Logger log = LoggerFactory.getLogger(Jynstrument.class); 104 105 public JPopupMenu getPopUpMenu() { 106 return myPopUpMenu; 107 } 108 109 public void setPopUpMenu(JPopupMenu myPopUpMenu) { 110 this.myPopUpMenu = myPopUpMenu; 111 } 112 113 public void setXml(Element e) { 114 // do nothing by default 115 } 116 117 public Element getXml() { 118 return null; 119 } 120 121}