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

📄 zstatecontrol.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.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Point;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Date;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.KeyStroke;
import javax.swing.undo.CannotUndoException;


/**
 * DOCUMENT ME!
 *
 * @version 1.00
 * @author W.John
 */
class ZSheetStateControl implements FocusListener, AdjustmentListener, DragSourceListener, DragGestureListener,
                                    DropTargetListener, MouseMotionListener, MouseListener, KeyListener, ComponentListener,
                                    ZEditorListener, ZScrollListener {
    // commands available
    final static int STATE_IDLE = 0;
    final static int STATE_SELECTING = 1;
    final static int STATE_RESIZING = 2;
    final static int SUBSTATE_IDLE = 0;
    final static int SUBSTATE_ROW_RESIZE_READY = 1;
    final static int SUBSTATE_COLUMN_RESIZE_READY = 2;
    final static int SUBSTATE_DRAG_READY = 3;
    final static int SUBSTATE_ROW_RESIZING = 4;
    final static int SUBSTATE_COLUMN_RESIZING = 5;
    final static int SUBSTATE_ROW_SELECTED = 6;
    final static int SUBSTATE_COLUMN_SELECTED = 7;
    final static int SUBSTATE_CELL_SELECTED = 8;
    private static Object dragSource;
    final static int EXPAND_SELECTION_UP = 0;
    final static int EXPAND_SELECTION_DOWN = 1;
    final static int EXPAND_SELECTION_LEFT = 2;
    final static int EXPAND_SELECTION_RIGHT = 3;
    final static int MOVE_FOCUS_IN_SELECTION_UP = 4;
    final static int MOVE_FOCUS_IN_SELECTION_DOWN = 5;
    final static int MOVE_FOCUS_IN_SELECTION_LEFT = 6;
    final static int MOVE_FOCUS_IN_SELECTION_RIGHT = 7;
    final static int MOVE_FOCUS_WITH_SELECTION_UP = 8;
    final static int MOVE_FOCUS_WITH_SELECTION_DOWN = 9;
    final static int MOVE_FOCUS_WITH_SELECTION_LEFT = 10;
    final static int MOVE_FOCUS_WITH_SELECTION_RIGHT = 11;
    final static int COPY = 12;
    final static int CUT = 13;
    final static int PASTE = 14;
    final static int CLEAR = 15;
    final static int UNDO = 16;
    final static int REDO = 17;
    protected ZSheetState modal;

    ///////////////////////////////
    //   private ZSheet sheet;
    protected _inputInfo info = new _inputInfo();
    private ZShape.HLine vertResizeBar;
    private ZShape.HLine horzResizeBar;
    private ZRect dropTarget = new ZRect();
    private ZRect dropHelper;
    private ZShape.Rectangle dragger;
    protected ZPen hiLinePen;
    protected ZEditor editor = new ZEditor();
    KeyStroke[] keyStrokes = {
        //EXPAND_SELECTION
        KeyStroke.getKeyStroke(KeyEvent.VK_UP, KeyEvent.SHIFT_MASK ), //UP
        KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, KeyEvent.SHIFT_MASK), //DOWN
        KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, KeyEvent.SHIFT_MASK), //LEFT
        KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, KeyEvent.SHIFT_MASK), //RIGHT

        // MOVE_FOCUS_IN_SELECTION
        null, //UP
        KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), //DOWN
        null, // LEFT
        null, // RIGHT

        //MOVE_FOCUS_WITH_SELECTION
        KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), //UP
        KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), //DOWN
        KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), //LEFT
        KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), //RIGHT
        KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK), //COPY
        KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK), //CUT
        KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_MASK), //PASTE
        KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), //CLEAR
        KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_MASK), //UNDO
        KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_MASK)
    };

    ZSheetStateControl(ZSheetState modal) {
        setModal(modal);
        editor.addListener(this);
    }

    ZSheetStateControl() {
    }

    /**
     *
     * @param event
     *
     * @return
     */
    public boolean isDragAcceptable(DropTargetDragEvent event) { // usually, you check the available data flavors here

        // in this program, we accept all flavors
        if (modal == null) {
            return false;
        }

        return (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0;
    }

    /**
     *
     * @param event
     *
     * @return
     */
    public boolean isDropAcceptable(DropTargetDropEvent event) { // usually, you check the available data flavors here

        if (modal == null) {
            return false;
        }

        // in this program, we accept all flavors
        return (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0;
    }

    /**
     *
     * @param p
     *
     * @return
     *
     * @throws ZException
     */
    public ZCellID getNearestSelectedCell(Point p) throws ZException {
        ZRect selection = modal.getSelection();
        ZCellID cid = modal.hitCell(p);

        if (selection.contain(cid)) {
            return cid;
        }

        if (cid.col == (selection.left - 1)) {
            cid.col++;
        } else if (cid.col == (selection.right + 1)) {
            cid.col--;
        }

        if (cid.row == (selection.top - 1)) {
            cid.row++;
        } else if (cid.row == (selection.bottom + 1)) {
            cid.row--;
        }

        //ifelse
        //  throw  new Exception("not exist nearsest cell!");
        return cid;
    }

    /**
     *
     * @param e
     */
    public void adjustmentValueChanged(AdjustmentEvent e) {
        ZRect visibleCells = (ZRect) modal.getVisibleCells().clone();

        if (((JScrollBar) e.getSource()).getOrientation() == JScrollBar.VERTICAL) {
            visibleCells.top = e.getValue();
        } else {
            visibleCells.left = e.getValue();
        }

        modal.setVisibleCells(visibleCells);
    }

    /**
     *
     * @param componentEvent
     */
    public void componentHidden(ComponentEvent componentEvent) {
    }

    /**
     *
     * @param componentEvent
     */
    public void componentMoved(ComponentEvent componentEvent) {
    }

    /**
     *
     * @param e
     */
    public void componentResized(ComponentEvent e) {
        if (modal == null) {
            return;
        }

        Dimension d = e.getComponent().getSize();
        modal.setClip(new ZRect(0, 0, d.width, d.height));
    }

    /**
     *
     * @param componentEvent
     */
    public void componentShown(ComponentEvent componentEvent) {
    }

    /**
     *
     * @param event
     */
    public void dragDropEnd(DragSourceDropEvent event) {
        if (event.getDropSuccess()) {
            try {
                if ((event.getDropAction() == DnDConstants.ACTION_MOVE) && (dragSource != null)) {
                    modal.getSheet().clearSelection(modal.getSelection());
                }
            } catch (Exception ex) {
            }

            dragSource = null;
        }
    }

    /**
     *
     * @param event
     */
    public void dragEnter(DragSourceDragEvent event) {
    }

    /**
     *
     * @param event
     */
    public void dragEnter(DropTargetDragEvent event) {
        if (!isDragAcceptable(event)) {
            event.rejectDrag();

            return;
        }

        Transferable selection = ZClipboard.getSystem().getContents(this);

        try {
            dropHelper = (ZRect) selection.getTransferData(ZSerializableSelection.serializableFlavor);

            if ((dropHelper.left == -1) && (dropHelper.right == -1)) {
                dropTarget.left = 0;
                dropTarget.right = modal.getSheet().getLastCol();
            } else if ((dropHelper.top == -1) && (dropHelper.bottom == -1)) {
                dropTarget.top = 0;
                dropTarget.bottom = modal.getSheet().getLastRow();
            } else {
                dropTarget.setRect(0, 0, 0, 0);
            }

            ZCellID cid = modal.hitCell(modal.dp2lp(event.getLocation()));

            if (dropTarget.type(modal.getSheet()) != ZRect.COLUMNS) {
                dropTarget.top = dropHelper.top + cid.row;
                dropTarget.bottom = dropHelper.bottom + cid.row;
            }

            if (dropTarget.type(modal.getSheet()) != ZRect.ROWS) {
                dropTarget.left = dropHelper.left + cid.col;
                dropTarget.right = dropHelper.right + cid.col;
            }

            /// add drag tracker
            ZRect visibleDragger = (ZRect) modal.getVisibleCells().clone();

            visibleDragger.intersection(dropTarget);

            // get draw rect of clip cells;
            ZRect frame = modal.getCellsRect(visibleDragger);
            dragger = new ZShape.Rectangle(frame.left, frame.top, frame.right, frame.bottom, -100);
            dragger.pen = hiLinePen;

            modal.addShape(dragger);
            info.startRow = cid.row;
            info.startCol = cid.col;
        } catch (Exception e) {
            e.printStackTrace();
            event.rejectDrag();
        }
    }

    /**
     *
     * @param event
     */
    public void dragExit(DragSourceEvent event) {
    }

    /**
     *
     * @param event
     */
    public void dragExit(DropTargetEvent event) {
        if (dragger == null) {
            return;
        }

        modal.killShape(dragger);
        dragger = null;
    }

    /**
     *
     * @param event
     */
    public void dragGestureRecognized(DragGestureEvent event) {
        if (info.subState == SUBSTATE_DRAG_READY) {
            try {
                ZRect dh = (ZRect) modal.getSelection().clone();
                ZCellID cid = getNearestSelectedCell(modal.dp2lp(event.getDragOrigin()));

                if (dh.type(modal.getSheet()) == ZRect.COLUMNS) {
                    dh.top = -1; // top == bottom == -1 ,
                    dh.bottom = -1;
                    dh.left -= cid.col;
                    dh.right -= cid.col;
                } else if (dh.type(modal.getSheet()) == ZRect.ROWS) {
                    dh.top -= cid.row;
                    dh.bottom -= cid.row;
                    dh.left = -1;
                    dh.right = -1;
                } else {
                    dh.top -= cid.row;
                    dh.bottom -= cid.row;
                    dh.left -= cid.col;
                    dh.right -= cid.col;
                }

                ZClipboard.getSystem().setContents(new ZSerializableSelection(dh), null);

                Transferable tf = modal.getSheet().getSelectionTransferable(modal.getSelection(), dh);
                event.startDrag(null, tf, this);
                dragSource = event.getComponent();
            } catch (Exception ex) {
            }
        }
    }

    /**
     *
     * @param event

⌨️ 快捷键说明

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