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.Dimension;
044: import java.awt.Graphics;
045:
046: import java.util.Collections;
047: import java.util.Comparator;
048: import java.util.List;
049:
050: import javax.swing.JComponent;
051: import javax.swing.JPanel;
052:
053: import static org.netbeans.modules.print.impl.util.UI.*;
054:
055: /**
056: * @author Vladimir Yaroslavskiy
057: * @version 2008.02.22
058: */
059: final class ComponentPanel extends JPanel {
060:
061: ComponentPanel(List<JComponent> components) {
062: myComponents = sort(components);
063:
064: myHeight = 0;
065: myWidth = 0;
066:
067: for (int i = 0; i < myComponents.size(); i++) {
068: JComponent component = myComponents.get(i);
069: //out();
070: //out("see: " + component.getClass().getName() + " " + component.getClientProperty(Integer.class));
071: int width = getWidth(component);
072: int height = getHeight(component);
073: //out(" width: " + width);
074: //out(" height: " + height);
075:
076: myWidth += width;
077:
078: if (height > myHeight) {
079: myHeight = height;
080: }
081: }
082: }
083:
084: @Override
085: public void print(Graphics g) {
086: for (JComponent component : myComponents) {
087: component.print(g);
088: // g.setColor(java.awt.Color.green);
089: // g.drawRect(0, 0, getWidth(component), getHeight(component));
090: g.translate(getWidth(component), 0);
091: }
092: }
093:
094: @Override
095: public int getWidth() {
096: return myWidth;
097: }
098:
099: @Override
100: public int getHeight() {
101: return myHeight;
102: }
103:
104: private int getWidth(JComponent component) {
105: Dimension dimension = getDimension(component);
106:
107: if (dimension == null) {
108: return component.getWidth();
109: }
110: return dimension.width;
111: }
112:
113: private int getHeight(JComponent component) {
114: Dimension dimension = getDimension(component);
115:
116: if (dimension == null) {
117: return component.getHeight();
118: }
119: return dimension.height;
120: }
121:
122: private Dimension getDimension(JComponent component) {
123: Object object = component.getClientProperty(Dimension.class);
124:
125: if (object instanceof Dimension) {
126: return (Dimension) object;
127: }
128: return null;
129: }
130:
131: private List<JComponent> sort(List<JComponent> components) {
132: Collections.sort(components, new Comparator<JComponent>() {
133: public int compare(JComponent component1,
134: JComponent component2) {
135: int weight1 = getInteger(component1).intValue();
136: int weight2 = getInteger(component2).intValue();
137:
138: if (weight1 < weight2) {
139: return -1;
140: }
141: if (weight1 == weight2) {
142: return 0;
143: }
144: return 1;
145: }
146:
147: private Integer getInteger(JComponent component) {
148: Object object = component
149: .getClientProperty(java.lang.Integer.class);
150:
151: if (object instanceof Integer) {
152: return (Integer) object;
153: }
154: return Integer.MIN_VALUE;
155: }
156: });
157:
158: return components;
159: }
160:
161: private int myWidth;
162: private int myHeight;
163: private List<JComponent> myComponents;
164: }
|