2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.netbeans.modules.print.impl.ui;
43
import java.awt.Color;
44
import java.awt.Dimension;
46
import java.awt.Graphics;
47
import java.awt.Graphics2D;
49
import java.text.SimpleDateFormat;
50
import java.util.Date;
51
import javax.swing.JComponent;
53
import org.netbeans.modules.print.spi.PrintPage;
54
import org.netbeans.modules.print.impl.util.Macro;
55
import org.netbeans.modules.print.impl.util.Util;
56
import static org.netbeans.modules.print.api.PrintUtil.*;
59
* @author Vladimir Yaroslavskiy
62
final class Paper extends JComponent {
73
myCount = String.valueOf(count);
76
myRowNumber = String.valueOf(row + 1);
77
myColumnNumber = String.valueOf(column + 1);
86
myLastModifiedDate = modified;
88
myPaperWidth = Util.getOption().getPaperWidth();
89
myPaperHeight = Util.getOption().getPaperHeight();
90
myPageX = Util.getOption().getPageX();
91
myPageY = Util.getOption().getPageY();
92
myPageWidth = Util.getOption().getPageWidth();
93
myPageHeight = Util.getOption().getPageHeight();
95
myHeaderY = Util.getOption().getHeaderY();
96
myHasHeader = Util.getOption().hasHeader();
97
myHeaderLeft = expandTitle(Util.getOption().getHeaderLeft());
98
myHeaderCenter = expandTitle(Util.getOption().getHeaderCenter());
99
myHeaderRight = expandTitle(Util.getOption().getHeaderRight());
100
myHeaderColor = Util.getOption().getHeaderColor();
101
myHeaderFont = Util.getOption().getHeaderFont();
103
myFooterY = Util.getOption().getFooterY();
104
myHasFooter = Util.getOption().hasFooter();
105
myFooterLeft = expandTitle(Util.getOption().getFooterLeft());
106
myFooterCenter = expandTitle(Util.getOption().getFooterCenter());
107
myFooterRight = expandTitle(Util.getOption().getFooterRight());
108
myFooterColor = Util.getOption().getFooterColor();
109
myFooterFont = Util.getOption().getFooterFont();
111
myHasBorder = Util.getOption().hasBorder();
112
myBorderColor = Util.getOption().getBorderColor();
126
void setScale(double scale) {
130
setPreferredSize(new Dimension (
131
(int) Math.floor((myPaperWidth + SHADOW_WIDTH) * myScale),
132
(int) Math.floor((myPaperHeight + SHADOW_WIDTH) * myScale)
136
setPreferredSize(new Dimension(myPaperWidth, myPaperHeight));
140
int getPaperWidth() {
141
return myPaperWidth + SHADOW_WIDTH;
144
int getPaperHeight() {
145
return myPaperHeight + SHADOW_WIDTH;
149
public void print(Graphics g)
151
myIsPainting = false;
158
public void paint(Graphics graphics)
160
Graphics2D g = Util.getGraphics(graphics);
164
g.scale(myScale, myScale);
168
g.setColor(Color.white);
169
g.fillRect(myPageX, myPageY, myPageWidth, myPageHeight);
172
g.translate(myPageX, myPageY);
174
g.translate(-myPageX, -myPageY);
177
g.setColor(Color.white);
181
myPaperWidth, myPageY
185
0, myPageY + myPageHeight,
186
myPaperWidth, myPaperHeight
214
g.setColor(Color.white);
218
myPageX, myPaperHeight
222
myPageX + myPageWidth, 0,
223
myPaperWidth, myPaperHeight
228
g.setColor(Color.gray.darker());
241
g.setColor(Color.lightGray);
242
g.fillRect(myPaperWidth, 0, SHADOW_WIDTH + 1, SHADOW_WIDTH + 1);
243
g.fillRect(0, myPaperHeight, SHADOW_WIDTH + 1, SHADOW_WIDTH + 1);
248
g.setColor(Color.black);
249
g.drawRect(0, 0, myPaperWidth, myPaperHeight);
254
g.setColor(myBorderColor);
255
g.drawRect(myPageX, myPageY, myPageWidth, myPageHeight);
260
g.setColor(NUMBER_FONT_COLOR);
261
g.setFont(NUMBER_FONT_NAME);
262
g.drawString(Integer.toString(myNumber), NUMBER_X, NUMBER_Y);
266
private void drawTitle(
276
drawTitle(g, left, myPageX, y, f);
277
drawTitle(g, center,myPageX + (myPageWidth - getWidth(center, f))/2, y, f);
278
drawTitle(g, right, myPageX + myPageWidth - getWidth(right, f), y, f);
281
private void drawTitle(
289
g.drawString(text, x, y);
292
private String expandTitle(String t) {
293
Date printed = new Date(System.currentTimeMillis());
295
t = Util.replace(t, Macro.NAME.getName(), myName);
296
t = Util.replace(t, Macro.ROW.getName(), myRowNumber);
297
t = Util.replace(t, Macro.COLUMN.getName(), myColumnNumber);
298
t = Util.replace(t, Macro.USER.getName(), USER_NAME);
299
t = Util.replace(t, Macro.COUNT.getName(), myCount);
300
t = Util.replace(t, Macro.MODIFIED_DATE.getName(),getDate(myLastModifiedDate));
301
t = Util.replace(t, Macro.MODIFIED_TIME.getName(),getTime(myLastModifiedDate));
302
t = Util.replace(t, Macro.PRINTED_DATE.getName(), getDate(printed));
303
t = Util.replace(t, Macro.PRINTED_TIME.getName(), getTime(printed));
308
private int getWidth(String text, Font font) {
309
return (int) Math.ceil(font.getStringBounds(
310
text, Util.FONT_RENDER_CONTEXT).getWidth());
313
private String getDate(Date timestamp) {
314
return getTimestamp(timestamp, "yyyy.MM.dd"); // NOI18N
317
private String getTime(Date timestamp) {
318
return getTimestamp(timestamp, "HH:mm:ss"); // NOI18N
321
private String getTimestamp(Date timestamp, String format) {
322
return new SimpleDateFormat(format).format(timestamp);
325
private int myNumber;
326
private double myScale;
327
private PrintPage myPage;
328
private boolean myIsPainting;
330
private int myPaperWidth;
331
private int myPaperHeight;
334
private int myPageWidth;
335
private int myPageHeight;
337
private int myHeaderY;
338
private boolean myHasHeader;
339
private String myHeaderLeft;
340
private String myHeaderCenter;
341
private String myHeaderRight;
342
private Color myHeaderColor;
343
private Font myHeaderFont;
345
private int myFooterY;
346
private boolean myHasFooter;
347
private String myFooterLeft;
348
private String myFooterCenter;
349
private String myFooterRight;
350
private Color myFooterColor;
351
private Font myFooterFont;
353
private boolean myHasBorder;
354
private Color myBorderColor;
355
private String myName;
356
private String myCount;
357
private String myRowNumber;
358
private String myColumnNumber;
359
private Date myLastModifiedDate;
362
private int myColumn;
364
private static final int NUMBER_FONT_SIZE = 35;
365
private static final int SHADOW_WIDTH = 10; // .pt
366
private static final int NUMBER_X = (int) Math.round(NUMBER_FONT_SIZE * 1.0);
367
private static final int NUMBER_Y = (int) Math.round(NUMBER_FONT_SIZE * 1.5);
368
private static final Color NUMBER_FONT_COLOR = new Color(125, 125, 255);
369
private static final String USER_NAME = System.getProperty("user.name"); // NOI18N
370
private static final Font NUMBER_FONT_NAME =
371
new Font("Serif", Font.BOLD, NUMBER_FONT_SIZE); // NOI18N