private List<INode> getSelectedNodes() {
    List<SelectionItem> selections =
        mEditor.getCanvasControl().getSelectionManager().getSelections();
    List<INode> nodes = new ArrayList<INode>(selections.size());
    for (SelectionItem item : selections) {
      nodes.add(item.getNode());
    }

    return nodes;
  }
Beispiel #2
0
  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Dimension sz = getSize();
    int top = Math.max(0, (sz.height - tableHeight) / 2);
    int left = Math.max(0, (sz.width - tableWidth) / 2);
    Model model = getModel();
    if (model == null) return;
    Selection sel = model.getSelection();
    int columns = sel.size();
    if (columns == 0) {
      g.setFont(BODY_FONT);
      GraphicsUtil.drawCenteredText(
          g, Strings.get("tableEmptyMessage"), sz.width / 2, sz.height / 2);
      return;
    }

    g.setColor(Color.GRAY);
    int lineY = top + cellHeight + HEADER_SEP / 2;
    g.drawLine(left, lineY, left + tableWidth, lineY);

    g.setColor(Color.BLACK);
    g.setFont(HEAD_FONT);
    FontMetrics headerMetric = g.getFontMetrics();
    int x = left;
    int y = top + headerMetric.getAscent() + 1;
    for (int i = 0; i < columns; i++) {
      x = paintHeader(sel.get(i).toShortString(), x, y, g, headerMetric);
    }

    g.setFont(BODY_FONT);
    FontMetrics bodyMetric = g.getFontMetrics();
    Rectangle clip = g.getClipBounds();
    int firstRow = Math.max(0, (clip.y - y) / cellHeight - 1);
    int lastRow = Math.min(rowCount, 2 + (clip.y + clip.height - y) / cellHeight);
    int y0 = top + cellHeight + HEADER_SEP;
    x = left;
    for (int col = 0; col < columns; col++) {
      SelectionItem item = sel.get(col);
      ValueLog log = model.getValueLog(item);
      int radix = item.getRadix();
      int offs = rowCount - log.size();
      y = y0 + Math.max(offs, firstRow) * cellHeight;
      for (int row = Math.max(offs, firstRow); row < lastRow; row++) {
        Value val = log.get(row - offs);
        String label = val.toDisplayString(radix);
        int width = bodyMetric.stringWidth(label);
        g.drawString(label, x + (cellWidth - width) / 2, y + bodyMetric.getAscent());
        y += cellHeight;
      }
      x += cellWidth + COLUMN_SEP;
    }
  }
Beispiel #3
0
  @Override
  public void paint(GC gc) {
    if (mHoverRect != null) {
      // Translate the hover rectangle (in canvas coordinates) to control
      // coordinates
      int x = mHScale.translate(mHoverRect.x);
      int y = mVScale.translate(mHoverRect.y);
      int w = mHScale.scale(mHoverRect.width);
      int h = mVScale.scale(mHoverRect.height);

      boolean hoverIsSelected = false;
      List<SelectionItem> selections = mCanvas.getSelectionManager().getSelections();
      for (SelectionItem item : selections) {
        if (mHoverRect.equals(item.getViewInfo().getSelectionRect())) {
          hoverIsSelected = true;
          break;
        }
      }

      Color stroke = hoverIsSelected ? mHoverSelectStrokeColor : mHoverStrokeColor;
      Color fill = hoverIsSelected ? mHoverSelectFillColor : mHoverFillColor;

      if (stroke != null) {
        int oldAlpha = gc.getAlpha();
        gc.setForeground(stroke);
        gc.setLineStyle(hoverIsSelected ? HOVER_SELECTION.getLineStyle() : HOVER.getLineStyle());
        gc.setAlpha(hoverIsSelected ? HOVER_SELECTION.getStrokeAlpha() : HOVER.getStrokeAlpha());
        gc.drawRectangle(x, y, w, h);
        gc.setAlpha(oldAlpha);
      }

      if (fill != null) {
        int oldAlpha = gc.getAlpha();
        gc.setAlpha(hoverIsSelected ? HOVER_SELECTION.getFillAlpha() : HOVER.getFillAlpha());
        gc.setBackground(fill);
        gc.fillRectangle(x, y, w, h);
        gc.setAlpha(oldAlpha);
      }
    }
  }
  /** Updates the layout contents based on the current selection */
  void updateSelection() {
    NodeProxy parent = null;
    LayoutCanvas canvas = mEditor.getCanvasControl();
    SelectionManager selectionManager = canvas.getSelectionManager();
    List<SelectionItem> selections = selectionManager.getSelections();
    if (selections.size() > 0) {
      // TODO: better handle multi-selection -- maybe we should disable it or
      // something.
      // What if you select children with different parents? Of different types?
      // etc.
      NodeProxy node = selections.get(0).getNode();
      if (node != null && node.getParent() != null) {
        parent = (NodeProxy) node.getParent();
      }
    }

    if (parent == null) {
      // Show the background's properties
      CanvasViewInfo root = canvas.getViewHierarchy().getRoot();
      if (root == null) {
        return;
      }
      parent = canvas.getNodeFactory().create(root);
      selections = Collections.emptyList();
    }

    RulesEngine engine = mEditor.getRulesEngine();
    List<NodeProxy> selectedNodes = new ArrayList<NodeProxy>();
    for (SelectionItem item : selections) {
      selectedNodes.add(item.getNode());
    }
    List<RuleAction> actions = new ArrayList<RuleAction>();
    engine.callAddLayoutActions(actions, parent, selectedNodes);

    // Place actions in the correct order (the actions may come from different
    // rules and should be merged properly via sorting keys)
    Collections.sort(actions);

    // Add in actions for the child as well, if there is exactly one.
    // These are not merged into the parent list of actions; they are appended
    // at the end.
    int index = -1;
    String label = null;
    if (selectedNodes.size() == 1) {
      List<RuleAction> itemActions = new ArrayList<RuleAction>();
      NodeProxy selectedNode = selectedNodes.get(0);
      engine.callAddLayoutActions(itemActions, selectedNode, null);
      if (itemActions.size() > 0) {
        Collections.sort(itemActions);

        if (!(itemActions.get(0) instanceof RuleAction.Separator)) {
          actions.add(RuleAction.createSeparator(0));
        }
        label = selectedNode.getStringAttr(ANDROID_URI, ATTR_ID);
        if (label != null) {
          label = BaseViewRule.stripIdPrefix(label);
          index = actions.size();
        }
        actions.addAll(itemActions);
      }
    }

    if (!updateActions(actions)) {
      updateToolbar(actions, index, label);
    }
    mPrevActions = actions;
  }