001: package net.sourceforge.squirrel_sql.plugins.graph;
002:
003: import net.sourceforge.squirrel_sql.client.gui.ScrollableDesktopPane;
004:
005: import javax.swing.*;
006: import java.awt.*;
007: import java.awt.geom.AffineTransform;
008: import java.awt.print.Printable;
009: import java.awt.print.PageFormat;
010: import java.awt.print.PrinterException;
011: import java.util.Vector;
012: import java.util.Arrays;
013:
014: public class GraphDesktopPane extends ScrollableDesktopPane implements
015: GraphPrintable {
016: private static final long serialVersionUID = 1L;
017: private Vector<GraphComponent> _graphComponents = new Vector<GraphComponent>();
018: private ConstraintViewListener _constraintViewListener;
019:
020: /////////////////////////////////////////////////////////
021: // Printing
022: private double _formatWidthInPixel;
023: private double _formatHeightInPixel;
024: private double _formatScale;
025: private boolean _isPrinting;
026:
027: //
028: /////////////////////////////////////////////////////////
029:
030: public GraphDesktopPane() {
031: _constraintViewListener = new ConstraintViewListener() {
032: public void foldingPointMoved(ConstraintView source) {
033: revalidate();
034: }
035: };
036: }
037:
038: public void paint(Graphics g) {
039: super .paintComponent(g);
040: super .paintBorder(g);
041:
042: paintGraphComponents(g);
043:
044: super .paintChildren(g);
045: }
046:
047: private void paintGraphComponents(Graphics g) {
048: for (int i = 0; i < _graphComponents.size(); i++) {
049: GraphComponent comp = _graphComponents.elementAt(i);
050: if (comp instanceof EdgesGraphComponent) {
051: ((EdgesGraphComponent) comp).setBounds(getWidth(),
052: getHeight());
053: }
054:
055: comp.paint(g, _isPrinting);
056: }
057: }
058:
059: public void putGraphComponents(GraphComponent[] graphComponents) {
060: for (int i = 0; i < graphComponents.length; i++) {
061: if (false == _graphComponents.contains(graphComponents[i])) {
062: if (graphComponents[i] instanceof ConstraintView) {
063: ((ConstraintView) graphComponents[i])
064: .addConstraintViewListener(_constraintViewListener);
065: }
066:
067: _graphComponents.add(graphComponents[i]);
068: }
069: }
070: }
071:
072: public void removeGraphComponents(GraphComponent[] graphComponents) {
073: _graphComponents.removeAll(Arrays.asList(graphComponents));
074: }
075:
076: public Vector<GraphComponent> getGraphComponents() {
077: return _graphComponents;
078: }
079:
080: public Dimension getRequiredSize() {
081: Dimension reqSize = super .getRequiredSize();
082: for (int i = 0; i < _graphComponents.size(); i++) {
083: GraphComponent graphComponent = _graphComponents
084: .elementAt(i);
085: Dimension buf = graphComponent.getRequiredSize();
086:
087: if (buf.width > reqSize.width) {
088: reqSize.width = buf.width;
089: }
090:
091: if (buf.height > reqSize.height) {
092: reqSize.height = buf.height;
093: }
094: }
095:
096: return reqSize;
097:
098: }
099:
100: ////////////////////////////////////////////////////////////////////////////////////////
101: // Printing
102: public void initPrint(double formatWidthInCm,
103: double formatHeightInCm, double formatScale) {
104: int pixelByCm = (int) (Toolkit.getDefaultToolkit()
105: .getScreenResolution()
106: * EdgesGraphComponent.CM_BY_INCH + 0.5);
107: _formatWidthInPixel = formatWidthInCm * pixelByCm;
108: _formatHeightInPixel = formatHeightInCm * pixelByCm;
109: _formatScale = formatScale;
110: }
111:
112: public Dimension initPrintNoScaleSinglePage() {
113: Dimension size = getSize();
114: _formatWidthInPixel = size.width;
115: _formatHeightInPixel = size.height;
116: _formatScale = 1;
117:
118: return size;
119:
120: }
121:
122: public int print(Graphics graphics, PageFormat pageFormat,
123: int pageIndex) throws PrinterException {
124: double edgesWitdthInPixel = _formatWidthInPixel * _formatScale;
125: double edgesHeightInPixel = _formatHeightInPixel * _formatScale;
126:
127: int pageCountHorizontal = getPageCountHorizontal(edgesWitdthInPixel);
128: int pageCountVertical = getPageCountVertical(edgesHeightInPixel);
129:
130: if (pageIndex >= pageCountHorizontal * pageCountVertical) {
131: return Printable.NO_SUCH_PAGE;
132: }
133:
134: Graphics2D g2d = (Graphics2D) graphics;
135:
136: AffineTransform oldTransform = g2d.getTransform();
137:
138: boolean origDoubleBufferingEnabled = RepaintManager
139: .currentManager(this ).isDoubleBufferingEnabled();
140:
141: try {
142: _isPrinting = true;
143: RepaintManager.currentManager(this )
144: .setDoubleBufferingEnabled(false);
145:
146: double tx = -getPageWidthInPixel(pageFormat)
147: * (pageIndex % pageCountHorizontal)
148: + pageFormat.getImageableX();
149: double ty = -getPageHeightInPixel(pageFormat)
150: * (pageIndex / pageCountHorizontal)
151: + pageFormat.getImageableY();
152:
153: g2d.translate(tx, ty);
154:
155: double sx = getPageWidthInPixel(pageFormat)
156: / edgesWitdthInPixel;
157: double sy = getPageHeightInPixel(pageFormat)
158: / edgesHeightInPixel;
159:
160: g2d.scale(sx, sy);
161:
162: paintGraphComponents(g2d);
163: super .paintChildren(g2d);
164:
165: } finally {
166: g2d.setTransform(oldTransform);
167: RepaintManager.currentManager(this )
168: .setDoubleBufferingEnabled(
169: origDoubleBufferingEnabled);
170: _isPrinting = false;
171: }
172:
173: return Printable.PAGE_EXISTS;
174: }
175:
176: private double getPageHeightInPixel(PageFormat pageFormat) {
177: return pageFormat.getImageableHeight();
178: }
179:
180: private double getPageWidthInPixel(PageFormat pageFormat) {
181: return pageFormat.getImageableWidth();
182: }
183:
184: public int getPageCountHorizontal(double pageWidthInPixel) {
185: return roundPageCount(getRequiredSize().width
186: / pageWidthInPixel);
187: }
188:
189: public int getPageCountVertical(double pageHeightInPixel) {
190: return roundPageCount(getRequiredSize().height
191: / pageHeightInPixel);
192: }
193:
194: private int roundPageCount(double d) {
195: return 0 < d - (int) d ? (int) (d + 1) : (int) d;
196: }
197: //
198: ///////////////////////////////////////////////////////////////////////////////////////
199:
200: }
|