|
| 1 | +package org.janelia.workstation.admin; |
| 2 | + |
| 3 | +import org.janelia.model.domain.tiledMicroscope.TmWorkspace; |
| 4 | +import org.janelia.model.domain.tiledMicroscope.TmWorkspaceInfo; |
| 5 | +import org.janelia.workstation.common.gui.support.DesktopApi; |
| 6 | +import org.janelia.workstation.controller.access.TiledMicroscopeDomainMgr; |
| 7 | +import org.janelia.workstation.core.util.Refreshable; |
| 8 | +import org.janelia.workstation.core.workers.BackgroundWorker; |
| 9 | +import org.janelia.workstation.core.workers.SimpleWorker; |
| 10 | +import org.slf4j.Logger; |
| 11 | +import org.slf4j.LoggerFactory; |
| 12 | + |
| 13 | +import javax.swing.*; |
| 14 | +import javax.swing.table.AbstractTableModel; |
| 15 | +import java.awt.*; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.concurrent.Callable; |
| 21 | + |
| 22 | +public class DatabaseCleanupPanel extends JPanel implements Refreshable { |
| 23 | + private static final Logger log = LoggerFactory.getLogger(DatabaseCleanupPanel.class); |
| 24 | + private final AdministrationTopComponent parent; |
| 25 | + private JTable workspaceTable; |
| 26 | + private WorkspaceTableModel tableModel; |
| 27 | + |
| 28 | + public DatabaseCleanupPanel(AdministrationTopComponent parent) { |
| 29 | + this.parent = parent; |
| 30 | + refresh(); |
| 31 | + } |
| 32 | + |
| 33 | + private void setupUI() { |
| 34 | + setLayout(new BorderLayout()); |
| 35 | + removeAll(); |
| 36 | + |
| 37 | + JPanel titlePanel = new TitlePanel("Manage Workspaces", "Return To Top Menu", |
| 38 | + event -> refresh(), |
| 39 | + event -> returnHome()); |
| 40 | + add(titlePanel, BorderLayout.NORTH); |
| 41 | + |
| 42 | + tableModel = new WorkspaceTableModel(); |
| 43 | + workspaceTable = new JTable(tableModel); |
| 44 | + JScrollPane scrollPane = new JScrollPane(workspaceTable); |
| 45 | + add(scrollPane, BorderLayout.CENTER); |
| 46 | + |
| 47 | + JButton refreshButton = new JButton("Load Largest Workspaces"); |
| 48 | + refreshButton.addActionListener(e -> loadLargestWorkspaces()); |
| 49 | + |
| 50 | + JButton deleteButton = new JButton("Delete Selected Workspaces"); |
| 51 | + deleteButton.addActionListener(e -> deleteSelectedWorkspaces()); |
| 52 | + |
| 53 | + JPanel buttonPanel = new JPanel(); |
| 54 | + buttonPanel.add(refreshButton); |
| 55 | + buttonPanel.add(deleteButton); |
| 56 | + add(buttonPanel, BorderLayout.SOUTH); |
| 57 | + |
| 58 | + revalidate(); |
| 59 | + } |
| 60 | + |
| 61 | + private void loadLargestWorkspaces() { |
| 62 | + SimpleWorker worker = new SimpleWorker() { |
| 63 | + private List<TmWorkspaceInfo> workspaceResults; |
| 64 | + |
| 65 | + @Override |
| 66 | + protected void doStuff() { |
| 67 | + TiledMicroscopeDomainMgr domainMgr = TiledMicroscopeDomainMgr.getDomainMgr(); |
| 68 | + workspaceResults = domainMgr.getLargestWorkspaces(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + protected void hadSuccess() { |
| 73 | + tableModel.clear(); |
| 74 | + tableModel.setWorkspaces(workspaceResults); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + protected void hadError(Throwable error) { |
| 79 | + log.error("Error loading workspaces", error); |
| 80 | + JOptionPane.showMessageDialog(DatabaseCleanupPanel.this, |
| 81 | + "Failed to load workspaces: " + error.getMessage(), "Error", |
| 82 | + JOptionPane.ERROR_MESSAGE); |
| 83 | + } |
| 84 | + }; |
| 85 | + worker.execute(); |
| 86 | + } |
| 87 | + |
| 88 | + private void deleteSelectedWorkspaces() { |
| 89 | + List<Long> selectedWorkspaces = tableModel.getSelectedWorkspaces(); |
| 90 | + if (selectedWorkspaces.isEmpty()) { |
| 91 | + JOptionPane.showMessageDialog(this, "Please select at least one workspace to delete.", "No Selection", JOptionPane.WARNING_MESSAGE); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + int confirmation = JOptionPane.showConfirmDialog(this, |
| 96 | + "Are you sure you want to delete the selected workspaces?", |
| 97 | + "Confirm Deletion", JOptionPane.YES_NO_OPTION); |
| 98 | + |
| 99 | + if (confirmation == JOptionPane.YES_OPTION) { |
| 100 | + |
| 101 | + BackgroundWorker deleter = new BackgroundWorker() { |
| 102 | + @Override |
| 103 | + public String getName() { |
| 104 | + return "Deleting TmWorkspaces"; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + protected void doStuff() { |
| 109 | + TiledMicroscopeDomainMgr domainMgr = TiledMicroscopeDomainMgr.getDomainMgr(); |
| 110 | + domainMgr.removeWorkspaces(selectedWorkspaces); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + protected void hadSuccess() { |
| 115 | + loadLargestWorkspaces(); |
| 116 | + JOptionPane.showMessageDialog(DatabaseCleanupPanel.this, |
| 117 | + "Selected workspaces deleted successfully.", "Success", JOptionPane.INFORMATION_MESSAGE); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + protected void hadError(Throwable error) { |
| 122 | + log.error("Error deleting workspaces", error); |
| 123 | + JOptionPane.showMessageDialog(DatabaseCleanupPanel.this, |
| 124 | + "Failed to delete workspaces: " + error.getMessage(), "Error", |
| 125 | + JOptionPane.ERROR_MESSAGE); |
| 126 | + } |
| 127 | + }; |
| 128 | + deleter.executeWithEvents(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void refresh() { |
| 134 | + setupUI(); |
| 135 | + } |
| 136 | + |
| 137 | + private void returnHome() { |
| 138 | + parent.viewTopMenu(); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +class WorkspaceTableModel extends AbstractTableModel { |
| 143 | + private final String[] columnNames = {"Select", "Name", "Owner", "Date Created", "Size"}; |
| 144 | + private List<TmWorkspaceInfo> workspaceResults = new ArrayList<>(); |
| 145 | + private final List<Boolean> selected = new ArrayList<>(); |
| 146 | + |
| 147 | + public void setWorkspaces(List<TmWorkspaceInfo> workspaceResults) { |
| 148 | + this.workspaceResults = workspaceResults; |
| 149 | + selected.clear(); |
| 150 | + for (int i = 0; i < workspaceResults.size(); i++) { |
| 151 | + selected.add(false); // Initialize selection state |
| 152 | + } |
| 153 | + fireTableDataChanged(); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public int getRowCount() { |
| 158 | + return workspaceResults.size(); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public int getColumnCount() { |
| 163 | + return columnNames.length; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public Object getValueAt(int rowIndex, int columnIndex) { |
| 168 | + TmWorkspaceInfo workspaceInfo = workspaceResults.get(rowIndex); |
| 169 | + switch (columnIndex) { |
| 170 | + case 0: |
| 171 | + return selected.get(rowIndex); // Boolean checkbox |
| 172 | + case 1: |
| 173 | + return workspaceInfo.getWorkspaceName(); |
| 174 | + case 2: |
| 175 | + return workspaceInfo.getOwnerKey(); |
| 176 | + case 3: |
| 177 | + return workspaceInfo.getDateCreated(); |
| 178 | + case 4: |
| 179 | + return workspaceInfo.getTotalSize(); |
| 180 | + default: |
| 181 | + return null; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public boolean isCellEditable(int rowIndex, int columnIndex) { |
| 187 | + return columnIndex == 0; // Allow only checkboxes to be editable |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
| 192 | + if (columnIndex == 0 && aValue instanceof Boolean) { |
| 193 | + selected.set(rowIndex, (Boolean) aValue); |
| 194 | + fireTableCellUpdated(rowIndex, columnIndex); // Notify JTable of the change |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + public void clear() { |
| 199 | + workspaceResults.clear(); |
| 200 | + selected.clear(); |
| 201 | + fireTableDataChanged(); // Notify the table UI that the data has changed |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public Class<?> getColumnClass(int columnIndex) { |
| 206 | + return columnIndex == 0 ? Boolean.class : String.class; |
| 207 | + } |
| 208 | + |
| 209 | + public List<Long> getSelectedWorkspaces() { |
| 210 | + List<Long> selectedWorkspaces = new ArrayList<>(); |
| 211 | + for (int i = 0; i < workspaceResults.size(); i++) { |
| 212 | + if (selected.get(i)) { |
| 213 | + selectedWorkspaces.add(workspaceResults.get(i).getWorkspaceId()); |
| 214 | + } |
| 215 | + } |
| 216 | + return selectedWorkspaces; |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + public String getColumnName(int column) { |
| 221 | + return columnNames[column]; |
| 222 | + } |
| 223 | +} |
| 224 | + |
0 commit comments