Example #1
0
  /**
   * Create a new Diva figure that visually represents this icon. The figure will be an instance of
   * LabelFigure that renders the values of the attributes of the container.
   *
   * @return A new CompositeFigure consisting of the label.
   */
  public Figure createFigure() {
    CompositeFigure result = (CompositeFigure) super.createFigure();
    String truncated = _displayString();

    // If there is no string to display now, then create a string
    // with a single blank.
    if (truncated == null) {
      truncated = " ";
    }

    // NOTE: This violates the Diva MVC architecture!
    // This attribute is part of the model, and should not have
    // a reference to this figure.  By doing so, it precludes the
    // possibility of having multiple views on this model.
    LabelFigure label = new LabelFigure(truncated, _labelFont, 1.0, SwingConstants.CENTER);
    Rectangle2D backBounds = result.getBackgroundFigure().getBounds();
    label.translateTo(backBounds.getCenterX(), backBounds.getCenterY());
    result.add(label);

    _addLiveFigure(label);
    return result;
  }
Example #2
0
  /**
   * Create a new background figure. This overrides the base class to draw a box around the value
   * display, where the width of the box depends on the value.
   *
   * @return A new figure.
   */
  public Figure createBackgroundFigure() {
    NamedObj container = getContainer();
    CompositeFigure result = new CompositeFigure();
    if (container != null) {
      try {
        ArrayToken fieldsValue = (ArrayToken) fields.getToken();
        Attribute associatedAttribute = container.getAttribute(variableName.getExpression());
        if (associatedAttribute instanceof Variable) {
          Token value = ((Variable) associatedAttribute).getToken();
          if (value instanceof ArrayToken) {
            // Find the number of rows and columns.
            int numRows = ((ArrayToken) value).length();
            int numColumns = fieldsValue.length();
            if (numColumns == 0) {
              // All columns should be included.
              // Make a pass to figure out how many that is.
              for (int i = 0; i < numRows; i++) {
                Token row = ((ArrayToken) value).getElement(i);
                if (row instanceof RecordToken) {
                  int rowWidth = ((RecordToken) row).labelSet().size();
                  if (rowWidth > numColumns) {
                    numColumns = rowWidth;
                  }
                }
              }
            }

            // Find the width of each column and the height of each row.
            // All rows are the same height, but column widths can vary.
            double rowHeight = 0.0;
            double columnWidth[] = new double[numColumns];
            for (int i = 1; i < numColumns; i++) {
              columnWidth[i] = 0.0;
            }
            LabelFigure tableElement[][] = new LabelFigure[numRows][numColumns];
            // Iterate over rows.
            for (int i = 0; i < numRows; i++) {
              Token row = ((ArrayToken) value).getElement(i);
              if (row instanceof RecordToken) {
                if (fieldsValue.length() == 0) {
                  // Display all fields.
                  Iterator labelSet = ((RecordToken) row).labelSet().iterator();
                  int j = 0;
                  while (labelSet.hasNext()) {
                    String column = (String) labelSet.next();
                    tableElement[i][j] = _labelFigure((RecordToken) row, column);
                    Rectangle2D bounds = tableElement[i][j].getBounds();
                    double width = bounds.getWidth();
                    if (width > columnWidth[j]) {
                      columnWidth[j] = width;
                    }
                    double height = bounds.getHeight();
                    if (height > rowHeight) {
                      rowHeight = height;
                    }
                    j++;
                  }
                } else {
                  // Display specified fields.
                  for (int j = 0; j < fieldsValue.length(); j++) {
                    if (j >= numColumns) {
                      break;
                    }
                    String column = ((StringToken) fieldsValue.getElement(j)).stringValue();
                    tableElement[i][j] = _labelFigure((RecordToken) row, column);
                    Rectangle2D bounds = tableElement[i][j].getBounds();
                    double width = bounds.getWidth();
                    if (width > columnWidth[j]) {
                      columnWidth[j] = width;
                    }
                    double height = bounds.getHeight();
                    if (height > rowHeight) {
                      rowHeight = height;
                    }
                  }
                }
              }
            }

            // Now make a pass to position and add all the figures.
            double rowPosition = _VERTICAL_PADDING;
            // Iterate over rows.
            for (int i = 0; i < numRows; i++) {
              Token row = ((ArrayToken) value).getElement(i);
              if (row instanceof RecordToken) {
                if (fieldsValue.length() == 0) {
                  // Display all fields.
                  Iterator labelSet = ((RecordToken) row).labelSet().iterator();
                  int j = 0;
                  double columnPosition = _HORIZONTAL_PADDING;
                  while (labelSet.hasNext()) {
                    /*String column = (String) */ labelSet.next();
                    tableElement[i][j].translateTo(columnPosition, rowPosition);
                    result.add(tableElement[i][j]);
                    columnPosition += columnWidth[j] + _HORIZONTAL_PADDING;
                    j++;
                  }
                } else {
                  // Display specified fields.
                  double columnPosition = _HORIZONTAL_PADDING;
                  for (int j = 0; j < fieldsValue.length(); j++) {
                    // String column = ((StringToken)fieldsValue.getElement(j)).stringValue();
                    tableElement[i][j].translateTo(columnPosition, rowPosition);
                    result.add(tableElement[i][j]);
                    columnPosition += columnWidth[j] + _HORIZONTAL_PADDING;
                  }
                }
              }
              rowPosition += rowHeight + _VERTICAL_PADDING;
            }
          }
        }
      } catch (IllegalActionException e) {
        // Stick the error message in the icon.
        result.add(new LabelFigure(e.getMessage()));
      }
    }
    // Now put a box around it all.
    Rectangle2D bounds = result.getBounds();
    // Double the padding below to allow for both sides.
    double width = Math.floor(bounds.getWidth()) + _HORIZONTAL_PADDING * 2;
    double height = Math.floor(bounds.getHeight()) + _VERTICAL_PADDING * 2;
    Figure rectangle = new BasicRectangle(0, 0, width, height, boxColor.asColor(), 1);
    CompositeFigure finalResult = new CompositeFigure(rectangle);
    finalResult.add(result);
    return finalResult;
  }