Skip to content
Snippets Groups Projects
Commit 41a84b70 authored by Céline  TREBEAU's avatar Céline TREBEAU
Browse files

feat(csv-export): allow user to choose save location for embedded batch mode CSV

- Integrated FileChooser in save mode to let users pick where to save the example.csv
- Extracts the embedded CSV from resources and writes it to the selected location
- Automatically opens the file in the default system editor after saving
parent 65503f32
No related branches found
No related tags found
2 merge requests!47Feature : Multi-channel and Binning,!41Resolve "Consider rescaling (binning) large images"
......@@ -45,6 +45,8 @@ import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Label;
import javafx.scene.control.MenuBar;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;
......@@ -53,9 +55,10 @@ import net.imglib2.type.numeric.RealType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.awt.*;
import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
......@@ -122,7 +125,7 @@ public class MainController< T extends RealType< T > & NativeType< T > > impleme
constructionController.getModel().getChannelInput().bind( preprocessingController.getModel().getInput_channel() );
constructionController.getModel().getInput().bind( preprocessingController.getModel().getInput() );
constructionController.getModel().getFactory().bind( preprocessingController.getModel().getFactory() );
projectionController.getModel().getColors().bind( preprocessingController.getModel().getColors());
projectionController.getModel().getColors().bind( preprocessingController.getModel().getColors() );
// Sets the projection display when the projection tab is clicked on.
tabPane.getTabs().get( 2 ).selectedProperty().addListener( ( observableValue, number, newValue ) ->
......@@ -159,40 +162,95 @@ public class MainController< T extends RealType< T > & NativeType< T > > impleme
}
@FXML
private void openFileDialog() {
private void openFileDialog()
{
BatchModeDialog dialog = new BatchModeDialog();
// Create a new Stage for the dialog
Stage dialogStage = new Stage();
dialogStage.setTitle("Batch Mode");
dialogStage.initModality( Modality.APPLICATION_MODAL); // Blocks interaction with main window
dialogStage.setScene(new Scene(dialog));
dialogStage.setTitle( "Batch Mode" );
dialogStage.initModality( Modality.APPLICATION_MODAL ); // Blocks interaction with main window
dialogStage.setScene( new Scene( dialog ) );
// Show the dialog window
dialogStage.showAndWait(); // Use showAndWait() to wait until the dialog is closed
}
/**
* Prompts the user to save the embedded CSV file wherever they want.
* Opens the file after saving, if possible.
*/
@FXML
public void openCSVExample()
public void saveCSVExample()
{
// Open the example CSV file in the default system text editor
// Create FileChooser in SAVE mode
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle( "Save Example CSV File" );
fileChooser.getExtensionFilters().add( new FileChooser.ExtensionFilter( "CSV Files", "*.csv" ) );
fileChooser.setInitialFileName( "batch_mode_csv_example.csv" );
// Show the save dialog
File selectedFile = fileChooser.showSaveDialog( null );
if ( selectedFile != null )
{
// Try extracting the embedded file to the chosen location
try ( InputStream inputStream = getClass().getResourceAsStream( "/batch_mode_csv_example.csv" );
OutputStream outputStream = Files.newOutputStream( selectedFile.toPath() ) )
{
// You can specify the initial directory where the file should be saved or loaded from
fileChooser.setInitialDirectory( new java.io.File( "src/main/resources/" ) );
if ( inputStream == null )
{
throw new FileNotFoundException( "Embedded resource 'batch_mode_csv_example.csv' not found." );
}
// Open the file
File file = fileChooser.showSaveDialog( null);
if ( file != null )
// Copy contents from resource to selected file
byte[] buffer = new byte[ 1024 ];
int length;
while ( ( length = inputStream.read( buffer ) ) != - 1 )
{
outputStream.write( buffer, 0, length );
}
LOGGER.debug( "File saved to: {}", selectedFile.getAbsolutePath() );
// Optionally open the file
openFile( selectedFile );
}
catch ( IOException e )
{
LOGGER.debug("Something went wrong with the batch-mode CSV example");
}
}
else
{
// Perform the necessary file operations here (e.g., writing to file)
System.out.println( "Save cancelled by user." );
}
}
/**
* Opens the file in the system's default app (e.g. Notepad, Excel).
*
* @param file The file to open.
*/
private void openFile( File file )
{
if ( Desktop.isDesktopSupported() )
{
try
{
Desktop.getDesktop().open( file );
}
catch ( IOException e )
{
LOGGER.debug( "Failed to open file: {}", file.getAbsolutePath() );
}
}
}
@FXML
public void saveParameters() throws IOException
{
......@@ -201,7 +259,7 @@ public class MainController< T extends RealType< T > & NativeType< T > > impleme
File file = fileChooser.showSaveDialog( null );
if ( file != null )
{
PretreatmentParameters prp = build(model.getPreprocessingModel());
PretreatmentParameters prp = build( model.getPreprocessingModel() );
SelectionParameters sp = build( model.getSelectionModel() );
ConstructionParameters[] cp = build( model.getConstructionModel() );
ArrayList< ProjectionParameters > pp = build( model.getProjectionModel() );
......@@ -210,17 +268,16 @@ public class MainController< T extends RealType< T > & NativeType< T > > impleme
}
private PretreatmentParameters build( PreprocessingModel<T> model)
private PretreatmentParameters build( PreprocessingModel< T > model )
{
return new PretreatmentParameters(model.getChannel().get(), model.binProperty().get() );
return new PretreatmentParameters( model.getChannel().get(), model.binProperty().get() );
}
private SelectionParameters build( SelectionModel model )
{
return new SelectionParameters(
model.amplitudeProperty().getValue().intValue(),
model.otsuProperty().getValue().intValue(),
model.amplitudeProperty().getValue(),
model.otsuProperty().getValue(),
model.xyBlurProperty().intValue(),
model.zBlurProperty().intValue(),
model.islandProperty().intValue() );
......@@ -228,13 +285,13 @@ public class MainController< T extends RealType< T > & NativeType< T > > impleme
private ConstructionParameters[] build( ConstructionModel< T > model )
{
ConstructionParameters[] parameters = new ConstructionParameters[2];
parameters[0] = new ConstructionParameters(
ConstructionParameters[] parameters = new ConstructionParameters[ 2 ];
parameters[ 0 ] = new ConstructionParameters(
model.c1Property().value(),
model.r1Property().getValue(),
model.st1Property().value(),
model.surfaceSizeProperty().get() );
parameters[1] = new ConstructionParameters(
parameters[ 1 ] = new ConstructionParameters(
model.c2Property().value(),
model.r2Property().getValue(),
model.st2Property().value(),
......@@ -242,34 +299,29 @@ public class MainController< T extends RealType< T > & NativeType< T > > impleme
return parameters;
}
private ArrayList< ProjectionParameters > build ( ProjectionModel<T> model)
private ArrayList< ProjectionParameters > build( ProjectionModel< T > model )
{
ArrayList<ProjectionParameters > list = new ArrayList<>();
for ( int i = 0; i < getProjectionController().getModel().getReferenceSurfaces().get().size() ; i++ )
ArrayList< ProjectionParameters > list = new ArrayList<>();
for ( int i = 0; i < getProjectionController().getModel().getReferenceSurfaces().get().size(); i++ )
{
ProjectionProperties<T> projectionProperties = model.getProjectionProperties().get().get( i );
ArrayList<ChannelProjectionParameter> channelProjectionParameters = new ArrayList<>();
ProjectionProperties< T > projectionProperties = model.getProjectionProperties().get().get( i );
ArrayList< ChannelProjectionParameter > channelProjectionParameters = new ArrayList<>();
for ( int j = 0; j < projectionProperties.getProperties().get().size(); j++ )
{
ChannelToolProperty<T> channelToolProperty = projectionProperties.getProperties().get().get( j );
ChannelToolProperty< T > channelToolProperty = projectionProperties.getProperties().get().get( j );
ChannelProjectionParameter cpp = new ChannelProjectionParameter(
channelToolProperty.getDeltaZ().get(),
channelToolProperty.getOffset().get(),
channelToolProperty.getMethod().get());
channelProjectionParameters.add(cpp);
channelToolProperty.getMethod().get() );
channelProjectionParameters.add( cpp );
}
ProjectionParameters projectionParameter= new ProjectionParameters( channelProjectionParameters );
list.add(projectionParameter);
ProjectionParameters projectionParameter = new ProjectionParameters( channelProjectionParameters );
list.add( projectionParameter );
}
return list;
}
private File chooseFile()
{
FileChooser fileChooser = chooseAFile( null, "Select a Zellige parameter file"
......@@ -289,7 +341,7 @@ public class MainController< T extends RealType< T > & NativeType< T > > impleme
private void setParameters( ZelligeParameters parameters )
{
preprocessingController.setParameters(parameters);
preprocessingController.setParameters( parameters );
selectionController.setParameters( parameters );
constructionController.setParameters( parameters );
projectionController.setParameters( parameters );
......
......@@ -32,7 +32,7 @@
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="Zellige Wiki" />
<MenuItem mnemonicParsing="false" onAction="#openCSVExample" text="Batch Mode CSV file example" />
<MenuItem mnemonicParsing="false" onAction="#saveCSVExample" text="Batch Mode CSV file example" />
</items>
</Menu>
</menus>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment