Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow deletion of views via the UI #1193

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/org/embl/mobie/lib/bdv/view/SliceViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class SliceViewer
public static final String UNDO_SEGMENT_SELECTIONS = "Undo Segment Selections [ Ctrl Shift N ]";
public static final String LOAD_ADDITIONAL_VIEWS = "Load Additional Views";
public static final String SAVE_CURRENT_SETTINGS_AS_VIEW = "Save Current View";
public static final String DELETE_VIEW = "Delete View";
public static final String FRAME_TITLE = "MoBIE BigDataViewer";
public static boolean tileRenderOverlay = false;
private final SourceAndConverterBdvDisplayService bdvDisplayService;
Expand Down Expand Up @@ -142,6 +143,10 @@ private void installContextMenuAndKeyboardShortCuts( )
moBIE.getViewManager().getViewsSaver().saveViewDialog( view );
} );

sacService.registerAction( DELETE_VIEW, sourceAndConverters -> {
moBIE.getViewManager().getViewsDeleter().deleteViewDialog();
});

final Set< String > actionsKeys = sacService.getActionsKeys();
final ArrayList< String > actions = new ArrayList< String >();
actions.add( SourceAndConverterService.getCommandName( SourcesInfoCommand.class ) );
Expand All @@ -157,6 +162,7 @@ private void installContextMenuAndKeyboardShortCuts( )
actions.add( UNDO_SEGMENT_SELECTIONS );
actions.add( LOAD_ADDITIONAL_VIEWS );
actions.add( SAVE_CURRENT_SETTINGS_AS_VIEW );
actions.add( DELETE_VIEW );

if ( projectCommands != null )
{
Expand Down
28 changes: 12 additions & 16 deletions src/main/java/org/embl/mobie/lib/create/ProjectCreatorHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,7 @@ public static boolean is2D( ImageData< ? > imageData ) {
* @return Map of ui selection group names to array of view names
*/
public static Map<String, ArrayList<String>> getGroupToViewsMap( Dataset dataset ) {
Map<String, ArrayList<String>> groupToViewsMap = new HashMap<>();
for ( String viewName: dataset.views().keySet() ) {
View view = dataset.views().get( viewName );
String group = view.getUiSelectionGroup();
if ( !groupToViewsMap.containsKey( group ) ) {
ArrayList<String> views = new ArrayList<>();
views.add( viewName );
groupToViewsMap.put( group, views );
} else {
groupToViewsMap.get( group ).add( viewName );
}
}

return groupToViewsMap;
return getGroupToViewsMap( dataset.views() );
}

/**
Expand All @@ -178,9 +165,18 @@ public static Map<String, ArrayList<String>> getGroupToViewsMap( Dataset dataset
* @return Map of ui selection group names to array of view names
*/
public static Map<String, ArrayList<String>> getGroupToViewsMap( AdditionalViews additionalViews ) {
return getGroupToViewsMap(additionalViews.views);
}

/**
* Get mapping of ui selection groups (i.e. MoBIE dropdown menu names) to views for given views
* @param viewNameToView map of view names to view
* @return Map of ui selection group names to array of view names
*/
public static Map<String, ArrayList<String>> getGroupToViewsMap( Map<String, View> viewNameToView ) {
Map<String, ArrayList<String>> groupToViewsMap = new HashMap<>();
for ( String viewName: additionalViews.views.keySet() ) {
View view = additionalViews.views.get( viewName );
for ( String viewName: viewNameToView.keySet() ) {
View view = viewNameToView.get( viewName );
String group = view.getUiSelectionGroup();
if ( !groupToViewsMap.containsKey( group ) ) {
ArrayList<String> views = new ArrayList<>();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/embl/mobie/lib/view/ViewManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.embl.mobie.lib.transform.TransformHelper;
import org.embl.mobie.lib.transform.ImageTransformer;
import org.embl.mobie.lib.transform.viewer.*;
import org.embl.mobie.lib.view.delete.ViewDeleter;
import org.embl.mobie.ui.UserInterface;
import org.embl.mobie.ui.WindowArrangementHelper;
import org.embl.mobie.lib.view.save.ViewSaver;
Expand Down Expand Up @@ -88,6 +89,7 @@ public class ViewManager
private final UniverseManager universeManager;
private final AdditionalViewsLoader additionalViewsLoader;
private final ViewSaver viewSaver;
private final ViewDeleter viewDeleter;

public ViewManager( MoBIE moBIE, UserInterface userInterface, boolean is2D )
{
Expand All @@ -98,6 +100,7 @@ public ViewManager( MoBIE moBIE, UserInterface userInterface, boolean is2D )
universeManager = new UniverseManager();
additionalViewsLoader = new AdditionalViewsLoader( moBIE );
viewSaver = new ViewSaver( moBIE );
viewDeleter = new ViewDeleter( moBIE );
sacService = ( SourceAndConverterService ) SourceAndConverterServices.getSourceAndConverterService();
}

Expand Down Expand Up @@ -192,6 +195,8 @@ public SliceViewer getSliceViewer()

public ViewSaver getViewsSaver() { return viewSaver; }

public ViewDeleter getViewsDeleter() { return viewDeleter; }

private void addImageTransforms( List< Transformation > transformations,
List< ? extends Image< ? > > images )
{
Expand Down
136 changes: 136 additions & 0 deletions src/main/java/org/embl/mobie/lib/view/delete/ViewDeleter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package org.embl.mobie.lib.view.delete;

import ij.IJ;
import org.apache.commons.lang.NotImplementedException;
import org.embl.mobie.MoBIE;
import org.embl.mobie.lib.io.FileLocation;
import org.embl.mobie.lib.serialize.AdditionalViewsJsonParser;
import org.embl.mobie.lib.serialize.Dataset;
import org.embl.mobie.lib.serialize.DatasetJsonParser;
import org.embl.mobie.lib.serialize.View;
import org.embl.mobie.lib.view.AdditionalViews;
import org.embl.mobie.lib.view.save.SelectExistingViewDialog;
import org.embl.mobie.ui.UserInterfaceHelper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.embl.mobie.io.github.GitHubUtils.isGithub;
import static org.embl.mobie.io.util.S3Utils.isS3;

public class ViewDeleter {

static { net.imagej.patcher.LegacyInjector.preinit(); }

private MoBIE moBIE;

/**
* Delete views from the current dataset, or an external file
*/
public ViewDeleter( MoBIE moBIE ) {
this.moBIE = moBIE;
}

public void deleteViewDialog()
{
new Thread( () -> {
try {
FileLocation fileLocation = UserInterfaceHelper.loadFromProjectOrFileSystemDialog("Delete from");

if ( fileLocation == FileLocation.CurrentProject ) {
removeViewsFromCurrentProject();
} else if ( fileLocation == FileLocation.ExternalFile ) {
removeViewsFromExternalFile();
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}

private void removeViewsFromCurrentProject() throws IOException {
String datasetJson = moBIE.absolutePath( "dataset.json");

if ( isGithub(datasetJson) || isS3(datasetJson) ) {
throw new NotImplementedException("View deletion is only implemented for local projects");
}

// Read views directly from dataset json rather than from MoBIE.getViews() (otherwise could include
// views loaded from external files via Load Additional Views)
Dataset dataset = new DatasetJsonParser().parseDataset( datasetJson );
Map<String, View> views = dataset.views();
// Remove default view, as this shouldn't be deleted
views.remove( View.DEFAULT );

if ( views.isEmpty() ) {
IJ.log("No valid views in dataset - can't remove default view");
return;
}

Map<String, View> viewsToRemove = selectViewsToRemove( views );
if ( viewsToRemove == null ) {
return;
}
removeViewsFromDatasetJson( viewsToRemove, datasetJson );
removeViewsFromUI( viewsToRemove );
}

private void removeViewsFromExternalFile() throws IOException {
String selectedFilePath = UserInterfaceHelper.selectFilePath( "json", "View", true );
if ( selectedFilePath == null ) {
return;
}

Map<String, View> views = new AdditionalViewsJsonParser().getViews( selectedFilePath ).views;
if ( views.isEmpty() ) {
IJ.log("No valid views in file");
return;
}

Map<String, View> viewsToRemove = selectViewsToRemove( views );
if ( viewsToRemove == null ) {
return;
}
removeViewsFromAdditionalViewsJson( viewsToRemove, selectedFilePath );
removeViewsFromUI( viewsToRemove );
}

private Map<String, View> selectViewsToRemove( Map<String, View> views ) {
String selectedView = new SelectExistingViewDialog( views ).getSelectedView(
"Choose a view to delete..."
);
if ( selectedView == null ) {
return null;
}

Map<String, View> viewsToRemove = new HashMap<>();
viewsToRemove.put( selectedView, views.get(selectedView) );

return viewsToRemove;
}

public void removeViewsFromDatasetJson( Map<String, View> views, String datasetJsonPath) throws IOException {
Dataset dataset = new DatasetJsonParser().parseDataset( datasetJsonPath );
dataset.views().keySet().removeAll( views.keySet() );

new DatasetJsonParser().saveDataset( dataset, datasetJsonPath );
IJ.log( "Views \"" + views.keySet() + "\" removed from dataset.json" );
}

public void removeViewsFromAdditionalViewsJson( Map<String, View> views, String jsonPath ) throws IOException
{
AdditionalViews additionalViews = new AdditionalViewsJsonParser().getViews( jsonPath );
additionalViews.views.keySet().removeAll( views.keySet() );

new AdditionalViewsJsonParser().saveViews( additionalViews, jsonPath );
IJ.log( "Views \"" + views.keySet() + "\" removed from " + jsonPath );
}

public void removeViewsFromUI( Map<String, View> views )
{
moBIE.getViews().keySet().removeAll( views.keySet() );
moBIE.getUserInterface().removeViews( views );
IJ.log( "The following views were removed:\n" + views.keySet() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import org.embl.mobie.lib.create.ProjectCreatorHelper;
import org.embl.mobie.lib.serialize.Dataset;
import org.embl.mobie.lib.serialize.View;
import org.embl.mobie.ui.MoBIELaf;
import org.embl.mobie.ui.SwingHelper;
import org.embl.mobie.ui.UserInterfaceHelper;
Expand All @@ -54,6 +55,7 @@ public class SelectExistingViewDialog {

private Map<String, ArrayList<String>> groupToViewsMap;
private String selectedView;
private String title = "Choose an existing view...";

// writing to dataset json
public SelectExistingViewDialog( Dataset dataset ) {
Expand All @@ -65,6 +67,15 @@ public SelectExistingViewDialog( AdditionalViews additionalViews ) {
groupToViewsMap = ProjectCreatorHelper.getGroupToViewsMap(additionalViews);
}

public SelectExistingViewDialog( Map<String, View> views ) {
groupToViewsMap = ProjectCreatorHelper.getGroupToViewsMap(views);
}

public String getSelectedView( String title ) {
this.title = title;
return getSelectedView();
}

public String getSelectedView() {
showViewSelectionUI();
return selectedView;
Expand All @@ -73,7 +84,7 @@ public String getSelectedView() {
private void showViewSelectionUI() {
MoBIELaf.MoBIELafOn();
dialog = new JDialog((Frame)null, true);
dialog.setTitle( "Choose an existing view..." );
dialog.setTitle( title );
dialog.getContentPane().setLayout( new BoxLayout(dialog.getContentPane(), BoxLayout.Y_AXIS ) );
dialog.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
createComboBoxes();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/embl/mobie/ui/UserInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ public void addViews( Map<String, View > views )
MoBIELaf.MoBIELafOff();
}

public void removeViews( Map<String, View > views ) {
userInterfaceHelper.removeViewsFromViewSelectionPanel( views );
refreshSelection();
}

public Map< String, Map< String, View > > getGroupingsToViews()
{
return userInterfaceHelper.getGroupingsToViews();
Expand Down
Loading
Loading