001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.forms.gui.form;
031:
032: import java.awt.Color;
033: import java.awt.Graphics;
034: import java.awt.Graphics2D;
035: import java.awt.Paint;
036: import java.awt.Rectangle;
037:
038: import javax.swing.JPanel;
039:
040: import com.jeta.forms.gui.effects.Painter;
041: import com.jeta.forms.store.properties.effects.PaintProperty;
042:
043: /**
044: * This class is a Swing component that paints fill effects for cells in the
045: * GridView. It iterates over all invalid cells (i.e. needing repaint) and
046: * paints those cells if they have an assigned painter object. Examples of fill
047: * effects are: solid, texture, linear gradient, and radial gradient. See:
048: * {@link com.jeta.forms.gui.effects.Painter}
049: *
050: * @author Jeff Tassin
051: */
052: public class CellPainter extends JPanel {
053: /**
054: * The view associated with this grid painter.
055: */
056: private GridView m_view;
057:
058: /**
059: * The parent form for the GridView
060: */
061: private FormComponent m_form;
062:
063: /**
064: * We keep a rectangle around so we don't have to instantiate with every
065: * paint.
066: */
067: private Rectangle m_gc_rect = new Rectangle();
068:
069: /**
070: * Creates a <code>CellPainter</code> associated with the given view.
071: *
072: * @param view
073: * the GridView associated with this cell painter.
074: */
075: public CellPainter(GridView view) {
076: m_view = view;
077: setOpaque(false);
078: }
079:
080: /**
081: * Override paintComponent so can render the fill effects for each cell that
082: * needs it.
083: */
084: public void paintComponent(Graphics g) {
085: Graphics2D g2 = (Graphics2D) g;
086: Color old_c = g2.getColor();
087: Paint old_paint = g2.getPaint();
088:
089: /**
090: * we need to increase the height of the clip rectangle by 2 pixels
091: * because the bottom line of the grid overlay is not painted for
092: * composite child views in some cases
093: */
094: Rectangle clip_rect = g.getClipBounds();
095: clip_rect.setBounds(clip_rect.x, clip_rect.y,
096: clip_rect.width + 2, clip_rect.height + 2);
097: g.setClip(clip_rect.x, clip_rect.y, clip_rect.width,
098: clip_rect.height);
099: int clip_x1 = (int) clip_rect.x;
100: int clip_x2 = (int) clip_rect.x + clip_rect.width;
101: int clip_y1 = (int) clip_rect.y;
102: int clip_y2 = (int) clip_rect.y + clip_rect.height;
103:
104: int min_row = -1;
105: int max_row = -1;
106:
107: int min_col = -1;
108: int max_col = -1;
109:
110: for (int row = 1; row <= m_view.getRowCount(); row++) {
111: int row_y1 = m_view.getRowOrgY(row);
112: int row_y2 = row_y1 + m_view.getRowHeight(row);
113: if (clip_y1 >= row_y1 && clip_y1 <= row_y2) {
114: if (min_row < 0)
115: min_row = row;
116: else
117: max_row = row;
118: } else if (clip_y2 >= row_y1 && clip_y2 <= row_y2) {
119: if (min_row < 0)
120: min_row = row;
121: else
122: max_row = row;
123:
124: } else if (row_y1 >= clip_y1 && row_y2 <= clip_y2) {
125: // here, the row is contained entirely in the clip
126: if (min_row < 0)
127: min_row = row;
128: else
129: max_row = row;
130: }
131: }
132:
133: for (int col = 1; col <= m_view.getColumnCount(); col++) {
134: int col_x1 = m_view.getColumnOrgX(col);
135: int col_x2 = col_x1 + m_view.getColumnWidth(col);
136: if (clip_x1 >= col_x1 && clip_x1 <= col_x2) {
137: if (min_col < 0)
138: min_col = col;
139: else
140: max_col = col;
141: } else if (clip_x2 >= col_x1 && clip_x2 <= col_x2) {
142: if (min_col < 0)
143: min_col = col;
144: else
145: max_col = col;
146:
147: } else if (col_x1 >= clip_x1 && col_x2 <= clip_x2) {
148: // here, the col is contained entirely in the clip
149: if (min_col < 0)
150: min_col = col;
151: else
152: max_col = col;
153: }
154: }
155:
156: if (min_row < 0 || min_col < 0)
157: return;
158:
159: if (max_row < 0)
160: max_row = min_row;
161: if (max_col < 0)
162: max_col = min_col;
163:
164: for (int row = min_row; row <= max_row; row++) {
165: for (int col = min_col; col <= max_col; col++) {
166: PaintProperty pp = m_view.getPaintProperty(col, row);
167: if (pp != null) {
168: Painter painter = pp.createPainter();
169: if (painter != null) {
170: GridComponent gc = m_view.getGridComponent(col,
171: row);
172: if (gc == null) {
173: m_gc_rect.setBounds(m_view
174: .getColumnOrgX(col), m_view
175: .getRowOrgY(row), m_view
176: .getColumnWidth(col), m_view
177: .getRowHeight(row));
178: } else {
179: m_gc_rect.setBounds(gc.getCellX(), gc
180: .getCellY(), gc.getCellWidth(), gc
181: .getCellHeight());
182: }
183:
184: if (m_gc_rect.intersects(clip_rect)) {
185: painter.paint(this, g, m_gc_rect);
186: }
187: }
188: }
189: }
190: }
191: g2.setColor(old_c);
192: g2.setPaint(old_paint);
193: }
194:
195: }
|