Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented ExportToCSV UI for moltable.

  • Loading branch information...
commit f20360afcbebdd7f1153339f252c3d3e02ea999f 1 parent a917ee1
@olas olas authored jonalv committed
View
27 plugins/net.bioclipse.cdk.ui.sdfeditor/plugin.xml
@@ -119,6 +119,11 @@
name="Extract Molecules">
</command>
<command
+ defaultHandler="net.bioclipse.cdk.ui.sdfeditor.handlers.ExportCSV"
+ id="net.bioclipse.cdk.moltable.exportcsv"
+ name="Export to CSV">
+ </command>
+ <command
defaultHandler="net.bioclipse.cdk.ui.sdfeditor.handlers.RemoveColumnHandler"
id="net.bioclipse.cdk.molTable.column.remove"
name="Remove Column">
@@ -242,7 +247,27 @@
</iterate>
</visibleWhen>
</command>
- </menuContribution>
+ <command
+ commandId="net.bioclipse.cdk.moltable.exportcsv"
+ label="Export to CSV"
+ style="push">
+ <visibleWhen
+ checkEnabled="true">
+ <iterate>
+ <or>
+ <instanceof
+ value="net.bioclipse.cdk.domain.ICDKMolecule">
+ </instanceof>
+ <instanceof
+ value="net.bioclipse.cdk.ui.sdfeditor.editor.MoleculeTableViewer$MolTableElement">
+ </instanceof>
+ <instanceof
+ value="net.bioclipse.cdk.ui.sdfeditor.editor.MoleculeTableViewer$MolTableSelection">
+ </instanceof>
+ </or>
+ </iterate>
+ </visibleWhen>
+ </command> </menuContribution>
</extension>
<extension
point="net.bioclipse.scripting.contribution">
View
8 ...e.cdk.ui.sdfeditor/src/net/bioclipse/cdk/ui/sdfeditor/business/IMoleculeTableManager.java
@@ -10,6 +10,7 @@
******************************************************************************/
package net.bioclipse.cdk.ui.sdfeditor.business;
+import java.io.IOException;
import java.util.Collection;
import net.bioclipse.cdk.domain.ICDKMolecule;
@@ -25,6 +26,8 @@
import net.bioclipse.managers.business.IBioclipseManager;
import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
public interface IMoleculeTableManager extends IBioclipseManager {
@@ -90,4 +93,9 @@ public void calculatePropertiesFor( IFile file,
@PublishedMethod( params="IMoleculesEditorModel mol, String filename",
methodSummary="Saves the molecule list as a file as CSV")
public void saveAsCSV(IMoleculesEditorModel model, String file) throws Exception;
+
+ public void saveAsCSV( IMoleculesEditorModel model,IFile file, IProgressMonitor monitor)
+ throws BioclipseException, IOException, CoreException;
+
+
}
View
5 ...se.cdk.ui.sdfeditor/src/net/bioclipse/cdk/ui/sdfeditor/business/MoleculeTableManager.java
@@ -627,7 +627,10 @@ public void saveAsCSV( IMoleculesEditorModel model,IFile file, IProgressMonitor
header.append( "\n" );
if(submon.isCanceled()) throw new OperationCanceledException();
ByteArrayInputStream input = new ByteArrayInputStream( header.toString().getBytes() );
- file.create(input, true, submon.newChild( 200 ));
+
+ //We allow for overwriting existing files
+ if (!file.exists())
+ file.create(input, true, submon.newChild( 200 ));
submon.setWorkRemaining( model.getNumberOfMolecules()*100 );
for(int i=0;i<model.getNumberOfMolecules();i++) {
View
119 ...net.bioclipse.cdk.ui.sdfeditor/src/net/bioclipse/cdk/ui/sdfeditor/handlers/ExportCSV.java
@@ -0,0 +1,119 @@
+package net.bioclipse.cdk.ui.sdfeditor.handlers;
+
+import java.io.File;
+
+import net.bioclipse.cdk.ui.sdfeditor.Activator;
+import net.bioclipse.cdk.ui.sdfeditor.business.IMoleculeTableManager;
+import net.bioclipse.cdk.ui.sdfeditor.editor.MoleculesEditor;
+import net.bioclipse.cdk.ui.sdfeditor.editor.MultiPageMoleculesEditorPart;
+import net.bioclipse.cdk.ui.views.IMoleculesEditorModel;
+import net.bioclipse.core.ResourcePathTransformer;
+import net.bioclipse.core.util.LogUtils;
+
+import org.apache.log4j.Logger;
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.handlers.HandlerUtil;
+
+/**
+ *
+ * @author ola
+ *
+ */
+public class ExportCSV extends AbstractHandler {
+
+ Logger logger = Logger.getLogger( ExportCSV.class );
+
+ @Override
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+
+ IEditorPart editorPart = HandlerUtil.getActiveEditor(event);
+
+
+ if(editorPart!=null && editorPart instanceof MultiPageMoleculesEditorPart) {
+
+ MultiPageMoleculesEditorPart mpmep = (MultiPageMoleculesEditorPart)
+ editorPart;
+ MoleculesEditor editor = (MoleculesEditor) mpmep
+ .getAdapter( MoleculesEditor.class );
+ // FIXME there can be other models besides SDFIndexEditorModel
+ if(editor!=null) {
+ final IMoleculesEditorModel model = findModel( editor );
+ if(model==null) {
+ logger.warn( "Failed to export to CSV, model is null" );
+ return null;
+ }
+
+ final Shell shell = HandlerUtil.getActiveShell(event);
+ final String name=editor.getEditorInput().getName();
+
+ //Open dialog and ask where to save
+ Display.getDefault().asyncExec( new Runnable() {
+
+ public void run() {
+ String path = showDialog(name , shell );
+ if (path==null)
+ return;
+
+ logger.debug("User entered file: " + path);
+
+ IMoleculeTableManager moltable = Activator.getDefault().getMoleculeTableManager();
+
+ try {
+ File file=new File(path);
+ if (!file.exists())
+ file.createNewFile();
+
+ IFile ifile = ResourcePathTransformer.getInstance().transform(path);
+
+ moltable.saveAsCSV(model, ifile, new NullProgressMonitor());
+ } catch (Exception e) {
+ LogUtils.handleException(e, logger, Activator.PLUGIN_ID);
+ }
+ }
+ });
+ }
+ }else {
+ logger.warn( "Failed to export to CSV" );
+ }
+
+ return null;
+ }
+
+ private IMoleculesEditorModel findModel(MoleculesEditor editor) {
+ return editor.getModel();
+ }
+
+ private String showDialog(String name, Shell shell) {
+
+ if (name==null || name.length()<=0)
+ name="moltable_export";
+ String destFile = name + ".csv";
+
+ FileDialog dialog = new FileDialog(shell,
+ SWT.SAVE | SWT.SHEET);
+ dialog.setFileName( destFile );
+ dialog.setText("Export CSV");
+ dialog.setFilterPath(System.getProperty( "user.home" ));
+ dialog.setFilterIndex( 1 );
+ String selectedDirectoryName = dialog.open();
+
+ if ( selectedDirectoryName == null ) {
+ return null;
+ }
+
+ int dot = selectedDirectoryName.lastIndexOf( "." );
+ if(dot==-1)
+ selectedDirectoryName = selectedDirectoryName+".csv";
+
+ return selectedDirectoryName;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.