|
| 1 | +package org.janelia.workstation.controller.action; |
| 2 | + |
| 3 | +import org.janelia.model.domain.DomainObject; |
| 4 | +import org.janelia.model.domain.Reference; |
| 5 | +import org.janelia.model.domain.tiledMicroscope.TmMappedNeuron; |
| 6 | +import org.janelia.model.domain.tiledMicroscope.TmSample; |
| 7 | +import org.janelia.model.domain.tiledMicroscope.TmWorkspace; |
| 8 | +import org.janelia.workstation.common.actions.BaseContextualNodeAction; |
| 9 | +import org.janelia.workstation.controller.TmViewerManager; |
| 10 | +import org.janelia.workstation.controller.access.TiledMicroscopeDomainMgr; |
| 11 | +import org.janelia.workstation.core.api.AccessManager; |
| 12 | +import org.janelia.workstation.core.api.DomainMgr; |
| 13 | +import org.janelia.workstation.core.workers.BackgroundWorker; |
| 14 | +import org.janelia.workstation.core.workers.SimpleWorker; |
| 15 | +import org.janelia.workstation.integration.util.FrameworkAccess; |
| 16 | +import org.openide.awt.ActionID; |
| 17 | +import org.openide.awt.ActionReference; |
| 18 | +import org.openide.awt.ActionReferences; |
| 19 | +import org.openide.awt.ActionRegistration; |
| 20 | +import org.openide.util.NbBundle; |
| 21 | +import org.openide.util.actions.SystemAction; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | + |
| 25 | +import javax.swing.*; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +/** |
| 30 | + * Launches the Data Viewer from a context-menu. |
| 31 | + */ |
| 32 | +@ActionID( |
| 33 | + category = "actions", |
| 34 | + id = "DeleteHortaWorkspaceAction" |
| 35 | +) |
| 36 | +@ActionRegistration( |
| 37 | + displayName = "#CTL_DeleteHortaWorkspaceAction", |
| 38 | + lazy = false |
| 39 | +) |
| 40 | +@ActionReferences({ |
| 41 | + @ActionReference(path = "Menu/Actions/Horta", position = 1510, separatorBefore = 1499) |
| 42 | +}) |
| 43 | +@NbBundle.Messages("CTL_DeleteHortaWorkspaceAction=Delete Workspace") |
| 44 | +public class DeleteHortaWorkspaceAction extends BaseContextualNodeAction { |
| 45 | + |
| 46 | + private static final Logger log = LoggerFactory.getLogger(DeleteHortaWorkspaceAction.class); |
| 47 | + private DomainObject domainObject; |
| 48 | + |
| 49 | + public static DeleteHortaWorkspaceAction get() { |
| 50 | + return SystemAction.get(DeleteHortaWorkspaceAction.class); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected void processContext() { |
| 55 | + if (getNodeContext().isSingleObjectOfType(TmWorkspace.class)) { |
| 56 | + if (AccessManager.getAccessManager().isAdmin() || |
| 57 | + domainObject.getOwnerKey().equals(AccessManager.getSubjectKey())) { |
| 58 | + domainObject = getNodeContext().getSingleObjectOfType(TmWorkspace.class); |
| 59 | + setEnabledAndVisible(true); |
| 60 | + } |
| 61 | + } |
| 62 | + else { |
| 63 | + domainObject = null; |
| 64 | + setEnabledAndVisible(false); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public void setDomainObject(DomainObject obj) { |
| 69 | + domainObject = obj; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void performAction() { |
| 74 | + |
| 75 | + DomainObject objectToOpen = domainObject; |
| 76 | + if (objectToOpen==null) { |
| 77 | + log.warn("Action performed with null domain object"); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + if (domainObject instanceof TmWorkspace) { |
| 82 | + TmWorkspace workspace = (TmWorkspace) this.domainObject; |
| 83 | + |
| 84 | + BackgroundWorker deleter = new BackgroundWorker() { |
| 85 | + @Override |
| 86 | + public String getName() { |
| 87 | + return "Deleting TmWorkspaces"; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + protected void doStuff() { |
| 92 | + TiledMicroscopeDomainMgr domainMgr = TiledMicroscopeDomainMgr.getDomainMgr(); |
| 93 | + List<Long> workspaceIds = new ArrayList<>(); |
| 94 | + workspaceIds.add(workspace.getId()); |
| 95 | + domainMgr.removeWorkspaces(workspaceIds); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + protected void hadSuccess() { |
| 100 | + JOptionPane.showMessageDialog( |
| 101 | + null, |
| 102 | + "Selected workspaces deleted successfully.", |
| 103 | + "Success", JOptionPane.INFORMATION_MESSAGE); |
| 104 | + }; |
| 105 | + |
| 106 | + @Override |
| 107 | + protected void hadError(Throwable error) { |
| 108 | + log.error("Error deleting workspaces", error); |
| 109 | + JOptionPane.showMessageDialog( |
| 110 | + null, |
| 111 | + "Failed to delete workspaces: " + error.getMessage(), "Error", |
| 112 | + JOptionPane.ERROR_MESSAGE); |
| 113 | + } |
| 114 | + }; |
| 115 | + deleter.executeWithEvents(); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
0 commit comments