private void addPlainAction(RuleAction menuAction) {
    final ToolItem button = new ToolItem(mLayoutToolBar, SWT.PUSH);

    URL iconUrl = menuAction.getIconUrl();
    String title = menuAction.getTitle();
    if (iconUrl != null) {
      button.setImage(IconFactory.getInstance().getIcon(iconUrl));
      button.setToolTipText(title);
    } else {
      button.setText(title);
    }
    button.setData(menuAction);

    button.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            RuleAction menuAction = (RuleAction) button.getData();
            menuAction
                .getCallback()
                .action(menuAction, getSelectedNodes(), menuAction.getId(), false);
            updateSelection();
          }
        });
  }
  private void addRadio(RuleAction.Choices choices) {
    List<URL> icons = choices.getIconUrls();
    List<String> titles = choices.getTitles();
    List<String> ids = choices.getIds();
    String current = choices.getCurrent() != null ? choices.getCurrent() : ""; // $NON-NLS-1$

    assert icons != null;
    assert icons.size() == titles.size();

    for (int i = 0; i < icons.size(); i++) {
      URL iconUrl = icons.get(i);
      String title = titles.get(i);
      final String id = ids.get(i);
      final ToolItem item = new ToolItem(mLayoutToolBar, SWT.RADIO);
      item.setToolTipText(title);
      item.setImage(IconFactory.getInstance().getIcon(iconUrl));
      item.setData(choices);
      item.setData(ATTR_ID, id);
      item.addSelectionListener(
          new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
              if (item.getSelection()) {
                RuleAction.Choices choices = (Choices) item.getData();
                choices.getCallback().action(choices, getSelectedNodes(), id, null);
                updateSelection();
              }
            }
          });
      boolean selected = current.equals(id);
      if (selected) {
        item.setSelection(true);
      }
    }
  }
  private void addToggle(Toggle toggle) {
    final ToolItem button = new ToolItem(mLayoutToolBar, SWT.CHECK);

    URL iconUrl = toggle.getIconUrl();
    String title = toggle.getTitle();
    if (iconUrl != null) {
      button.setImage(IconFactory.getInstance().getIcon(iconUrl));
      button.setToolTipText(title);
    } else {
      button.setText(title);
    }
    button.setData(toggle);

    button.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            Toggle toggle = (Toggle) button.getData();
            toggle
                .getCallback()
                .action(toggle, getSelectedNodes(), toggle.getId(), button.getSelection());
            updateSelection();
          }
        });
    if (toggle.isChecked()) {
      button.setSelection(true);
    }
  }
  private void addDropdown(RuleAction.Choices choices) {
    final ToolItem combo = new ToolItem(mLayoutToolBar, SWT.DROP_DOWN);
    URL iconUrl = choices.getIconUrl();
    if (iconUrl != null) {
      combo.setImage(IconFactory.getInstance().getIcon(iconUrl));
      combo.setToolTipText(choices.getTitle());
    } else {
      combo.setText(choices.getTitle());
    }
    combo.setData(choices);

    Listener menuListener =
        new Listener() {
          @Override
          public void handleEvent(Event event) {
            Menu menu = new Menu(mLayoutToolBar.getShell(), SWT.POP_UP);
            RuleAction.Choices choices = (Choices) combo.getData();
            List<URL> icons = choices.getIconUrls();
            List<String> titles = choices.getTitles();
            List<String> ids = choices.getIds();
            String current =
                choices.getCurrent() != null ? choices.getCurrent() : ""; // $NON-NLS-1$

            for (int i = 0; i < titles.size(); i++) {
              String title = titles.get(i);
              final String id = ids.get(i);
              URL itemIconUrl = icons != null && icons.size() > 0 ? icons.get(i) : null;
              MenuItem item = new MenuItem(menu, SWT.CHECK);
              item.setText(title);
              if (itemIconUrl != null) {
                Image itemIcon = IconFactory.getInstance().getIcon(itemIconUrl);
                item.setImage(itemIcon);
              }

              boolean selected = id.equals(current);
              if (selected) {
                item.setSelection(true);
              }

              item.addSelectionListener(
                  new SelectionAdapter() {
                    @Override
                    public void widgetSelected(SelectionEvent e) {
                      RuleAction.Choices choices = (Choices) combo.getData();
                      choices.getCallback().action(choices, getSelectedNodes(), id, null);
                      updateSelection();
                    }
                  });
            }

            Rectangle bounds = combo.getBounds();
            Point location = new Point(bounds.x, bounds.y + bounds.height);
            location = combo.getParent().toDisplay(location);
            menu.setLocation(location.x, location.y);
            menu.setVisible(true);
          }
        };
    combo.addListener(SWT.Selection, menuListener);
  }
 static {
   ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
   IconFactory icons = IconFactory.getInstance();
   CLOSE_ICON = sharedImages.getImage(ISharedImages.IMG_ETOOL_DELETE);
   EDIT_ICON = icons.getIcon("editPreview"); // $NON-NLS-1$
   ZOOM_IN_ICON = icons.getIcon("zoomplus"); // $NON-NLS-1$
   ZOOM_OUT_ICON = icons.getIcon("zoomminus"); // $NON-NLS-1$
   CLOSE_ICON_WIDTH = CLOSE_ICON.getImageData().width;
   EDIT_ICON_WIDTH = EDIT_ICON.getImageData().width;
   ZOOM_IN_ICON_WIDTH = ZOOM_IN_ICON.getImageData().width;
   ZOOM_OUT_ICON_WIDTH = ZOOM_OUT_ICON.getImageData().width;
 }
 public Image getPageImage() {
   return IconFactory.getInstance()
       .getIcon(getTitle(), IconFactory.COLOR_RED, IconFactory.SHAPE_RECT);
 }
  /**
   * Sets whether the action bar should show the "lint warnings" button
   *
   * @param hasLintWarnings whether there are lint errors to be shown
   */
  private void updateErrorIndicator(final int markerCount) {
    Display display = getDisplay();
    if (display.getThread() != Thread.currentThread()) {
      display.asyncExec(
          new Runnable() {
            @Override
            public void run() {
              if (!isDisposed()) {
                updateErrorIndicator(markerCount);
              }
            }
          });
      return;
    }

    GridData layoutData = (GridData) mLintToolBar.getLayoutData();
    Integer existing = (Integer) mLintToolBar.getData();
    Integer current = Integer.valueOf(markerCount);
    if (!current.equals(existing)) {
      mLintToolBar.setData(current);
      boolean layout = false;
      boolean hasLintWarnings = markerCount > 0 && AdtPrefs.getPrefs().isLintOnSave();
      if (layoutData.exclude == hasLintWarnings) {
        layoutData.exclude = !hasLintWarnings;
        mLintToolBar.setVisible(hasLintWarnings);
        layout = true;
      }
      if (markerCount > 0) {
        String iconName = "";
        switch (markerCount) {
          case 1:
            iconName = "lint1";
            break; //$NON-NLS-1$
          case 2:
            iconName = "lint2";
            break; //$NON-NLS-1$
          case 3:
            iconName = "lint3";
            break; //$NON-NLS-1$
          case 4:
            iconName = "lint4";
            break; //$NON-NLS-1$
          case 5:
            iconName = "lint5";
            break; //$NON-NLS-1$
          case 6:
            iconName = "lint6";
            break; //$NON-NLS-1$
          case 7:
            iconName = "lint7";
            break; //$NON-NLS-1$
          case 8:
            iconName = "lint8";
            break; //$NON-NLS-1$
          case 9:
            iconName = "lint9";
            break; //$NON-NLS-1$
          default:
            iconName = "lint9p";
            break; //$NON-NLS-1$
        }
        mLintButton.setImage(IconFactory.getInstance().getIcon(iconName));
      }
      if (layout) {
        layout();
      }
      redraw();
    }
  }
  @SuppressWarnings("unused") // SWT constructors have side effects, they are not unused
  private ToolBar createZoomControls() {
    ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT | SWT.HORIZONTAL);

    IconFactory iconFactory = IconFactory.getInstance();
    mZoomRealSizeButton = new ToolItem(toolBar, SWT.CHECK);
    mZoomRealSizeButton.setToolTipText("Emulate Real Size");
    mZoomRealSizeButton.setImage(iconFactory.getIcon("zoomreal")); // $NON-NLS-1$);
    mZoomRealSizeButton.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            boolean newState = mZoomRealSizeButton.getSelection();
            if (rescaleToReal(newState)) {
              mZoomOutButton.setEnabled(!newState);
              mZoomResetButton.setEnabled(!newState);
              mZoomInButton.setEnabled(!newState);
              mZoomFitButton.setEnabled(!newState);
            } else {
              mZoomRealSizeButton.setSelection(!newState);
            }
          }
        });

    mZoomFitButton = new ToolItem(toolBar, SWT.PUSH);
    mZoomFitButton.setToolTipText("Zoom to Fit (0)");
    mZoomFitButton.setImage(iconFactory.getIcon("zoomfit")); // $NON-NLS-1$);
    mZoomFitButton.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            rescaleToFit(true);
          }
        });

    mZoomResetButton = new ToolItem(toolBar, SWT.PUSH);
    mZoomResetButton.setToolTipText("Reset Zoom to 100% (1)");
    mZoomResetButton.setImage(iconFactory.getIcon("zoom100")); // $NON-NLS-1$);
    mZoomResetButton.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            resetScale();
          }
        });

    // Group zoom in/out separately
    new ToolItem(toolBar, SWT.SEPARATOR);

    mZoomOutButton = new ToolItem(toolBar, SWT.PUSH);
    mZoomOutButton.setToolTipText("Zoom Out (-)");
    mZoomOutButton.setImage(iconFactory.getIcon("zoomminus")); // $NON-NLS-1$);
    mZoomOutButton.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            rescale(-1);
          }
        });

    mZoomInButton = new ToolItem(toolBar, SWT.PUSH);
    mZoomInButton.setToolTipText("Zoom In (+)");
    mZoomInButton.setImage(iconFactory.getIcon("zoomplus")); // $NON-NLS-1$);
    mZoomInButton.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            rescale(+1);
          }
        });

    return toolBar;
  }
 @Override
 public Image getIcon() {
   return IconFactory.getInstance().getIcon("ratio"); // $NON-NLS-1$
 }
  /**
   * Paints the preview title at the given position (and returns the required height)
   *
   * @param gc the graphics context to paint into
   * @param x the left edge of the preview rectangle
   * @param y the top edge of the preview rectangle
   * @param displayName the title string to be used
   */
  int paintTitle(GC gc, int x, int y, boolean showFile, String displayName) {
    int titleHeight = 0;

    if (showFile && mIncludedWithin != null) {
      if (mManager.getMode() != INCLUDES) {
        displayName = "<include>";
      } else {
        // Skip: just paint footer instead
        displayName = null;
      }
    }

    int width = getWidth();
    int labelTop = y + 1;
    gc.setClipping(x, labelTop, width, 100);

    // Use font height rather than extent height since we want two adjacent
    // previews (which may have different display names and therefore end
    // up with slightly different extent heights) to have identical title
    // heights such that they are aligned identically
    int fontHeight = gc.getFontMetrics().getHeight();

    if (displayName != null && displayName.length() > 0) {
      gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE));
      Point extent = gc.textExtent(displayName);
      int labelLeft = Math.max(x, x + (width - extent.x) / 2);
      Image icon = null;
      Locale locale = mConfiguration.getLocale();
      if (locale != null
          && (locale.hasLanguage() || locale.hasRegion())
          && (!(mConfiguration instanceof NestedConfiguration)
              || ((NestedConfiguration) mConfiguration).isOverridingLocale())) {
        icon = locale.getFlagImage();
      }

      if (icon != null) {
        int flagWidth = icon.getImageData().width;
        int flagHeight = icon.getImageData().height;
        labelLeft = Math.max(x + flagWidth / 2, labelLeft);
        gc.drawImage(icon, labelLeft - flagWidth / 2 - 1, labelTop);
        labelLeft += flagWidth / 2 + 1;
        gc.drawText(displayName, labelLeft, labelTop - (extent.y - flagHeight) / 2, true);
      } else {
        gc.drawText(displayName, labelLeft, labelTop, true);
      }

      labelTop += extent.y;
      titleHeight += fontHeight;
    }

    if (showFile && (mAlternateInput != null || mIncludedWithin != null)) {
      // Draw file flag, and parent folder name
      IFile file = mAlternateInput != null ? mAlternateInput : mIncludedWithin.getFile();
      String fileName = file.getParent().getName() + File.separator + file.getName();
      Point extent = gc.textExtent(fileName);
      Image icon = IconFactory.getInstance().getIcon("android_file"); // $NON-NLS-1$
      int flagWidth = icon.getImageData().width;
      int flagHeight = icon.getImageData().height;

      int labelLeft = Math.max(x, x + (width - extent.x - flagWidth - 1) / 2);

      gc.drawImage(icon, labelLeft, labelTop);

      gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_GRAY));
      labelLeft += flagWidth + 1;
      labelTop -= (extent.y - flagHeight) / 2;
      gc.drawText(fileName, labelLeft, labelTop, true);

      titleHeight += Math.max(titleHeight, icon.getImageData().height);
    }

    gc.setClipping((Region) null);

    return titleHeight;
  }
  /**
   * Paints the preview at the given x/y position
   *
   * @param gc the graphics context to paint it into
   * @param x the x coordinate to paint the preview at
   * @param y the y coordinate to paint the preview at
   */
  void paint(GC gc, int x, int y) {
    mTitleHeight = paintTitle(gc, x, y, true /*showFile*/);
    y += mTitleHeight;
    y += 2;

    int width = getWidth();
    int height = getHeight();
    if (mThumbnail != null && mError == null) {
      gc.drawImage(mThumbnail, x, y);

      if (mActive) {
        int oldWidth = gc.getLineWidth();
        gc.setLineWidth(3);
        gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_LIST_SELECTION));
        gc.drawRectangle(x - 1, y - 1, width + 2, height + 2);
        gc.setLineWidth(oldWidth);
      }
    } else if (mError != null) {
      if (mThumbnail != null) {
        gc.drawImage(mThumbnail, x, y);
      } else {
        gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_BORDER));
        gc.drawRectangle(x, y, width, height);
      }

      gc.setClipping(x, y, width, height);
      Image icon = IconFactory.getInstance().getIcon("renderError"); // $NON-NLS-1$
      ImageData data = icon.getImageData();
      int prevAlpha = gc.getAlpha();
      int alpha = 96;
      if (mThumbnail != null) {
        alpha -= 32;
      }
      gc.setAlpha(alpha);
      gc.drawImage(icon, x + (width - data.width) / 2, y + (height - data.height) / 2);

      String msg = mError;
      Density density = mConfiguration.getDensity();
      if (density == Density.TV || density == Density.LOW) {
        msg =
            "Broken rendering library; unsupported DPI. Try using the SDK manager "
                + "to get updated layout libraries.";
      }
      int charWidth = gc.getFontMetrics().getAverageCharWidth();
      int charsPerLine = (width - 10) / charWidth;
      msg = SdkUtils.wrap(msg, charsPerLine, null);
      gc.setAlpha(255);
      gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_BLACK));
      gc.drawText(msg, x + 5, y + HEADER_HEIGHT, true);
      gc.setAlpha(prevAlpha);
      gc.setClipping((Region) null);
    } else {
      gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_BORDER));
      gc.drawRectangle(x, y, width, height);

      Image icon = IconFactory.getInstance().getIcon("refreshPreview"); // $NON-NLS-1$
      ImageData data = icon.getImageData();
      int prevAlpha = gc.getAlpha();
      gc.setAlpha(96);
      gc.drawImage(icon, x + (width - data.width) / 2, y + (height - data.height) / 2);
      gc.setAlpha(prevAlpha);
    }

    if (mActive) {
      int left = x;
      int prevAlpha = gc.getAlpha();
      gc.setAlpha(208);
      Color bg = mCanvas.getDisplay().getSystemColor(SWT.COLOR_WHITE);
      gc.setBackground(bg);
      gc.fillRectangle(left, y, x + width - left, HEADER_HEIGHT);
      gc.setAlpha(prevAlpha);

      y += 2;

      // Paint icons
      gc.drawImage(CLOSE_ICON, left, y);
      left += CLOSE_ICON_WIDTH;

      gc.drawImage(ZOOM_IN_ICON, left, y);
      left += ZOOM_IN_ICON_WIDTH;

      gc.drawImage(ZOOM_OUT_ICON, left, y);
      left += ZOOM_OUT_ICON_WIDTH;

      gc.drawImage(EDIT_ICON, left, y);
      left += EDIT_ICON_WIDTH;
    }
  }