ucar.unidata.idv
Class JythonManager

java.lang.Object
  extended by ucar.unidata.ui.WindowHolder
      extended by ucar.unidata.idv.IdvManager
          extended by ucar.unidata.idv.JythonManager
All Implemented Interfaces:
ActionListener, EventListener, IdvConstants

public class JythonManager
extends IdvManager
implements ActionListener

Manages jython related functionality:

Author:
IDV development team

Nested Class Summary
static class JythonManager.LibHolder
          Class LibHolder holds all things for a single lib
 
Field Summary
static String PROP_JYTHON_EDITOR
          The path to the editor executable
 
Fields inherited from class ucar.unidata.ui.WindowHolder
contents, dialog, frame, window
 
Fields inherited from interface ucar.unidata.idv.IdvConstants
ARG_B64BUNDLE, ARG_BUNDLE, ARG_CATALOG, ARG_CHOOSER, ARG_CLEARDEFAULT, ARG_CODE, ARG_CONNECT, ARG_CURRENTTIME, ARG_DATA, ARG_DEBUG, ARG_DEFAULT, ARG_DISPLAY, ARG_DXML, ARG_FIXEDTIME, ARG_HELP, ARG_IMAGE, ARG_IMAGESERVER, ARG_INSTALLPLUGIN, ARG_ISLFILE, ARG_ISLINTERACTIVE, ARG_LISTRESOURCES, ARG_MAINCLASS, ARG_MOVIE, ARG_MSG_DEBUG, ARG_MSG_RECORD, ARG_NODEFAULT, ARG_NOGUI, ARG_NOONEINSTANCE, ARG_NOPLUGINS, ARG_NOPREF, ARG_ONEINSTANCEPORT, ARG_PLUGIN, ARG_PORT, ARG_PRINTJNLP, ARG_PROPERTIES, ARG_SCRIPT, ARG_SERVER, ARG_SETFILES, ARG_SITEPATH, ARG_TEMPLATE, ARG_TEST, ARG_TESTEVAL, ARG_TRACE, ARG_TRACEONLY, ARG_USERPATH, DEFAULT_DATE_FORMAT, DEFAULT_DOCPATH, DEFAULT_TIMEZONE, FILE_VMSTATE, FILTER_CPT, FILTER_ISL, FILTER_JNLP, FILTER_XIDV, FILTER_XIDVZIDV, FILTER_XML, FILTER_ZIDV, ID_COMMANDSTORUN, ID_DATASOURCES, ID_DISPLAYCONTROLS, ID_IDV, ID_JYTHON, ID_MESSAGE, ID_MISCHASHTABLE, ID_VERSION, ID_VIEWMANAGERS, ID_WINDOWS, INITIAL_PROBE_EARTHLOCATION, NULL_DATA_SELECTION, NULL_STRING, PREF_ARCHIVEDIR, PREF_ARCHIVENAME, PREF_AUTODISPLAYS_ENABLE, PREF_AUTODISPLAYS_SHOWGUI, PREF_AUTOSELECTDATA, PREF_CACHESIZE, PREF_CONTROLSINTABS, PREF_DATAPREFIX, PREF_DATE_FORMAT, PREF_DISTANCEUNIT, PREF_DOCACHE, PREF_EMBEDDATACHOOSERINDASHBOARD, PREF_EMBEDFIELDSELECTORINDASHBOARD, PREF_EMBEDLEGENDINDASHBOARD, PREF_EMBEDQUICKLINKSINDASHBOARD, PREF_EVENT_KEYBOARDMAP, PREF_EVENT_MOUSEMAP, PREF_EVENT_WHEELMAP, PREF_FAST_RENDER, PREF_FIELD_CACHETHRESHOLD, PREF_FILEREADDIR, PREF_FILEWRITEDIR, PREF_HISTORY, PREF_LASTVERSION, PREF_LATLON_FORMAT, PREF_LOCALE, PREF_LOOKANDFEEL, PREF_MAXIMAGESIZE, PREF_OPEN_ASK, PREF_OPEN_MERGE, PREF_OPEN_REMOVE, PREF_PROJ_LIST, PREF_SAMPLINGMODE, PREF_SHOWCONTROLWINDOW, PREF_SHOWDASHBOARD, PREF_SHOWDECODEDIALOG, PREF_SHOWHIDDENFILES, PREF_SHOWQUITCONFIRM, PREF_SITEPATH, PREF_TIMEZONE, PREF_VERTICALCS, PREF_VERTICALUNIT, PREF_ZIDV_ASK, PREF_ZIDV_DIRECTORY, PREF_ZIDV_SAVETOTMP, PROP_3DMODE, PROP_ABOUTTEXT, PROP_APPRESOURCEPATH, PROP_CHOOSERS, PROP_CHOOSERS_ALL, PROP_CONTROLDESCRIPTORS, PROP_CONTROLDESCRIPTORS_ALL, PROP_DATAHOLDERBOUNDS, PROP_DEFAULTLOCATIONS, PROP_JNLPCODEBASE, PROP_JNLPTEMPLATE, PROP_JNLPTITLE, PROP_LOADINGXML, PROP_OLDCONTROLDESCRIPTORS, PROP_ONEINSTANCEPORT, PROP_PREFERENCES, PROP_PROJ_NAME, PROP_RESOURCEFILES, PROP_SHOWDASHBOARD, PROP_SHOWDATATREE, PROP_SHOWFORMULAS, PROP_SHOWSPLASH, PROP_SPLASHICON, PROP_UI_DESKTOP, PROP_VERSIONFILE, PROP_WINDOW_SCREENSIZEOFFSET, PROP_WINDOW_SIZEHEIGHT, PROP_WINDOW_SIZEWIDTH, PROP_WINDOW_USESCREENSIZE, SUFFIX_BAT, SUFFIX_CPT, SUFFIX_ISL, SUFFIX_JNLP, SUFFIX_RBI, SUFFIX_SH, SUFFIX_XIDV, SUFFIX_XML, SUFFIX_ZIDV
 
Constructor Summary
JythonManager(IntegratedDataViewer idv)
          Create the manager and call initPython.
 
Method Summary
 void addFormula(DerivedDataDescriptor ddd)
          Add a formula to the IDV.
 void appendJython(String jython)
          APpend jython to main editable lib
 void appendJythonFromBundle(String jython)
          Append the given jython to that is from a bundle to the users jython
 void appendTmpJython(String jython)
          Append the given jython to the temp jython
protected  void applicationClosing()
          Gets called when the IDV is quitting.
protected static boolean checkUntrustedJython(String jython)
          Make sure the given jython code matches the pattern (after removing whitespace): idv.procedure_name ('arg1', arg2, ..., argn) where if an arg is not in single quotes it cannot contain a procedure call.
 org.python.util.PythonInterpreter createInterpreter()
          Factory method to create and interpreter.
 JythonShell createShell()
          Create a jython shell
 void dataGroupsChanged()
          Update derived needs when the DataGroups change
 void deleteKeyPressed(DataChoice dataChoice)
          Delete the data choice if it is a user formula
 void descriptorChanged(DerivedDataDescriptor ddd)
          Called when a formula data choice has changed (i.e., added, removed or edited.
protected  JComponent doMakeContents()
          Create the jython editor.
 void doMakeDataChoiceMenuItems(DataChoice dataChoice, List items)
          Create the list of menu items for editing a data choice that represents an end user formula.
 List doMakeEditMenuItems()
          make the edit menu items for the formula data source
 List doMakeEditMenuItems(DescriptorDataSource dds)
          make the edit menu items for the given formula data source
 List doMakeFormulaDataSourceMenuItems(DataSource dataSource)
          Return the list of menu items to use when the user has clicked on a formula DataSource.
 void doSearch()
          _more_
 void doSearchPrevious()
          _more_
 void editInExternalEditor()
          Edit the jython in the external editor
 void editInExternalEditorInner(JythonManager.LibHolder holder)
          Edit the jython in the external editor
 void evaluateDataChoice(DataChoice dataChoice)
          This simply clones the given data choice and calls getData on it.
 void evaluateTrusted(String code)
          Interpret the given jython code.
 void evaluateTrusted(String code, Hashtable properties)
          Interpret the given jython code.
 void evaluateUntrusted(String jythonCode)
          Evaluate the given jython code.
 void evaluateUntrusted(String jythonCode, Hashtable properties)
          Evaluate the given jython code.
 void exportFormulas()
          Export user formulas
 void exportFormulasToPlugin()
          Export selected formulas to plugin
 void exportSelectedToPlugin()
          Export selcted text of current tab to plugin
 void exportToPlugin()
          Export to plugin
 List findJythonMethods(boolean justList)
          find methods
 List findJythonMethods(boolean justList, List holders)
          find methods
 List getDefaultDescriptors()
          Get all end user formulas
 org.python.util.PythonInterpreter getDerivedDataInterpreter()
          Create a (singleton) jython interpreter and initialize it with the set of classes defined in the xml
 org.python.util.PythonInterpreter getDerivedDataInterpreter(String methodName)
          Create a (singleton) jython interpreter and initialize it with the set of classes defined in the xml and (if needed) with the class path represented by the methodName argument (if methodName is of the form: some.package.path.SomeClass.someMethod).
 DescriptorDataSource getDescriptorDataSource()
          Get the descriptor data source
 List getDescriptors()
          get formula descriptors
 List getEndUserDescriptors()
          Get all end user formulas
 boolean getInError()
          Any errors
 List getLibHolders()
          the libs
 List getLocalDescriptors()
          Get all local descriptors
 String getUsersJythonText()
          Get the end user edited text from the jython editor.
 String getWindowTitle()
          Get the window titlexxx
 void importFormulas()
          Import user formulas
protected  void initUserFormulas(IdvResourceManager newIrm)
          Initialize the DerivedDataDescriptors that are defined in the RSC_DERIVED resource collection from the given resource manager.
static void main(String[] args)
          main
 void makeFileMenu(JMenu fileMenu)
          make men
 void makeFormula(org.python.core.PyFunction func)
          Make a formula
 void makeNewLibrary()
          make new library
 List makeProcedureMenu(Object object, String method, String prefix)
          Make menu
 void removeFormula(DerivedDataChoice dataChoice)
          Remove a formula from the IDV.
 void removeFormula(DerivedDataDescriptor ddd)
          remove formula
 void removeInterpreter(org.python.util.PythonInterpreter interp)
          Remove the interpreter from the list of interpreters.
 void removeLibrary(JythonManager.LibHolder holder)
          remove lib
 boolean saveOnExit()
          Save on exit if anything is changed
 void searchFor()
          _more_
protected  List selectFormulas()
          Popup dialog to select formulas
 void showFormulaDialog()
          Show the formula dialog with no initial state.
 void showFormulaDialog(DerivedDataDescriptor descriptor)
          Show formula dialog with the given initial DDD.
 void showFormulaDialog(DerivedDataDescriptor descriptor, boolean isNew)
           
 void showHelp()
          SHow help
 void showHelp(String help)
          _more_
 void showJythonEditor()
          Create, if needed, and show the jython editor.
 boolean writeJythonLib(JythonManager.LibHolder holder)
          Save the end user jython code from the jython editor into the user's .metapps area.
protected  void writeUserFormulas()
          Save the user created formulas.
 
Methods inherited from class ucar.unidata.idv.IdvManager
getArgsManager, getCollabManager, getColorTableManager, getDataManager, getIdv, getIdvChooserManager, getIdvClass, getIdvProjectionManager, getIdvUIManager, getImageGenerator, getJythonManager, getPersistenceManager, getPreferenceManager, getProperty, getProperty, getProperty, getPublishManager, getResourceManager, getStateManager, getStationModelManager, getStore, getVMManager, logException, showNormalCursor, showWaitCursor, waitUntilDisplaysAreDone
 
Methods inherited from class ucar.unidata.ui.WindowHolder
actionPerformed, close, getContents, getDialog, getFrame, getWindow, isShowing, removeAll, setWindowTitle, shouldMakeDialog, show, show, showModal, toFront, windowIsClosing
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.awt.event.ActionListener
actionPerformed
 

Field Detail

PROP_JYTHON_EDITOR

public static final String PROP_JYTHON_EDITOR
The path to the editor executable

See Also:
Constant Field Values
Constructor Detail

JythonManager

public JythonManager(IntegratedDataViewer idv)
Create the manager and call initPython.

Parameters:
idv - The IDV
Method Detail

showJythonEditor

public void showJythonEditor()
Create, if needed, and show the jython editor.


exportSelectedToPlugin

public void exportSelectedToPlugin()
Export selcted text of current tab to plugin


exportToPlugin

public void exportToPlugin()
Export to plugin


searchFor

public void searchFor()
_more_


doSearch

public void doSearch()
_more_


doSearchPrevious

public void doSearchPrevious()
_more_


doMakeContents

protected JComponent doMakeContents()
Create the jython editor. We create a tree panel that holds each valid jython library defined in the IDV's resource manager.

Overrides:
doMakeContents in class WindowHolder
Returns:
The gui contents

saveOnExit

public boolean saveOnExit()
Save on exit if anything is changed

Returns:
continue with exit

makeFormula

public void makeFormula(org.python.core.PyFunction func)
Make a formula

Parameters:
func - function

makeNewLibrary

public void makeNewLibrary()
make new library


getLibHolders

public List getLibHolders()
the libs

Returns:
the libs

removeLibrary

public void removeLibrary(JythonManager.LibHolder holder)
remove lib

Parameters:
holder - lib

makeFileMenu

public void makeFileMenu(JMenu fileMenu)
make men

Parameters:
fileMenu - menu

applicationClosing

protected void applicationClosing()
Gets called when the IDV is quitting. Kills the editor process if there is one


editInExternalEditor

public void editInExternalEditor()
Edit the jython in the external editor


editInExternalEditorInner

public void editInExternalEditorInner(JythonManager.LibHolder holder)
Edit the jython in the external editor

Parameters:
holder - lib

getWindowTitle

public String getWindowTitle()
Get the window titlexxx

Overrides:
getWindowTitle in class WindowHolder
Returns:
window title

showHelp

public void showHelp()
SHow help


showHelp

public void showHelp(String help)
_more_

Parameters:
help - _more_

createInterpreter

public org.python.util.PythonInterpreter createInterpreter()
Factory method to create and interpreter. This also adds the interpreter into the list of interpreters.

Returns:
The new interpreter

createShell

public JythonShell createShell()
Create a jython shell

Returns:
shell

removeInterpreter

public void removeInterpreter(org.python.util.PythonInterpreter interp)
Remove the interpreter from the list of interpreters.

Parameters:
interp - The interpreter to remove

getInError

public boolean getInError()
Any errors

Returns:
in error

getUsersJythonText

public String getUsersJythonText()
Get the end user edited text from the jython editor.

Returns:
The end user jython code

appendTmpJython

public void appendTmpJython(String jython)
Append the given jython to the temp jython

Parameters:
jython - The jython from the bundle

appendJythonFromBundle

public void appendJythonFromBundle(String jython)
Append the given jython to that is from a bundle to the users jython

Parameters:
jython - The jython from the bundle

appendJython

public void appendJython(String jython)
APpend jython to main editable lib

Parameters:
jython - jython

writeJythonLib

public boolean writeJythonLib(JythonManager.LibHolder holder)
Save the end user jython code from the jython editor into the user's .metapps area.

Parameters:
holder - lib
Returns:
success

checkUntrustedJython

protected static boolean checkUntrustedJython(String jython)
Make sure the given jython code matches the pattern (after removing whitespace): idv.procedure_name ('arg1', arg2, ..., argn) where if an arg is not in single quotes it cannot contain a procedure call.

We have this here so (hopefully) a user won't inadvertently execute rogue jython code on their machine.

Parameters:
jython - The code
Returns:
Does the code just call into idv or datamanager methods.

evaluateUntrusted

public void evaluateUntrusted(String jythonCode)
Evaluate the given jython code. This code is untrusted and has to be of the form (idv|datamanager).some_method (param1, param2, ..., paramN);

Parameters:
jythonCode - The code to execute

evaluateUntrusted

public void evaluateUntrusted(String jythonCode,
                              Hashtable properties)
Evaluate the given jython code. This code is untrusted and has to be of the form (idv|datamanager).some_method (param1, param2, ..., paramN);

Parameters:
jythonCode - The code to execute
properties - If non-null then populate the interpreter with the name/value pairs

evaluateTrusted

public void evaluateTrusted(String code)
Interpret the given jython code. This code is trusted, i.e., it is not checked to make sure it is only calling idv or datamanager methods.

Parameters:
code - The code toe evaluate

evaluateTrusted

public void evaluateTrusted(String code,
                            Hashtable properties)
Interpret the given jython code. This code is trusted, i.e., it is not checked to make sure it is only calling idv or datamanager methods.

Parameters:
code - The code toe evaluate
properties - If non-null then populate the interpreter with the name/value pairs

dataGroupsChanged

public void dataGroupsChanged()
Update derived needs when the DataGroups change


initUserFormulas

protected void initUserFormulas(IdvResourceManager newIrm)
Initialize the DerivedDataDescriptors that are defined in the RSC_DERIVED resource collection from the given resource manager.

Parameters:
newIrm - The resource manager to get the derived resources from

selectFormulas

protected List selectFormulas()
Popup dialog to select formulas

Returns:
List of selected formulas

exportFormulasToPlugin

public void exportFormulasToPlugin()
Export selected formulas to plugin


exportFormulas

public void exportFormulas()
Export user formulas


importFormulas

public void importFormulas()
Import user formulas


writeUserFormulas

protected void writeUserFormulas()
Save the user created formulas.


doMakeDataChoiceMenuItems

public void doMakeDataChoiceMenuItems(DataChoice dataChoice,
                                      List items)
Create the list of menu items for editing a data choice that represents an end user formula.

Parameters:
dataChoice - The end user formula data choice
items - List of menu items to add to

deleteKeyPressed

public void deleteKeyPressed(DataChoice dataChoice)
Delete the data choice if it is a user formula

Parameters:
dataChoice - The data choice to delete

evaluateDataChoice

public void evaluateDataChoice(DataChoice dataChoice)
This simply clones the given data choice and calls getData on it. We have this here so the user can explicitly, through the GUI, evaluate a formula data choice. This way they don't have to create a display to simply evaluate a formula.

Parameters:
dataChoice - The data chocie to evaluate

removeFormula

public void removeFormula(DerivedDataChoice dataChoice)
Remove a formula from the IDV. You can only remove end user formulas that are in the editable list.

Parameters:
dataChoice - formula data choice

removeFormula

public void removeFormula(DerivedDataDescriptor ddd)
remove formula

Parameters:
ddd - ddd

descriptorChanged

public void descriptorChanged(DerivedDataDescriptor ddd)
Called when a formula data choice has changed (i.e., added, removed or edited.

Parameters:
ddd - descriptor for the formula.

addFormula

public void addFormula(DerivedDataDescriptor ddd)
Add a formula to the IDV.

Parameters:
ddd - formula descriptor

doMakeFormulaDataSourceMenuItems

public List doMakeFormulaDataSourceMenuItems(DataSource dataSource)
Return the list of menu items to use when the user has clicked on a formula DataSource.

Parameters:
dataSource - The data source clicked on
Returns:
List of menu items

doMakeEditMenuItems

public List doMakeEditMenuItems()
make the edit menu items for the formula data source

Returns:
List of menu items

doMakeEditMenuItems

public List doMakeEditMenuItems(DescriptorDataSource dds)
make the edit menu items for the given formula data source

Parameters:
dds - The formula data source
Returns:
List of menu items

showFormulaDialog

public void showFormulaDialog()
Show the formula dialog with no initial state. We do this to create a new formula.


getDescriptors

public List getDescriptors()
get formula descriptors

Returns:
descriptors

getEndUserDescriptors

public List getEndUserDescriptors()
Get all end user formulas

Returns:
end user formulas

getLocalDescriptors

public List getLocalDescriptors()
Get all local descriptors

Returns:
local descriptors

getDefaultDescriptors

public List getDefaultDescriptors()
Get all end user formulas

Returns:
end user formulas

showFormulaDialog

public void showFormulaDialog(DerivedDataDescriptor descriptor)
Show formula dialog with the given initial DDD.

Parameters:
descriptor - The descriptor for the formula.

showFormulaDialog

public void showFormulaDialog(DerivedDataDescriptor descriptor,
                              boolean isNew)

getDescriptorDataSource

public DescriptorDataSource getDescriptorDataSource()
Get the descriptor data source

Returns:
The descriptor data source

getDerivedDataInterpreter

public org.python.util.PythonInterpreter getDerivedDataInterpreter()
Create a (singleton) jython interpreter and initialize it with the set of classes defined in the xml

Returns:
The singleton Jython interpreter for derived data execution

getDerivedDataInterpreter

public org.python.util.PythonInterpreter getDerivedDataInterpreter(String methodName)
Create a (singleton) jython interpreter and initialize it with the set of classes defined in the xml and (if needed) with the class path represented by the methodName argument (if methodName is of the form: some.package.path.SomeClass.someMethod).

Parameters:
methodName - Used to initialize the interpreter (if non -null)
Returns:
The singleton Jython interpreter for derived data execution

makeProcedureMenu

public List makeProcedureMenu(Object object,
                              String method,
                              String prefix)
Make menu

Parameters:
object - object to call
method - method to call
prefix - prefic
Returns:
menus

findJythonMethods

public List findJythonMethods(boolean justList)
find methods

Parameters:
justList - If true just the functions
Returns:
A list of Object arrays. First element in array is the name of the lib. Second is the list of PyFunction-s

findJythonMethods

public List findJythonMethods(boolean justList,
                              List holders)
find methods

Parameters:
justList - If true just the function
holders - libs
Returns:
A list of Object arrays. First element in array is the name of the lib. Second is the list of PyFunction-s

main

public static void main(String[] args)
                 throws Exception
main

Parameters:
args - args
Throws:
Exception - on badness