| /******************************************************************************* |
| * Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency |
| * 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: |
| * Pierre Allard, |
| * Regent L'Archeveque, |
| * Olivier L. Larouche - initial API and implementation |
| * |
| * SPDX-License-Identifier: EPL-1.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.apogy.core.invocator.ui.handlers; |
| |
| import java.util.List; |
| |
| import org.eclipse.apogy.core.invocator.AbstractProgramRuntime; |
| import org.eclipse.apogy.core.invocator.ApogyCoreInvocatorFacade; |
| import org.eclipse.apogy.core.invocator.Program; |
| import org.eclipse.apogy.core.invocator.ProgramRuntimeState; |
| import org.eclipse.apogy.core.invocator.ui.parts.ProgramPart; |
| import org.eclipse.e4.core.di.annotations.CanExecute; |
| import org.eclipse.e4.core.di.annotations.Execute; |
| import org.eclipse.e4.ui.model.application.ui.basic.MPart; |
| import org.eclipse.e4.ui.model.application.ui.basic.impl.PartImpl; |
| |
| @SuppressWarnings("restriction") |
| public class SuspendProgramRuntimeHandler extends AbstractProgramRuntimeHandler { |
| |
| @Execute |
| public void execute(MPart part) { |
| ProgramPart programPart = (ProgramPart) ((PartImpl) part).getObject(); |
| |
| Program program = programPart.getSelectedProgram(); |
| |
| List<AbstractProgramRuntime> runtimes = programPart.getRuntimes(); |
| if (runtimes.size() > 0) { |
| for (int i = runtimes.size() - 1; i >= 0; i--) { |
| AbstractProgramRuntime runtime = runtimes.get(i); |
| if (runtime.getProgram() == program) { |
| runtime.suspend(); |
| break; |
| } |
| } |
| } |
| } |
| |
| @Override |
| @CanExecute |
| public boolean canExecute(MPart part) { |
| if (ApogyCoreInvocatorFacade.INSTANCE.getActiveInvocatorSession() == null) { |
| return false; |
| } |
| |
| if (ApogyCoreInvocatorFacade.INSTANCE.getActiveInvocatorSession().getProgramRuntimesList() == null) { |
| return false; |
| } |
| |
| if (part instanceof PartImpl && ((PartImpl) part).getObject() instanceof ProgramPart) { |
| ProgramPart programPart = (ProgramPart) ((PartImpl) part).getObject(); |
| |
| Program program = programPart.getSelectedProgram(); |
| |
| if (program == null) |
| return false; |
| |
| for (AbstractProgramRuntime runtime : programPart.getRuntimes()) { |
| if (programPart.getRuntimes().indexOf(runtime) == programPart.getRuntimes().size() - 1 |
| && runtime.getProgram() == program && runtime.getState() == ProgramRuntimeState.RUNNING) { |
| /** Can be executed if runtime is running */ |
| return true; |
| } |
| } |
| } |
| return false; |
| } |
| } |