Search in sources :

Example 21 with Image

use of org.apache.pivot.wtk.media.Image in project pivot by apache.

the class ButtonDataRenderer method render.

@Override
public void render(Object data, Button button, boolean highlighted) {
    Image icon = null;
    String text = null;
    if (data instanceof BaseContent) {
        BaseContent baseContent = (BaseContent) data;
        icon = baseContent.getIcon();
    } else if (data instanceof Image) {
        icon = (Image) data;
    }
    text = toString(data);
    // Update the image view
    if (icon == null) {
        imageView.setVisible(false);
    } else {
        imageView.setVisible(true);
        imageView.setImage(icon);
        imageView.getStyles().put(Style.opacity, button.isEnabled() ? Float.valueOf(1.0f) : Float.valueOf(0.5f));
        if (getFillIcon()) {
            int buttonWidth = button.getWidth();
            int buttonHeight = button.getHeight();
            Insets padding = (Insets) button.getStyles().get(Style.padding);
            if (buttonWidth > 0) {
                imageView.setPreferredWidth(Math.max(buttonWidth - (padding.left + padding.right + 2), 0));
            }
            if (buttonHeight > 0) {
                imageView.setPreferredWidth(Math.max(buttonHeight - (padding.top + padding.bottom + 2), 0));
            }
        }
    }
    // Update the label
    label.setText(text != null ? text : "");
    if (text == null || (text.length() == 0 && getFillIcon())) {
        label.setVisible(false);
    } else {
        label.setVisible(true);
        Font font = button.getStyles().getFont(Style.font);
        label.getStyles().put(Style.font, font);
        Color color;
        if (button.isEnabled()) {
            color = button.getStyles().getColor(Style.color);
        } else {
            color = button.getStyles().getColor(Style.disabledColor);
        }
        label.getStyles().put(Style.color, color);
    }
}
Also used : Insets(org.apache.pivot.wtk.Insets) Color(java.awt.Color) Image(org.apache.pivot.wtk.media.Image) Font(java.awt.Font)

Example 22 with Image

use of org.apache.pivot.wtk.media.Image in project pivot by apache.

the class TableViewImageCellRenderer method render.

@Override
public void render(Object row, int rowIndex, int columnIndex, TableView tableView, String columnName, boolean selected, boolean highlighted, boolean disabled) {
    if (row != null) {
        Image image = null;
        // Get the row and cell data
        if (columnName != null) {
            Object cellData = JSON.get(row, columnName);
            if (cellData == null || cellData instanceof Image) {
                image = (Image) cellData;
            } else {
                throw new IllegalArgumentException("Data for \"" + columnName + "\" is not an instance of " + Image.class.getName());
            }
        }
        setImage(image);
        getStyles().put(Style.opacity, disabled ? 0.5f : 1.0f);
    }
}
Also used : Image(org.apache.pivot.wtk.media.Image)

Example 23 with Image

use of org.apache.pivot.wtk.media.Image in project pivot by apache.

the class MenuItemDataRenderer method render.

@Override
public void render(Object data, Button button, boolean highlighted) {
    Image icon = null;
    String text = null;
    Keyboard.KeyStroke keyboardShortcut = null;
    if (data instanceof BaseContent) {
        BaseContent buttonData = (BaseContent) data;
        icon = buttonData.getIcon();
        if (buttonData instanceof MenuItemData) {
            MenuItemData menuItemData = (MenuItemData) buttonData;
            keyboardShortcut = menuItemData.getKeyboardShortcut();
        }
    } else if (data instanceof Image) {
        icon = (Image) data;
    }
    text = toString(data);
    // attempt to retrieve icon from button data
    if (button.isSelected()) {
        icon = (Image) button.getStyles().get(Style.checkmarkImage);
    }
    // Update the image view
    Menu.Item menuItem = (Menu.Item) button;
    Menu menu = (Menu) menuItem.getParent();
    int margin = menu.getStyles().getInt(Style.margin);
    Insets padding = (Insets) getStyles().get(Style.padding);
    imageView.setImage(icon);
    imageView.setPreferredWidth(margin - padding.left * 2);
    imageView.getStyles().put(Style.opacity, button.isEnabled() ? new Float(1.0f) : new Float(0.5f));
    // Update the labels
    textLabel.setText(text != null ? text : "");
    Font font = menu.getStyles().getFont(Style.font);
    textLabel.getStyles().put(Style.font, font);
    keyboardShortcutLabel.getStyles().put(Style.font, font.deriveFont(Font.ITALIC));
    Color color;
    if (button.isEnabled()) {
        if (highlighted) {
            color = menu.getStyles().getColor(Style.activeColor);
        } else {
            color = menu.getStyles().getColor(Style.color);
        }
    } else {
        color = menu.getStyles().getColor(Style.disabledColor);
    }
    textLabel.getStyles().put(Style.color, color);
    keyboardShortcutLabel.getStyles().put(Style.color, color);
    boolean showKeyboardShortcuts = false;
    if (menu.getStyles().containsKey(Style.showKeyboardShortcuts)) {
        showKeyboardShortcuts = menu.getStyles().getBoolean(Style.showKeyboardShortcuts);
    }
    if (showKeyboardShortcuts) {
        keyboardShortcutLabel.setVisible(true);
        keyboardShortcutLabel.setText(keyboardShortcut != null ? keyboardShortcut.toString() : "");
    } else {
        keyboardShortcutLabel.setVisible(false);
    }
}
Also used : Insets(org.apache.pivot.wtk.Insets) Keyboard(org.apache.pivot.wtk.Keyboard) Color(java.awt.Color) Image(org.apache.pivot.wtk.media.Image) Font(java.awt.Font) Menu(org.apache.pivot.wtk.Menu)

Example 24 with Image

use of org.apache.pivot.wtk.media.Image in project pivot by apache.

the class ImageView method setImage.

/**
 * Sets the image view's current image by URL. <p> If the icon already
 * exists in the application context resource cache, the cached value will
 * be used. Otherwise, the icon will be loaded synchronously and added to
 * the cache.
 *
 * @param imageURL The location of the image to set.
 */
public final void setImage(final URL imageURL) {
    Utils.checkNull(imageURL, "imageURL");
    Image imageLocal = (Image) ApplicationContext.getResourceCache().get(imageURL);
    if (imageLocal == null) {
        // Convert to URI because using a URL as a key causes performance problems
        final java.net.URI imageURI;
        try {
            imageURI = imageURL.toURI();
        } catch (URISyntaxException exception) {
            throw new RuntimeException(exception);
        }
        if (asynchronous) {
            if (loadMap.containsKey(imageURI)) {
                // Add this to the list of image views that are interested in
                // the image at this URL
                loadMap.get(imageURI).add(this);
            } else {
                Image.load(imageURL, new TaskAdapter<>(new TaskListener<Image>() {

                    @Override
                    public void taskExecuted(Task<Image> task) {
                        Image imageLoadedLocal = task.getResult();
                        // Update the contents of all image views that requested this image
                        for (ImageView imageView : loadMap.get(imageURI)) {
                            imageView.setImage(imageLoadedLocal);
                        }
                        loadMap.remove(imageURI);
                        // Add the image to the cache
                        ApplicationContext.getResourceCache().put(imageURL, imageLoadedLocal);
                    }

                    @Override
                    public void executeFailed(Task<Image> task) {
                    // No-op
                    }
                }));
                loadMap.put(imageURI, new ArrayList<>(this));
            }
        } else {
            imageLocal = Image.loadFromCache(imageURL);
        }
    }
    setImage(imageLocal);
}
Also used : Task(org.apache.pivot.util.concurrent.Task) TaskListener(org.apache.pivot.util.concurrent.TaskListener) URISyntaxException(java.net.URISyntaxException) Image(org.apache.pivot.wtk.media.Image)

Example 25 with Image

use of org.apache.pivot.wtk.media.Image in project pivot by apache.

the class ImageViewSkin method getBaseline.

@Override
public int getBaseline(int width, int height) {
    ImageView imageView = (ImageView) getComponent();
    Image image = imageView.getImage();
    int baseline = -1;
    if (image != null) {
        baseline = image.getBaseline();
        if (baseline != -1) {
            Dimensions imageSize = image.getSize();
            if (fill) {
                // Scale to fit
                if (preserveAspectRatio) {
                    float aspectRatio = (float) width / (float) height;
                    float imageAspectRatio = (float) imageSize.width / (float) imageSize.height;
                    if (aspectRatio > imageAspectRatio) {
                        baseline *= (float) height / (float) imageSize.height;
                    } else {
                        float scaleYLocal = (float) width / (float) imageSize.width;
                        baseline *= scaleYLocal;
                        baseline += (int) (height - imageSize.height * scaleYLocal) / 2;
                    }
                } else {
                    baseline *= (float) height / (float) imageSize.height;
                }
            } else {
                if (verticalAlignment == VerticalAlignment.CENTER) {
                    baseline += (height - imageSize.height) / 2;
                } else if (verticalAlignment == VerticalAlignment.BOTTOM) {
                    baseline += height - imageSize.height;
                }
            }
        }
    }
    return baseline;
}
Also used : Dimensions(org.apache.pivot.wtk.Dimensions) ImageView(org.apache.pivot.wtk.ImageView) Image(org.apache.pivot.wtk.media.Image)

Aggregations

Image (org.apache.pivot.wtk.media.Image)35 Color (java.awt.Color)8 ImageView (org.apache.pivot.wtk.ImageView)8 Font (java.awt.Font)6 Component (org.apache.pivot.wtk.Component)5 ImageNode (org.apache.pivot.wtk.text.ImageNode)5 Dimensions (org.apache.pivot.wtk.Dimensions)3 Point (org.apache.pivot.wtk.Point)3 BasicStroke (java.awt.BasicStroke)2 Graphics2D (java.awt.Graphics2D)2 IOException (java.io.IOException)2 DragSource (org.apache.pivot.wtk.DragSource)2 DropAction (org.apache.pivot.wtk.DropAction)2 DropTarget (org.apache.pivot.wtk.DropTarget)2 Insets (org.apache.pivot.wtk.Insets)2 LocalManifest (org.apache.pivot.wtk.LocalManifest)2 Manifest (org.apache.pivot.wtk.Manifest)2 AlphaComposite (java.awt.AlphaComposite)1 Composite (java.awt.Composite)1 GradientPaint (java.awt.GradientPaint)1