/* (non-Javadoc)
   * @see org.eclipse.vtp.desktop.editors.core.theme.ElementFrame#getIncursionPoint(int, int)
   */
  public Point getIncursionPoint(int x, int y) {
    if ((upperLeft == null) || (lowerRight == null)) {
      return null;
    }

    Point p1 = new Point(x, y);
    Point p2 = new Point(uiElement.getCenterPoint().x, uiElement.getCenterPoint().y);
    Point ret =
        getIntersection(
            p1, p2, new Point(upperLeft.x, upperLeft.y), new Point(lowerRight.x, upperLeft.y));

    if (ret == null) {
      ret =
          getIntersection(
              p1, p2, new Point(lowerRight.x, upperLeft.y), new Point(lowerRight.x, lowerRight.y));
    }

    if (ret == null) {
      ret =
          getIntersection(
              p1, p2, new Point(upperLeft.x, upperLeft.y), new Point(upperLeft.x, lowerRight.y));
    }

    if (ret == null) {
      ret =
          getIntersection(
              p1, p2, new Point(upperLeft.x, lowerRight.y), new Point(lowerRight.x, lowerRight.y));
    }

    return ret;
  }
 /* (non-Javadoc)
  * @see org.eclipse.vtp.desktop.editors.core.theme.ComponentFrame#initializeGraphics(org.eclipse.swt.graphics.GC, java.util.Map)
  */
 public void initializeGraphics(GC gc, Map<String, Object> resourceMap) {
   Font originalFont = gc.getFont();
   Font nameFont = new Font(gc.getDevice(), "Arial", 10, SWT.NORMAL);
   gc.setFont(nameFont);
   Point extent = gc.stringExtent(this.getDesignElement().getName());
   if (extent.x > 110) // too long, attempt to wrap text
   {
     extent.x = 0;
     String[] parts = this.getDesignElement().getName().split(" ");
     Point stringExtent = gc.stringExtent(parts[0]);
     int ew = 0;
     ew += stringExtent.x;
     extent.x = stringExtent.x;
     for (int i = 1; i < parts.length; i++) {
       stringExtent = gc.stringExtent(" " + parts[i]);
       if (ew + stringExtent.x > 110) // wrap it
       {
         stringExtent = gc.stringExtent(parts[i]);
         if (extent.x < ew) extent.x = ew;
         ew = stringExtent.x;
         extent.y += 2 + stringExtent.y;
       } else ew += stringExtent.x;
     }
     if (extent.x < ew) extent.x = ew;
   }
   int width = (extent.x + 35);
   int height = (extent.y + 10 + (uiElement.hasConnectors() ? 12 : 0));
   Point centerPoint = uiElement.getCenterPoint();
   upperLeft = new Point(centerPoint.x - (width / 2), centerPoint.y - (height / 2));
   int buffer = width % 2;
   lowerRight = new Point(upperLeft.x + width + buffer, upperLeft.y + height + buffer);
   gc.setFont(originalFont);
   nameFont.dispose();
 }
 /* (non-Javadoc)
  * @see org.eclipse.vtp.desktop.editors.core.theme.ComponentFrame#adjustPosition(int, int)
  */
 public void adjustPosition(int xoffset, int yoffset) {
   Point newCenter =
       new Point(uiElement.getCenterPoint().x + xoffset, uiElement.getCenterPoint().y + yoffset);
   uiElement.setCenterPoint(newCenter);
   upperLeft.x = upperLeft.x + xoffset;
   upperLeft.y = upperLeft.y + yoffset;
   lowerRight.x = lowerRight.x + xoffset;
   lowerRight.y = lowerRight.y + yoffset;
 }
 /**
  * Creates a new instance that represents the given ui element.
  *
  * @param uiElement The ui element this frame represents
  */
 public MantisElementFrame(IDesignElement uiElement) {
   super(uiElement);
   this.uiElement = uiElement;
   uiElement.addListener(this);
   uiElement.addPropertyListener(this);
   if (uiElement.getIcon() != null) icon = uiElement.getIcon();
   else
     icon =
         org.eclipse.vtp.desktop.core.Activator.getDefault().getImageRegistry().get("ICON_MODULE");
 }
 /* (non-Javadoc)
  * @see org.eclipse.vtp.desktop.editors.core.theme.ComponentFrame#mouseDown(org.eclipse.vtp.desktop.editors.core.commands.CommandListener, int, int, int)
  */
 public void mouseDown(CommandListener commandListener, int x, int y, int modifiers) {
   if (uiElement.getConnectorRecords().size() > 0
       && new Rectangle(lowerRight.x - 17, lowerRight.y - 17, 14, 14).contains(x, y)) {
     commandListener.executeCommand(new BeginConnector());
   } else if (getBounds().contains(x, y)) {
     commandListener.executeCommand(new StartMove());
   }
 }
 /* (non-Javadoc)
  * @see org.eclipse.vtp.desktop.editors.core.theme.ElementFrame#alignCenter(int, int)
  */
 public void alignCenter(int orientation, int xy) {
   int diffX = 0;
   int diffY = 0;
   Point center = uiElement.getCenterPoint();
   if (orientation == SWT.HORIZONTAL) {
     diffY = xy - center.y;
   } else if (orientation == SWT.VERTICAL) {
     diffX = xy - center.x;
   } else return;
   adjustPosition(diffX, diffY);
   this.fireChange();
 }
 /** @param name */
 public ReturnVariablesPropertyPanel(String name, IDesignElement element) {
   super(name, element);
   info = (ReturnInformationProvider) ((PrimitiveElement) element).getInformationProvider();
   exportedVars = new ArrayList<String>(info.getExports());
   List<Variable> vars = element.getDesign().getVariablesFor(element);
   outer:
   for (Variable v : vars) {
     for (int i = 0; i < variables.size(); i++) {
       if (variables.get(i).getName().compareToIgnoreCase(v.getName()) > 0) {
         variables.add(i, v);
         continue outer;
       }
     }
     variables.add(v);
   }
 }
  /* (non-Javadoc)
   * @see org.eclipse.vtp.desktop.editors.themes.mantis.MantisComponentFrame#renderFrame(org.eclipse.swt.graphics.GC, int, int, java.util.Map)
   */
  public void renderFrame(GC gc, int renderingPhase, int options, Map<String, Object> resourceMap) {
    Font originalFont = gc.getFont();
    Color foreground = gc.getForeground();
    Color background = gc.getBackground();
    Color selectedColor = getColor(gc, resourceMap, "rust.selection.color", 82, 140, 55);
    Color elementBlue = getColor(gc, resourceMap, "rust.element.color", 207, 234, 195);
    Color elementGradBlue = getColor(gc, resourceMap, "rust.element.color.gradient", 161, 211, 137);
    Font nameFont = new Font(gc.getDevice(), "Arial", 10, SWT.NORMAL);
    gc.setFont(nameFont);
    if (upperLeft == null) {
      initializeGraphics(gc, resourceMap);
    }
    int width = lowerRight.x - upperLeft.x;
    int height = lowerRight.y - upperLeft.y;
    int mainBodyHeight = height - (uiElement.hasConnectors() ? 12 : 0);

    if (uiElement.hasConnectors()) {
      // draw inspector tray
      gc.setBackground(elementBlue);
      gc.fillRoundRectangle(upperLeft.x, upperLeft.y + 3, width - 1, height - 3, 20, 20);

      // draw inspector tray border
      if (selected && (options & Theme.RENDER_FLAG_NO_SELECTION) == 0) {
        gc.setForeground(selectedColor);
      }
      gc.drawRoundRectangle(upperLeft.x, upperLeft.y + 3, width - 1, height - 3, 20, 20);
    }

    // draw main body
    gc.setBackground(elementBlue);
    gc.fillRoundRectangle(upperLeft.x, upperLeft.y, width - 1, mainBodyHeight - 1, 20, 20);
    gc.setBackground(elementGradBlue);
    gc.fillRoundRectangle(
        upperLeft.x + 1,
        upperLeft.y + (mainBodyHeight / 2) - 1,
        lowerRight.x - upperLeft.x - 2,
        (mainBodyHeight / 2) + (mainBodyHeight % 2),
        20,
        20);
    //		gc.setForeground(elementBlue);
    //		gc.fillGradientRectangle(upperLeft.x + 2,
    //			upperLeft.y
    //			+ (mainBodyHeight / 3),
    //			lowerRight.x - upperLeft.x - 4,
    //			((mainBodyHeight / 3) * 2) - 5,
    //			true);
    gc.setForeground(foreground);
    gc.setBackground(background);

    // draw main body border
    if (selected && (options & Theme.RENDER_FLAG_NO_SELECTION) == 0) {
      gc.setForeground(selectedColor);
    }
    gc.drawRoundRectangle(upperLeft.x, upperLeft.y, width - 1, mainBodyHeight - 1, 20, 20);

    gc.setForeground(foreground);
    // draw connector hot spot
    if (uiElement.hasConnectors()) {
      gc.drawLine(lowerRight.x - 19, lowerRight.y - 8, lowerRight.x - 5, lowerRight.y - 8);
      //			gc.drawLine(lowerRight.x - 8, lowerRight.y - 15, lowerRight.x - 3, lowerRight.y - 10);
      gc.drawLine(lowerRight.x - 10, lowerRight.y - 3, lowerRight.x - 5, lowerRight.y - 8);
    }

    // draw icon
    if ((options & Theme.RENDER_FLAG_NO_ICONS) == 0) {
      gc.drawImage(
          icon,
          upperLeft.x + 5,
          upperLeft.y + (((mainBodyHeight - 16) / 2) + ((mainBodyHeight - 16) % 2)));
    }

    // draw element name
    int curX = upperLeft.x + 25;
    int curY = upperLeft.y + 5;
    String[] parts = this.getDesignElement().getName().split(" ");
    Point stringExtent = gc.stringExtent(parts[0]);
    int ew = stringExtent.x;
    gc.drawString(parts[0], curX, curY, true);
    curX += stringExtent.x;
    for (int i = 1; i < parts.length; i++) {
      stringExtent = gc.stringExtent(" " + parts[i]);
      boolean wrapped = false;
      if (ew + stringExtent.x > 110) // wrap it
      {
        stringExtent = gc.stringExtent(parts[i]);
        ew = stringExtent.x;
        curY += 2 + stringExtent.y;
        curX = upperLeft.x + 25;
        wrapped = true;
      } else ew += stringExtent.x;
      gc.drawString((wrapped ? "" : " ") + parts[i], curX, curY, true);
      curX += stringExtent.x;
    }

    // draw decorator icons
    if ((options & Theme.RENDER_FLAG_NO_MARKERS) == 0) {
      if (uiElement.hasErrors())
        gc.drawImage(
            org.eclipse.vtp.desktop.core.Activator.getDefault()
                .getImageRegistry()
                .get("ICON_ERROR"),
            lowerRight.x - 17,
            upperLeft.y);
      else if (uiElement.hasWarnings())
        gc.drawImage(
            org.eclipse.vtp.desktop.core.Activator.getDefault()
                .getImageRegistry()
                .get("ICON_WARNING"),
            lowerRight.x - 16,
            upperLeft.y);
      else if (uiElement.hasTodo())
        gc.drawImage(
            org.eclipse.vtp.desktop.core.Activator.getDefault().getImageRegistry().get("ICON_TASK"),
            lowerRight.x - 18,
            upperLeft.y + 2);
    }

    // resource cleanup
    if (selected) {
      gc.setForeground(foreground);
    }
    gc.setFont(originalFont);
    nameFont.dispose();
  }
  /* (non-Javadoc)
   * @see org.eclipse.vtp.desktop.editors.themes.attraction.AttractionComponentFrame#renderFrame(org.eclipse.swt.graphics.GC, int, int, java.util.Map)
   */
  public void renderFrame(GC gc, int renderingPhase, int options, Map<String, Object> resourceMap) {
    Font originalFont = gc.getFont();
    Color foreground = gc.getForeground();
    Color background = gc.getBackground();
    Color selectedColor = getColor(gc, resourceMap, "attraction.selection.color", 0, 0, 255);
    Color elementBlue = getColor(gc, resourceMap, "attraction.element.color", 201, 229, 255);
    Color elementGradBlue =
        getColor(gc, resourceMap, "attraction.element.color.gradient", 153, 206, 255);
    Font nameFont = getFont(gc, resourceMap, "attraction.element.font", "Arial", 10, SWT.NORMAL);
    gc.setFont(nameFont);

    if (upperLeft == null) {
      initializeGraphics(gc, resourceMap);
    }
    int width = lowerRight.x - upperLeft.x;
    int height = lowerRight.y - upperLeft.y;

    gc.setBackground(elementBlue);
    gc.fillRoundRectangle(upperLeft.x, upperLeft.y, width - 1, height - 1, 12, 12);
    if ((options & Theme.RENDER_FLAG_PRINTING) == 0) {
      gc.setBackground(elementGradBlue);
      gc.fillRoundRectangle(
          upperLeft.x,
          upperLeft.y + ((lowerRight.y - upperLeft.y) / 2),
          lowerRight.x - upperLeft.x,
          ((lowerRight.y - upperLeft.y) / 2),
          12,
          12);
      gc.setForeground(elementBlue);
      gc.fillGradientRectangle(
          upperLeft.x,
          upperLeft.y + ((lowerRight.y - upperLeft.y) / 3),
          lowerRight.x - upperLeft.x,
          (((lowerRight.y - upperLeft.y) / 3) * 2) - 5,
          true);
    }
    gc.setForeground(foreground);
    gc.setBackground(background);

    // draw connector hot spot
    if (uiElement.hasConnectors()) {
      gc.drawLine(lowerRight.x - 17, lowerRight.y - 10, lowerRight.x - 3, lowerRight.y - 10);
      gc.drawLine(lowerRight.x - 8, lowerRight.y - 15, lowerRight.x - 3, lowerRight.y - 10);
      gc.drawLine(lowerRight.x - 8, lowerRight.y - 5, lowerRight.x - 3, lowerRight.y - 10);
    }

    if (selected && (options & Theme.RENDER_FLAG_NO_SELECTION) == 0) {
      gc.setForeground(selectedColor);
    }
    gc.drawRoundRectangle(upperLeft.x, upperLeft.y, width - 1, height - 1, 12, 12);
    if ((options & Theme.RENDER_FLAG_NO_ICONS) == 0) {
      gc.drawImage(icon, upperLeft.x + 10, upperLeft.y + 12);
    }
    int curX = upperLeft.x + 30;
    int curY = upperLeft.y + 15;
    String[] parts = this.getDesignElement().getName().split(" ");
    Point stringExtent = gc.stringExtent(parts[0]);
    int ew = stringExtent.x;
    gc.drawString(parts[0], curX, curY, true);
    curX += stringExtent.x;
    for (int i = 1; i < parts.length; i++) {
      stringExtent = gc.stringExtent(" " + parts[i]);
      boolean wrapped = false;
      if (ew + stringExtent.x > 110) // wrap it
      {
        stringExtent = gc.stringExtent(parts[i]);
        ew = stringExtent.x;
        curY += 3 + stringExtent.y;
        curX = upperLeft.x + 30;
        wrapped = true;
      } else ew += stringExtent.x;
      gc.drawString((wrapped ? "" : " ") + parts[i], curX, curY, true);
      curX += stringExtent.x;
    }
    if (selected) {
      gc.setForeground(foreground);
    }
    gc.setFont(originalFont);
    if ((options & Theme.RENDER_FLAG_NO_MARKERS) == 0) {
      if (uiElement.hasErrors())
        gc.drawImage(
            org.eclipse.vtp.desktop.core.Activator.getDefault()
                .getImageRegistry()
                .get("ICON_ERROR"),
            lowerRight.x - 17,
            upperLeft.y);
      else if (uiElement.hasWarnings())
        gc.drawImage(
            org.eclipse.vtp.desktop.core.Activator.getDefault()
                .getImageRegistry()
                .get("ICON_WARNING"),
            lowerRight.x - 16,
            upperLeft.y);
      else if (uiElement.hasTodo())
        gc.drawImage(
            org.eclipse.vtp.desktop.core.Activator.getDefault().getImageRegistry().get("ICON_TASK"),
            lowerRight.x - 18,
            upperLeft.y + 2);
    }
  }