001/* ===========================================================
002 * Orson Charts : a 3D chart library for the Java(tm) platform
003 * ===========================================================
004 * 
005 * (C)opyright 2013-2022, by David Gilbert.  All rights reserved.
006 * 
007 * https://github.com/jfree/orson-charts
008 * 
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as published by
011 * the Free Software Foundation, either version 3 of the License, or
012 * (at your option) any later version.
013 *
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 *
019 * You should have received a copy of the GNU General Public License
020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
021 * 
022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
023 * Other names may be trademarks of their respective owners.]
024 * 
025 * If you do not wish to be bound by the terms of the GPL, an alternative
026 * commercial license can be purchased.  For details, please see visit the
027 * Orson Charts home page:
028 * 
029 * http://www.object-refinery.com/orsoncharts/index.html
030 * 
031 */
032
033package org.jfree.chart3d.table;
034
035import java.awt.Color;
036import java.awt.Graphics2D;
037import java.awt.Insets;
038import java.awt.geom.Dimension2D;
039import java.awt.geom.Rectangle2D;
040import java.io.Serializable;
041import java.util.HashMap;
042import java.util.Map;
043import org.jfree.chart3d.graphics2d.RefPt2D;
044
045import org.jfree.chart3d.internal.Args;
046import org.jfree.chart3d.internal.ObjectUtils;
047
048/**
049 * A base class that can be used to implement a {@link TableElement}.
050 * <br><br>
051 * NOTE: This class is serializable, but the serialization format is subject 
052 * to change in future releases and should not be relied upon for persisting 
053 * instances of this class.
054 */
055@SuppressWarnings("serial")
056public abstract class AbstractTableElement implements Serializable {
057    
058    /** The default background color. */
059    private static final Color DEFAULT_BACKGROUND_COLOR 
060            = new Color(255, 255, 255, 127);
061    
062    /** The reference point used to align the element when rendering. */
063    private RefPt2D refPt;
064    
065    /** The insets. */
066    private Insets insets;
067    
068    /** The background paint (this can be {@code null}). */
069    private RectanglePainter background;
070    
071    /** A tag that can be used to identify the class of element. */
072    private String tag;
073    
074    /** Stores properties for the element. */
075    private final HashMap<String, Object> properties;
076    
077    /**
078     * Creates a new instance.
079     */
080    public AbstractTableElement() {
081        this.refPt = RefPt2D.CENTER;
082        this.insets = new Insets(2, 2, 2, 2);
083        this.background = new StandardRectanglePainter(
084                DEFAULT_BACKGROUND_COLOR);
085        this.tag = "";
086        this.properties = new HashMap<>();
087    }
088
089    /**
090     * Returns the anchor point used to align the element with the bounding
091     * rectangle within which it is drawn.  The default value is 
092     * {@link RefPt2D#CENTER}.
093     * 
094     * @return The anchor point (never {@code null}). 
095     * 
096     * @since 1.1
097     */
098    public RefPt2D getRefPoint() {
099        return this.refPt;
100    }
101    
102    /**
103     * Sets the reference point.
104     * 
105     * @param refPt  the reference point ({@code null} not permitted).
106     * 
107     * @since 1.1
108     */
109    public void setRefPoint(RefPt2D refPt) {
110        Args.nullNotPermitted(refPt, "refPt");
111        this.refPt = refPt;
112    }
113
114    /**
115     * Returns the insets.  The default value is {@code Insets(2, 2, 2, 2)}.
116     * 
117     * @return The insets (never {@code null}).
118     */
119    public Insets getInsets() {
120        return this.insets;
121    }
122    
123    /**
124     * Sets the insets.
125     * 
126     * @param insets  the insets ({@code null} not permitted). 
127     */
128    public void setInsets(Insets insets) {
129        Args.nullNotPermitted(insets, "insets");
130        this.insets = insets;
131    }
132
133    /**
134     * Returns the background painter for the element.
135     * 
136     * @return The background painter (possibly {@code null}). 
137     */
138    public RectanglePainter getBackground() {
139        return this.background;
140    }
141    
142    /**
143     * Sets the background for the element.
144     * 
145     * @param background  the new background ({@code null} permitted).
146     */
147    public void setBackground(RectanglePainter background) {
148        this.background = background;
149    }
150    
151    /**
152     * Sets the background painter to fill the element with the specified 
153     * color.  If the color is {@code null}, the background painter will
154     * be set to {@code null}.
155     * 
156     * @param color  the color ({@code null} permitted). 
157     * 
158     * @since 1.2
159     */
160    public void setBackgroundColor(Color color) {
161        if (color != null) {
162            this.background = new StandardRectanglePainter(color);
163        } else {
164            this.background = null;
165        }
166    }
167    
168    /**
169     * Returns the tag for this element.  The default value is an empty string.
170     * 
171     * @return The tag (never {@code null}). 
172     * 
173     * @since 1.2
174     */
175    public String getTag() {
176        return this.tag;
177    }
178    
179    /**
180     * Sets the tag.
181     * 
182     * @param tag  the tag ({@code null} not permitted).
183     * 
184     * @since 1.2
185     */
186    public void setTag(String tag) {
187        Args.nullNotPermitted(tag, "tag");
188        this.tag = tag;
189    }
190    
191    /**
192     * Returns the value of the property with the specified key, or 
193     * {@code null}.
194     * 
195     * @param key  the key ({@code null} not permitted).
196     * 
197     * @return The property value or {@code null}. 
198     * 
199     * @since 1.3
200     */
201    public Object getProperty(String key) {
202        return this.properties.get(key);
203    }
204    
205    /**
206     * Sets the value of the property with the specified key.
207     * 
208     * @param key  the key ({@code null} not permitted).
209     * @param value  the value ({@code null} permitted).
210     * 
211     * @since 1.3
212     */
213    public void setProperty(String key, Object value) {
214        Args.nullNotPermitted(key, "key");
215        this.properties.put(key, value);
216    }
217    
218    /**
219     * Returns the preferred size of the element (including insets).
220     * 
221     * @param g2  the graphics target.
222     * @param bounds  the bounds.
223     * 
224     * @return The preferred size. 
225     */
226    public Dimension2D preferredSize(Graphics2D g2, Rectangle2D bounds) {
227        return preferredSize(g2, bounds, null);
228    }
229
230    /**
231     * Returns the preferred size of the element (including insets).
232     * 
233     * @param g2  the graphics target.
234     * @param bounds  the bounds.
235     * @param constraints  the constraints (ignored for now).
236     * 
237     * @return The preferred size. 
238     */
239    public abstract Dimension2D preferredSize(Graphics2D g2, Rectangle2D bounds, 
240            Map<String, Object> constraints);
241    
242    /**
243     * Tests this instance for equality with an arbitrary object.
244     * 
245     * @param obj  the object ({@code null} permitted).
246     * 
247     * @return A boolean. 
248     */
249    @Override
250    public boolean equals(Object obj) {
251        if (obj == this) {
252            return true;
253        }
254        if (!(obj instanceof AbstractTableElement)) {
255            return false;
256        }
257        AbstractTableElement that = (AbstractTableElement) obj;
258        if (!this.insets.equals(that.insets)) {
259            return false;
260        }
261        if (!ObjectUtils.equals(this.background, that.background)) {
262            return false;
263        }
264        return true;
265    }
266   
267}