示例#1
0
 /**
  * Remove the EditorIcons defined for the entity.
  *
  * @param entity The entity.
  * @return true if at least one EditorIcon is found and removed; false otherwise.
  * @exception KernelException If error occurs while removing the EditorIcons.
  */
 private static boolean _removeEditorIcons(GTEntity entity) throws KernelException {
   NamedObj object = (NamedObj) entity;
   boolean foundPersistentIcon = false;
   try {
     object.workspace().getReadAccess();
     for (Object iconObject : object.attributeList(EditorIcon.class)) {
       EditorIcon icon = (EditorIcon) iconObject;
       if (icon.isPersistent()) {
         foundPersistentIcon = true;
       } else {
         icon.setContainer(null);
       }
     }
   } finally {
     object.workspace().doneReading();
   }
   return !foundPersistentIcon;
 }
示例#2
0
  /**
   * Check whether the attribute is unique in the given container.
   *
   * @param attribute The attribute to check.
   * @param container The container.
   * @exception IllegalActionException If the container already has an attribute in the same class.
   */
  public static void checkUniqueness(Attribute attribute, NamedObj container)
      throws IllegalActionException {
    if (container instanceof EntityLibrary) {
      return;
    }

    try {
      container.workspace().getReadAccess();
      List<? extends Attribute> attributeList = container.attributeList(attribute.getClass());
      for (Attribute existingAttribute : attributeList) {
        if (existingAttribute != attribute && existingAttribute.isPersistent()) {
          _delete(attribute);
          throw new IllegalActionException(
              "Only 1 " + attribute.getClass().getSimpleName() + " can be used.");
        }
      }
    } finally {
      container.workspace().doneReading();
    }
  }
示例#3
0
  /**
   * Update the appearance (icons and ports) of the entity with the change in a
   * GTIngredientAttribute, such as a criterion or an operation.
   *
   * @param entity The entity.
   * @param attribute The attribute whose recent change leads to an update of the entity.
   */
  public static void updateAppearance(final GTEntity entity, GTIngredientsAttribute attribute) {

    NamedObj object = (NamedObj) entity;
    Workspace workspace = object.workspace();
    try {
      workspace.getWriteAccess();

      List<?> icons = object.attributeList(EditorIcon.class);
      boolean foundPersistentIcon = false;
      for (Object iconObject : icons) {
        EditorIcon icon = (EditorIcon) iconObject;
        if (icon.isPersistent()) {
          foundPersistentIcon = true;
          break;
        }
      }

      Set<String> preservedPortNames = new HashSet<String>();
      boolean isIconSet = false;
      int i = 1;
      GTIngredientList list = attribute.getIngredientList();
      for (GTIngredient ingredient : list) {
        if (ingredient instanceof PortCriterion) {
          PortCriterion criterion = (PortCriterion) ingredient;
          String portID = criterion.getPortID(list);
          preservedPortNames.add(portID);

          TypedIOPort port = (TypedIOPort) ((ComponentEntity) entity).getPort(portID);
          boolean isInput = criterion.isInput();
          boolean isOutput = criterion.isOutput();
          boolean isMultiport = !criterion.isMultiportEnabled() || criterion.isMultiport();
          if (port != null) {
            if (port instanceof PortMatcher) {
              port.setInput(isInput);
              port.setOutput(isOutput);
            } else {
              MoMLChangeRequest request =
                  new MoMLChangeRequest(
                      entity, object, "<deletePort name=\"" + port.getName() + "\"/>");
              request.setUndoable(true);
              request.setMergeWithPreviousUndo(true);
              request.execute();
              port =
                  new PortMatcher(criterion, (ComponentEntity) entity, portID, isInput, isOutput);
              port.setDerivedLevel(1);
            }
          } else {
            port = new PortMatcher(criterion, (ComponentEntity) entity, portID, isInput, isOutput);
            port.setDerivedLevel(1);
          }
          port.setMultiport(isMultiport);
        } else if (ingredient instanceof SubclassCriterion && !isIconSet && !foundPersistentIcon) {
          SubclassCriterion criterion = (SubclassCriterion) ingredient;
          final String superclass = criterion.getSuperclass();
          object.requestChange(
              new ChangeRequest(entity, "Deferred load actor icon action.") {
                protected void _execute() throws Exception {
                  _loadActorIcon(entity, superclass);
                }
              });
          isIconSet = true;
        }
        i++;
      }
      if (!isIconSet && !foundPersistentIcon) {
        object.requestChange(new RestoreAppearanceChangeRequest(entity));
      }

      ComponentEntity component = (ComponentEntity) entity;
      try {
        component.workspace().getReadAccess();
        List<?> portList = new LinkedList<Object>(component.portList());
        for (i = 0; i < portList.size(); i++) {
          Port port = (Port) portList.get(i);
          if (port instanceof PortMatcher && !preservedPortNames.contains(port.getName())) {
            ((PortMatcher) port)._setPortCriterion(null);
            port.setContainer(null);
          }
        }
      } finally {
        component.workspace().doneReading();
      }

    } catch (KernelException e) {
      throw new KernelRuntimeException(
          e, "Cannot update appearance for " + "actor " + entity.getName() + ".");

    } finally {
      workspace.doneWriting();
    }
  }