Skip to content

FileExtensionIndicator

roosit edited this page Oct 16, 2026 · 1 revision

FileExtensionIndicator

Up | Previous | Next | Русский

Full source with short explanations

Below is the full source of the class in documentation form. The comments inside the block explain the purpose of the parts of the class, but they are not stored in the working file FileExtensionIndicator.java: the production code stays clean, and the explanations live in the wiki.

package com.velitask.plugin.official;

import com.velitask.sdk.FrameContext;
import com.velitask.sdk.Indicator;
import com.velitask.sdk.IndicatorContext;
import com.velitask.sdk.properties.FileSourceProperty;
import com.velitask.sdk.properties.FontColorProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.IntegerSliderProperty;
import com.velitask.sdk.properties.PlayerSourceStrategy;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import org.abricos.core.state.maket.HorizontalAlign;
import org.abricos.core.state.maket.Maket;
import org.abricos.core.state.maket.VerticalAlign;

public class FileExtensionIndicator extends Indicator {

    /* Indicator name; the full layer name in JSON: com.velitask.plagin.official.fileExtension. */
    public static final String NAME = "fileExtension";

    /* Prefix of the indicator localization keys. */
    private static final String KEY = _KEY + "." + NAME;

    /* File source; the source the player is opened on has priority. */
    private final FileSourceProperty mSource = new FileSourceProperty() {
        {
            setStrategy(new PlayerSourceStrategy());
        }
    };

    /* Font and text color; the size is picked automatically. */
    private final FontColorProperty mFont = new FontColorProperty();

    /* Padding from the edges in percent of the smaller side of the layer: 0..40, 12 by default. */
    private final IntegerSliderProperty mPadding
            = new IntegerSliderProperty(12, "padding", localized(KEY + ".padding.title")) {
        {
            setRange(0, 40);
        }
    };

    /* The default color is slightly dimmer than white (E0E0E0); for saved layers the loaded value replaces it. */
    public FileExtensionIndicator() {
        mFont.getColor().set(new Color(0xE0E0E0));
    }

    @Override
    public String getName() {
        return NAME;
    }

    @Override
    public String getTitle() {
        return localized(KEY + ".title");
    }

    @Override
    public String getDescription() {
        return localized(KEY + ".description");
    }

    @Override
    public String getTags() {
        return localized(KEY + ".tags");
    }

    @Override
    /* Default maket: the whole canvas, no margins. */
    public void defineMaket(Maket maket) {
        maket.setVertical(VerticalAlign.FILL);
        maket.setHorizontal(HorizontalAlign.FILL);
        maket.setLayerMargin(0d, 0d, 0d, 0d);
    }

    @Override
    /* The order of the properties defines the order in the panel and in JSON. */
    public IProperty[] defineProperties() {
        return new IProperty[]{mSource, mFont, mPadding};
    }

    @Override
    /* Drawing of one frame. */
    public void render(IndicatorContext indicatorContext) {
        FileExtensionContext ctx = (FileExtensionContext) indicatorContext;

        /* No extension - draw nothing. */
        String text = ctx.extension;
        if (text == null || text.isEmpty()) {
            return;
        }

        /* Padding in pixels from the smaller side of the layer. */
        int pad = Math.round(Math.min(ctx.width, ctx.height) * ctx.padding / 100f);
        int maxW = ctx.width - 2 * pad;
        int maxH = ctx.height - 2 * pad;
        /* The area for the text has vanished - leave. */
        if (maxW <= 0 || maxH <= 0) {
            return;
        }

        Graphics2D g = ctx.graphics;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        /* The base font defines name, style and color; the size is picked below. */
        Font base = ctx.font.toFont(ctx.scale);
        Font fitted = fitFont(g, base, text, maxW, maxH);
        g.setFont(fitted);
        g.setColor(ctx.font.color.value);

        /* Centering by the text bounds. */
        FontMetrics fm = g.getFontMetrics();
        Rectangle2D b = fm.getStringBounds(text, g);
        int tx = (int) Math.round((ctx.width - b.getWidth()) / 2 - b.getX());
        int ty = (int) Math.round((ctx.height - b.getHeight()) / 2 - b.getY());
        g.drawString(text, tx, ty);
    }

    /* Binary search for the largest size at which the text fits into the area. */
    private static Font fitFont(Graphics2D g, Font base, String text, int maxW, int maxH) {
        int lo = 1;
        int hi = maxH;
        int best = 1;
        while (lo <= hi) {
            int mid = (lo + hi) >>> 1;
            Font f = base.deriveFont((float) mid);
            FontMetrics fm = g.getFontMetrics(f);
            Rectangle2D b = fm.getStringBounds(text, g);
            if (b.getWidth() <= maxW && b.getHeight() <= maxH) {
                best = mid;
                lo = mid + 1;
            } else {
                hi = mid - 1;
            }
        }
        return base.deriveFont((float) best);
    }

    /* Snapshot of the property values for one frame. */
    public class FileExtensionContext extends IndicatorContext {

        public final FontColorProperty.FontColorContext font;
        public final String extension;
        public final int padding;

        public FileExtensionContext(FrameContext frame) {
            super(frame);
            font = mFont.createContext(frame);
            padding = mPadding.get(frame);
            /* The lowercase extension is read straight from the source property, not from the frame snapshot. */
            String ext = mSource.getExtension();
            extension = ext == null ? null : ext.toLowerCase();
        }
    }

    @Override
    /* Creation of the frame context. */
    public IndicatorContext createContext(FrameContext frame) {
        return new FileExtensionContext(frame);
    }
}

Purpose

FileExtensionIndicator draws the extension of the source file (for example, dat) as large text over the whole layer. It is used as a fallback preview for files that have no preview template of their own (the thumbnail-undefined template).

How it works

Data. FileSourceProperty filters the project sources by IFileSource. The PlayerSourceStrategy strategy selects the source the player is opened on (the anchor), and if it is not among the candidates, the first one that fits by time (FirstInRangeStrategy). In the FileExtensionContext context the extension is taken as mSource.getExtension() and converted to lowercase (toLowerCase()); if no source is bound, the extension is null.

Settings. Three properties in the order of defineProperties():

  • fileSource is FileSourceProperty;
  • font is FontColorProperty; the default color is set in the constructor (E0E0E0), the font size does not affect drawing;
  • padding is IntegerSliderProperty, range 0..40, 12 (percent) by default.

There are no display groups or skins.

Drawing. The padding is pad = round(min(width, height) × padding / 100); the area for the text is the layer minus the padding on each side. The font size is picked by binary search (fitFont): the largest size from 1 up to the height of the area at which the bounds of the string (FontMetrics.getStringBounds) fit into the area. The text is centered by these bounds.

Features and limitations

  • No extension - no picture. If no source is bound or the extension is empty, render returns immediately.
  • The font size from the settings is not used. It is picked automatically; the name, style and color are taken from font.
  • The scale (ctx.scale) affects only the starting font, which is then fully overridden by fitFont.
  • The extension is read from the live property of the source, not from the frame snapshot (mSource.getExtension() is called in the context constructor).
  • The area for the text can vanish only on a degenerately small layer (after subtracting the padding the width or the height is not positive) - then there is no picture. The maximum padding of the slider is 40%.
  • Binding to the "player source" works when the layer is compiled; in a layer added by hand, with several files nearby a neighboring one may be selected.

Clone this wiki locally