Skip to content

Instantly share code, notes, and snippets.

@mattyb149
Created July 10, 2015 19:17
Show Gist options
  • Save mattyb149/d462a9fb7b721b7688e5 to your computer and use it in GitHub Desktop.
Save mattyb149/d462a9fb7b721b7688e5 to your computer and use it in GitHub Desktop.
Add Drag & Drop support for CSV files in PDI
import org.w3c.dom.*
import org.pentaho.di.core.*
import org.pentaho.di.core.exception.*
import org.pentaho.di.core.gui.*
import org.pentaho.di.core.plugins.*
import org.pentaho.di.trans.step.*
import org.pentaho.di.ui.spoon.*
class CsvListener implements FileListener {
public boolean open( Node transNode, String fname, boolean importfile ) throws KettleMissingPluginsException {
def csvInputPlugin = PluginRegistry.instance.findPluginWithName(StepPluginType, 'CSV file input')
def csvInputMetaClass = PluginRegistry.instance.loadClass(csvInputPlugin)
csvInputMetaClass.setDefault()
def pid = PluginRegistry.instance.getPluginId(csvInputPlugin.pluginType, csvInputMetaClass)
def csv = new StepMeta(pid, csvInputPlugin.name, csvInputMetaClass)
csv.stepMetaInterface.setFilename(fname)
csv.setName(fname?.substring(fname?.lastIndexOf(File.separator)+1,fname?.indexOf('.')) ?: 'CSV file input')
csv?.location= new Point(20,20)
csv?.draw = true
Spoon.instance.activeTransformation?.addStep(csv)
Spoon.instance.activeTransGraph?.redraw()
true
}
public boolean save( EngineMetaInterface meta, String fname, boolean isExport ){
false
}
public void syncMetaName( EngineMetaInterface meta, String name ){
}
public boolean accepts( String fileName ){
def x = Arrays.asList(getSupportedExtensions()).contains(fileName?.substring(fileName?.indexOf('.')+1))
}
public boolean acceptsXml( String nodeName ){
false
}
public String[] getSupportedExtensions(){
['csv'] as String[]
}
public String[] getFileTypeDisplayNames( Locale locale ){
['CSV'] as String[]
}
public String getRootNodeName(){
null
}
}
Spoon.instance.addFileListener(new CsvListener())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment