blob: b82a8f0ba8f1d16a84203d2878e42fe4c9bc8538 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2018 ALL4TEC.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Thanh Liem PHAN (ALL4TEC) - initial API and implementation
*******************************************************************************/
package org.eclipse.opencert.infra.dashboard.pages.adapters;
import java.util.Objects;
import org.eclipse.opencert.infra.dashboard.helpers.ChessPapyrusHelper;
import org.eclipse.opencert.infra.dashboard.helpers.EclipseHelper;
import org.eclipse.papyrus.commands.Activator;
import org.eclipse.papyrus.commands.ICreationCommand;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.ui.editor.IMultiDiagramEditor;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.uml2.uml.Model;
/**
* A {@link HyperlinkAdapter} to create a new CHESS diagram.
*/
public abstract class AbstractCreateNewChessDiagramHyperLinkAdapter
extends AbstractCreateNewChessElementHyperLinkAdapter {
/**
* {@inheritDoc}
*
* Overridden to create a new diagram.
*/
@Override
protected void createChessElement() {
// Create a new diagram command
ICreationCommand vCreationCommand = getDiagramCreationCommand();
// Get the first opened Chess Papyrus editor
IMultiDiagramEditor vActivePapyrusEditor = (IMultiDiagramEditor) EclipseHelper.getFirstPapyrusDiagramEditorPart();
// If there is one Chess graphical editor is opened,
// try to goto the right package and create the new diagram there
if (Objects.nonNull(vActivePapyrusEditor)) {
ServicesRegistry vRegistry = vActivePapyrusEditor.getServicesRegistry();
if (Objects.nonNull(vRegistry)) {
ModelSet vModelSet = null;
try {
vModelSet = vRegistry.getService(ModelSet.class);
} catch (ServiceException pException) {
Activator.log.error(pException);
}
if (Objects.nonNull(vModelSet)) {
// Get the root model
Model vRootModel = ChessPapyrusHelper.INSTANCE.getUmlModelFromModelSet(vModelSet);
// Get the target view package
org.eclipse.uml2.uml.Package vTargetViewPackage = getTargetPackage(vRootModel);
if (Objects.nonNull(vTargetViewPackage)) {
// Create the new diagram inside the target view package
vCreationCommand.createDiagram(vModelSet, vTargetViewPackage, null);
// Then goto the active Papyrus editor
EclipseHelper.getActiveWorkbenchPage().activate(vActivePapyrusEditor);
}
}
}
} else {
// Otherwise, no Chess graphical editor is already opened, ask user to open one
ChessPapyrusHelper.INSTANCE.openNoActivePapyrusEditorMessageBox();
}
}
/**
* @return The creation command to create a diagram
*/
protected abstract ICreationCommand getDiagramCreationCommand();
/**
* Get the target package in which the new diagram will be created.
*
* @param pRootModel The root model
* @return The target package
*/
protected abstract org.eclipse.uml2.uml.Package getTargetPackage(Model pRootModel);
}