KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > print > View


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.print;
15
16 import java.awt.*;
17 import java.awt.print.*;
18 import java.util.*;
19 import javax.swing.*;
20 import javax.print.attribute.*;
21
22 import org.compiere.swing.*;
23 import org.compiere.util.*;
24 import org.compiere.model.*;
25 import org.compiere.print.layout.*;
26
27 /**
28  * View Panel
29  *
30  * @author Jorg Janke
31  * @version $Id: View.java,v 1.15 2002/08/30 15:44:46 jjanke Exp $
32  */

33 public class View extends CPanel
34 {
35     /**
36      * Print Preview
37      * @param layout Layout
38      */

39     public View (LayoutEngine layout)
40     {
41         m_layout = layout;
42     } // View
43

44     /** Layout to be Printed */
45     private LayoutEngine m_layout;
46
47
48     /** Zoom Level */
49     private int m_zoomLevel = 0;
50     /** Zoom Options */
51     public static final String JavaDoc[] ZOOM_OPTIONS = new String JavaDoc[]
52         {"100%", "75%", "50%"};
53     /** Margin around paper */
54     public static int MARGIN = 5;
55     /** Margin Background Color */
56     private static Color COLOR_BACKGROUND = Color.lightGray;
57
58
59     /*************************************************************************/
60
61     /**
62      * Minimum Size
63      * @return Max Page Size
64      */

65     public Dimension getMinimumSize()
66     {
67         return getMaximumSize();
68     } // getMinimumSize
69

70     /**
71      * Minimum Size
72      * @return Max Page Size
73      */

74     public Dimension getMaximumSize()
75     {
76         return new Dimension (getPaperWidth()+(2*MARGIN),
77             (getPaperHeight()+MARGIN)*getPageCount()+MARGIN);
78     } // getMaximumSize
79

80     /**
81      * Preferred Size
82      * @return Max Page Size
83      */

84     public Dimension getPreferredSize()
85     {
86         return getMaximumSize();
87     } // getPreferredSize
88

89
90     /**
91      * Paint Component
92      * @param g Graphics
93      */

94     public void paintComponent (Graphics g)
95     {
96     // Log.trace(Log.l6_Database, "View.paintComponent", g.getClip());
97
Graphics2D g2D = (Graphics2D)g;
98         Rectangle bounds = g2D.getClipBounds();
99         //
100
g2D.setColor(COLOR_BACKGROUND);
101         g2D.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
102
103         // for all pages
104
for (int page = 0; page < m_layout.getPages().size(); page++)
105         {
106             Rectangle pageRectangle = getRectangleOfPage(page+1);
107             if (bounds.intersects(pageRectangle))
108             {
109                 Page p = (Page)m_layout.getPages().get(page);
110                 p.paint (g2D, pageRectangle, true, false); // sets context
111
m_layout.getHeaderFooter().paint(g2D, pageRectangle, true);
112             } // paint page
113
} // for all pages
114
} // paintComponent
115

116
117     /*************************************************************************/
118
119     /**
120      * Set Zoom Level
121      * @param level zoom level
122      */

123     public void setZoomLevel(int level)
124     {
125         m_zoomLevel = level;
126     } // setZoomLevel
127

128     /**
129      * Set Zoom Level
130      * @param levelString zoom level string
131      */

132     public void setZoomLevel(String JavaDoc levelString)
133     {
134         for (int i = 0; i < ZOOM_OPTIONS.length; i++)
135         {
136             if (ZOOM_OPTIONS[i].equals(levelString))
137             {
138                 m_zoomLevel = i;
139                 break;
140             }
141         }
142     } // setZoomLevel
143

144     /**
145      * Get Zoom Level
146      * @return zoom level
147      */

148     public int getZoomLevel()
149     {
150         return m_zoomLevel;
151     } // getZoomLevel
152

153     /**
154      * Get Rectange of Page
155      * @param pageNo page no
156      * @return rectangle
157      */

158     public Rectangle getRectangleOfPage(int pageNo)
159     {
160         int y = MARGIN + ((pageNo-1) * (getPaperHeight() + MARGIN));
161         return new Rectangle (MARGIN, y, getPaperWidth(), getPaperHeight());
162     } // getRectangleOfPage
163

164
165     /**
166      * Get Page at Point
167      * @param p Point
168      * @return page as float to determine also position on page
169      */

170     public float getPageNoAt (Point p)
171     {
172         float y = p.y;
173         float pageHeight = getPaperHeight() + MARGIN;
174         return 1f + (y/pageHeight);
175     } // getPageAt
176

177     /**
178      * Get Page Count
179      * @return page count
180      */

181     public int getPageCount()
182     {
183         return m_layout.getPages().size();
184     } // getPageCount
185

186     /**
187      * Get Page Info for Multi-Page tables
188      * @param pageNo page
189      * @return info e.g. (1,1)
190      */

191     public String JavaDoc getPageInfo(int pageNo)
192     {
193         return m_layout.getPageInfo(pageNo);
194     } // getPageInfo
195

196     /**
197      * Get Max Page Info for Multi-Page tables
198      * @return info e.g. (3,2)
199      */

200     public String JavaDoc getPageInfoMax()
201     {
202         return m_layout.getPageInfoMax();
203     } // getPageInfo
204

205     /**
206      * Get Paper
207      * @return paper
208      */

209     public CPaper getPaper()
210     {
211         return m_layout.getPaper();
212     } // getPaper
213

214     /**
215      * Get Paper Height
216      * @return paper height
217      */

218     public int getPaperHeight()
219     {
220         return (int)m_layout.getPaper().getHeight(true);
221     } // getPaperHeight
222

223     /**
224      * Get Paper Height
225      * @return paper height
226      */

227     public int getPaperWidth()
228     {
229         return (int)m_layout.getPaper().getWidth(true);
230     } // getPaperHeight
231

232     /**
233      * Get Drill Down
234      * @param absolutePoint point
235      * @return Drill Down
236      */

237     public MQuery getDrillDown (Point absolutePoint)
238     {
239         int pageNo = (int)getPageNoAt(absolutePoint);
240         Rectangle pageRectangle = getRectangleOfPage(pageNo);
241         Point relativePoint = new Point (absolutePoint.x-pageRectangle.x,
242             absolutePoint.y-pageRectangle.y);
243         Page page = (Page)m_layout.getPages().get(pageNo-1);
244         //
245
Log.trace(Log.l4_Data, "View.getDrillDown", "RelativePoint=" + relativePoint + ", Page=" + page);
246     // Log.trace(Log.l4_Data, "View.getDrillDown", "AbsolutePoint=" + absolutePoint + ", PageNo=" + pageNo + ", pageRectangle=" + pageRectangle);
247
MQuery retValue = page.getDrillDown (relativePoint);
248         if (retValue == null)
249             retValue = m_layout.getHeaderFooter().getDrillDown (relativePoint);
250         return retValue;
251     } // getDrillDown
252

253     /**
254      * Get Drill Across
255      * @param absolutePoint point
256      * @return Drill Across
257      */

258     public MQuery getDrillAcross (Point absolutePoint)
259     {
260         int pageNo = (int)getPageNoAt(absolutePoint);
261         Rectangle pageRectangle = getRectangleOfPage(pageNo);
262         Point relativePoint = new Point (absolutePoint.x-pageRectangle.x,
263             absolutePoint.y-pageRectangle.y);
264         Page page = (Page)m_layout.getPages().get(pageNo-1);
265         //
266
Log.trace(Log.l4_Data, "View.getDrillAcross", "RelativePoint=" + relativePoint + ", Page=" + page);
267     // Log.trace(Log.l4_Data, "View.getDrillAcross", "AbsolutePoint=" + absolutePoint + ", PageNo=" + pageNo + ", pageRectangle=" + pageRectangle);
268
return page.getDrillAcross (relativePoint);
269     } // getDrillAcross
270

271 } // View
272
Popular Tags