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

📄 zsheetstate.java

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

import java.awt.Cursor;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;
import javax.swing.event.ChangeEvent;


/**
 * put your documentation comment here
 */
class ZSheetState implements ZSheetListener {
    final static int HIT_SELECTION_FRAME = -1;
    final static int HIT_TOP_BREAK = -2;
    final static int HIT_LEFT_BREAK = -3;
    final static int HIT_CELL = -4;
    final static int HIT_DRAG_TRACKER = -5;
    final static int HIT_UNKNOWN = -100;
    final static int DEFAULT_CURSOR = 0;
    final static int RESIZE_ROW_CURSOR = 1;
    final static int RESIZE_COL_CURSOR = 2;
    final static int MOVE_CURSOR = 3;
    final static int UP = 0;
    final static int DOWN = 1;
    final static int LEFT = 2;
    final static int RIGHT = 3;

    //temp
    static ZShape.Cell tmpCell = new ZShape.Cell(new ZRect(), 0, 0);
    static _RowComparator rowComparator = new _RowComparator();
    static _ColComparator colComparator = new _ColComparator();
    static int lastShape = 100;
    private ZSheet sheet;
    private ZDefaultUI ui;
    private Vector listeners = new Vector();
    private Vector alisteners = new Vector();
    private ZRect selection;
    private ZCellID focus;
    private ZRect visibleCells;
    private ZRect canVisibleCells;
    int resizableBreakSize;

    // transform
    float scale = 1.0f;
    Point offset = new Point(0, 0);
    AffineTransform af;
    Vector hilines = new Vector();
    ZEditor editor = new ZEditor();

    // layout table
    ArrayList rows = new ArrayList(); //ZShape.Row[1024];
    ArrayList cols = new ArrayList(); //new ZShape.Col[1024];
    boolean tableDirty = true;
    ZShape.Rectangle dragTracker = new ZShape.Rectangle(ZSheetState.HIT_DRAG_TRACKER);
    ZRect clip = new ZRect(0, 0, 500, 100);
    Cursor[] cursors = new Cursor[5];
    int cursor = -1;
    private boolean leftHeadVisible;
    private boolean topHeadVisible;

    /// can hit detection shapes
    Vector shapes = new Vector();
    Vector breaks = new Vector();

    //
    ZRect dirtyArea;
    boolean editable = true;
    ZCellID focusFrom = new ZCellID(1, 1);

    /**
     * put your documentation comment here
     * @param     ZSheet sheet
     */
    ZSheetState(ZSheet sheet) {
        init(sheet);
    }

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

        int ri;
        int ci;
        ri = (cell.getRow() == 0) ? 0 : cell.getRow() - visibleCells.top + 1;
        ci = (cell.getCol() == 0) ? 0 : cell.getCol() - visibleCells.left + 1;

        if ((ri < 0) || (ri > (visibleCells.getHeight() + 1)) || (ci < 0) || (ci > (visibleCells.getWidth() + 1))) {
            throw new ZException(cell + "visible cells not contained it!");
        }

        ZShape.Row row = (ZShape.Row) rows.get(ri);
        ZShape.Col col = (ZShape.Col) cols.get(ci);

        return new ZRect(col.left, row.top, col.right, row.bottom);
    }

    /**
     * put your documentation comment here
     * @param cells
     * @return
     */
    public ZRect getCellsRect(ZRect cells) {
        ZRect tl = null;
        ZRect br = null;
        int ri;
        int ci;

        try {
            tl = getCellRect(sheet.getCell(cells.top, cells.left));
        } catch (Exception ex) {
        }

        try {
            br = getCellRect(sheet.getCell(cells.bottom, cells.right));
        } catch (Exception ex) {
        }

        if ((tl != null) && (br != null)) {
            return new ZRect(tl.left, tl.top, br.right, br.bottom);
        } else if (tl != null) {
            for (ri = cells.top + 1; ri <= cells.bottom; ri++)
                tl.bottom += sheet.getRowHeight(ri);

            for (ci = cells.left + 1; ci <= cells.right; ci++)
                tl.right += sheet.getColWidth(ci);

            return tl;
        } else if (br != null) {
            for (ri = cells.bottom - 1; ri >= cells.top; ri--)
                br.top -= sheet.getRowHeight(ri);

            for (ci = cells.right - 1; ci >= cells.left; ci--)
                br.left -= sheet.getColWidth(ci);

            return br;
        }

        // case br == null and tl == null
        int i;
        ZRect loc = null;

        try {
            loc = getCellRect(sheet.getCell(0, 0));
            loc.left = loc.right;
            loc.top = loc.bottom;

            if (cells.left > visibleCells.left) {
                for (i = visibleCells.left; i < cells.left; i++)
                    loc.left += sheet.getColWidth(i);
            } else if (cells.left < visibleCells.left) {
                for (i = cells.left; i <= visibleCells.left; i++)
                    loc.left -= sheet.getColWidth(i);
            }

            loc.right = loc.left;

            for (i = cells.left; i <= cells.right; i++)
                loc.right += sheet.getColWidth(i);

            if (cells.top > visibleCells.top) {
                for (i = visibleCells.top; i < cells.top; i++)
                    loc.top += sheet.getRowHeight(i);
            } else if (cells.top < visibleCells.top) {
                for (i = cells.top; i <= visibleCells.top; i++)
                    loc.top -= sheet.getRowHeight(i);
            }

            loc.bottom = loc.top;

            for (i = cells.top; i <= cells.bottom; i++)
                loc.bottom += sheet.getRowHeight(i);
        } catch (Exception ex) {
        }

        return loc;
    }

    /**
     * put your documentation comment here
     * @param clip
     */
    public void setClip(ZRect clip) {
        if (this.clip.equals(clip)) {
            return;
        }

        this.clip = clip;
        tableDirty = true;
        fireUIStateChanged();
    }

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

    /**
     * put your documentation comment here
     * @param index
     */
    public void setCursor(int index) {
        if (cursor == index) {
            return;
        }

        cursor = index;

        for (int i = 0; i < listeners.size(); i++) {
            ZSheetStateListener lst = (ZSheetStateListener) listeners.get(i);
            lst.cursorChanged(this);
        }
    }

    /**
     * put your documentation comment here
     * @param index
     * @return
     */
    public Cursor getCursor(int index) {
        return cursors[index];
    }

    /**
     * put your documentation comment here
     * @return
     */
    public Cursor getCursor() {
        return getCursor(cursor);
    }

    /**
     * put your documentation comment here
     * @return
     */
    public ZShape.Rectangle getDragTracker() {
        return dragTracker;
    }

    /**
     * put your documentation comment here
     * @param editable
     */
    public void setEditable(boolean editable) {
        this.editable = editable;
    }

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

    /**
     * put your documentation comment here
     * @return
     */
    public ZEditor getEditor() {
        return editor;
    }

    /**
     * put your documentation comment here
     * @param focus
     */
    public void setFocus(ZCellID focus) {
        if (this.focus.equal(focus)) {
            return;
        }

        invalidate(new ZRect(this.focus.col, this.focus.row, this.focus.col, this.focus.row));
        invalidate(new ZRect(focus.col, focus.row, focus.col, focus.row));

        ZCell cell = sheet.getCell(focus.row, focus.col);

        try {
            if (cell.isChild()) {
                cell = cell.getParent();
                this.focus.row = cell.getRow();
                this.focus.col = cell.getCol();
            } else {
                this.focus = (ZCellID) focus.clone();
            }

            fireUIStateChanged();
        } catch (Exception ex) {
        }

        focusFrom = (ZCellID) focus.clone();
        fireActiveCellChanged();
    }

    /**
     * put your documentation comment here
     * @return
     */
    public ZCellID getFocus() {
        return focus;
    }

    /**
     * put your documentation comment here
     * @return
     */
    public ZCell getFocusCell() {
        return sheet.getCell(focus.row, focus.col);
    }

    /**
     * put your documentation comment here
     * @return
     */
    public synchronized ZRect getInvalidate() {
        if (dirtyArea != null) {
            dirtyArea.intersection(visibleCells);
            dirtyArea.grow(1, 1);
        }

        return dirtyArea;
    }

    /**
     * put your documentation comment here
     * @param x
     * @param y
     */
    public void setOffset(int x, int y) {
        if ((offset.x == x) && (offset.y == y)) {
            return;
        }

        offset = new Point(x, y);
        buildTransform();
        fireUIStateChanged();
    }

    /**
     * put your documentation comment here
     * @return
     */
    public Point getOffset() {
        return offset;
    }

    /**
     * put your documentation comment here
     * @param scale
     */
    public void setScale(float scale) {
        if (this.scale == scale) {
            return;
        }

        this.scale = scale;
        buildTransform();
        fireUIStateChanged();
    }

    /**
     * put your documentation comment here
     * @return
     */
    public float getScale() {
        return scale;
    }

    /**
     * put your documentation comment here
     * @param selection
     */
    public void setSelection(ZRect selection) {
        if (this.selection.equals(selection)) {
            return;
        }

        invalidate(this.selection);

        if (selection.type(sheet) == ZRect.CELLS) {
            this.selection = sheet.getMerged(selection);
        } else {
            this.selection = (ZRect) selection.clone();
        }

        invalidate(this.selection);
        fireUIStateChanged();
    }

    /**
     *
     * @return
     */
    public ZRect getSelection() {
        return selection;
    }

    /**
     * put your documentation comment here
     * @param sheet
     */
    public void setSheet(ZSheet sheet) {
        init(sheet);
    }

    /**
     *
     * @return
     */
    public ZSheet getSheet() {
        return sheet;
    }

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

    /**
     * put your documentation comment here
     * @return
     */
    public AffineTransform getTransform() {
        return af;
    }

    /**
     * put your documentation comment here
     * @param ui
     */
    public void setUI(ZDefaultUI ui) {
        this.ui = ui;
        updateUI(ui);
        fireUIChnaged();
    }

    /**
     * put your documentation comment here
     * @return
     */
    public ZDefaultUI getUI() {
        return ui;
    }

    /**
     * put your documentation comment here
     * @param visibleCells
     */
    public void setVisibleCells(ZRect visibleCells) {
        if (this.visibleCells.equals(visibleCells)) {
            return;
        }

        this.visibleCells = visibleCells;
        this.visibleCells.intersection(canVisibleCells);
        tableDirty = true;
        dirtyArea = null;
        fireUIStateChanged();
    }

    /**
     * put your documentation comment here
     * @return
     */
    public ZRect getVisibleCells() {
        return visibleCells;
    }

    /**
     * put your documentation comment here
     * @param editor
     */
    public void addEditor(ZEditor editor) {
        if (!editable) {
            return;
        }

        editor.setModal(this);

        for (int i = 0; i < listeners.size(); i++) {
            ZSheetStateListener lst = (ZSheetStateListener) listeners.get(i);
            lst.editorAdded(editor);
        }
    }

⌨️ 快捷键说明

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