Skip to content

Commit

Permalink
Support exporting bundle contents via context menus
Browse files Browse the repository at this point in the history
  • Loading branch information
Col-E committed Jan 4, 2025
1 parent 17371d7 commit 547031b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import software.coley.recaf.ui.control.popup.DecompileAllPopup;
import software.coley.recaf.workspace.model.Workspace;
import software.coley.recaf.workspace.model.bundle.Bundle;
import software.coley.recaf.workspace.model.bundle.FileBundle;
import software.coley.recaf.workspace.model.bundle.JvmClassBundle;
import software.coley.recaf.workspace.model.resource.WorkspaceResource;

Expand All @@ -34,10 +35,10 @@ public class BasicBundleContextMenuProviderFactory extends AbstractContextMenuPr

@Inject
public BasicBundleContextMenuProviderFactory(@Nonnull TextProviderService textService,
@Nonnull IconProviderService iconService,
@Nonnull Instance<DecompileAllPopup> decompileAllPaneProvider,
@Nonnull Instance<ChangeClassVersionForAllPopup> changeClassVersionProvider,
@Nonnull Actions actions) {
@Nonnull IconProviderService iconService,
@Nonnull Instance<DecompileAllPopup> decompileAllPaneProvider,
@Nonnull Instance<ChangeClassVersionForAllPopup> changeClassVersionProvider,
@Nonnull Actions actions) {
super(textService, iconService, actions);
this.decompileAllPaneProvider = decompileAllPaneProvider;
this.changeClassVersionProvider = changeClassVersionProvider;
Expand All @@ -46,9 +47,9 @@ public BasicBundleContextMenuProviderFactory(@Nonnull TextProviderService textSe
@Nonnull
@Override
public ContextMenuProvider getBundleContextMenuProvider(@Nonnull ContextSource source,
@Nonnull Workspace workspace,
@Nonnull WorkspaceResource resource,
@Nonnull Bundle<? extends Info> bundle) {
@Nonnull Workspace workspace,
@Nonnull WorkspaceResource resource,
@Nonnull Bundle<? extends Info> bundle) {
return () -> {
TextProvider nameProvider = textService.getBundleTextProvider(workspace, resource, bundle);
IconProvider iconProvider = iconService.getBundleIconProvider(workspace, resource, bundle);
Expand All @@ -59,6 +60,7 @@ public ContextMenuProvider getBundleContextMenuProvider(@Nonnull ContextSource s
edit.item("misc.clear", TRASH_CAN, bundle::clear);

if (bundle instanceof JvmClassBundle jvmBundle) {
builder.item("menu.export.classes", DOCUMENT_EXPORT, () -> actions.exportClasses(workspace, resource, jvmBundle));
builder.item("menu.file.decompileall", DOCUMENT_EXPORT, () -> {
DecompileAllPopup popup = decompileAllPaneProvider.get();
popup.setTargetBundle(jvmBundle);
Expand All @@ -69,6 +71,8 @@ public ContextMenuProvider getBundleContextMenuProvider(@Nonnull ContextSource s
popup.setTargetBundle(jvmBundle);
popup.show();
});
} else if (bundle instanceof FileBundle fileBundle) {
builder.item("menu.export.files", DOCUMENT_EXPORT, () -> actions.exportFiles(workspace, resource, fileBundle));
}
return menu;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,26 @@ else if (cls.getName().startsWith(packageName + "/"))
pathExportingManager.export(workspaceCopy, "package", false);
}

/**
* Exports all classes in a bundle, prompting the user to select a location to save the file to.
*
* @param workspace
* Containing workspace.
* @param resource
* Containing resource.
* @param bundle
* Bundle with contents to export.
*/
public void exportClasses(@Nonnull Workspace workspace,
@Nonnull WorkspaceResource resource,
@Nonnull JvmClassBundle bundle) {
BasicJvmClassBundle bundleCopy = new BasicJvmClassBundle();
bundle.valuesAsCopy().forEach(bundleCopy::initialPut);
WorkspaceResource resourceCopy = new WorkspaceResourceBuilder().withJvmClassBundle(bundleCopy).build();
Workspace workspaceCopy = new BasicWorkspace(resourceCopy);
pathExportingManager.export(workspaceCopy, "bundle", false);
}

/**
* Exports a directory, prompting the user to select a location to save the file to.
*
Expand Down Expand Up @@ -1751,6 +1771,26 @@ else if (cls.getName().startsWith(directoryName + "/"))
pathExportingManager.export(workspaceCopy, "directory", false);
}

/**
* Exports all files in a bundle, prompting the user to select a location to save the file to.
*
* @param workspace
* Containing workspace.
* @param resource
* Containing resource.
* @param bundle
* Bundle with contents to export.
*/
public void exportFiles(@Nonnull Workspace workspace,
@Nonnull WorkspaceResource resource,
@Nonnull FileBundle bundle) {
BasicFileBundle bundleCopy = new BasicFileBundle();
bundle.valuesAsCopy().forEach(bundleCopy::initialPut);
WorkspaceResource resourceCopy = new WorkspaceResourceBuilder().withFileBundle(bundleCopy).build();
Workspace workspaceCopy = new BasicWorkspace(resourceCopy);
pathExportingManager.export(workspaceCopy, "bundle", false);
}

/**
* Prompts the user <i>(if configured, otherwise prompt is skipped)</i> to delete the class.
*
Expand Down
2 changes: 2 additions & 0 deletions recaf-ui/src/main/resources/translations/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ menu.edit.changeversion.up=Upgrade
menu.edit.changeversion.down=Downgrade
menu.export.class=Export class
menu.export.file=Export file
menu.export.classes=Export classes
menu.export.files=Export files
menu.export.package=Export package
menu.export.directory=Export directory
menu.help=Help
Expand Down

0 comments on commit 547031b

Please sign in to comment.