if (o==null) return;
if (!(o instanceof SourceTemplate)) return;
SourceTemplate t = (SourceTemplate)o;
// Ask the plugin to create the source
DataSource ds = t.plugin.createSource(t.template, name);
if (ds==null) return;
// Add the source to the panel
DataSourcePool.global.addDataSource(ds);
ExpressionPanel.this.sourceTree.setSelectedValue(ds);
return;
}
String exp = tfExpression.getText();
if ((name == null) || name.equals("")) {
JOptionPane.showMessageDialog(JSynoptic.gui.getOwner(),resources.getString("PleaseEnterNewSourceName"),resources.getString("Error"),JOptionPane.ERROR_MESSAGE);
return;
}
if ((exp == null) || exp.equals("")) {
JOptionPane.showMessageDialog(JSynoptic.gui.getOwner(),resources.getString("PleaseEnterAMathematicalExpression"),resources.getString("Error"),JOptionPane.ERROR_MESSAGE);
return;
}
ExpressionNode node = null;
try {
ExpressionParser ep = new ExpressionParser(exp);
// Add the data sources as variables
// start by looping through collections
Set collections = DataSourcePool.global.dataSourceCollections();
for (Iterator it = collections.iterator(); it.hasNext(); ) {
Collection c = (Collection)it.next();
for (Iterator it2 = c.iterator(); it2.hasNext();) addVariable(ep, it2.next());
}
// also add sources
Set sources = DataSourcePool.global.dataSources();
for (Iterator it = sources.iterator(); it.hasNext();) addVariable(ep, it.next());
// Add known plugins => may bring in more mathematical functions
for (Iterator it = Run.plugins.iterator(); it.hasNext();) ep.addClass(it.next().getClass());
node = ep.parse();
} catch (ParseException pe) {
// Also catches duplicate variable names
JOptionPane.showMessageDialog(JSynoptic.gui.getOwner(),pe.getLocalizedMessage(),resources.getString("InvalidExpression"),JOptionPane.ERROR_MESSAGE);
return;
} catch (Error err) {
JOptionPane.showMessageDialog(JSynoptic.gui.getOwner(),err.getLocalizedMessage(),resources.getString("InvalidExpression"),JOptionPane.ERROR_MESSAGE);
return;
}
// Now we have a correct node, ready to evaluate()
// Create a data source
boolean newSource = true;
// Check if the source exists, and in this case change the expression on the fly
for (Iterator it = DataSourcePool.global.dataSources().iterator(); it.hasNext(); ) {
Object o = it.next();
if (!(o instanceof ExpressionDataSource)) continue;
ExpressionDataSource ds = (ExpressionDataSource)o;
if (name.equals(DataInfo.getLabel(ds)) || name.equals(DataInfo.getAlias(ds))) {
ds.changeExpression(exp, node);
newSource = false;
ExpressionPanel.this.sourceTree.setSelectedValue(ds);
TreeNode tn = (TreeNode)(ExpressionPanel.this.sourceTree.getSelectionPath().getLastPathComponent());
((DefaultTreeModel)(ExpressionPanel.this.sourceTree.getModel())).nodeChanged(tn);
break;