001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.print.impl.ui;
042:
043: import java.awt.Color;
044: import java.awt.Dimension;
045: import java.awt.Font;
046: import java.awt.Graphics;
047: import java.awt.Graphics2D;
048:
049: import java.text.SimpleDateFormat;
050: import java.util.Date;
051: import javax.swing.JComponent;
052:
053: import org.netbeans.modules.print.spi.PrintPage;
054: import org.netbeans.modules.print.impl.util.Macro;
055: import org.netbeans.modules.print.impl.util.Option;
056: import static org.netbeans.modules.print.impl.util.UI.*;
057:
058: /**
059: * @author Vladimir Yaroslavskiy
060: * @version 2005.12.14
061: */
062: final class Paper extends JComponent {
063:
064: Paper(PrintPage page, String name, Date modified) {
065: myPage = page;
066: myName = name;
067: myLastModifiedDate = modified;
068:
069: myPaperWidth = Option.getDefault().getPaperWidth();
070: myPaperHeight = Option.getDefault().getPaperHeight();
071: myPageX = Option.getDefault().getPageX();
072: myPageY = Option.getDefault().getPageY();
073: myPageWidth = Option.getDefault().getPageWidth();
074: myPageHeight = Option.getDefault().getPageHeight();
075:
076: myHasBorder = Option.getDefault().hasBorder();
077: myBorderColor = Option.getDefault().getBorderColor();
078:
079: myIsPainting = true;
080: }
081:
082: void setCoordinate(int number, int row, int column, double scale) {
083: myNumber = number;
084: myRow = row;
085: myColumn = column;
086: myRowNumber = String.valueOf(row + 1);
087: myColumnNumber = String.valueOf(column + 1);
088: setScale(scale);
089: }
090:
091: void setCount(int count) {
092: myCount = String.valueOf(count);
093:
094: myHeaderY = Option.getDefault().getHeaderY();
095: myHasHeader = Option.getDefault().hasHeader();
096: myHeaderLeft = expandTitle(Option.getDefault().getHeaderLeft());
097: myHeaderCenter = expandTitle(Option.getDefault()
098: .getHeaderCenter());
099: myHeaderRight = expandTitle(Option.getDefault()
100: .getHeaderRight());
101: myHeaderColor = Option.getDefault().getHeaderColor();
102: myHeaderFont = Option.getDefault().getHeaderFont();
103:
104: myFooterY = Option.getDefault().getFooterY();
105: myHasFooter = Option.getDefault().hasFooter();
106: myFooterLeft = expandTitle(Option.getDefault().getFooterLeft());
107: myFooterCenter = expandTitle(Option.getDefault()
108: .getFooterCenter());
109: myFooterRight = expandTitle(Option.getDefault()
110: .getFooterRight());
111: myFooterColor = Option.getDefault().getFooterColor();
112: myFooterFont = Option.getDefault().getFooterFont();
113: }
114:
115: int getRow() {
116: return myRow;
117: }
118:
119: int getColumn() {
120: return myColumn;
121: }
122:
123: void setScale(double scale) {
124: myScale = scale;
125:
126: if (myIsPainting) {
127: setPreferredSize(new Dimension((int) Math
128: .floor((myPaperWidth + SHADOW_WIDTH) * myScale),
129: (int) Math.floor((myPaperHeight + SHADOW_WIDTH)
130: * myScale)));
131: } else {
132: setPreferredSize(new Dimension(myPaperWidth, myPaperHeight));
133: }
134: }
135:
136: int getPaperWidth() {
137: return myPaperWidth + SHADOW_WIDTH;
138: }
139:
140: int getPaperHeight() {
141: return myPaperHeight + SHADOW_WIDTH;
142: }
143:
144: @Override
145: public void print(Graphics g) {
146: myIsPainting = false;
147: setScale(1.0);
148: super .print(g);
149: myIsPainting = true;
150: }
151:
152: @Override
153: public void paint(Graphics graphics) {
154: Graphics2D g = Option.getDefault().getGraphics(graphics);
155:
156: // scaling
157: if (myIsPainting) {
158: g.scale(myScale, myScale);
159: }
160:
161: // background
162: g.setColor(Color.white);
163: g.fillRect(myPageX, myPageY, myPageWidth, myPageHeight);
164:
165: // page
166: g.translate(myPageX, myPageY);
167: myPage.print(g);
168: g.translate(-myPageX, -myPageY);
169:
170: // horizontal margin
171: g.setColor(Color.white);
172:
173: g.fillRect(0, 0, myPaperWidth, myPageY);
174:
175: g.fillRect(0, myPageY + myPageHeight, myPaperWidth,
176: myPaperHeight);
177:
178: // header
179: if (myHasHeader) {
180: drawTitle(g, myHeaderLeft, myHeaderCenter, myHeaderRight,
181: myHeaderY, myHeaderColor, myHeaderFont);
182: }
183:
184: // footer
185: if (myHasFooter) {
186: drawTitle(g, myFooterLeft, myFooterCenter, myFooterRight,
187: myFooterY, myFooterColor, myFooterFont);
188: }
189:
190: // vertical margin
191: g.setColor(Color.white);
192:
193: g.fillRect(0, 0, myPageX, myPaperHeight);
194:
195: g.fillRect(myPageX + myPageWidth, 0, myPaperWidth,
196: myPaperHeight);
197:
198: // shadow
199: if (myIsPainting) {
200: g.setColor(Color.gray.darker());
201: g.fillRect(myPaperWidth, SHADOW_WIDTH, SHADOW_WIDTH + 1,
202: myPaperHeight);
203: g.fillRect(SHADOW_WIDTH, myPaperHeight, myPaperWidth,
204: SHADOW_WIDTH + 1);
205: g.setColor(Color.lightGray);
206: g.fillRect(myPaperWidth, 0, SHADOW_WIDTH + 1,
207: SHADOW_WIDTH + 1);
208: g.fillRect(0, myPaperHeight, SHADOW_WIDTH + 1,
209: SHADOW_WIDTH + 1);
210: }
211:
212: // box
213: if (myIsPainting) {
214: g.setColor(Color.black);
215: g.drawRect(0, 0, myPaperWidth, myPaperHeight);
216: }
217:
218: // border
219: if (myHasBorder) {
220: g.setColor(myBorderColor);
221: g.drawRect(myPageX, myPageY, myPageWidth, myPageHeight);
222: }
223:
224: // number
225: if (myIsPainting) {
226: g.setColor(NUMBER_FONT_COLOR);
227: g.setFont(NUMBER_FONT_NAME);
228: g
229: .drawString(Integer.toString(myNumber), NUMBER_X,
230: NUMBER_Y);
231: }
232: }
233:
234: private void drawTitle(Graphics2D g, String left, String center,
235: String right, int y, Color color, Font f) {
236: g.setColor(color);
237: drawTitle(g, left, myPageX, y, f);
238: drawTitle(g, center, myPageX
239: + (myPageWidth - getWidth(center, f)) / 2, y, f);
240: drawTitle(g, right, myPageX + myPageWidth - getWidth(right, f),
241: y, f);
242: }
243:
244: private void drawTitle(Graphics2D g, String text, int x, int y,
245: Font font) {
246: g.setFont(font);
247: g.drawString(text, x, y);
248: }
249:
250: private String expandTitle(String t) {
251: Date printed = new Date(System.currentTimeMillis());
252:
253: t = replace(t, Macro.NAME.getName(), myName);
254: t = replace(t, Macro.ROW.getName(), myRowNumber);
255: t = replace(t, Macro.COLUMN.getName(), myColumnNumber);
256: t = replace(t, Macro.USER.getName(), USER_NAME);
257: t = replace(t, Macro.COUNT.getName(), myCount);
258: t = replace(t, Macro.MODIFIED_DATE.getName(),
259: getDate(myLastModifiedDate));
260: t = replace(t, Macro.MODIFIED_TIME.getName(),
261: getTime(myLastModifiedDate));
262: t = replace(t, Macro.PRINTED_DATE.getName(), getDate(printed));
263: t = replace(t, Macro.PRINTED_TIME.getName(), getTime(printed));
264:
265: return t;
266: }
267:
268: private int getWidth(String text, Font font) {
269: return (int) Math.ceil(font.getStringBounds(text,
270: Option.FONT_RENDER_CONTEXT).getWidth());
271: }
272:
273: private String getDate(Date timestamp) {
274: return getTimestamp(timestamp, "yyyy.MM.dd"); // NOI18N
275: }
276:
277: private String getTime(Date timestamp) {
278: return getTimestamp(timestamp, "HH:mm:ss"); // NOI18N
279: }
280:
281: private String getTimestamp(Date timestamp, String format) {
282: return new SimpleDateFormat(format).format(timestamp);
283: }
284:
285: private int myNumber;
286: private double myScale;
287: private PrintPage myPage;
288: private boolean myIsPainting;
289:
290: private int myPaperWidth;
291: private int myPaperHeight;
292: private int myPageX;
293: private int myPageY;
294: private int myPageWidth;
295: private int myPageHeight;
296:
297: private int myHeaderY;
298: private boolean myHasHeader;
299: private String myHeaderLeft;
300: private String myHeaderCenter;
301: private String myHeaderRight;
302: private Color myHeaderColor;
303: private Font myHeaderFont;
304:
305: private int myFooterY;
306: private boolean myHasFooter;
307: private String myFooterLeft;
308: private String myFooterCenter;
309: private String myFooterRight;
310: private Color myFooterColor;
311: private Font myFooterFont;
312:
313: private boolean myHasBorder;
314: private Color myBorderColor;
315: private String myName;
316: private String myCount;
317: private String myRowNumber;
318: private String myColumnNumber;
319: private Date myLastModifiedDate;
320:
321: private int myRow;
322: private int myColumn;
323:
324: private static final int NUMBER_FONT_SIZE = 35;
325: private static final int SHADOW_WIDTH = 10; // .pt
326: private static final int NUMBER_X = (int) Math
327: .round(NUMBER_FONT_SIZE * 1.0);
328: private static final int NUMBER_Y = (int) Math
329: .round(NUMBER_FONT_SIZE * 1.5);
330: private static final Color NUMBER_FONT_COLOR = new Color(125, 125,
331: 255);
332: private static final String USER_NAME = System
333: .getProperty("user.name"); // NOI18N
334: private static final Font NUMBER_FONT_NAME = new Font("Serif",
335: Font.BOLD, NUMBER_FONT_SIZE); // NOI18N
336: }
|