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

📄 zdefaultsheet.java

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

import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Collections;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.event.ChangeListener;


/**
 * A ZDefaultSheet acts as a modal in MVC frame , It maintains collections of ZCell,ZRow and ZCol,
 * It provides these services :<p>
 *
 * 1.Contains cells rows,columns<p>
 * 2.Provides support for adding and removing one of elements through the add and remove methods.<p>
 * 3.Notifies ZDefaultSheet listeners of changes to the elements and its own properties..<p>
 * 4.Allows you to get an element at specified position.<p>
 * 5.Allows get information about its own properties,such as its title, its size dimension.<p>
 * 6.Provides a shared ZCalculator for every ZCell, A ZCell can query result after feeding its formula.<p>
 * 7.Serialization also is provided.<p>
 * @author unascribed
 * @version 1.0
 */
public class ZDefaultSheet implements Cloneable, Serializable, ZSheet {
    private int maxElement = 25;
    private int lastRow = (maxElement * maxElement) - 1;
    private int lastCol = (maxElement * maxElement) - 1;

    // hash table of cells,rows, columns
    private Vector cellsTable;
    private Vector rowsTable;
    private Vector colsTable;

    // vector of cells,rows, columns,parent cells
    private Vector cellsList;
    private Vector rowsList;
    private Vector colsList;
    private Vector parentCellsList;
    private int cookie;
    private ZRect printingCells;
    private Point activeCell;
    public ZBase defaultBase;
    private ZDefaultCell defCell;
    private ZRow defRow;
    private ZCol defCol;
    private ZDefaultCell defHeadCell;
    private ZDefaultBook book;
    private String title;
    private Vector listeners;
    private ZCalculator calculator;

    public ZDefaultSheet(ZDefaultBook book) {
        this();
        this.book = book;
    }

    /**
     * put your documentation comment here
     */
    public ZDefaultSheet() {
        init();

        // for test
    }

    /**
     *
     * @param activeCell
     */
    public void setActiveCell(Point activeCell) {
        this.activeCell = activeCell;
    }

    /**
     *
     * @return
     */
    public Point getActiveCell() {
        return activeCell;
    }

    /**
     * put your documentation comment here
     * @param book
     */
    public void setBook(ZDefaultBook book) {
        this.book = book;
    }

    /**
     * put your documentation comment here
     * @return
     */
    public ZBook getBook() {
        return book;
    }

    /**
     *
     * @return
     */
    public ZCalculator getCalculator() {
        if (calculator == null) {
            calculator = new ZCalculator(this);
        }

        return calculator;
    }

    /**
     * put your documentation comment here
     * @param row
     * @param col
     * @return
     */
    public ZCell getCell(int row, int col) {
        ZDefaultCell cell = getExistCell(row, col);

        if (cell != null) {
            return cell;
        } else {
            defCell.setRow(row);
            defCell.setCol(col);

            return defCell;
        }
    }

    /**
     *
     * @param row
     * @param col
     *
     * @return
     */
    public ZDefaultCell getCell2(int row, int col) {
        ZDefaultCell cell = (ZDefaultCell) getCell(row, col);

        try {
            if (cell.isParent()) {
                cell = (ZDefaultCell) cell.getParent();
            }
        } catch (Exception e) {
        }

        ;

        return cell;
    }

    /**
     * put your documentation comment here
     * @param row
     * @param col
     * @param text
     */
    public ZCmdFormat setCellText(int row, int col, String text) {
        ZCmdFormat cmd = new ZCmdFormat(this, new ZRect(col, row, col, row), ZBase.PTY_Text, text,
                                        new ZCmdWorker() {
            /**
             * put your documentation comment here
             * @param cmd
             * @param obj
             */
            public void workOnObject(ZCmdFormat cmd, ZBase obj) {
                if (obj.type() != ZBase.CELL) {
                    return;
                }

                ZDefaultCell cell = (ZDefaultCell) obj;
                cell.setText((String) cmd.getValueObject());
            }
        });
        cmd.commit();
        getCalculator().set(getCell(row, col));

        return cmd;
    }

    /**
     * put your documentation comment here
     * @return
     */
    public Vector getCells() {
        return cellsList;
    }

    /**
     * put your documentation comment here
     * @return
     */
    /**
     * put your documentation comment here
     * @return
     */
    public Enumeration getCellsEnum(ZRect rect) {
        return new _CellsEnum(rect);
    }

    /**
     * put your documentation comment here
     * @param col
     * @return
     */
    public ZCol getCol(int col) {
        ZCol tc = getExistCol(col);

        if (tc != null) {
            return tc;
        } else {
            defCol.setRow(col);

            return defCol;
        }
    }

    /**
     * put your documentation comment here
     * @param col
     * @return
     */
    public int getColWidth(int col) {
        return getCol(col).getWidth();
    }

    /**
     * put your documentation comment here
     * @return
     */
    public Vector getCols() {
        return colsList;
    }

    /**
     * put your documentation comment here
     * @return
     */
    public Enumeration getColsEnum(ZRect rect) {
        return new _ColsEnum(rect);
    }

    /**
     * put your documentation comment here
     * @return
     */
    public int getCookie() {
        return cookie;
    }

    /**
     * put your documentation comment here
     * @param defCell
     */
    public void setDefCell(ZDefaultCell defCell) {
        defCell.setSheet(this);
        this.defaultBase = defCell;
    }

    /**
     * put your documentation comment here
     * @param defHeadCell
     */
    public void setDefHeadCell(ZDefaultCell defHeadCell) {
        defHeadCell.setSheet(this);
        this.defHeadCell = defHeadCell;
    }

    /**
     * put your documentation comment here
     * @return
     */
    public ZDefaultCell getDefHeadCell() {
        return defHeadCell;
    }

    /**
     * put your documentation comment here
     * @param row
     * @param col
     * @return
     */
    public ZDefaultCell getExistCell(int row, int col) {
        ZDefaultCell cell = null;
        int r0;
        int r1;
        int c0;
        int c1;

        try {
            r0 = row / maxElement;
            c0 = col / maxElement;
            r1 = row % maxElement;
            c1 = col % maxElement;

            Vector cells1 = (Vector) cellsTable.elementAt((maxElement * r0) + c0);

            if (cells1 != null) {
                cell = (ZDefaultCell) cells1.elementAt((maxElement * r1) + c1);
            }
        } catch (Exception ex) {
        }

        return cell;
    }

    /**
     * put your documentation comment here
     * @param col
     * @return
     */
    public ZCol getExistCol(int col) {
        ZCol c = null;

        try {
            int c0 = col / maxElement;
            int c1 = col % maxElement;
            Vector colVect = (Vector) colsTable.elementAt(c0);

            if (colVect != null) {
                c = (ZCol) colVect.elementAt(c1);
            }
        } catch (Exception ex) {
        }

        return c;
    }

    /**
     * put your documentation comment here
     * @param row
     * @return
     */
    public ZRow getExistRow(int row) {
        ZRow r = null;

        try {
            r = null;

            int r0 = row / maxElement;
            int r1 = row % maxElement;
            Vector rowVect = (Vector) rowsTable.elementAt(r0);

            if (rowVect != null) {
                r = (ZRow) rowVect.elementAt(r1);
            }
        } catch (Exception ex) {
        }

        return r;
    }

    /**
     * put your documentation comment here
     * @param rect
     * @param fontSize
     */
    public void setSelectionFontSize(ZRect rect, int fontSize)
                              throws ZSelectionNoSupported {
        validateSelection(rect);

        ZCmdFormat cmd = new ZCmdFormat(this, (ZRect) rect.clone(), ZBase.PTY_Font, new Integer(fontSize),
                                        new ZCmdWorker() {
            /**
             * put your documentation comment here
             * @param cmd
             * @param obj
             */
            public void workOnObject(ZCmdFormat cmd, ZBase obj) {
                Integer fontSize = (Integer) cmd.getValueObject();
                obj.setFontSize(fontSize.intValue());
            }
        });
        cmd.commit();
    }

    /**
     * put your documentation comment here
     * @param rect
     * @param format
     */
    public void setSelectionFormat(ZRect rect, String format)
                            throws ZSelectionNoSupported {
        validateSelection(rect);

        ZCmdFormat cmd = new ZCmdFormat(this, (ZRect) rect.clone(), ZBase.PTY_Format, format,
                                        new ZCmdWorker() {
            /**
             * put your documentation comment here
             * @param cmd
             * @param obj
             */
            public void workOnObject(ZCmdFormat cmd, ZBase obj) {
                String format = (String) cmd.getValueObject();
                obj.setFormat(format);
            }
        });
        cmd.commit();
    }

    /**
     * put your documentation comment here
     * @param rect
     * @param height
     */
    public void setSelectionHeight(ZRect rect, int height)
                            throws ZSelectionNoSupported {
        //  validateSelection(rect, ZRect.ROWS | ZRect.CELLS);
        rect = (ZRect) rect.clone();
        rect.left = 0;
        rect.right = lastCol;

        //
        // make sure selection type is rows
        //
        ZCmdFormat cmd = new ZCmdFormat(this, rect, ZBase.PTY_Height, new Integer(height),
                                        new ZCmdWorker(ZCmdWorker.ACCEPT_ROW) {
            /**
             * put your documentation comment here
             * @param cmd
             * @param obj
             */
            public void workOnObject(ZCmdFormat cmd, ZBase obj) {
                Integer height = (Integer) cmd.getValueObject();
                ZRow row = (ZRow) obj;
                row.setHeight(height.intValue());
            }
        });
        cmd.commit();
    }

    /**
     *
     * @param rect
     * @param value
     *
     * @throws ZSelectionNoSupported
     */
    public void setSelectionHorzAlign(ZRect rect, int value)
                               throws ZSelectionNoSupported {
        validateSelection(rect);

        ZCmdFormat cmd = new ZCmdFormat(this, (ZRect) rect.clone(), ZBase.PTY_HorzAlign, new Integer(value),
                                        new ZCmdWorker() {
            /**
             * put your documentation comment here
             * @param cmd
             * @param obj
             */
            public void workOnObject(ZCmdFormat cmd, ZBase obj) {
                Integer value = (Integer) cmd.getValueObject();
                obj.setHorzAlign(value.intValue());
            }
        });
        cmd.commit();
    }

    /**

⌨️ 快捷键说明

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