/**
  * Has all the ports have the default value ?
  *
  * @param ports the ports list
  * @return true if they all have the default values
  */
 private boolean hasDefaultValue(final List<InputPort> ports) {
   boolean allPortIsDefaultLabel = true;
   for (InputPort port : ports) {
     if (port.getValue() instanceof String) {
       String current = port.getValue().toString();
       if (!NOT_PRINTED_LABEL.equals(current)) {
         allPortIsDefaultLabel = false;
         break;
       }
     }
   }
   return allPortIsDefaultLabel;
 }
  /**
   * Fill the port with the parameters from the model structure.
   *
   * @param port the target instance
   */
  private void decodeModel(InputPort port) {
    ScilabDouble dataLines = (ScilabDouble) model.get(MODEL_IN_DATALINE_INDEX);
    ScilabDouble dataColumns = (ScilabDouble) model.get(MODEL_IN_DATACOL_INDEX);
    ScilabDouble dataType = (ScilabDouble) model.get(MODEL_IN_DATATYPE_INDEX);

    // The number of row of the port
    int nbLines;
    if (dataLines.getRealPart() != null) {
      nbLines = (int) dataLines.getRealPart()[alreadyDecodedCount][0];
    } else {
      nbLines = 1;
    }

    // The number of column of the port
    int nbColumns;
    if (dataColumns.getRealPart() != null) {
      try {
        nbColumns = (int) dataColumns.getRealPart()[alreadyDecodedCount][0];
      } catch (ArrayIndexOutOfBoundsException e) {
        nbColumns = 1;
      }
    } else {
      nbColumns = 1;
    }

    // port scilab type
    int type;
    if (dataType.getRealPart() != null) {
      try {
        type = (int) dataType.getRealPart()[alreadyDecodedCount][0];
      } catch (ArrayIndexOutOfBoundsException e) {
        type = 1;
      }
    } else {
      type = 1;
    }

    VectorOfInt v = new VectorOfInt(3);
    v.set(0, nbLines);
    v.set(1, nbColumns);
    v.set(2, type);
    controller.setObjectProperty(port.getUID(), port.getKind(), ObjectProperties.DATATYPE, v);
  }
  /**
   * Decode Scicos element into the block.
   *
   * @param element the scicos element
   * @param into the previously instantiated block.
   * @return the modified into block.
   * @throws ScicosFormatException on error.
   * @see org.scilab.modules.xcos.io.scicos.Element#decode(org.scilab.modules.types.ScilabType,
   *     java.lang.Object)
   */
  @Override
  public InputPort decode(ScilabType element, InputPort into) throws ScicosFormatException {

    InputPort port;
    data = (ScilabMList) element;

    port = allocatePort();
    port.setId(new UID().toString());

    port = beforeDecode(element, port);

    decodeModel(port);
    decodeGraphics(port);

    // Update the index counter
    alreadyDecodedCount++;

    port = afterDecode(element, port);

    return port;
  }
  /**
   * Change the label of the port according to the integer parameters property.
   *
   * @param source the source of the block
   */
  public void updateLabel(final BasicBlock source) {
    /** Get the input port children */
    final List<InputPort> ports = new ArrayList<InputPort>();
    for (int i = 0; i < source.getChildCount(); i++) {
      final mxICell port = source.getChildAt(i);

      if (port instanceof InputPort) {
        ports.add((InputPort) port);
      }
    }

    /** Set the ports labels */
    JavaController controller = new JavaController();
    VectorOfInt ipar = new VectorOfInt();
    controller.getObjectProperty(source.getUID(), Kind.BLOCK, ObjectProperties.IPAR, ipar);

    for (int i = 0; i < ports.size(); i++) {
      final int gain;

      if (i < ipar.size()) {
        gain = ipar.get(i);
      } else {
        gain = 1;
      }

      ports.get(i).setValue(getLabel(gain));
    }

    /** Check if all the values are equal to the default one. */
    if (!hasDefaultValue(ports)) {
      return;
    }

    /** When all values are equal to the default one, set it to the block and hide the children. */
    source.setValue(NOT_PRINTED_LABEL);
    for (InputPort port : ports) {
      port.setValue("");
    }
  }
  /**
   * Fill the port with the parameters from the graphics structure.
   *
   * @param port the target instance
   */
  private void decodeGraphics(InputPort port) {
    // protection against previously stored blocks
    if (graphics.size() > GRAPHICS_INSTYLE_INDEX
        && !isEmptyField(graphics.get(GRAPHICS_INSTYLE_INDEX))) {
      final ScilabString styles = (ScilabString) graphics.get(GRAPHICS_INSTYLE_INDEX);

      boolean isColumnDominant = styles.getHeight() >= styles.getWidth();
      int[] indexes = getIndexes(alreadyDecodedCount, isColumnDominant);

      if (canGet(styles, indexes)) {
        final String style;

        style = styles.getData()[indexes[0]][indexes[1]];
        port.setStyle(new StyleMap(port.getStyle()).putAll(style).toString());
      }
    }

    // protection against previously stored blocks
    if (graphics.size() > GRAPHICS_INLABEL_INDEX
        && !isEmptyField(graphics.get(GRAPHICS_INLABEL_INDEX))) {
      final ScilabString labels = (ScilabString) graphics.get(GRAPHICS_INLABEL_INDEX);

      boolean isColumnDominant = labels.getHeight() >= labels.getWidth();
      int[] indexes = getIndexes(alreadyDecodedCount, isColumnDominant);

      if (canGet(labels, indexes)) {
        final String label = labels.getData()[indexes[0]][indexes[1]];

        if (label != null) {
          port.setValue(label);
        } else {
          port.setValue("");
        }
      }
    }
  }