⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zsheetcanvas.java

📁 用Java写的报表.功能如下: 0.内建网络打印,网络预览功能! 1.文件操作。包括url 指定的文件。 2.全功能打印支持。包括打印预览。 3.Undo 和 redo。 4.合并单元格。 5.Cel
💻 JAVA
字号:
/*
 * Copyright 2002 EZCell , Inc. All rights reserved.
 * Version  1.0.
 * Author   W.John
 */
package ezcell;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.*;
import java.awt.geom.AffineTransform;
import javax.swing.JComponent;


/**
 * DOCUMENT ME!
 *
 * @version 1.00
 * @author W.John
 */
public class ZSheetCanvas implements ZSheetStateListener {
    private int cookie;
    protected ZSheetState modal;
    JComponent canvas;
    private ZSheetPainter sheetPainter;
    private ZCellPainter headCellPainter;
    private ZCellPainter commCellPainter;
    private ZSelectionPainter selectionPainter;
    private ZCell defaultHeadCell;
    protected ZPen hiLinePen;
    private boolean selectionVisible;

    //  private int horzResizebar;
    //  private int vertResizebar;
    private int edit;

    // ui state
    ZSheet sheet;
    ZRect selection;
    ZCellID focus;
    ZRect visibleCells;

    //
    AffineTransform af = new AffineTransform();

    // misc
    // cookie ,prevent multi painting on one cell
    ZSheetCanvas(ZSheetState modal) {
        setModal(modal);

        ZDefaultUI ui = modal.getUI();

        if (ui == null) {
            ui = ZUIManager.getDefault();
        }

        updateUI(ui);
        getState();
    }

    /**
     * put your documentation comment here
     * @param cell
     * @return
     * @exception Exception
     */
    public ZRect getCellRect(ZCell cell) throws Exception {
        return modal.getCellRect(cell);
    }

    /**
     * put your documentation comment here
     * @param comp
     */
    public void setComponent(JComponent comp) {
        this.canvas = comp;
    }

    /**
     * put your documentation comment here
     * @param modal
     */
    public void setModal(ZSheetState modal) {
        if (this.modal == modal) {
            return;
        }

        this.modal = modal;
        this.modal.addListener(this);
    }

    /**
     *
     * @param sheetPainter
     * @param headCellPainter
     * @param commCellPainter
     * @param selectionPainter
     */
    public void setPainter(ZSheetPainter sheetPainter, ZCellPainter headCellPainter, ZCellPainter commCellPainter,
                           ZSelectionPainter selectionPainter) {
        this.sheetPainter = sheetPainter;
        this.headCellPainter = headCellPainter;
        this.commCellPainter = commCellPainter;
        this.selectionPainter = selectionPainter;
    }

    /**
     * put your documentation comment here
     * @return
     */
    public ZSheet getSheet() {
        return sheet;
    }

    /**
     * put your documentation comment here
     * @param shape
     */
    public void animationStarted(ZShape shape) {
        if (!shape.isAnimating()) {
            shape.startAnimation();
        }
    }

    /**
     * put your documentation comment here
     * @param shape
     */
    public void animationStopped(ZShape shape) {
        if (shape.isAnimating()) {
            shape.stopAnimation();
        }
    }

    /**
     * put your documentation comment here
     * @param modal
     */
    public void cursorChanged(ZSheetState modal) {
        canvas.setCursor(modal.getCursor());
    }

    /**
     * put your documentation comment here
     * @param editor
     */
    public void editorAdded(ZEditor editor) {
        editor.showIn(canvas);
    }

    /**
     * put your documentation comment here
     * @param editor
     */
    public void editorKilled(ZEditor editor) {
        editor.setVisible(false);

    }

    /**
     * put your documentation comment here
     * @param modal
     */
    public void focusGained(ZSheetState modal) {
        if (canvas != null && !canvas.hasFocus() ) {

            canvas.requestFocus();

        }
    }

    /**
     * put your documentation comment here
     * @param g2
     */
    public void paint(Graphics2D g2) {
        if (sheetPainter == null) {
            return;
        }

        if (modal.isTableDirty()) {
            modal.buildTable();
        }

        cookie++;
     //   g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING ,RenderingHints.VALUE_ANTIALIAS_ON);

        Rectangle dc = g2.getClipBounds(); // clip in device coordernate

        Point tl = modal.dp2lp(new Point(dc.x, dc.y));
        Point br = modal.dp2lp(new Point(dc.x + dc.width, dc.y + dc.height));
        ZRect lc = new ZRect(tl.x, tl.y, br.x, br.y); //// clip in logical coordernate
        AffineTransform afSave = g2.getTransform();
        g2.transform(af);

        for (int ri = 0; ri <= visibleCells.bottom; ri = (ri == 0) ? visibleCells.top : (++ri)) {
            for (int ci = 0; ci <= visibleCells.right; ci = (ci == 0) ? visibleCells.left : (++ci)) {
                ZCell cell;

                if ((ri == 0) || (ci == 0)) {
                    cell = defaultHeadCell.clone(sheet, ri, ci);
                } else {
                    cell = sheet.getCell(ri, ci);
                }

                try {
                    ZRect loc = modal.getCellRect(cell);
             //       loc.grow(-2,-2);
                   // g2.draw3DRect(loc.left,loc.top,loc.getWidth() ,loc.getHeight(),true );
               //     loc.grow(-2,-2);
                    if ((loc.getWidth() > 0) && (loc.getHeight() > 0) && (lc.contain(loc) || lc.isIntersects(loc))) {
                        paintCell(g2, loc, cell);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        paintSelection(g2);
        g2.setTransform(afSave);
        modal.clearInvalidate();
    }

    /**
     *
     * @param g2
     * @param cellLoc
     * @param cell
     */
    public void paintCell(Graphics2D g2, ZRect cellLoc, ZCell cell) {
        if (cell.isHead()) {
            if (headCellPainter != null) {
                headCellPainter.paint(g2, cellLoc, cell);
            }

            return;
        } else if (cell.isParent()) {
            if (cookie == cell.getCookie()) {
                return;
            }

            cellLoc = modal.getCellsRect(cell.getSpan());

            Shape gc = g2.getClip();

            try {
                ZRect rect = modal.getCellRect(sheet.getCell(0, 0));
                ZRect mc = modal.getClip();
                Point p = modal.dp2lp(new Point(mc.right, mc.bottom));
                g2.clipRect(rect.right, rect.bottom, p.x, p.y);
            } catch (Exception ex) {
            }


            //////////////////////////////////////////////////////////////////////////////////////
            commCellPainter.paint(g2, cellLoc, cell);
            cell.setCookie(cookie);
            g2.setClip(gc);
        } else if (cell.isChild()) {
            try {
                ZCell parent = cell.getParent();

                if (!parent.isParent()) {
                    return;
                }

                paintCell(g2, cellLoc, parent);
            } catch (Exception ex) {
            }

            return;
        } else {
            commCellPainter.paint(g2, cellLoc, cell);
        }
    }

    /**
     * put your documentation comment here
     * @param g2
     */
    public void paintSelection(Graphics2D g2) {
        if (!selectionVisible || (selectionPainter == null)) {
            return;
        }

        Shape gc = g2.getClip();

        try {
            //modal.clipClient(g2);
        } catch (Exception ex) {
        }

        //////////////////////////////////////////////////////////////////////////////////////
        // clip selection from visible cells
        ZRect visibleSelection = (ZRect) modal.getVisibleCells().clone();

        if (!visibleSelection.isIntersects(selection)) {
            return;
        }

        visibleSelection.intersection(selection);

        // get draw rect of clip cells;
        ZRect frame = modal.getCellsRect(visibleSelection);
        frame.grow(1, 1);
        selectionPainter.paint(g2, frame);

        //   g2.setClip(gc);
        ZShape.Rectangle tracker = modal.getDragTracker();
        tracker.left = frame.left;
        tracker.top = frame.top;
        tracker.bottom = frame.bottom;
        tracker.right = frame.right;

        ZCell cell = modal.getSheet().getCell(focus.row, focus.col);
        g2.clipRect(frame.left + 1, frame.top + 1, frame.getWidth() - 2, frame.getHeight() - 2); //setClip(gc) ;

        if (visibleSelection.containCell(focus.row, focus.col) ||
                (cell.isParent() && visibleSelection.isIntersects(cell.getSpan()))) {
            try {
                cell.setCookie(cookie - 1);

                ZRect loc = modal.getCellRect(cell);
                paintCell(g2, loc, cell);
            } catch (Exception ex) {
            }
        }
    }

    /**
     * put your documentation comment here
     * @param shape
     */
    public void paintStarted(ZShape shape) {
        shape.paint((Graphics2D) canvas.getGraphics());
    }

    /**
     * put your documentation comment here
     * @param shape
     */
    public void shapeAdded(ZShape shape) {
        shape.paint((Graphics2D) canvas.getGraphics());
    }

    /**
     * put your documentation comment here
     * @param shape
     */
    public void shapeKilled(ZShape shape) {
        if (shape.isAnimating()) {
            shape.stopAnimation();
        } else {
            shape.erase((Graphics2D) canvas.getGraphics());
        }
    }

    /**
     * put your documentation comment here
     * @param shape
     * @param to
     */
    public void shapeMoving(ZShape shape, Point to) {
        if (!shape.isAnimating()) {
            shape.erase((Graphics2D) canvas.getGraphics());
            shape.moveTo(to.x, to.y);
            shape.paint((Graphics2D) canvas.getGraphics());
        } else {
            shape.moveTo(to.x, to.y);
        }
    }

    /**
     * put your documentation comment here
     * @param shape
     * @param to
     */
    public void shapeMoving(ZShape shape, ZRect to) {
        if (!shape.isAnimating()) {
            shape.erase((Graphics2D) canvas.getGraphics());

            ZRect b = shape.getBound();
            shape.setBound(to.left, to.top, to.right, to.bottom);
            shape.paint((Graphics2D) canvas.getGraphics());
            shape.setBound(b.left, b.top, b.right, b.bottom);
        } else {
            shape.setBound(to.left, to.top, to.right, to.bottom);
        }
    }

    /**
     * put your documentation comment here
     * @param modal
     */
    public void sheetPropertyChanged(ZSheetState modal) {
        update();
    }

    /**
     * put your documentation comment here
     * @param modal
     */
    public void uiChanged(ZSheetState modal) {
        headCellPainter.updateUI(modal.getUI());
        commCellPainter.updateUI(modal.getUI());
        updateUI(modal.getUI());
        update();
    }

    /**
     * put your documentation comment here
     * @param modal
     */
    public void uiStateChanged(ZSheetState modal) {
        getState();
        update();
    }

    /**
     * put your documentation comment here
     */
    public void update() {
        if (canvas == null) {
            return;
        }

        ZRect inv = modal.getInvalidate();
/*
        if(inv == null)
        {
          System.out.println("ZSheetCanvas.upate(): all") ;
        }
        else
        System.out.println("ZSheetCanvas.upate(): "+inv) ;
*/

        if (inv != null) {
            try {
                if (modal.isTableDirty()) {
                    modal.buildTable();
                }

                inv = modal.getCellsRect(inv);

                Point tl = modal.lp2dp(inv.getTopLeft());
                Point br = modal.lp2dp(inv.getBottomRight());
                canvas.repaint(tl.x + 1, tl.y + 1, br.x - tl.x - 1, br.y - tl.y - 1);

                return;
            } catch (Exception ex) {
            }
        }

        canvas.repaint();
    }

    /**
     * put your documentation comment here
     */
    private void getState() {
        sheet = modal.getSheet();
        selection = modal.getSelection();
        focus = modal.getFocus();
        visibleCells = modal.getVisibleCells();


        //transform
        af = modal.getTransform();

        //af.rotate(3,3,3);
    }

    /**
     * put your documentation comment here
     * @param ui
     */
    private void updateUI(ZDefaultUI ui) {
        if (sheetPainter == null) {
            sheetPainter = (ZSheetPainter) ui.get(ZDefaultUI.PAINTER_SHEET);
        }

        if (commCellPainter == null) {
            commCellPainter = (ZCellPainter) ui.get(ZDefaultUI.PAINTER_COMMON_CELL);
        }

        if (headCellPainter == null) {
            headCellPainter = (ZCellPainter) ui.get(ZDefaultUI.PAINTER_HEAD_CELL);
        }

        if (selectionPainter == null) {
            selectionPainter = (ZSelectionPainter) ui.get(ZDefaultUI.PAINTER_SELECTION);
        }

        hiLinePen = (ZPen) ui.get(ZDefaultUI.RESIZE_BAR_PEN);
        selectionVisible = ((Boolean) ui.get(ZDefaultUI.SELECTION_VISIBLE)).booleanValue();
        defaultHeadCell = (ZCell) ui.get(ZDefaultUI.DEFAULT_HEAD_CELL);
    }
} // end of ZSheetCanvas

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -