Ejemplo n.º 1
0
  private BufferedImage getImage(String path, boolean isPluginRelative) {
    BufferedImage image = mImageCache.get(path);
    if (image == null) {
      try {
        if (isPluginRelative) {
          image = GraphicGenerator.getStencilImage(path);
        } else {
          File file = new File(path);

          // Requires Batik
          // if (file.getName().endsWith(DOT_SVG)) {
          //    image = loadSvgImage(file);
          // }

          if (image == null) {
            image = ImageIO.read(file);
          }
        }
      } catch (IOException e) {
        setErrorMessage(e.getLocalizedMessage());
      }

      if (image == null) {
        image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
      }

      mImageCache.put(path, image);
    }

    return image;
  }
  public void testClipartSource() throws Exception {
    myState.put(ATTR_SOURCE_TYPE, SourceType.CLIPART);

    ArgumentCaptor<ActionBarIconGenerator.ActionBarOptions> argument = runImageTest();

    BufferedImage expectedImage =
        GraphicGenerator.getClipartImage(myState.getString(ATTR_CLIPART_NAME));
    assertImagesSimilar("ClipartImage", expectedImage, argument.getValue().sourceImage, 5.0f);
  }
  /**
   * Displays a modal dialog with one button for each entry in the {@link GraphicGenerator} clipart
   * library. Clicking on a button sets that entry into the {@link ATTR_CLIPART_NAME} parameter.
   */
  private void displayClipartDialog() {
    Window window = SwingUtilities.getWindowAncestor(myPanel);
    final JDialog dialog = new JDialog(window, Dialog.ModalityType.DOCUMENT_MODAL);
    FlowLayout layout = new FlowLayout();
    dialog.getRootPane().setLayout(layout);
    int count = 0;
    for (Iterator<String> iter = GraphicGenerator.getClipartNames(); iter.hasNext(); ) {
      final String name = iter.next();
      try {
        JButton btn = new JButton();

        btn.setIcon(new ImageIcon(GraphicGenerator.getClipartIcon(name)));
        Dimension d = new Dimension(CLIPART_ICON_SIZE, CLIPART_ICON_SIZE);
        btn.setMaximumSize(d);
        btn.setPreferredSize(d);
        btn.addActionListener(
            new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                myTemplateState.put(ATTR_CLIPART_NAME, name);
                dialog.setVisible(false);
                update();
              }
            });
        dialog.getRootPane().add(btn);
        count++;
      } catch (IOException e) {
        LOG.error(e);
      }
    }
    int size =
        (int) (Math.sqrt(count) + 1) * (CLIPART_ICON_SIZE + layout.getHgap())
            + CLIPART_DIALOG_BORDER * 2;
    dialog.setSize(size, size + DIALOG_HEADER);
    dialog.setLocationRelativeTo(window);
    dialog.setVisible(true);
  }
Ejemplo n.º 4
0
  Map<String, Map<String, BufferedImage>> generateImages(boolean previewOnly) {
    // Map of ids to images: Preserve insertion order (the densities)
    Map<String, Map<String, BufferedImage>> categoryMap =
        new LinkedHashMap<String, Map<String, BufferedImage>>();

    CreateAssetSetWizard wizard = (CreateAssetSetWizard) getWizard();
    AssetType type = wizard.getAssetType();
    boolean crop = mTrimCheckBox.getSelection();

    BufferedImage sourceImage = null;
    if (mImageRadio.getSelection()) {
      // Load the image
      // TODO: Only do this when the source image type is image
      String path = mImagePathText.getText().trim();
      if (path.length() == 0) {
        setErrorMessage("Enter a filename");
        return Collections.emptyMap();
      }
      File file = new File(path);
      if (!file.exists()) {
        setErrorMessage(String.format("%1$s does not exist", file.getPath()));
        return Collections.emptyMap();
      }

      setErrorMessage(null);
      sourceImage = getImage(path, false);
      if (sourceImage != null) {
        if (crop) {
          sourceImage = ImageUtils.cropBlank(sourceImage, null, TYPE_INT_ARGB);
        }
        int padding = getPadding();
        if (padding != 0) {
          sourceImage = Util.paddedImage(sourceImage, padding);
        }
      }
    } else if (mTextRadio.getSelection()) {
      String text = mText.getText();
      TextRenderUtil.Options options = new TextRenderUtil.Options();
      options.font = getSelectedFont();
      int color;
      if (type.needsColors()) {
        color = 0xFF000000 | (mFgColor.red << 16) | (mFgColor.green << 8) | mFgColor.blue;
      } else {
        color = 0xFFFFFFFF;
      }
      options.foregroundColor = color;
      sourceImage = TextRenderUtil.renderTextImage(text, getPadding(), options);

      if (crop) {
        sourceImage = ImageUtils.cropBlank(sourceImage, null, TYPE_INT_ARGB);
      }

      int padding = getPadding();
      if (padding != 0) {
        sourceImage = Util.paddedImage(sourceImage, padding);
      }
    } else {
      assert mClipartRadio.getSelection();
      assert mSelectedClipart != null;
      try {
        sourceImage = GraphicGenerator.getClipartImage(mSelectedClipart);

        if (crop) {
          sourceImage = ImageUtils.cropBlank(sourceImage, null, TYPE_INT_ARGB);
        }

        if (type.needsColors()) {
          int color = 0xFF000000 | (mFgColor.red << 16) | (mFgColor.green << 8) | mFgColor.blue;
          Paint paint = new java.awt.Color(color);
          sourceImage = Util.filledImage(sourceImage, paint);
        }

        int padding = getPadding();
        if (padding != 0) {
          sourceImage = Util.paddedImage(sourceImage, padding);
        }
      } catch (IOException e) {
        AdtPlugin.log(e, null);
        return categoryMap;
      }
    }

    GraphicGenerator generator = null;
    GraphicGenerator.Options options = null;
    switch (type) {
      case LAUNCHER:
        {
          generator = new LauncherIconGenerator();
          LauncherIconGenerator.LauncherOptions launcherOptions =
              new LauncherIconGenerator.LauncherOptions();
          launcherOptions.shape =
              mCircleButton.getSelection()
                  ? GraphicGenerator.Shape.CIRCLE
                  : GraphicGenerator.Shape.SQUARE;
          launcherOptions.crop = mCropRadio.getSelection();

          if (SUPPORT_LAUNCHER_ICON_TYPES) {
            launcherOptions.style =
                mFancyRadio.getSelection()
                    ? GraphicGenerator.Style.FANCY
                    : mGlossyRadio.getSelection()
                        ? GraphicGenerator.Style.GLOSSY
                        : GraphicGenerator.Style.SIMPLE;
          } else {
            launcherOptions.style = GraphicGenerator.Style.SIMPLE;
          }

          int color = (mBgColor.red << 16) | (mBgColor.green << 8) | mBgColor.blue;
          launcherOptions.backgroundColor = color;
          // Flag which tells the generator iterator to include a web graphic
          launcherOptions.isWebGraphic = !previewOnly;
          options = launcherOptions;

          break;
        }
      case MENU:
        generator = new MenuIconGenerator();
        options = new GraphicGenerator.Options();
        break;
      case ACTIONBAR:
        {
          generator = new ActionBarIconGenerator();
          ActionBarIconGenerator.ActionBarOptions actionBarOptions =
              new ActionBarIconGenerator.ActionBarOptions();
          actionBarOptions.theme =
              mHoloDarkRadio.getSelection()
                  ? ActionBarIconGenerator.Theme.HOLO_DARK
                  : ActionBarIconGenerator.Theme.HOLO_LIGHT;

          options = actionBarOptions;
          break;
        }
      case NOTIFICATION:
        {
          generator = new NotificationIconGenerator();
          NotificationIconGenerator.NotificationOptions notificationOptions =
              new NotificationIconGenerator.NotificationOptions();
          notificationOptions.shape =
              mCircleButton.getSelection()
                  ? GraphicGenerator.Shape.CIRCLE
                  : GraphicGenerator.Shape.SQUARE;
          options = notificationOptions;
          break;
        }
      case TAB:
        generator = new TabIconGenerator();
        options = new TabIconGenerator.TabOptions();
        break;
      default:
        AdtPlugin.log(IStatus.ERROR, "Unsupported asset type: %1$s", type);
        return categoryMap;
    }

    options.sourceImage = sourceImage;

    IProject project = wizard.getProject();
    Pair<Integer, Integer> v = ManifestInfo.computeSdkVersions(project);
    options.minSdk = v.getFirst();

    String baseName = wizard.getBaseName();
    generator.generate(null, categoryMap, this, options, baseName);

    return categoryMap;
  }