001/* 002 * $Id: JythonManager.java,v 1.7 2011/03/24 16:06:31 davep Exp $ 003 * 004 * This file is part of McIDAS-V 005 * 006 * Copyright 2007-2011 007 * Space Science and Engineering Center (SSEC) 008 * University of Wisconsin - Madison 009 * 1225 W. Dayton Street, Madison, WI 53706, USA 010 * https://www.ssec.wisc.edu/mcidas 011 * 012 * All Rights Reserved 013 * 014 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and 015 * some McIDAS-V source code is based on IDV and VisAD source code. 016 * 017 * McIDAS-V is free software; you can redistribute it and/or modify 018 * it under the terms of the GNU Lesser Public License as published by 019 * the Free Software Foundation; either version 3 of the License, or 020 * (at your option) any later version. 021 * 022 * McIDAS-V is distributed in the hope that it will be useful, 023 * but WITHOUT ANY WARRANTY; without even the implied warranty of 024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 025 * GNU Lesser Public License for more details. 026 * 027 * You should have received a copy of the GNU Lesser Public License 028 * along with this program. If not, see http://www.gnu.org/licenses. 029 */ 030package edu.wisc.ssec.mcidasv; 031 032import static ucar.unidata.util.GuiUtils.makeMenu; 033import static ucar.unidata.util.GuiUtils.makeMenuItem; 034import static ucar.unidata.util.MenuUtil.MENU_SEPARATOR; 035 036import java.util.ArrayList; 037import java.util.Hashtable; 038import java.util.List; 039import java.util.Map; 040 041import org.python.util.PythonInterpreter; 042 043import org.slf4j.Logger; 044import org.slf4j.LoggerFactory; 045 046import edu.wisc.ssec.mcidasv.util.CollectionHelpers; 047 048import ucar.unidata.data.DataSource; 049import ucar.unidata.data.DescriptorDataSource; 050import ucar.unidata.idv.IntegratedDataViewer; 051import ucar.unidata.idv.ui.ImageGenerator; 052 053public class JythonManager extends ucar.unidata.idv.JythonManager { 054 055 /** Trusty logging object. */ 056 private static final Logger logger = LoggerFactory.getLogger(JythonManager.class); 057 058 /** 059 * Create the manager and call initPython. 060 * 061 * @param idv The IDV 062 */ 063 public JythonManager(IntegratedDataViewer idv) { 064 super(idv); 065 } 066 067 /** 068 * Overridden so that McIDAS-V can inject a variable named {@code _idv} 069 * into {@code interpreter's} globals. 070 * 071 * @param interpreter Jython interpreter being initialized by the IDV. Cannot be {@code null}. 072 */ 073 @Override protected void initBasicInterpreter(PythonInterpreter interpreter) { 074 interpreter.set("_idv", getIdv()); 075 super.initBasicInterpreter(interpreter); 076 } 077 078 /** 079 * Overridden so that McIDAS-V can add an {@code islInterpreter} object 080 * to the interpreter's locals (before executing the contents of {@code}. 081 * 082 * @param code Jython code to evaluate. {@code null} is probably a bad idea. 083 * @param properties {@code String->Object} pairs to insert into the 084 * locals. Parameter may be {@code null}. 085 */ 086 @SuppressWarnings("unchecked") // dealing with idv code that predates generics. 087 @Override public void evaluateTrusted(String code, Map<String, Object> properties) { 088 if (properties == null) { 089 properties = CollectionHelpers.newMap(); 090 } 091 if (!properties.containsKey("islInterpreter")) { 092 properties.put("islInterpreter", new ImageGenerator(getIdv())); 093 } 094 if (!properties.containsKey("_idv")) { 095 properties.put("_idv", getIdv()); 096 } 097 super.evaluateTrusted(code, properties); 098 } 099 100 /** 101 * Return the list of menu items to use when the user has clicked on a 102 * formula {@link DataSource}. 103 * 104 * @param dataSource The data source clicked on. 105 * 106 * @return {@link List} of menu items. 107 */ 108 @SuppressWarnings("unchecked") // dealing with idv code that predates generics. 109 @Override public List doMakeFormulaDataSourceMenuItems(DataSource dataSource) { 110 List menuItems = new ArrayList(); 111 menuItems.add(makeMenuItem("Create Formula", this, "showFormulaDialog")); 112 List editItems; 113 if (dataSource instanceof DescriptorDataSource) { 114 editItems = doMakeEditMenuItems((DescriptorDataSource)dataSource); 115 } 116 else { 117 editItems = doMakeEditMenuItems(); 118 } 119 menuItems.add(makeMenu("Edit Formulas", editItems)); 120 menuItems.add(MENU_SEPARATOR); 121 menuItems.add(makeMenuItem("Jython Library", this, "showJythonEditor")); 122 menuItems.add(makeMenuItem("Jython Shell", this, "createShell")); 123 menuItems.add(MENU_SEPARATOR); 124 menuItems.add(makeMenuItem("Import", this, "importFormulas")); 125 menuItems.add(makeMenuItem("Export", this, "exportFormulas")); 126 return menuItems; 127 } 128 129}