示例#1
0
  /**
   * Find the image (SWT) in registry Store image in registry if it is not found
   *
   * @param umlImage to retrieve as SWT Image in registry
   * @return the stored SWT image
   */
  public static Image getImageInRegistry(
      org.eclipse.uml2.uml.Image umlImage, VisibilityKind visibility) {
    // Retrieve registry
    ImageRegistry papyrusRegistry = getDefault().getImageRegistry();

    // Get image id for registry
    String image_id = ImageUtil.getImageId(umlImage);
    if (visibility != null) {
      image_id = image_id + "_" + visibility.getLiteral();
    }

    // Get SWT image for shape in the registry
    Image image = papyrusRegistry.get(image_id);

    // If image was not found in registry,
    // try to find an image and to update registry
    if (image == null) {

      try {
        // Try to retrieve image from UML Image content property
        image = ImageUtil.getContent(umlImage);
      } catch (Exception e) {
        // Activator.log.error(e);
      }

      // If no image was found in Content
      // Try to retrieve image from UML Image location property
      if (image == null) {
        image = ImageUtil.getImageFromLocation(umlImage);
      }

      // Add visibility decorator
      if (visibility != null) {

        getDefault();
        ImageDescriptor visDesc =
            Activator.getImageDescriptor(
                UML_VIS_ICONS_16x16 + "VisibilityKind_" + visibility.getLiteral() + ".gif");

        // Overlay custom image over base image
        OverlayVisibilityIcon overlayIcon = new OverlayVisibilityIcon(image, visDesc);
        image = overlayIcon.getImage();
      }

      if (image != null) {
        // Store image in registry
        ImageData imdata = image.getImageData();
        papyrusRegistry.put(image_id, ImageDescriptor.createFromImageData(imdata));
        image = papyrusRegistry.get(image_id);
      }
    }

    return image;
  }
  public void refresh() {
    if (!browseButton.isDisposed()) {

      /* initialization of buttons enabling */
      if (!(getElement() instanceof Image)) {
        return;
      }

      // Get Image content
      org.eclipse.swt.graphics.Image image = null;

      try {
        image = ImageUtil.getContent((Image) getElement());
      } catch (Exception e) {
        Activator.log.error(e);
      }

      // Refresh text
      if (image != null) {
        // Resize icon to 32x32
        org.eclipse.swt.graphics.Image resizedIcon =
            new org.eclipse.swt.graphics.Image(
                image.getDevice(), image.getImageData().scaledTo(32, 32));
        browseButton.setImage(resizedIcon);
        removeButton.setEnabled(true);

      } else {
        browseButton.setImage(Activator.getDefault().getImage(addImagePath));
        removeButton.setEnabled(false);
      }
    }
  }