~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to print/src/org/netbeans/modules/print/impl/ui/Paper.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
package org.netbeans.modules.print.impl.ui;
 
42
 
 
43
import java.awt.Color;
 
44
import java.awt.Dimension;
 
45
import java.awt.Font;
 
46
import java.awt.Graphics;
 
47
import java.awt.Graphics2D;
 
48
 
 
49
import java.text.SimpleDateFormat;
 
50
import java.util.Date;
 
51
import javax.swing.JComponent;
 
52
 
 
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.*;
 
57
 
 
58
/**
 
59
 * @author Vladimir Yaroslavskiy
 
60
 * @version 2005.12.14
 
61
 */
 
62
final class Paper extends JComponent {
 
63
 
 
64
  Paper(
 
65
    PrintPage page,
 
66
    int number,
 
67
    int count,
 
68
    int row,
 
69
    int column)
 
70
  {
 
71
    myPage = page;
 
72
    myNumber = number;
 
73
    myCount = String.valueOf(count);
 
74
    myRow = row;
 
75
    myColumn = column;
 
76
    myRowNumber = String.valueOf(row + 1);
 
77
    myColumnNumber = String.valueOf(column + 1);
 
78
  }
 
79
 
 
80
  void setInfo(
 
81
    String name,
 
82
    Date modified,
 
83
    double scale)
 
84
  {
 
85
    myName = name;
 
86
    myLastModifiedDate = modified;
 
87
 
 
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();
 
94
 
 
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();
 
102
 
 
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();
 
110
 
 
111
    myHasBorder = Util.getOption().hasBorder();
 
112
    myBorderColor = Util.getOption().getBorderColor();
 
113
    myIsPainting = true;
 
114
 
 
115
    setScale(scale);
 
116
  }
 
117
 
 
118
  int getRow() {
 
119
    return myRow;
 
120
  }
 
121
 
 
122
  int getColumn() {
 
123
    return myColumn;
 
124
  }
 
125
 
 
126
  void setScale(double scale) {
 
127
    myScale = scale;
 
128
 
 
129
    if (myIsPainting) {
 
130
      setPreferredSize(new Dimension (
 
131
        (int) Math.floor((myPaperWidth + SHADOW_WIDTH) * myScale),
 
132
        (int) Math.floor((myPaperHeight + SHADOW_WIDTH) * myScale)
 
133
      ));
 
134
    }
 
135
    else {
 
136
      setPreferredSize(new Dimension(myPaperWidth, myPaperHeight));
 
137
    }
 
138
  }
 
139
 
 
140
  int getPaperWidth() {
 
141
    return myPaperWidth + SHADOW_WIDTH;
 
142
  }
 
143
 
 
144
  int getPaperHeight() {
 
145
    return myPaperHeight + SHADOW_WIDTH;
 
146
  }
 
147
 
 
148
  @Override
 
149
  public void print(Graphics g)
 
150
  {
 
151
    myIsPainting = false;
 
152
    setScale(1.0);
 
153
    super.print(g);
 
154
    myIsPainting = true;
 
155
  }
 
156
 
 
157
  @Override
 
158
  public void paint(Graphics graphics)
 
159
  {
 
160
    Graphics2D g = Util.getGraphics(graphics);
 
161
 
 
162
    // scaling
 
163
    if (myIsPainting) {
 
164
      g.scale(myScale, myScale);
 
165
    }
 
166
 
 
167
    // background
 
168
    g.setColor(Color.white);
 
169
    g.fillRect(myPageX, myPageY, myPageWidth, myPageHeight);
 
170
 
 
171
    // page
 
172
    g.translate(myPageX, myPageY);
 
173
    myPage.print(g);
 
174
    g.translate(-myPageX, -myPageY);
 
175
 
 
176
    // horizontal margin
 
177
    g.setColor(Color.white);
 
178
 
 
179
    g.fillRect(
 
180
      0, 0,
 
181
      myPaperWidth, myPageY
 
182
    );
 
183
    
 
184
    g.fillRect(
 
185
      0, myPageY + myPageHeight,
 
186
      myPaperWidth, myPaperHeight
 
187
    );
 
188
 
 
189
    // header
 
190
    if (myHasHeader) {
 
191
      drawTitle(g,
 
192
        myHeaderLeft,
 
193
        myHeaderCenter,
 
194
        myHeaderRight,
 
195
        myHeaderY,
 
196
        myHeaderColor,
 
197
        myHeaderFont
 
198
      );
 
199
    }
 
200
 
 
201
    // footer
 
202
    if (myHasFooter) {
 
203
      drawTitle(g,
 
204
        myFooterLeft,
 
205
        myFooterCenter,
 
206
        myFooterRight,
 
207
        myFooterY,
 
208
        myFooterColor,
 
209
        myFooterFont
 
210
      );
 
211
    }
 
212
 
 
213
    // vertical margin
 
214
    g.setColor(Color.white);
 
215
 
 
216
    g.fillRect(
 
217
      0, 0,
 
218
      myPageX, myPaperHeight
 
219
    );
 
220
    
 
221
    g.fillRect(
 
222
      myPageX + myPageWidth, 0,
 
223
      myPaperWidth, myPaperHeight
 
224
    );
 
225
    
 
226
    // shadow
 
227
    if (myIsPainting) {
 
228
      g.setColor(Color.gray.darker());
 
229
      g.fillRect(
 
230
        myPaperWidth,
 
231
        SHADOW_WIDTH,
 
232
        SHADOW_WIDTH + 1,
 
233
        myPaperHeight
 
234
      );
 
235
      g.fillRect(
 
236
        SHADOW_WIDTH,
 
237
        myPaperHeight,
 
238
        myPaperWidth,
 
239
        SHADOW_WIDTH + 1
 
240
      );
 
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);
 
244
    }
 
245
    
 
246
    // box
 
247
    if (myIsPainting) {
 
248
      g.setColor(Color.black);
 
249
      g.drawRect(0, 0, myPaperWidth, myPaperHeight);
 
250
    }
 
251
 
 
252
    // border
 
253
    if (myHasBorder) {
 
254
      g.setColor(myBorderColor);
 
255
      g.drawRect(myPageX, myPageY, myPageWidth, myPageHeight);
 
256
    }
 
257
 
 
258
    // number
 
259
    if (myIsPainting) {
 
260
      g.setColor(NUMBER_FONT_COLOR);
 
261
      g.setFont(NUMBER_FONT_NAME);
 
262
      g.drawString(Integer.toString(myNumber), NUMBER_X, NUMBER_Y);
 
263
    }
 
264
  }
 
265
 
 
266
  private void drawTitle(
 
267
    Graphics2D g,
 
268
    String left,
 
269
    String center,
 
270
    String right,
 
271
    int y,
 
272
    Color color,
 
273
    Font f)
 
274
  {
 
275
    g.setColor(color);
 
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);
 
279
  }
 
280
 
 
281
  private void drawTitle(
 
282
    Graphics2D g,
 
283
    String text,
 
284
    int x,
 
285
    int y,
 
286
    Font font)
 
287
  {
 
288
    g.setFont(font);
 
289
    g.drawString(text, x, y);
 
290
  }
 
291
 
 
292
  private String expandTitle(String t) {
 
293
    Date printed = new Date(System.currentTimeMillis());
 
294
 
 
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));
 
304
 
 
305
    return t;
 
306
  }
 
307
 
 
308
  private int getWidth(String text, Font font) {
 
309
    return (int) Math.ceil(font.getStringBounds(
 
310
      text, Util.FONT_RENDER_CONTEXT).getWidth());
 
311
  }
 
312
 
 
313
  private String getDate(Date timestamp) {
 
314
    return getTimestamp(timestamp, "yyyy.MM.dd"); // NOI18N
 
315
  }
 
316
 
 
317
  private String getTime(Date timestamp) {
 
318
    return getTimestamp(timestamp, "HH:mm:ss"); // NOI18N
 
319
  }
 
320
 
 
321
  private String getTimestamp(Date timestamp, String format) {
 
322
    return new SimpleDateFormat(format).format(timestamp);
 
323
  }
 
324
 
 
325
  private int myNumber;
 
326
  private double myScale;
 
327
  private PrintPage myPage;
 
328
  private boolean myIsPainting;
 
329
 
 
330
  private int myPaperWidth;
 
331
  private int myPaperHeight;
 
332
  private int myPageX;
 
333
  private int myPageY;
 
334
  private int myPageWidth;
 
335
  private int myPageHeight;
 
336
 
 
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;
 
344
 
 
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;
 
352
 
 
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;
 
360
 
 
361
  private int myRow;
 
362
  private int myColumn;
 
363
 
 
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
 
372
}