001/*
002 Copyright (c) 2003-2014 The Regents of the University of California
003 All rights reserved.
004 Permission is hereby granted, without written agreement and without
005 license or royalty fees, to use, copy, modify, and distribute this
006 software and its documentation for any purpose, provided that the above
007 copyright notice and the following two paragraphs appear in all copies
008 of this software.
009
010 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
011 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
012 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
013 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
014 SUCH DAMAGE.
015
016 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
017 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
018 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
019 PROVIDED HEREUNDER IS ON AN  BASIS, AND THE UNIVERSITY OF
020 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
021 ENHANCEMENTS, OR MODIFICATIONS.
022
023 PT_COPYRIGHT_VERSION_3
024 COPYRIGHTENDKEY
025 *
026 */
027package ptolemy.vergil.unit;
028
029import java.awt.BasicStroke;
030import java.awt.Color;
031import java.awt.Graphics2D;
032import java.awt.Paint;
033import java.awt.Shape;
034import java.awt.Stroke;
035import java.awt.geom.Rectangle2D;
036
037import diva.canvas.DamageRegion;
038import diva.canvas.Figure;
039import diva.canvas.FigureDecorator;
040
041///////////////////////////////////////////////////////////////////
042//// BasicEdgeHighlighter
043
044/**
045 A decorator figure that displays a highlight behind an edge. This capability
046 was planned for BasicHighlighter but there doesn't seem to be an easy way to
047 determine that an edge is being painted. This class is a stripped down
048 version of BasicHighlighter with the paint method modified so that it
049 assumes that an edge is being highlighted.
050 @version        $Id$
051 @author         Rowland R Johnson
052 @since Ptolemy II 8.0
053 @Pt.ProposedRating Red (rowland)
054 @Pt.AcceptedRating Red (rowland)
055 */
056public class BasicEdgeHighlighter extends FigureDecorator {
057    /** Create a new highlighter with a default paint, "halo", and stroke.
058     */
059    public BasicEdgeHighlighter() {
060        this._paint = new Color(255, 255, 0, 200);
061        this._halo = 4.0f;
062        _stroke = new BasicStroke(2 * _halo);
063    }
064
065    ///////////////////////////////////////////////////////////////////
066    ////                         public methods                    ////
067
068    /** Return false. This method always returns false, as it
069     * is meaningless (and dangerous!) to be able to hit a highlight.
070     */
071    @Override
072    public boolean hit(Rectangle2D region) {
073        return false;
074    }
075
076    /** Create a new instance of this highlighter.
077     */
078    @Override
079    public FigureDecorator newInstance(Figure f) {
080        return new BasicEdgeHighlighter();
081    }
082
083    /** Paint the edge. This method first paints the highlight over
084     * the contained edge by drawing a line width determined by halo. It
085     * then paints the contained edge.
086     */
087    @Override
088    public void paint(Graphics2D g) {
089        g.setPaint(_paint);
090
091        Shape shape = getChild().getShape();
092
093        if (_stroke != null) {
094            g.setStroke(_stroke);
095        }
096
097        g.draw(shape);
098
099        // Draw the child
100        getChild().paint(g);
101    }
102
103    /** Receive repaint notification. This method generates another
104     * repaint() call, with a larger region, in order to ensure
105     * that the highlight is repainted.
106     */
107    @Override
108    public void repaint(DamageRegion d) {
109        repaint();
110    }
111
112    /** Request a repaint of the figure and highlight.
113     * This method reads the bounding box of the highlighted
114     * figure, and requests a repaint of that box stretched
115     * in each direction by the halo.
116     */
117    @Override
118    public void repaint() {
119        Rectangle2D bounds = getChild().getBounds();
120        double x = bounds.getX() - _halo;
121        double y = bounds.getY() - _halo;
122        double w = bounds.getWidth() + 2 * _halo;
123        double h = bounds.getHeight() + 2 * _halo;
124
125        getParent().repaint(DamageRegion
126                .createDamageRegion(getTransformContext(), x, y, w, h));
127    }
128
129    ///////////////////////////////////////////////////////////////////
130    ////                         private variables                 ////
131
132    /* The "halo" size
133     */
134    private float _halo;
135
136    /* The highlight paint, or null if none.
137     */
138    private Paint _paint;
139
140    /* The highlight stroke, or null if none.
141     */
142    private Stroke _stroke;
143}