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

📄 zcmdformat.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.Point;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.swing.undo.AbstractUndoableEdit;


/**
 * DOCUMENT ME!
 *
 * @version 1.00
 * @author W.John
 */
class ZCmdFormat extends AbstractUndoableEdit implements ZCommand {
    static public ZUndoManager undoManager = new ZUndoManager();
    static boolean autoUndo = true;
    private boolean autoSelect;
    protected ZArchive clip;
    protected int property;
    protected ZDefaultSheet sheet;
    protected ZRect effectCells;
    protected ZCmdWorker worker;
    private Object valueObject;

    // viewer context
    private ZRect selections;
    private ZRect visibleCells;
    private Point focusedCell;

    // viewer context
    private ZRect selections1;
    private ZRect visibleCells1;
    private Point focusedCell1;
    private boolean firstDo;
    private Hashtable table;
    private int selectionMode = ZCommand.SELECT_010;

    public ZCmdFormat(ZDefaultSheet sheet, ZRect effectCells, int property, Object valueObject, ZCmdWorker worker) {
        this.sheet = sheet;
        this.effectCells = effectCells;
        this.property = property;
        this.valueObject = valueObject;
        this.worker = worker;
        clip = new ZArchive(sheet);
        save();

        if (autoUndo) {
            undoManager.addCommand(this);
        }

        firstDo = true;
    }

    /**
    * put your documentation comment here
    * @return
    */
    public boolean isAutoSelect() {
        return autoSelect;
    }

    /**
    * put your documentation comment here
    * @return
    */
    public boolean isAutoUndo() {
        return autoUndo;
    }

    /**
    *
    * @return
    */
    public ZRect getCells() {
        return effectCells;
    }

    /**
    * put your documentation comment here
    * @return
    */
    public ZArchive getClip() {
        return clip;
    }

    /**
    * put your documentation comment here
    * @param cmd
    */
    /**
    * put your documentation comment here
    * @param effectCells
    */
    /**
    * put your documentation comment here
    * @return
    */
    public ZRect getEffectCells() {
        return effectCells;
    }

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

    /**
    *
    * @return
    */
    public int getSelectMode() {
        return selectionMode;
    }

    /**
    *
    * @param mode
    */
    public void setSelectionMode(int mode) {
        selectionMode = mode;
    }

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

    /**
    * put your documentation comment here
    * @return
    */
    public Object getValueObject() {
        return valueObject;
    }

    /**
    * put your documentation comment here
    * @param worker
    */
    /**
    * put your documentation comment here
    * @return
    */
    public ZCmdWorker getWorker() {
        return null; //worker;
    }

    /**
    */
    public void commit() {
        redo();
        sheet.setModified(true);
        firstDo = false;
    }

    /**
    *
    * @param key
    *
    * @return
    */
    public Object get(String key) {
        if (table == null) {
            return null;
        } else {
            return table.get(key);
        }
    }

    /**
    *
    * @param key
    * @param value
    */
    public void put(String key, Object value) {
        if (table == null) {
            table = new Hashtable();
        }

        table.put(key, value);
    }

    /**
    * put your documentation comment here
    */
    public void redo() {
        if (!firstDo) {
            super.redo();
        }

        int r;
        int c;
        ZDefaultCell cell;
        ZRow row;
        ZCol col;
        Enumeration enum;

        //  bool needUndo = ((CEZCellApp *) AfxGetApp())->book()->needUndo();
        //  ((CEZCellApp *) AfxGetApp())->book()->setNeedUndo(false);
        switch (effectCells.type(sheet)) {
        case ZRect.CELLS:

            //make cells in the area, and set property one by one
            if (worker.needWork(ZCmdWorker.ACCEPT_CELL)) {
                for (r = effectCells.top; r <= effectCells.bottom; r++)
                    for (c = effectCells.left; c <= effectCells.right; c++) {
                        cell = sheet.makeCell(r, c);

                        // set property of the cell
                        if (((property & (ZBase.PTY_MergeAsChild | ZBase.PTY_MergeAsParent)) != 0) || !cell.isChild()) {
                            worker.workOnObject(this, cell);
                        }
                    }
            }

            break;

        case ZRect.ROWS:
        {
            // make rows in the area, and set property one by one
            if (worker.needWork(ZCmdWorker.ACCEPT_ROW)) {
                for (r = effectCells.top; r <= effectCells.bottom; r++) {
                    row = sheet.makeRow(r);


                    // set property of the cell
                    worker.workOnObject(this, row);
                }
            }

            // if cell in the row ,and its property exists ,then change it
            if (worker.needWork(ZCmdWorker.ACCEPT_CELL)) {
                enum = sheet.getCellsEnum(effectCells);

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

                    if (cell.hasProperty(property)) {
                        worker.workOnObject(this, cell);
                    }
                }
            }
        }

        break;

        case ZRect.COLUMNS:
        {
            if (worker.needWork(ZCmdWorker.ACCEPT_COLUMN)) {
                for (c = effectCells.left; c <= effectCells.right; c++) {
                    col = sheet.makeCol(c);


                    // set property of the cell
                    worker.workOnObject(this, col);
                }
            }

            // if cell in the row ,and its property exists ,then change it
            if (worker.needWork(ZCmdWorker.ACCEPT_CELL)) {
                enum = sheet.getRowsEnum(effectCells);

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

                    if (row.hasProperty(property)) // if the row has the property , please create all cells in it
                    {
                        for (c = effectCells.left; c <= effectCells.right; c++)
                            sheet.makeCell(row.getRow(), c);

                        // here we needn't work on it,it will be dont next step
                    }
                }

                enum = sheet.getCellsEnum(effectCells);

                while (enum.hasMoreElements()) {
                    cell = (ZDefaultCell) enum.nextElement();
                    worker.workOnObject(this, cell);
                }
            }
        }

        break;
        }

        sheet.firePropertyChanged(this, false);
    }

    /**
    * put your documentation comment here
    */
    public void undo() {
        super.undo();

        try {
            if (worker != null) {
                worker.prepareUndo(this);
            }

            clip.createInputStream();

            while (true)
                clip.in.readObject();
        } catch (Exception e) {
        }

        sheet.firePropertyChanged(this, true);
    }

    /**
    * put your documentation comment here
    */
    protected void restoreViewerContext() {
        // sheet.canvas.setSelection(selections);
        //sheet.canvas.setVisibleCells(visibleCells);
        // sheet.canvas.setFocusedCell(focusedCell);
    }

    /**
    * put your documentation comment here
    */
    protected void saveViewerContext() {
        //selections = new ZRect(sheet.canvas.getSelection());
        //visibleCells = new ZRect(sheet.canvas.getVisibleCells());
        //focusedCell = new Point(sheet.canvas.getFocusedCell());
    }

    /**
    * put your documentation comment here
    * @param au
    */
    static void setAutoUndo(boolean au) {
        autoUndo = au;
    }

    /**
    * put your documentation comment here
    * @return
    */
    static boolean getAutoUndo() {
        return autoUndo;
    }

    /**
    *    when the command object constructed, take a snapshot of the sheet
    *    if cells selected ,
    *       if exist cells , save it
    *       else    create a erasor of it
    *    if rows selected
    *       for all rows in the selection
    *           if exist save it
    *           else ,create a erasor of it
    *       for all cells in the selection
    *           if has this property save it
    *           else ,do nothing ,because in this case ,the cell will not be changed anyway
    *    if cols selected
    *       for all columns in the selection
    *           if exist ,save it
    *           else, create a erasor of it
    *       for all cells that interacted between customed rows and the selected columns
    *           if not exist create a erasor of it
    *       for all cells in the selection
    *           if has this property save it
    *           else ,do nothing!
    *
    */
    void save() {
        try {
            saveViewerContext();

            int r;
            int c;
            ZDefaultCell cell;
            ZRow row;
            ZCol col;
            Enumeration enum;

            switch (effectCells.type(sheet)) {
            case ZRect.CELLS:

                if (worker.needSave(ZCmdWorker.ACCEPT_CELL)) {
                    for (r = effectCells.top; r <= effectCells.bottom; r++)
                        for (c = effectCells.left; c <= effectCells.right; c++) {
                            cell = sheet.getExistCell(r, c);

                            if (cell != null) {
                                clip.out.writeObject(cell);
                            } else {
                                clip.out.writeObject(new ZCellErasor(r, c));
                            }
                        }
                }

                break;

            case ZRect.ROWS:
            {
                if (worker.needSave(ZCmdWorker.ACCEPT_ROW)) {
                    for (r = effectCells.top; r <= effectCells.bottom; r++) {
                        if ((row = sheet.getExistRow(r)) != null) {
                            clip.out.writeObject(row);
                        } else {
                            clip.out.writeObject(new ZRowErasor(r));
                        }
                    }
                }

                if (worker.needSave(ZCmdWorker.ACCEPT_CELL)) {
                    enum = sheet.getCellsEnum(effectCells);

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


                        //    if (cell.hasProperty(property))
                        clip.out.writeObject(cell);
                    }
                }
            }

            break;

            case ZRect.COLUMNS:
            {
                if (worker.needSave(ZCmdWorker.ACCEPT_COLUMN)) {
                    for (c = effectCells.left; c <= effectCells.right; c++) {
                        if ((col = sheet.getExistCol(c)) != null) {
                            clip.out.writeObject(col);
                        } else {
                            clip.out.writeObject(new ZColErasor(c));
                        }
                    }
                }

                // if cell in the row ,and its property exists ,then change it
                if (worker.needSave(ZCmdWorker.ACCEPT_CELL)) {
                    enum = sheet.getRowsEnum(effectCells);

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

                        // if the row has the property , please create all cells in it
                        if (row.hasProperty(property)) {
                            for (c = effectCells.left; c <= effectCells.right; c++) {
                                cell = sheet.getExistCell(row.getRow(), c);

                                // this cell will be created when call "redo", so when undo,it must be erased
                                if (cell == null) {
                                    clip.out.writeObject(new ZCellErasor(row.getRow(), c));
                                }
                            }
                        }
                    }

                    enum = sheet.getCellsEnum(effectCells);

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


                        //if (cell.hasProperty(property))
                        clip.out.writeObject(cell);
                    }
                }
            }

            break;
            }

            clip.flush();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}

⌨️ 快捷键说明

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