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.provider;
042:
043: import java.awt.Rectangle;
044: import java.util.ArrayList;
045: import java.util.List;
046: import java.util.Date;
047:
048: import javax.swing.JComponent;
049:
050: import org.netbeans.modules.print.spi.PrintPage;
051: import org.netbeans.modules.print.spi.PrintProvider;
052: import org.netbeans.modules.print.impl.util.Percent;
053:
054: /**
055: * @author Vladimir Yaroslavskiy
056: * @version 2006.01.12
057: */
058: public class ComponentProvider implements PrintProvider {
059:
060: public ComponentProvider(List<JComponent> components, String name,
061: Date modified) {
062: myName = name;
063: myLastModifiedDate = modified;
064:
065: if (components != null) {
066: myComponent = new ComponentPanel(components);
067: }
068: }
069:
070: protected JComponent getComponent() {
071: return myComponent;
072: }
073:
074: public PrintPage[][] getPages(int pageWidth, int pageHeight,
075: double pageZoom) {
076: List<ComponentPage> pages = new ArrayList<ComponentPage>();
077: JComponent component = getComponent();
078:
079: int componentWidth = component.getWidth();
080: int componentHeight = component.getHeight();
081:
082: double zoom = getZoom(pageZoom, pageWidth, pageHeight,
083: componentWidth, componentHeight);
084:
085: componentWidth = (int) Math.floor(componentWidth * zoom);
086: componentHeight = (int) Math.floor(componentHeight * zoom);
087:
088: int row = 0;
089: int column = 0;
090:
091: for (int h = 0; h < componentHeight; h += pageHeight) {
092: row++;
093: column = 0;
094:
095: for (int w = 0; w < componentWidth; w += pageWidth) {
096: column++;
097:
098: Rectangle piece = new Rectangle((column - 1)
099: * pageWidth, (row - 1) * pageHeight, pageWidth,
100: pageHeight);
101: pages.add(new ComponentPage(component, piece, zoom,
102: row - 1, column - 1));
103: }
104: }
105: PrintPage[][] printPages = new PrintPage[row][column];
106:
107: for (ComponentPage page : pages) {
108: printPages[page.getRow()][page.getColumn()] = page;
109: }
110: return printPages;
111: }
112:
113: private double getZoom(double zoom, int pageWidth, int pageHeight,
114: int componentWidth, int componentHeight) {
115: double factor = Percent.getZoomFactor(zoom, -1.0);
116:
117: if (0 < factor) {
118: return factor;
119: }
120: if (Percent.isZoomPage(zoom)) {
121: factor = 0.0;
122: }
123: int zoomWidth = Percent.getZoomWidth(zoom, -1);
124: int zoomHeight = Percent.getZoomHeight(zoom, -1);
125:
126: if (factor == 0.0) {
127: zoomWidth = 1;
128: zoomHeight = 1;
129: }
130: return getZoom((double) (pageWidth * zoomWidth)
131: / (double) componentWidth,
132: (double) (pageHeight * zoomHeight)
133: / (double) componentHeight);
134: }
135:
136: private double getZoom(double widthZoom, double heightZoom) {
137: if (widthZoom > 0 && heightZoom > 0) {
138: return Math.min(widthZoom, heightZoom);
139: }
140: if (widthZoom < 0 && heightZoom > 0) {
141: return heightZoom;
142: }
143: if (widthZoom > 0 && heightZoom < 0) {
144: return widthZoom;
145: }
146: return 1.0;
147: }
148:
149: public String getName() {
150: return myName;
151: }
152:
153: public Date getLastModifiedDate() {
154: return myLastModifiedDate;
155: }
156:
157: private String myName;
158: private JComponent myComponent;
159: private Date myLastModifiedDate;
160: }
|