/** {@inheritDoc} */
  @Override
  public void refresh(final Resource resource) {
    this.dcdResource = resource;

    final DeviceConfiguration dcd = ModelUtil.getDeviceConfiguration(this.dcdResource);

    setEditable(!getPage().getEditingDomain().isReadOnly(this.dcdResource));

    for (final Binding binding : this.bindings) {
      binding.dispose();
    }
    this.bindings.clear();

    final SoftPkg model = getSoftPkg();
    if (model == null) {
      return;
    }

    final DataBindingContext context = this.getPage().getEditor().getDataBindingContext();

    this.bindings.add(
        context.bindValue(
            WidgetProperties.text(SWT.Modify)
                .observeDelayed(
                    SCAFormEditor.getFieldBindingDelay(), this.client.getIdEntry().getText()),
            EMFEditObservables.observeValue(
                getEditingDomain(), dcd, DcdPackage.Literals.DEVICE_CONFIGURATION__ID),
            new EMFEmptyStringToNullUpdateValueStrategy(),
            null));

    this.bindings.add(
        context.bindValue(
            WidgetProperties.text(SWT.Modify)
                .observeDelayed(
                    SCAFormEditor.getFieldBindingDelay(), this.client.getNameEntry().getText()),
            EMFEditObservables.observeValue(
                getEditingDomain(), dcd, DcdPackage.Literals.DEVICE_CONFIGURATION__NAME),
            new EMFEmptyStringToNullUpdateValueStrategy(),
            null));

    this.bindings.add(
        context.bindValue(
            WidgetProperties.text(SWT.Modify)
                .observeDelayed(
                    SCAFormEditor.getFieldBindingDelay(),
                    this.client.getDescriptionEntry().getText()),
            EMFEditObservables.observeValue(
                getEditingDomain(), dcd, DcdPackage.Literals.DEVICE_CONFIGURATION__DESCRIPTION),
            new EMFEmptyStringToNullUpdateValueStrategy(),
            null));

    this.client.setEditable(SCAEditorUtil.isEditableResource(getPage(), this.dcdResource));
  }
    /** {@inheritDoc} */
    @Override
    public IStatus validate(final Object value) {
      final String s = ((String) value).trim(); // Project names are
      // always stripped of
      // whitespace (see the
      // Java Project Wizard)
      if ((s == null) || (s.length() == 0)) {
        return ValidationStatus.error("Enter a SAD file.");
      }
      final File sadFile = new File(s);
      if (!sadFile.exists()) {
        return ValidationStatus.error("SAD file does not exist.");
      }

      final URI fileURI = URI.createFileURI(sadFile.getAbsolutePath());
      try {
        final SoftwareAssembly sad = ModelUtil.loadSoftwareAssembly(fileURI);
        if (sad == null) {
          return ValidationStatus.error("Invalid SAD file selected.");
        }
      } catch (final Exception e) { // SUPPRESS CHECKSTYLE Logged Error
        return ValidationStatus.error("Unable to parse SAD file.");
      }

      return ValidationStatus.ok();
    }
  @Override
  public CExternalSetting[] getSettings(final IProject project, ICConfigurationDescription cfg) {
    // Create empty settings entry
    final List<ICSettingEntry> settingEntries = new ArrayList<ICSettingEntry>();
    final SoftPkg spd = ModelUtil.getSoftPkg(project);
    if (spd == null) {
      return new CExternalSetting[0];
    }
    Set<IPath> incPaths;
    try {
      incPaths =
          ScaModelCommand.runExclusive(
              spd,
              new RunnableWithResult.Impl<Set<IPath>>() {

                @Override
                public void run() {
                  Set<IPath> incPaths = new HashSet<IPath>();
                  populateIncludePaths(spd, incPaths);
                  setResult(incPaths);
                }
              });
    } catch (InterruptedException e) {
      incPaths = Collections.emptySet();
    }

    for (IPath incPath : incPaths) {
      final ICPathEntry pathEntry = new CIncludePathEntry(incPath, ICSettingEntry.READONLY);
      settingEntries.add(pathEntry);
    }

    final ICSettingEntry[] settings =
        settingEntries.toArray(new ICSettingEntry[settingEntries.size()]);
    return new CExternalSetting[] {new CExternalSetting(null, null, null, settings)};
  }
  /** {@inheritDoc} */
  @Override
  public Object execute(final ExecutionEvent event) throws ExecutionException {
    final ISelection selection =
        HandlerUtil.getActiveWorkbenchWindow(event).getSelectionService().getSelection();
    final ComponentEditor editor = (ComponentEditor) HandlerUtil.getActiveEditor(event);
    this.editingDomain = editor.getEditingDomain();
    this.resource = editor.getMainResource();
    this.softPkg = ModelUtil.getSoftPkg(this.resource);
    this.interfaceMap = PortsHandlerUtil.getInterfaceMap(this.softPkg);
    final List<Object> ports = Arrays.asList(((IStructuredSelection) selection).toArray());

    Command command = this.createRemovePortCommand(ports, new HashSet<String>());
    this.editingDomain.getCommandStack().execute(command);

    return null;
  }
  /** {@inheritDoc} */
  public String generate(Object argument) {
    final StringBuffer stringBuffer = new StringBuffer();

    TemplateParameter templ = (TemplateParameter) argument;
    ImplementationSettings implSettings = templ.getImplSettings();
    Implementation impl = templ.getImpl();
    SoftPkg softPkg = (SoftPkg) impl.eContainer();
    String PREFIX =
        gov.redhawk.ide.codegen.util.CodegenFileHelper.getPreferredFilePrefix(
            softPkg, implSettings);
    boolean hasSddsPort = false;
    Date date = new Date(System.currentTimeMillis());
    EList<Provides> provides =
        softPkg.getDescriptor().getComponent().getComponentFeatures().getPorts().getProvides();

    for (Provides entry : provides) {
      if (entry.getRepID().contains("BULKIO/dataSDDS")) {
        hasSddsPort = true;
      }
    }

    stringBuffer.append(TEXT_1);
    stringBuffer.append(ModelUtil.getSpdFileName(softPkg));
    stringBuffer.append(TEXT_2);
    stringBuffer.append(date.toString());

    String[] output;
    IProduct product = Platform.getProduct();
    if (product != null) {
      output = product.getProperty("aboutText").split("\n");

      stringBuffer.append(TEXT_3);
      stringBuffer.append(output[0]);
      stringBuffer.append(TEXT_4);
      stringBuffer.append(output[1]);
      stringBuffer.append(TEXT_5);
      stringBuffer.append(output[2]);
    }

    stringBuffer.append(TEXT_6);
    stringBuffer.append(PREFIX);
    stringBuffer.append(TEXT_7);
    stringBuffer.append(PREFIX);
    stringBuffer.append(TEXT_8);
    if (!templ.isDevice()) {
      stringBuffer.append(TEXT_9);
      stringBuffer.append(PREFIX);
      stringBuffer.append(TEXT_10);
      stringBuffer.append(PREFIX);
      stringBuffer.append(TEXT_11);
      stringBuffer.append(TEXT_12);
      stringBuffer.append(PREFIX);
      stringBuffer.append(TEXT_13);
    } else {
      stringBuffer.append(TEXT_14);
      stringBuffer.append(PREFIX);
      stringBuffer.append(TEXT_15);
      stringBuffer.append(PREFIX);
      stringBuffer.append(TEXT_16);
      stringBuffer.append(TEXT_17);
      stringBuffer.append(PREFIX);
      stringBuffer.append(TEXT_18);
      stringBuffer.append(TEXT_19);
      stringBuffer.append(PREFIX);
      stringBuffer.append(TEXT_20);
      stringBuffer.append(PREFIX);
      stringBuffer.append(TEXT_21);
      stringBuffer.append(TEXT_22);
      stringBuffer.append(PREFIX);
      stringBuffer.append(TEXT_23);
      stringBuffer.append(TEXT_24);
      stringBuffer.append(PREFIX);
      stringBuffer.append(TEXT_25);
      stringBuffer.append(PREFIX);
      stringBuffer.append(TEXT_26);
      stringBuffer.append(TEXT_27);
      stringBuffer.append(PREFIX);
      stringBuffer.append(TEXT_28);
      stringBuffer.append(TEXT_29);
      stringBuffer.append(PREFIX);
      stringBuffer.append(TEXT_30);
      stringBuffer.append(PREFIX);
      stringBuffer.append(TEXT_31);
      stringBuffer.append(TEXT_32);
      stringBuffer.append(PREFIX);
      stringBuffer.append(TEXT_33);
    }
    stringBuffer.append(TEXT_34);
    stringBuffer.append(TEXT_35);
    stringBuffer.append(PREFIX);
    stringBuffer.append(TEXT_36);
    stringBuffer.append(PREFIX);
    stringBuffer.append(TEXT_37);
    if (hasSddsPort) {
      stringBuffer.append(TEXT_38);
      stringBuffer.append(PREFIX);
      stringBuffer.append(TEXT_39);
      stringBuffer.append(PREFIX);
      stringBuffer.append(TEXT_40);
    }
    stringBuffer.append(TEXT_41);
    stringBuffer.append(PREFIX);
    stringBuffer.append(TEXT_42);
    stringBuffer.append(PREFIX);
    stringBuffer.append(TEXT_43);
    stringBuffer.append(PREFIX);
    stringBuffer.append(TEXT_44);
    stringBuffer.append(TEXT_45);
    stringBuffer.append(PREFIX);
    stringBuffer.append(TEXT_46);
    stringBuffer.append(PREFIX);
    stringBuffer.append(TEXT_47);
    stringBuffer.append(TEXT_48);
    stringBuffer.append(PREFIX);
    stringBuffer.append(TEXT_49);
    stringBuffer.append(PREFIX);
    stringBuffer.append(TEXT_50);
    stringBuffer.append(PREFIX);
    stringBuffer.append(TEXT_51);
    stringBuffer.append(PREFIX);
    stringBuffer.append(TEXT_52);
    stringBuffer.append(PREFIX);
    stringBuffer.append(TEXT_53);
    stringBuffer.append(PREFIX);
    stringBuffer.append(TEXT_54);
    stringBuffer.append(TEXT_55);
    return stringBuffer.toString();
  }
  public String generate(Object argument) throws CoreException {
    final StringBuffer stringBuffer = new StringBuffer();

    final TemplateParameter param = (TemplateParameter) argument;
    final EMap<String, ImplementationSettings> settings =
        ((WaveDevSettings) param.getImplSettings().eContainer().eContainer()).getImplSettings();
    final String name = param.getSoftPkg().getName();
    final String version;
    if (param.getSoftPkg().getVersion() != null
        && param.getSoftPkg().getVersion().trim().length() != 0) {
      version = param.getSoftPkg().getVersion();
    } else {
      version = "1.0.0";
    }

    // Interfaces
    final Set<String> interfaceNamespaces = new HashSet<String>();
    final List<Interface> interfaces =
        param.getSoftPkg().getDescriptor().getComponent().getInterfaces().getInterface();
    final Pattern idlPattern = Pattern.compile("^IDL:(\\w+)/");
    for (Interface iface : interfaces) {
      final Matcher match = idlPattern.matcher(iface.getRepid());
      if (match.find()) {
        if (!"CF".equals(match.group(1))) {
          interfaceNamespaces.add(match.group(1));
        }
      }
    }

    // Language
    boolean hasCpp = false, hasJava = false, hasPython = false;
    for (final Implementation impl : param.getSoftPkg().getImplementation()) {
      final String language = impl.getProgrammingLanguage().getName();
      if ("C++".equals(language)) {
        hasCpp = true;
      } else if ("Java".equals(language)) {
        hasJava = true;
      } else if ("Python".equals(language)) {
        hasPython = true;
      }
    }

    // Strings that depend on project type
    final String sdrrootFolder;
    final String projectType;
    final SoftwareComponent scd = param.getSoftPkg().getDescriptor().getComponent();
    final ComponentType componentType = SoftwareComponent.Util.getWellKnownComponentType(scd);
    switch (componentType) {
      case RESOURCE:
        sdrrootFolder = "dom/components";
        projectType = "Component";
        break;
      case DEVICE:
        sdrrootFolder = "dev/devices";
        projectType = "Device";
        break;
      case SERVICE:
        sdrrootFolder = "dev/services";
        projectType = "Service";
        break;
      default:
        // For backwards compatibility, support non-spec strings the IDE used to generate
        try {
          if (scd != null && scd.getComponentType() != null) {
            final String compTypeStr = scd.getComponentType();
            if (compTypeStr.equals("executabledevice") || compTypeStr.equals("loadabledevice")) {
              sdrrootFolder = "dev/devices";
              projectType = "Device";
              break;
            }
          }
        } catch (NullPointerException e) {
          // PASS
        }
        throw new CoreException(
            new Status(
                IStatus.ERROR,
                Activator.PLUGIN_ID,
                "Template file does not support the specified project type"));
    }

    stringBuffer.append(TEXT_1);
    stringBuffer.append(name);
    stringBuffer.append(TEXT_2);
    stringBuffer.append(projectType);
    stringBuffer.append(TEXT_3);

    if (param.getSoftPkg().getTitle() != null
        && param.getSoftPkg().getTitle().trim().length() > 0) {

      stringBuffer.append(TEXT_4);
      stringBuffer.append(param.getSoftPkg().getTitle());
      stringBuffer.append(TEXT_5);
    }
    stringBuffer.append(TEXT_6);
    stringBuffer.append(version);
    stringBuffer.append(TEXT_7);
    stringBuffer.append(projectType);
    stringBuffer.append(TEXT_8);

    if (interfaceNamespaces.size() > 0) {
      boolean foundOther = false;
      // these interfaces are available through the redhawk install
      //  so no additional rpms are needed
      for (String ifaceNamespace : interfaceNamespaces) {
        if ((!ifaceNamespace.equals("CF"))
            && (!ifaceNamespace.equals("PortTypes"))
            && (!ifaceNamespace.equals("ExtendedEvent"))
            && (!ifaceNamespace.equals("ExtendedCF"))
            && (!ifaceNamespace.equals("StandardEvent"))
            && (!ifaceNamespace.equals("WKP"))) {
          foundOther = true;
          break;
        }
      }
      if (foundOther) {

        stringBuffer.append(TEXT_9);

        for (String ifaceNamespace : interfaceNamespaces) {
          if ((!ifaceNamespace.equals("CF"))
              && (!ifaceNamespace.equals("PortTypes"))
              && (!ifaceNamespace.equals("ExtendedEvent"))
              && (!ifaceNamespace.equals("ExtendedCF"))
              && (!ifaceNamespace.equals("StandardEvent"))
              && (!ifaceNamespace.equals("WKP"))) {

            stringBuffer.append(TEXT_10);
            stringBuffer.append(ifaceNamespace.toLowerCase());
            stringBuffer.append(TEXT_11);
          }
        }

        stringBuffer.append(TEXT_12);

        for (String ifaceNamespace : interfaceNamespaces) {
          if ((!ifaceNamespace.equals("CF"))
              && (!ifaceNamespace.equals("PortTypes"))
              && (!ifaceNamespace.equals("ExtendedEvent"))
              && (!ifaceNamespace.equals("ExtendedCF"))
              && (!ifaceNamespace.equals("StandardEvent"))
              && (!ifaceNamespace.equals("WKP"))) {

            stringBuffer.append(TEXT_13);
            stringBuffer.append(ifaceNamespace.toLowerCase());
            stringBuffer.append(TEXT_14);
          }
        }

        stringBuffer.append(TEXT_15);
      }
    }
    if (!hasCpp && (hasJava | hasPython)) {

      stringBuffer.append(TEXT_16);
    }
    if (hasCpp) {

      stringBuffer.append(TEXT_17);
    }
    if (hasJava) {

      stringBuffer.append(TEXT_18);
    }
    if (hasPython) {

      stringBuffer.append(TEXT_19);
    }

    stringBuffer.append(TEXT_20);

    if (param.getSoftPkg().getDescription() != null) {

      stringBuffer.append(TEXT_21);
      stringBuffer.append(param.getSoftPkg().getDescription());

    } else {

      stringBuffer.append(TEXT_22);
      stringBuffer.append(projectType);
      stringBuffer.append(TEXT_23);
    }

    stringBuffer.append(TEXT_24);

    for (final Implementation impl : param.getSoftPkg().getImplementation()) {
      final ImplementationSettings implSettings = settings.get(impl.getId());
      if (implSettings == null) {
        continue;
      }

      stringBuffer.append(TEXT_25);
      stringBuffer.append(impl.getId());
      stringBuffer.append(TEXT_26);
      stringBuffer.append(implSettings.getOutputDir());
      stringBuffer.append(TEXT_27);
      stringBuffer.append(sdrrootFolder);
      stringBuffer.append(TEXT_28);
      stringBuffer.append(name);
      stringBuffer.append(TEXT_29);
      stringBuffer.append(implSettings.getOutputDir());
      stringBuffer.append(TEXT_30);
    }

    stringBuffer.append(TEXT_31);

    for (final Implementation impl : param.getSoftPkg().getImplementation()) {
      final ImplementationSettings implSettings = settings.get(impl.getId());
      if (implSettings == null) {
        continue;
      }

      stringBuffer.append(TEXT_32);
      stringBuffer.append(impl.getId());
      stringBuffer.append(TEXT_33);
      stringBuffer.append(implSettings.getOutputDir());
      stringBuffer.append(TEXT_34);
      stringBuffer.append(sdrrootFolder);
      stringBuffer.append(TEXT_35);
      stringBuffer.append(name);
      stringBuffer.append(TEXT_36);
      stringBuffer.append(implSettings.getOutputDir());
      stringBuffer.append(TEXT_37);
    }

    stringBuffer.append(TEXT_38);
    stringBuffer.append(sdrrootFolder);
    stringBuffer.append(TEXT_39);
    stringBuffer.append(sdrrootFolder);
    stringBuffer.append(TEXT_40);
    stringBuffer.append(ModelUtil.getSpdFileName(param.getSoftPkg()));

    if (param.getSoftPkg().getPropertyFile() != null) {

      stringBuffer.append(TEXT_41);
      stringBuffer.append(sdrrootFolder);
      stringBuffer.append(TEXT_42);
      stringBuffer.append(ModelUtil.getPrfFileName(param.getSoftPkg().getPropertyFile()));
    }

    stringBuffer.append(TEXT_43);
    stringBuffer.append(sdrrootFolder);
    stringBuffer.append(TEXT_44);
    stringBuffer.append(ModelUtil.getScdFileName(param.getSoftPkg()));

    for (final Implementation impl : param.getSoftPkg().getImplementation()) {
      final ImplementationSettings implSettings = settings.get(impl.getId());
      if (implSettings == null) {
        continue;
      }

      stringBuffer.append(TEXT_45);
      stringBuffer.append(sdrrootFolder);
      stringBuffer.append(TEXT_46);
      stringBuffer.append(implSettings.getOutputDir());
    }

    return stringBuffer.toString();
  }
 private DeviceConfiguration getDeviceConfiguration() {
   return ModelUtil.getDeviceConfiguration(this.dcdResource);
 }
 /**
  * Gets the project.
  *
  * @return the project
  */
 protected IProject getProject() {
   return ModelUtil.getProject(getSoftPkg());
 }