Skip to content

Commit

Permalink
Add undo/redo for peak/scan deletion.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mailaender committed Jan 27, 2022
1 parent a33485b commit eeae90e
Show file tree
Hide file tree
Showing 7 changed files with 403 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.help;bundle-version="3.8.100",
org.eclipse.chemclipse.chromatogram.wsd.identifier;bundle-version="0.9.0",
org.eclipse.chemclipse.tsd.model;bundle-version="0.9.0",
org.eclipse.chemclipse.tsd.converter;bundle-version="0.9.0"
org.eclipse.chemclipse.tsd.converter;bundle-version="0.9.0",
org.eclipse.ui.workbench,
org.eclipse.core.commands
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Service-Component: OSGI-INF/org.eclipse.chemclipse.ux.extension.xxd.ui.editors.EditorProcessTypeSupplier.xml
Bundle-ActivationPolicy: lazy
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*******************************************************************************
* Copyright (c) 2008, 2022 Lablicate GmbH.
*
* All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/eplv10.html
*
* Contributors:
* Dr. Philip Wenig - initial API and implementation
* Matthias Mailänder - reimplemented using Eclipse API
*******************************************************************************/
package org.eclipse.chemclipse.ux.extension.xxd.ui.internal.handlers;

import javax.inject.Named;

import org.eclipse.chemclipse.logging.core.Logger;
import org.eclipse.chemclipse.progress.core.InfoType;
import org.eclipse.chemclipse.progress.core.StatusLineLogger;
import org.eclipse.chemclipse.support.ui.workbench.DisplayUtils;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.IOperationHistory;
import org.eclipse.core.commands.operations.IUndoContext;
import org.eclipse.e4.core.di.annotations.CanExecute;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.di.UISynchronize;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.operations.IWorkbenchOperationSupport;

public class RedoOperationHandler {

private static final Logger logger = Logger.getLogger(RedoOperationHandler.class);

@CanExecute
boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart part) {

IWorkbenchOperationSupport operationSupport = PlatformUI.getWorkbench().getOperationSupport();
return operationSupport.getOperationHistory().canRedo(operationSupport.getUndoContext());
}

@Execute
public void execute(UISynchronize uiSynchronize, final @Named(IServiceConstants.ACTIVE_SHELL) Shell shell, @Named(IServiceConstants.ACTIVE_PART) MPart part) {

StatusLineLogger.setInfo(InfoType.MESSAGE, "Start Redo Operation");
uiSynchronize.syncExec(new Runnable() {

@Override
public void run() {

Cursor cursor = shell.getCursor();
try {
Cursor cursorNew = DisplayUtils.getDisplay().getSystemCursor(SWT.CURSOR_WAIT);
shell.setCursor(cursorNew);
/*
* Undo the operation.
*/
IWorkbenchOperationSupport operationSupport = PlatformUI.getWorkbench().getOperationSupport();
IOperationHistory operationHistory = operationSupport.getOperationHistory();
IUndoContext undoContext = operationSupport.getUndoContext();
operationHistory.redo(undoContext, null, null);
} catch(ExecutionException e) {
logger.warn(e);
} finally {
shell.setCursor(cursor);
}
}
});
StatusLineLogger.setInfo(InfoType.MESSAGE, "Redo Operation finished");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*******************************************************************************
* Copyright (c) 2008, 2022 Lablicate GmbH.
*
* All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/eplv10.html
*
* Contributors:
* Dr. Philip Wenig - initial API and implementation
* Matthias Mailänder - reimplemented using Eclipse API
*******************************************************************************/
package org.eclipse.chemclipse.ux.extension.xxd.ui.internal.handlers;

import javax.inject.Named;

import org.eclipse.chemclipse.logging.core.Logger;
import org.eclipse.chemclipse.progress.core.InfoType;
import org.eclipse.chemclipse.progress.core.StatusLineLogger;
import org.eclipse.chemclipse.support.ui.workbench.DisplayUtils;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.IOperationHistory;
import org.eclipse.core.commands.operations.IUndoContext;
import org.eclipse.e4.core.di.annotations.CanExecute;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.di.UISynchronize;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.operations.IWorkbenchOperationSupport;

public class UndoOperationHandler {

private static final Logger logger = Logger.getLogger(UndoOperationHandler.class);

@CanExecute
boolean canExecute() {

IWorkbenchOperationSupport operationSupport = PlatformUI.getWorkbench().getOperationSupport();
return operationSupport.getOperationHistory().canUndo(operationSupport.getUndoContext());
}

@Execute
public void execute(UISynchronize uiSynchronize, final @Named(IServiceConstants.ACTIVE_SHELL) Shell shell, @Named(IServiceConstants.ACTIVE_PART) MPart part) {

StatusLineLogger.setInfo(InfoType.MESSAGE, "Start Undo Operation");
uiSynchronize.syncExec(new Runnable() {

@Override
public void run() {

Cursor cursor = shell.getCursor();
try {
Cursor cursorNew = DisplayUtils.getDisplay().getSystemCursor(SWT.CURSOR_WAIT);
shell.setCursor(cursorNew);
/*
* Undo the operation.
*/
IWorkbenchOperationSupport operationSupport = PlatformUI.getWorkbench().getOperationSupport();
IOperationHistory operationHistory = operationSupport.getOperationHistory();
IUndoContext undoContext = operationSupport.getUndoContext();
operationHistory.undo(undoContext, null, null);
} catch(ExecutionException e) {
logger.warn(e);
} finally {
shell.setCursor(cursor);
}
}
});
StatusLineLogger.setInfo(InfoType.MESSAGE, "Undo Operation finished");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*******************************************************************************
* Copyright (c) 2021, 2022 Lablicate GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Matthias Mailänder - initial API and implementation
*******************************************************************************/
package org.eclipse.chemclipse.ux.extension.xxd.ui.operations;

import java.util.List;

import org.eclipse.chemclipse.model.core.IChromatogram;
import org.eclipse.chemclipse.model.core.IPeak;
import org.eclipse.chemclipse.model.selection.IChromatogramSelection;
import org.eclipse.chemclipse.support.events.IChemClipseEvents;
import org.eclipse.chemclipse.swt.ui.notifier.UpdateNotifierUI;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.AbstractOperation;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.swt.widgets.Display;

@SuppressWarnings("rawtypes")
public class DeletePeaksOperation extends AbstractOperation {

private IChromatogramSelection<?, ?> chromatogramSelection;
private Display display;
private List<IPeak> peaksToDelete;

public DeletePeaksOperation(Display display, IChromatogramSelection chromatogramSelection, List<IPeak> peaksToDelete) {

super("Delete Peaks");
this.display = display;
this.chromatogramSelection = chromatogramSelection;
this.peaksToDelete = peaksToDelete;
}

@Override
public boolean canExecute() {

return !peaksToDelete.isEmpty() && chromatogramSelection != null;
}

@Override
public boolean canRedo() {

return !peaksToDelete.isEmpty();
}

@Override
public boolean canUndo() {

return !peaksToDelete.isEmpty();
}

@SuppressWarnings("unchecked")
@Override
public IStatus execute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {

IChromatogram chromatogram = chromatogramSelection.getChromatogram();
chromatogram.removePeaks(peaksToDelete);
update("Peaks were deleted.");
return Status.OK_STATUS;
}

private void update(String message) {

chromatogramSelection.setSelectedPeak(null);
// chromatogramSelection.update(true);
//
UpdateNotifierUI.update(display, chromatogramSelection);
UpdateNotifierUI.update(display, IChemClipseEvents.TOPIC_IDENTIFICATION_TARGETS_UPDATE_SELECTION, message);
}

@Override
public String getLabel() {

return "Delete Peaks";
}

@Override
public IStatus redo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {

return execute(monitor, info);
}

@Override
@SuppressWarnings("unchecked")
public IStatus undo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {

IChromatogram chromatogram = chromatogramSelection.getChromatogram();
for(IPeak peak : peaksToDelete) {
chromatogram.addPeak(peak);
}
update("Peaks were added back again.");
return Status.OK_STATUS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*******************************************************************************
* Copyright (c) 2022 Lablicate GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Matthias Mailänder - initial API and implementation
*******************************************************************************/
package org.eclipse.chemclipse.ux.extension.xxd.ui.operations;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.SerializationUtils;
import org.eclipse.chemclipse.model.core.IScan;
import org.eclipse.chemclipse.model.selection.IChromatogramSelection;
import org.eclipse.chemclipse.support.events.IChemClipseEvents;
import org.eclipse.chemclipse.swt.ui.notifier.UpdateNotifierUI;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.AbstractOperation;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.swt.widgets.Display;

@SuppressWarnings("rawtypes")
public class DeleteScansOperation extends AbstractOperation {

private IChromatogramSelection<?, ?> chromatogramSelection;
private Display display;
private List<IScan> scansToClear;
private List<IScan> backupScans;

public DeleteScansOperation(Display display, IChromatogramSelection chromatogramSelection, List<IScan> scansToClear) {

super("Delete Scans");
this.display = display;
this.chromatogramSelection = chromatogramSelection;
this.scansToClear = scansToClear;
backupScans = new ArrayList<IScan>();
}

@Override
public boolean canExecute() {

return !scansToClear.isEmpty() && chromatogramSelection != null;
}

@Override
public boolean canRedo() {

return !scansToClear.isEmpty() && chromatogramSelection != null;
}

@Override
public boolean canUndo() {

return !scansToClear.isEmpty();
}

@Override
public IStatus execute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {

for(IScan scan : scansToClear) {
IScan deepCopy = SerializationUtils.clone(scan);
backupScans.add(deepCopy);
scan.getTargets().clear();
}
chromatogramSelection.setSelectedIdentifiedScan(null);
update();
return Status.OK_STATUS;
}

private void update() {

if(chromatogramSelection != null) {
chromatogramSelection.setSelectedPeak(null);
chromatogramSelection.update(true);
//
UpdateNotifierUI.update(display, chromatogramSelection);
}
UpdateNotifierUI.update(display, IChemClipseEvents.TOPIC_IDENTIFICATION_TARGETS_UPDATE_SELECTION, "Peaks were deleted.");
}

@Override
public String getLabel() {

return "Delete Scans";
}

@Override
public IStatus redo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {

return execute(monitor, info);
}

@Override
public IStatus undo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {

for(IScan scan : backupScans) {
chromatogramSelection.getChromatogram().getScan(scan.getScanNumber()).getTargets().addAll(scan.getTargets());
}
update();
return Status.OK_STATUS;
}
}
Loading

0 comments on commit eeae90e

Please sign in to comment.