Package DisplayProject.printing

Source Code of DisplayProject.printing.PrintPage

/*
Copyright (c) 2003-2008 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package DisplayProject.printing;

import java.awt.Font;
import java.awt.Insets;
import java.awt.print.PageFormat;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.log4j.Logger;

import DisplayProject.Array_Of_DataPoint;
import DisplayProject.Constants;
import DisplayProject.DataPoint;
import DisplayProject.UIutils;
import DisplayProject.actions.BottomMargin;
import DisplayProject.actions.ColourChange;
import DisplayProject.actions.FrameColor;
import DisplayProject.actions.FrameWeight;
import DisplayProject.actions.Height;
import DisplayProject.actions.LeftMargin;
import DisplayProject.actions.Parent;
import DisplayProject.actions.RightMargin;
import DisplayProject.actions.TopMargin;
import DisplayProject.actions.VisibleColumns;
import DisplayProject.actions.VisibleLines;
import DisplayProject.actions.Widget;
import DisplayProject.actions.WidgetX;
import DisplayProject.actions.WidgetY;
import DisplayProject.actions.Width;
import DisplayProject.controls.Ellipse;
import DisplayProject.controls.Line;
import DisplayProject.controls.PictureGraphic;
import DisplayProject.controls.Point;
import DisplayProject.controls.PolyLine;
import DisplayProject.controls.Rectangle;
import DisplayProject.controls.TextGraphic;
import DisplayProject.factory.GraphicFactory;
import Framework.ErrorMgr;
import Framework.ImageData;
import Framework.TextData;
import Framework.UsageException;

/**
* The PrintPage class defines the page used for printing a user window. The
* page is a container for the user window�s form and provides properties and
* methods that allow you to prepare the page for printing
*/
@SuppressWarnings("serial")
public class PrintPage extends JComponent {

    protected static JPanel form;
    /**
     * Font used for drawing text.
     */
    protected static Font DefaultFont;

    /**
     * Height, in mils, of a line.
     */
    protected static int defaultLineHeight;

    /**
     * Specifies line spacing used for page.
     */
    protected static int lineSpacing;

    /**
     * Pen location in columns from left margin.
     */
    protected static int penColumn;

    /**
     * Pen location in lines from top.
     */
    protected static int penLine;

    /**
     * Pen location in mils from left margin.
     */
    protected static int xPen;

    /**
     * Pen location in mils from top margin.
     */
    protected static int yPen;

    /**
     * Postpone processing GUI actions for some later time
     */
    protected static boolean postponeActions;

    /**
     * the boundaries of the pen, stored in mils
     */
    protected static Insets penBounds = new Insets(0, 0, 0, 0);

    /**
     * the average char width based on the default font;
     */
    protected static int averageCharInMils;
    /**
     * the default line height in mill based on the default font
     */

    static Logger _log = Logger.getLogger(PrintPage.class);

    private PrintPage() {

    }

    protected static void processActions() {
        if (!postponeActions)
            UIutils.processGUIActions();

    }

    /**
     * Clears the page.
     */
    public static void clearPage(JPanel form) {
        if (form != null)
            form.removeAll();

    }

    /**
     * Sets several positions and dimensions at once.
     *
     * @param x
     * @param y
     * @param height
     * @param width
     */
    public static JComponent configure(JPanel form, int x, int y, int height,
            int width) {
        setForm(form);
        Widget.configure(form, x, y, width, height);
        processActions();
        return form;
    }

    /**
     * Draws an ellipse.
     *
     * @param width
     * @param height
     * @param startX
     * @param startY
     * @param penColor
     * @param fillColor
     * @param lineStyle
     * @param lineWeight
     * @param contrastColor
     * @param contrastPattern
     */
    public static Ellipse drawEllipse(JPanel form, int width, int height,
            int startX, int startY, int penColor, int fillColor, int lineStyle,
            int lineWeight, int contrastColor, int contrastPattern) {
        setForm(form);
        Ellipse ellipse = new Ellipse();

        ellipse.setContrastColor(contrastColor);
        ellipse.setContrastPattern(contrastPattern);
        ellipse.setLineWeight(lineWeight);
        ellipse.setLineStyle(lineStyle);
        ellipse.setVisible(true);
        java.awt.Point loc = location(startX, startY);
        ColourChange.setForeground(ellipse, penColor);
        ColourChange.setBackground(ellipse, fillColor);
        Widget.configure(ellipse, loc.x, loc.y, width, height);
        Parent.set(ellipse, form);
        processActions();
        setXPen(form, loc.x + Width.get(ellipse));
        setYPen(form, loc.y);
        return ellipse;
    }

    /**
     * Draws a line.
     *
     * @param distHorizontal
     * @param distVertical
     * @param startX
     * @param startY
     * @param penColor
     * @param lineStyle
     * @param lineWeight
     */
    public static Line drawLine(JPanel form, int distHorizontal,
            int distVertical, int startX, int startY, int penColor,
            int lineStyle, int lineWeight) {
        setForm(form);
        java.awt.Point loc = location(startX, startY);
        int H = loc.x + distHorizontal;
        int V = loc.y + distVertical;
        Line line = GraphicFactory.newLine(loc.x, loc.y, H, V);
        line.setLineStyle(lineStyle);
        line.setLineWeight(lineWeight);
        line.setVisible(true);
        ColourChange.setForeground(line, penColor);
        Parent.set(line, form);
        processActions();
        setXPen(form, loc.x + distHorizontal);
        setYPen(form, loc.y + distVertical);
        return line;

    }

    /**
     * Draws multiple lines of text.
     */
    public static TextGraphic drawMultiLineText(JPanel form, TextData text,
            int startX, int startY, Font font, int horizAlign, int penColor,
            int width, int height, int visibleColumns, int visibleLines,
            boolean hasWordWrap, int lineSpacing) {
        if (height != -1 && visibleLines != -1) {
            UsageException errorVar = new UsageException("You cannot specify both Height and VisibleLines");
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
        if (width != -1 && visibleColumns != -1) {
            UsageException errorVar = new UsageException("You cannot specify both Width and VisibleColumns");
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
        setForm(form);
        TextGraphic tg = GraphicFactory.newTextGraphic(text);
        if (font != null)
            tg.setFont(font);
        tg.setSize(tg.getPreferredSize());
        tg.setVisible(true);
        java.awt.Point loc = location(startX, startY);
        switch (horizAlign) {
        case Constants.TA_LEFT:
            tg.setHorizontalAlignment(JLabel.LEFT);
            break;
        case Constants.TA_RIGHT:
            tg.setHorizontalAlignment(JLabel.RIGHT);
            break;
        case Constants.TA_CENTER:
            tg.setHorizontalAlignment(JLabel.CENTER);
            break;
        default:
            tg.setHorizontalAlignment(JLabel.LEFT);

        }
        WidgetX.set(tg, loc.x);
        WidgetY.set(tg, loc.y);
        if (width > -1)
            Width.set(tg, width);
        else if (visibleColumns > -1)
            VisibleColumns.set(tg, visibleColumns);

        if (height > -1)
            Height.set(tg, height);
        else if (visibleLines > -1)
            VisibleLines.set(tg, visibleLines);

        Parent.set(tg, form);
        processActions();
        setXPen(form, loc.x + Width.get(tg));
        setYPen(form, loc.y + Height.get(tg));
        return tg;

    }

    public static TextGraphic drawMultiLineText(JPanel form, String text,
            int startX, int startY, Font font, int horizAlign, int penColor,
            int width, int height, int visibleColumns, int visibleLines,
            boolean hasWordWrap, int lineSpacing) {
        return drawMultiLineText(form, new TextData(text), startX, startY,
                font, horizAlign, penColor, width, height, visibleColumns,
                visibleLines, hasWordWrap, lineSpacing);
    }

    /**
     * Draws an image.
     *
     * @param imageValue
     * @param width
     * @param height
     * @param startX
     * @param startY
     * @param imageGravity
     * @param penColor
     * @param fillColor
     * @param frameColor
     * @param frameWeight
     * @param imageSizePolicy
     */
    public static PictureGraphic drawPictureGraphic(JPanel form,
            ImageData imageValue, int width, int height, int startX,
            int startY, int penColor, int fillColor, int frameColor,
            int frameWeight, int imageGravity, int scalingPolicy,
            int imageSizePolicy) {
        setForm(form);
        PictureGraphic pg = GraphicFactory.newPictureGraphic(null, imageValue);
        pg.setImageGravity(imageGravity);
        pg.setImageSizePolicy(imageSizePolicy);
        pg.setVisible(true);
        ColourChange.setForeground(pg, penColor);
        ColourChange.setBackground(pg, fillColor);
        FrameWeight.set(pg, frameWeight);
        FrameColor.set(pg, frameColor);
        java.awt.Point loc = location(startX, startY);
        Widget.configure(pg, loc.x, loc.y, width, height);
        Parent.set(pg, form);
        processActions();
        setXPen(form, loc.x + Width.get(pg));
        setYPen(form, loc.y);
        return pg;

    }

    /**
     * Draws a point.
     *
     * @param width
     * @param height
     * @param startX
     * @param startY
     * @param penColor
     * @param symbol
     */
    public static Point drawPoint(JPanel form, int width, int height,
            int startX, int startY, int penColor, int symbol) {
        setForm(form);
        Point point = new DisplayProject.controls.Point();
        point.setVisible(true);
        ColourChange.setForeground(point, penColor);
        Widget.configure(point, startX, startY, width, height);
        Parent.set(point, form);
        processActions();
        setXPen(form, point.getX());
        setYPen(form, point.getY());
        return point;
    }

    /**
     * Draws a polyline.
     */
    public static PolyLine drawPolyLine(JPanel form, Array_Of_DataPoint<DataPoint> points,
            int penColor, int lineStyle, int lineWeight) {
        setForm(form);
        PolyLine line = new PolyLine();
        line.setPointArray(points);
        line.setLineStyle(lineStyle);
        line.setLineWeight(lineWeight);
        line.setVisible(true);
        DataPoint lastPoint = points.get(points.size()-1);
        ColourChange.setForeground(line, penColor);
        Parent.set(line, form);
        processActions();
        setXPen(form, lastPoint.X);
        setYPen(form, lastPoint.Y);
        return line;

    }

    /**
     * Draws a rectangle.
     *
     * @param width
     * @param height
     * @param startX
     * @param startY
     * @param penColor
     * @param fillColor
     * @param lineStyle
     * @param lineWeight
     * @param contrastColor
     * @param contrastPattern
     */
    public static Rectangle drawRectangle(JPanel form, int width, int height,
            int startX, int startY, int penColor, int fillColor, int lineStyle,
            int lineWeight, int contrastColor, int contrastPattern) {
        setForm(form);
        Rectangle rect = new Rectangle();
        rect.setVisible(true);
        if (lineStyle != -1)
            rect.setLineStyle(lineStyle);
        if (lineWeight != -127)
            rect.setLineWeight(lineWeight);
        if (penColor != 0)
            ColourChange.setForeground(rect, penColor);
        if (fillColor != 0)
            ColourChange.setBackground(rect, fillColor);
        java.awt.Point loc = location(startX, startY);
        Widget.configure(rect, loc.x, loc.y, width, height);
        Parent.set(rect, form);
        processActions();
        setXPen(form, loc.x + width);
        setYPen(form, loc.y)
        return rect;

    }

    /**
     * Draws a single line of text.
     *
     * @param text
     * @param startX
     * @param startY
     * @param font
     * @param horizAlign
     * @param vertAlign
     * @param penColor
     * @param width
     * @param visibleColumns
     */
    public static TextGraphic drawSingleLineText(JPanel form, TextData text,
            int startX, int startY, Font font, int horizAlign, int vertAlign,
            int penColor, int width, int visibleColumns) {
        if (width != -1 && visibleColumns != -1) {
            UsageException errorVar = new UsageException("You cannot specify both Width and VisibleColumns");
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
        setForm(form);
        TextGraphic tg = GraphicFactory.newTextGraphic(text);
        if (font != null)
            tg.setFont(font);
        tg.setSize(tg.getPreferredSize());
        java.awt.Point p = location(startX, startY);
        setYPen(form, p.y);
        tg.setVisible(true);
        ColourChange.setForeground(tg, penColor);
        if (width > -1)
            Width.set(tg, width);
        else if (visibleColumns > -1)
            Width.set(tg, UIutils.pixelsToMils(UIutils.colsToPixels(visibleColumns, tg)));
        switch (horizAlign) {
        case Constants.TA_RIGHT:
            tg.setHorizontalAlignment(JLabel.RIGHT);
            WidgetX.set(tg, p.x - Width.get(tg));
            setXPen(form, WidgetX.get(tg));
            break;
        case Constants.TA_CENTER:
            tg.setHorizontalAlignment(JLabel.CENTER);
            WidgetX.set(tg, WidgetX.get(tg) + (Width.get(tg)/2));
            break;
        default:
            tg.setHorizontalAlignment(JLabel.LEFT);
            WidgetX.set(tg, p.x);
            setXPen(form, WidgetX.get(tg) + Width.get(tg));
            break;
        }
        WidgetY.set(tg, p.getY());
        Parent.set(tg, form);
        processActions();
        return tg;

    }

    public static TextGraphic drawSingleLineText(JPanel form, String text,
            int startX, int startY, Font font, int horizAlign, int vertAlign,
            int penColor, int width, int visibleColumns) {

        return drawSingleLineText(form, new TextData(text), startX, startY,
                font, horizAlign, vertAlign, penColor, width, visibleColumns);
    }

    /**
     * Moves current location by specified lines and columns.
     *
     * @param numLines
     * @param numColumns
     */
    public static void movePenCell(JPanel form, int numLines, int numColumns) {
        setPenColumn(form, getPenColumn(form) + numColumns);
        setPenLine(form, getPenLine(form) + numLines);
    }

    /**
     * Move pen to specified line and column.
     *
     * @param line
     * @param column
     */
    public static void movePenCellTo(JPanel form, int line, int column) {
        setPenColumn(form, column);
        setPenLine(form, line);
    }

    /**
     * Limits the area of the page where the pen can draw to the section of the
     * page you specify (by coordinates)
     *
     *
     */
    public static int setPenBounds(JPanel form, int leftBoundMils,
            int topBoundMils, int rightBoundMils, int bottomBoundMils) {
        setForm(form);
        if (leftBoundMils == 0 && topBoundMils == 0 && rightBoundMils == 0
                && leftBoundMils == 0) {
            PageFormat page = PrintUtilities.getPrinterJob().defaultPage();
            double height = page.getHeight();
            double width = page.getWidth();
            penBounds = new Insets(0, 0, PrintUtilities.pointsToMils(height),
                    PrintUtilities.pointsToMils(width));
        } else {
            penBounds = new Insets(topBoundMils, leftBoundMils,
                    bottomBoundMils, rightBoundMils);
        }

        return 0;
    }

    /**
     * Limits the area of the page where the pen can be (by FieldWidget)
     *
     *
     */
    public static int setPenBounds(JPanel form, JComponent boundingField) {
        if (boundingField == null) {
            PageFormat page = PrintUtilities.getPrinterJob().defaultPage();
            double height = page.getHeight();
            double width = page.getWidth();
            penBounds = new Insets(0, 0, PrintUtilities.pointsToMils(height),
                    PrintUtilities.pointsToMils(width));
        } else {
            int Y = WidgetY.get(boundingField);
            int X = WidgetX.get(boundingField);
            penBounds = new Insets(Y, X,
                    Height.get(boundingField) + Y, Width.get(boundingField) + X);
        }
        return 0;
    }

    public static Font getDefaultFont(JPanel form) {
        setForm(form);
        return DefaultFont;
    }

    public static void setDefaultFont(JPanel form, Font defaultFont) {
        setForm(form);
        DefaultFont = defaultFont;
        form.setFont(defaultFont);
        averageCharInMils = PrintUtilities.averageCharInMils(form.getFont());
        defaultLineHeight = PrintUtilities.defaultLineHeight(form.getFont());
    }

    public static int getDefaultLineHeight(JPanel form) {
        setForm(form);
        return defaultLineHeight;
    }

    public static void setDefaultLineHeight(JPanel form, int lineHeight) {
        setForm(form);
        defaultLineHeight = lineHeight;
    }

    public static int getLineSpacing(JPanel form) {
        setForm(form);
        return lineSpacing;
    }

    public static void setLineSpacing(JPanel form, int spacing) {
        setForm(form);
        lineSpacing = spacing;
    }

    /**
     * Read-only property that contains the number of columns on the page, based
     * on the current margins and the average character width of the default
     * font.
     *
     * @param form
     */
    public static int getNumColumns(JPanel form) {
        setForm(form);
        PageFormat page = PrintUtilities.getPrinterJob().defaultPage();
        int left = penBounds.left;
        int right = penBounds.right;
        double width = page.getWidth();
        int widthInMils = PrintUtilities.pointsToMils(width);
        return (int) (widthInMils - left - right) / averageCharInMils;
    }

    /**
     * Read-only property that contains the number of columns on the page, based
     * on the current margins and the height of the default font.
     *
     */
    public static int getNumLines(JPanel form) {
        setForm(form);
        PageFormat page = PrintUtilities.getPrinterJob().defaultPage();
        int top = penBounds.top;
        int bottom = penBounds.bottom;
        double height = page.getHeight();
        int numLines = (int) (PrintUtilities.pointsToMils(height) - top - bottom) / defaultLineHeight;
        return numLines;
    }

    public static int getPenColumn(JPanel form) {
        setForm(form);
        return penColumn;
    }

    public static void setPenColumn(JPanel form, int column) {
        setForm(form);
        penColumn = column;
        int mils = 0;
        if (penColumn > 0)
            mils = averageCharInMils * (penColumn -1);
        setXPen(form, mils);
    }

    public static int getPenLine(JPanel form) {
        setForm(form);
        return penLine;
    }

    public static void setPenLine(JPanel form, int line) {
        setForm(form);
        penLine = line;
        int mils = 0;
        if (penLine > 0)
            mils = defaultLineHeight * (penLine -1);
        setYPen(form, mils);
    }

    public static int getXPen(JPanel form) {
        setForm(form);
        return xPen;
    }

    public static void setXPen(JPanel form, int pen) {
        setForm(form);
        xPen = pen;
        _log.debug("xPen changed to: " + xPen);
    }

    public static int getYPen(JPanel form) {
        setForm(form);
        return yPen;
    }

    public static void setYPen(JPanel form, int pen) {
        setForm(form);
        yPen = pen;
        _log.debug("yPen changed to: " + yPen);
    }

    public static boolean isPostponeActions() {
        return postponeActions;
    }

    public static void setPostponeActions(boolean postpone) {
        postponeActions = postpone;
        processActions();
    }
    /**
     * sets the printing form associated with this print page.
     * @param newForm
     */
    public static void setForm(JPanel newForm) {
        if ((form == null) || (!form.equals(newForm))) {
            form = newForm;
            resetDefaults();
            formSizeFromDefault(form);
        }

    }
    /**
     * sets the for size to that of the page
     * @param form
     */
    private static void formSizeFromDefault(JPanel form){
        PageFormat page = PrintUtilities.getPrinterJob().defaultPage();
        int width = PrintUtilities.pointsToMils(page.getWidth());
        int height = PrintUtilities.pointsToMils(page.getHeight());
        Width.set(form, width);
        Height.set(form, height);
        UIutils.processGUIActions();
    }
    /**
     * sets the print margins based on any margins set on the form
     * @param form
     */
//    private static void adjustFormMargins(JPanel form){
//        int top = TopMargin.get(form);
//        int bottom = BottomMargin.get(form);
//        int left = LeftMargin.get(form);
//        int right = RightMargin.get(form);
//        setPenBounds(form, left, top, right, bottom);
//    }
    /**
     * resets the form parameters to defaults
     */
    private static void resetDefaults() {
        setYPen(getForm(), 0);
        setXPen(getForm(), 0);
        penLine = 0;
        penColumn = 0;
        lineSpacing = 0;
        defaultLineHeight = 0;
        DefaultFont = new JLabel().getFont();
        // PageFormat page = PrintUtilities.getPrinterJob().defaultPage();
        if (getForm() != null){
            penBounds = new Insets(TopMargin.get(getForm()),
            LeftMargin.get(getForm()),
            BottomMargin.get(getForm()),
            RightMargin.get(getForm()));
        }else{
            penBounds = new Insets(0, 0, 0, 0);
        }
        averageCharInMils = PrintUtilities.averageCharInMils(DefaultFont);
    }

    public static JPanel getForm() {
        return form;
    }

    public static void updatePage(JPanel form) {
        UIutils.processGUIActions();
    }
    /**
     * this method calculates the location of the component based on the passed in coordinates or the xPen/yPen position
     * @param startX default value 0
     * @param startY default value 0
     */
    private static java.awt.Point location(int startX, int startY){
        int x = xPen;
        if (startX > -1)
            x = startX;
        int y = yPen;
        if (startY > -1)
            y = startY;
        java.awt.Point p = new java.awt.Point(x, y);
        return p;
    }

}
TOP

Related Classes of DisplayProject.printing.PrintPage

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.