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

📄 zdefaultsheet.java

📁 用Java写的报表.功能如下: 0.内建网络打印,网络预览功能! 1.文件操作。包括url 指定的文件。 2.全功能打印支持。包括打印预览。 3.Undo 和 redo。 4.合并单元格。 5.Cel
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                    if (enum.hasMoreElements()) {
                        col = (ZCol) enum.nextElement();
                    } else {
                        break;
                    }
                }

                break;
            }
        }

        obs = (Vector) getCells().clone();
        ZDefaultCell.compareByRow = false;
        Collections.sort(obs);
        enum = obs.elements();

        while (enum.hasMoreElements()) {
            ZDefaultCell cell = (ZDefaultCell) enum.nextElement();

            if (cell.getCol() >= from) {
                while (cell.getCol() <= to) {
                    moveCell(cell, cell.getRow(), cell.getCol() - offset);

                    if (enum.hasMoreElements()) {
                        cell = (ZDefaultCell) enum.nextElement();
                    } else {
                        break;
                    }
                }

                break;
            }
        }
    }

    /**
     * put your documentation comment here
     * @param from
     * @param to
     * @param offset
     */
    public void moveObjectsUp(int from, int to, int offset) {
        ZBase.compareReverse = false;

        Vector obs = (Vector) getRows().clone();
        Collections.sort(obs);

        // if cell in the row ,and its property exists ,then change it
        Enumeration enum = obs.elements();

        while (enum.hasMoreElements()) {
            ZRow row = (ZRow) enum.nextElement();

            if (row.getRow() >= from) {
                while (row.getRow() <= to) { // use the second loop, to increase efficiency
                    moveRow(row, row.getRow() - offset);

                    if (enum.hasMoreElements()) {
                        row = (ZRow) enum.nextElement();
                    } else {
                        break;
                    }
                }

                break;
            }
        }

        obs = (Vector) getCells().clone();
        ZDefaultCell.compareByRow = true;
        Collections.sort(obs);
        enum = obs.elements();

        while (enum.hasMoreElements()) {
            ZDefaultCell cell = (ZDefaultCell) enum.nextElement();

            if (cell.getRow() >= from) {
                while (cell.getRow() <= to) {
                    moveCell(cell, cell.getRow() - offset, cell.getCol());

                    if (enum.hasMoreElements()) {
                        cell = (ZDefaultCell) enum.nextElement();
                    } else {
                        break;
                    }
                }

                break;
            }
        }
    }

    /**
     * put your documentation comment here
     * @param row
     * @param pos
     */
    public void moveRow(ZRow row, int pos) {
        remove(row);
        row.row = pos;
        add(row);
    }

    /**
     *
     * @param rect
     *
     * @throws ZSelectionNoSupported
     * @throws Exception
     */
    public void pasteIntoSelection(ZRect rect) throws ZSelectionNoSupported, Exception {
        //  Clipboard clipboard = new Clipboard("copyitem");
        Transferable selection = ZClipboard.getSystem().getContents(this);

        if (selection.isDataFlavorSupported(ZSerializableSelection.serializableFlavor)) {
            ZCopyItem item = (ZCopyItem) selection.getTransferData(ZSerializableSelection.serializableFlavor);
            item.pasteToWithFormat(this, rect, null);
        } else if (selection.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            pasteTo(selection, rect);
        }
    }

    /**
     * put your documentation comment here
     * @param rect
     */
    public void pasteTextIntoSelection(ZRect rect) throws ZSelectionNoSupported, Exception {
        //  Clipboard clipboard = new Clipboard("copyitem");
        Transferable selection = ZClipboard.getSystem().getContents(this);

        if (selection.isDataFlavorSupported(ZSerializableSelection.serializableFlavor)) {
            ZCopyItem item = (ZCopyItem) selection.getTransferData(ZSerializableSelection.serializableFlavor);
            item.pasteTo(this, rect, null);
        } else if (selection.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            pasteTo(selection, rect);
        }
    }

    /**
     * put your documentation comment here
     * @param obj
     */
    public void remove(ZBase obj) {
        int i;
        int r0 = obj.getRow() / maxElement;
        int c0 = obj.getCol() / maxElement;
        int r1 = obj.getRow() % maxElement;
        int c1 = obj.getCol() % maxElement;
        Vector tmpVect = null;

        switch (obj.type()) {
        case ZBase.CELL:

            if ((tmpVect = (Vector) cellsTable.elementAt((maxElement * r0) + c0)) != null) {
                tmpVect.setElementAt(null, (maxElement * r1) + c1);
            }

            cellsList.remove(obj);

            if (obj.hasProperty(ZBase.PTY_MergeAsParent)) {
                removeParentCell((ZDefaultCell) obj);
            }

            getCalculator().remove((ZDefaultCell) obj);

            break;

        case ZBase.ROW:

            if ((tmpVect = (Vector) rowsTable.elementAt(r0)) != null) {
                tmpVect.setElementAt(null, r1);
            }

            rowsList.remove(obj);

            break;

        case ZBase.COLUMN:

            if ((tmpVect = (Vector) colsTable.elementAt(c0)) != null) {
                tmpVect.setElementAt(null, c1);
            }

            colsList.remove(obj);

            break;
        }
    }

    /**
     *
     * @param rect
     *
     * @throws ZSelectionNoSupported
     */
    public void removeColsSelection(ZRect rect) throws ZSelectionNoSupported {
        validateSelection(rect, ZRect.COLUMNS);

        ZCmdRemove cmd = new ZCmdRemove(this, (ZRect) rect.clone());
        cmd.commit();
    }

    /**
     *
     * @param listener
     */
    public void removeListener(ChangeListener listener) {
        listeners.add(listener);
    }

    /**
     * put your documentation comment here
     * @param cell
     */
    public void removeParentCell(ZDefaultCell cell) {
        parentCellsList.remove(cell);
    }

    /**
     *
     * @param rect
     *
     * @throws ZSelectionNoSupported
     */
    public void removeRowsSelection(ZRect rect) throws ZSelectionNoSupported {
        validateSelection(rect, ZRect.ROWS);

        ZCmdRemove cmd = new ZCmdRemove(this, (ZRect) rect.clone());
        cmd.commit();
    }

    /**
     * put your documentation comment here
     * @param rect
     */
    public void removeSelection(ZRect rect) throws ZSelectionNoSupported {
        validateSelection(rect, ZRect.COLUMNS | ZRect.ROWS);

        ZCmdRemove cmd = new ZCmdRemove(this, (ZRect) rect.clone());
        cmd.commit();
    }

    /**
     * put your documentation comment here
     * @param from
     * @param to
     * @return
     */
    public int rowsHeight(int from, int to) {
        int result = 0;

        for (int i = from; i <= to; i++)
            result += getRow(i).getHeight();

        return result;
    }

    /**
     * put your documentation comment here
     * @param rect
     * @param type
     * @exception ZSelectionNoSupported
     */
    public void validateSelection(ZRect rect, int type)
                           throws ZSelectionNoSupported {
        if ((rect.type(this) & (~type)) != 0) {
            throw new ZSelectionNoSupported();
        }

        validateSelection(rect);
    }

    /**
     *
     * @param rect
     *
     * @throws ZSelectionNoSupported
     */
    public void validateSelection(ZRect rect) throws ZSelectionNoSupported {
        for (int i = 0; i < parentCellsList.size(); i++) {
            ZRect pr = ((ZCell) parentCellsList.get(i)).getSpan();

            if (rect.isIntersects(pr) && !rect.contain(pr)) {
                throw new ZSelectionNoSupported();
            }
        }
    }

    /**
     * put your documentation comment here
     */
    private void init() {
        cellsTable = new Vector(maxElement * maxElement);
        rowsTable = new Vector(maxElement);
        colsTable = new Vector(maxElement);


        // vector of cells,rows, columns,parent cells
        cellsList = new Vector();
        rowsList = new Vector();
        colsList = new Vector();
        parentCellsList = new Vector();
        printingCells = new ZRect();

        defaultBase = new ZBase(this);
        defCell = new ZDefaultCell(this);
        defRow = new ZRow(this);
        defCol = new ZCol(this);
        title = "Untitled Sheet";


        // create fixedcell
        defHeadCell = new ZDefaultCell(this);
        defHeadCell.setBackBrush(new ZColorBrush(192, 200, 192));
        defHeadCell.setHorzAlign(ZBase.XCENTER);
        defHeadCell.setVertAlign(ZBase.YCENTER);
        defHeadCell.setProperty(ZBase.PTY_BaseAll);


        //
        //
        defaultBase.setProperty(ZBase.PTY_All);
        cellsTable.setSize(maxElement * maxElement);
        rowsTable.setSize(maxElement);
        colsTable.setSize(maxElement);

        ZCol leftCol = new ZCol(this, 0);
        leftCol.setWidth(30);
        add(leftCol);
        listeners = new Vector();
    }

    /**
     * Pastes plain text into some rect
     * @param selection
     * @param rect
     */
    private void pasteTo(Transferable selection, ZRect rect) {
        // Don't automatically add the following operations to undo list.
        // Each of them composes a group command.
        boolean autoUndo = ZCmdFormat.getAutoUndo();

        try {
            String text = (String) selection.getTransferData(DataFlavor.stringFlavor);

            // Creates a ZCmdGroup used for holding every text edit(ZCmdFormat)    .
            ZCmdGroup cmdGroup = new ZCmdGroup(this, rect, 0);

            ZCmdFormat.setAutoUndo(false);

            ZRect target = (ZRect) rect.clone();
            target.right = target.left;
            target.bottom = target.top;

            StringTokenizer st1 = new StringTokenizer(text, "\n");

            for (int i = 0; st1.hasMoreTokens(); i++) {
                String rowstring = st1.nextToken();
                StringTokenizer st2 = new StringTokenizer(rowstring, "\t");

                for (int j = 0; st2.hasMoreTokens(); j++) {
                    String value = (String) st2.nextToken();

                    if (((rect.top + i) <= this.getLastRow()) && ((rect.left + j) <= this.getLastCol()) &&
                            !getCell(rect.top + i, rect.left + j).isChild()) {
                        ZCmdFormat cmd = setCellText(rect.top + i, rect.left + j, value);
                        cmdGroup.add(cmd);
                        target.right = Math.max(target.left + j, target.right);
                        target.bottom = Math.max(target.top + i, target.bottom);
                    }
                }
            }

            cmdGroup.setRect(target);
        } catch (Exception ex) {
            ex.printStackTrace();
        }


        // From now on,allows automatically undo journal.
        ZCmdFormat.setAutoUndo(autoUndo);
    }

    /**
     * put your documentation comment here
     * @param ois
     * @exception ClassNotFoundException, IOException
     */
    private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
        maxElement = ois.readInt();
        lastRow = ois.readInt();
        lastCol = ois.readInt();
        init();
        title = (String) ois.readObject();

        int objCount = ois.readInt();
        ObjectInputStream2 ois2 = (ObjectInputStream2) ois;
        ois2.sheet = this;

        for (int i = 0; i < objCount; i++) {
            ZBase obj = (ZBase) ois2.readObject();
        }

        setBook(ois2.book);
    }

    /**
     * put your documentation comment here
     * @param oos
     * @exception IOException
     */
    private void writeObject(ObjectOutputStream oos) throws IOException {
        oos.writeInt(maxElement);
        oos.writeInt(lastRow);
        oos.writeInt(lastCol);
        oos.writeObject(title);

        Vector objList = (Vector) cellsList.clone();
        objList.addAll(rowsList);
        objList.addAll(colsList);

        int objCount = objList.size();


        // write count of obj colleciton;
        oos.writeInt(objCount);

        for (int i = 0; i < objCount; i++)
            oos.writeObject(objList.get(i));
    }

    /**
     * DOCUMENT ME!
     *
     * @version 1.00
     * @author W.John
     */
    class _CellsEnum implements Enumeration {
        Enumeration enum;
        ZRect rect;
        ZDefaultCell cell;

        /**
         * put your documentation comment here
         * @param         ZRect rect
         */
        public _CellsEnum(ZRect rect) {
            enum = cellsList.elements();
            this.rect = rect;
        }

        /**
         * put your documentation comment here
         * @return
         */
        public boolean hasMoreElements() {
            while (enum.hasMoreElements()) {
                cell = (ZDefaultCell) enum.nextElement();

                if ((rect.left <= cell.getCol()) && (rect.right >= cell.getCol()) && (rect.top <= cell.getRow()) &&
                        (rect.bottom >= cell.getRow())) {
                    return true;
                }
            }

         

⌨️ 快捷键说明

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