NewAadlPackageWizard.java

/**
 * Copyright (c) 2004-2025 Carnegie Mellon University and others. (see Contributors file).
 * All Rights Reserved.
 * 
 * NO WARRANTY. ALL MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE
 * OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT
 * MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.
 * 
 * This program and the accompanying materials are made available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Created, in part, with funding and support from the United States Government. (see Acknowledgments file).
 * 
 * This program includes and/or can make use of certain third party source code, object code, documentation and other
 * files ("Third Party Software"). The Third Party Software that is used by this program is dependent upon your system
 * configuration. By using this program, You agree to comply with any and all relevant Third Party Software terms and
 * conditions contained in any such Third Party Software or separate license file distributed with such Third Party
 * Software. The parties who own the Third Party Software ("Third Party Licensors") are intended third party benefici-
 * aries to this license with respect to the terms applicable to their Third Party Software. Third Party Software li-
 * censes only apply to the Third Party Software and not any other portion of this program or this program as a whole.
 */
package org.osate.ui.wizards;

import java.util.Map;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.statushandlers.StatusManager;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.osate.aadl2.NamedElement;
import org.osate.aadl2.modelsupport.Activator;
import org.osate.aadl2.modelsupport.FileNameConstants;
import org.osate.ge.internal.services.DiagramService;
import org.osate.xtext.aadl2.ui.util.Aadl2NameValidators;

@SuppressWarnings("all")
public final class NewAadlPackageWizard extends AbstractNewModelUnitWizard {
  private static final String PACKAGE_LABEL = "AADL package name";

  private Button textButton;

  private Button graphicalButton;

  public NewAadlPackageWizard() {
    super("AADL Package", "AADL package");
    this.addField(NewAadlPackageWizard.PACKAGE_LABEL, Aadl2NameValidators.PACKAGE_NAME_VALIDATOR);
  }

  @Override
  public void addLocalControls(final Composite parent) {
    Label _label = new Label(parent, SWT.NONE);
    final Procedure1<Label> _function = (Label label) -> {
      label.setText("Open with");
      GridData _gridData = new GridData(SWT.END, SWT.TOP, false, false);
      label.setLayoutData(_gridData);
    };
    ObjectExtensions.<Label>operator_doubleArrow(_label, _function);
    Composite _composite = new Composite(parent, SWT.NONE);
    final Procedure1<Composite> _function_1 = (Composite radios) -> {
      GridLayout _gridLayout = new GridLayout(1, false);
      radios.setLayout(_gridLayout);
      Button _button = new Button(radios, (SWT.RADIO | SWT.LEFT));
      final Procedure1<Button> _function_2 = (Button button) -> {
        GridData _gridData = new GridData(SWT.BEGINNING, SWT.TOP, false, false);
        button.setLayoutData(_gridData);
        button.setText("Text Editor");
      };
      Button _doubleArrow = ObjectExtensions.<Button>operator_doubleArrow(_button, _function_2);
      this.textButton = _doubleArrow;
      Button _button_1 = new Button(radios, (SWT.RADIO | SWT.LEFT));
      final Procedure1<Button> _function_3 = (Button button) -> {
        GridData _gridData = new GridData(SWT.BEGINNING, SWT.TOP, false, false);
        button.setLayoutData(_gridData);
        button.setText("Diagram Editor");
      };
      Button _doubleArrow_1 = ObjectExtensions.<Button>operator_doubleArrow(_button_1, _function_3);
      this.graphicalButton = _doubleArrow_1;
      this.textButton.setSelection(true);
      this.graphicalButton.setSelection(false);
    };
    ObjectExtensions.<Composite>operator_doubleArrow(_composite, _function_1);
  }

  @Override
  public void openEditor(final IFile newFile, final String contents) {
    boolean _selection = this.textButton.getSelection();
    if (_selection) {
      this.openDefaultEditor(newFile, contents);
    } else {
      boolean _selection_1 = this.graphicalButton.getSelection();
      if (_selection_1) {
        this.openGraphicalEditor(newFile);
      }
    }
  }

  private void openGraphicalEditor(final IFile newFile) {
    if ((this.project != null)) {
      try {
        NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
        this.project.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, _nullProgressMonitor);
      } catch (final Throwable _t) {
        if (_t instanceof CoreException) {
          final CoreException ex = (CoreException)_t;
          String _pluginId = Activator.getPluginId();
          final Status status = new Status(IStatus.ERROR, _pluginId, 
            "An error building the AADL project after creating a new AADL file.", ex);
          StatusManager.getManager().handle(status, (StatusManager.SHOW | StatusManager.LOG));
        } else if (_t instanceof RuntimeException) {
          final RuntimeException ex_1 = (RuntimeException)_t;
          String _pluginId_1 = Activator.getPluginId();
          final Status status_1 = new Status(IStatus.ERROR, _pluginId_1, 
            "An error building the AADL project after creating a new AADL file.", ex_1);
          StatusManager.getManager().handle(status_1, (StatusManager.SHOW | StatusManager.LOG));
        } else {
          throw Exceptions.sneakyThrow(_t);
        }
      }
    }
    final URI uri = URI.createPlatformResourceURI(newFile.getFullPath().toString(), true);
    final XtextResourceSet resourceSet = new XtextResourceSet();
    final Resource pkgResource = resourceSet.getResource(uri, true);
    if (((pkgResource.getContents().size() > 0) && (pkgResource.getContents().get(0) instanceof NamedElement))) {
      final DiagramService diagramService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().<DiagramService>getService(DiagramService.class);
      diagramService.openOrCreateDiagramForBusinessObject(pkgResource.getContents().get(0), false, false);
    } else {
      String _pluginId = Activator.getPluginId();
      final Status status = new Status(IStatus.ERROR, _pluginId, 
        "Unable to retrieve package from resource.", null);
      StatusManager.getManager().handle(status, StatusManager.LOG);
      ErrorDialog.openError(this.getContainer().getShell(), "Unable to Create Diagram", null, status);
    }
  }

  @Override
  public String fileContents(final Map<String, String> fieldValues) {
    String _xblockexpression = null;
    {
      final String packageName = fieldValues.get(NewAadlPackageWizard.PACKAGE_LABEL);
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("package ");
      _builder.append(packageName);
      _builder.newLineIfNotEmpty();
      _builder.append("public");
      _builder.newLine();
      _builder.append("\t");
      _builder.newLine();
      _builder.append("end ");
      _builder.append(packageName);
      _builder.append(";");
      _xblockexpression = _builder.toString();
    }
    return _xblockexpression;
  }

  @Override
  protected String getFileName(final String enteredName) {
    return super.getFileName(enteredName).replaceAll(FileNameConstants.AADL_PACKAGE_SEPARATOR, 
      FileNameConstants.FILE_PACKAGE_SEPARATOR);
  }
}