All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.blaisemath.graphics.core.LabeledPointGraphic Maven / Gradle / Ivy

/**
 * LabeledPointGraphic.java
 * Created Aug 21, 2012
 */
package com.googlecode.blaisemath.graphics.core;

/*
 * #%L
 * BlaiseGraphics
 * --
 * Copyright (C) 2009 - 2015 Elisha Peterson
 * --
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */



import com.google.common.base.Strings;
import com.googlecode.blaisemath.style.AttributeSet;
import com.googlecode.blaisemath.style.ObjectStyler;
import com.googlecode.blaisemath.style.Renderer;
import com.googlecode.blaisemath.util.AnchoredText;
import java.awt.Point;
import java.awt.geom.Point2D;

/**
 * Uses an {@link ObjectStyler} and a source object to draw a labeled point on a
 * canvas. The style of the point and label is managed by the styler, along with
 * the tooltip.
 *
 * @param  source object type
 * @param  graphics canvas type
 * @author Elisha
 */
public class LabeledPointGraphic extends DelegatingPrimitiveGraphic {
    
    public static final String LABEL_RENDERER_PROP = "labelRenderer";

    private Renderer textRenderer;
    
    public LabeledPointGraphic() {
        this(null, new Point(), new ObjectStyler());
    }

    public LabeledPointGraphic(O source, Point2D primitive, ObjectStyler styler) {
        super(source, primitive, styler, null);
    }
    
    //
    //
    // PROPERTY PATTERNS
    //
    public Renderer getLabelRenderer() {
        return textRenderer;
    }

    public void setLabelRenderer(Renderer textRenderer) {
        if (this.textRenderer != textRenderer) {
            Object old = this.textRenderer;
            this.textRenderer = textRenderer;
            pcs.firePropertyChange(LABEL_RENDERER_PROP, old, textRenderer);
        }
    }
    
    //
    
    /**
     * Return label, if its visible.
     * @return label, or null if there is none visible
     */
    private String visibleLabel() {
        if (styler.getLabelDelegate() == null || getLabelRenderer() == null
                || (styler.getLabelFilter() != null && !styler.getLabelFilter().apply(source))) {
            return null;
        }
        String label = styler.label(source);
        return Strings.isNullOrEmpty(label) ? null : label;
    }
    
    @Override
    public void renderTo(G canvas) {
        super.renderTo(canvas);

        String label = visibleLabel();
        if (label != null) {
            AttributeSet style = styler.labelStyle(source);
            if (style != null) {
                AnchoredText alabel = new AnchoredText(primitive, label);
                getLabelRenderer().render(alabel, style, canvas);
            }
        }
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy