📄 zprintpreview.java
字号:
/*
* Copyright 2002 EZCell , Inc. All rights reserved.
* Version 1.0.
* Author W.John
*/
package ezcell;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragSource;
import java.awt.dnd.DropTarget;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.border.MatteBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
* DOCUMENT ME!
*
* @version 1.00
* @author W.John
*/
class ZPrintPreview extends JScrollPane implements ChangeListener {
static final int MINIUM_SPACE = 200;
// components
private _PageCanvas pagePane;
// document ;
private ZDocument doc;
ZPrintPreviewControl control;
Vector listeners = new Vector();
private ArrayList marginers = new ArrayList();
private ArrayList marginLines = new ArrayList();
boolean marginersDirty = true;
boolean marginable = true;
private ZRect header;
private ZRect body;
private ZRect footer;
ZPrintPreview(ZDocument doc, int edit) {
super(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// mvc
this.doc = doc;
doc.addListener(this);
init(edit);
}
/**
* DOCUMENT ME!
*
* @param enable
*/
public void setMarginable(boolean enable) {
if (marginable == enable) {
return;
}
marginable = enable;
pagePane.repaint();
}
/**
* DOCUMENT ME!
*
* @return
*/
public boolean getMarginable() {
return marginable;
}
/**
* DOCUMENT ME!
*
* @return
*/
public ZSheetState getModal() {
return control.getModal();
}
/**
* DOCUMENT ME!
*
* @param shape
*/
public void add(ZShape shape) {
shape.paint((Graphics2D) pagePane.getGraphics());
}
/**
* DOCUMENT ME!
*
* @param lst
*/
public void addListener(ChangeListener lst) {
listeners.add(lst);
}
/**
* DOCUMENT ME!
*/
public void editFooter() {
if (control.getModal() == doc.getFooterModal()) {
return;
}
control.setModal(doc.getFooterModal());
fireStateChanged();
}
/**
* DOCUMENT ME!
*/
public void editHeader() {
if (control.getModal() == doc.getHeaderModal()) {
return;
}
control.setModal(doc.getHeaderModal());
fireStateChanged();
}
/**
* DOCUMENT ME!
*/
public void fireStateChanged() {
ChangeEvent e = new ChangeEvent(this);
for (int i = 0; i < listeners.size(); i++) {
ChangeListener lst = (ChangeListener) listeners.get(i);
lst.stateChanged(e);
}
}
/**
* DOCUMENT ME!
*
* @param p
*
* @return
*/
public ZShape hit(Point p) {
for (int i = 0; i < marginers.size(); i++) {
ZShape sp = (ZShape) marginers.get(i);
if (sp.hit(p) != null) {
return sp;
}
}
return null;
}
/**
* DOCUMENT ME!
*
* @param shape
*/
public void kill(ZShape shape) {
shape.erase((Graphics2D) pagePane.getGraphics());
}
/**
* DOCUMENT ME!
*
* @param shape
* @param to
*/
public void move(ZShape shape, Point to) {
shape.erase((Graphics2D) pagePane.getGraphics());
shape.moveTo(to.x, to.y);
shape.paint((Graphics2D) pagePane.getGraphics());
}
/**
* DOCUMENT ME!
*
* @param lst
*/
public void removeListener(ChangeListener lst) {
listeners.remove(lst);
}
/**
* DOCUMENT ME!
*
* @param changeEvent
*/
public void stateChanged(ChangeEvent changeEvent) {
marginersDirty = true;
updateLayout();
}
/**
* DOCUMENT ME!
*/
public void updateLayout() {
_PageContainer pc = (_PageContainer) getViewport().getComponent(0);
pc.revalidate();
}
/**
* DOCUMENT ME!
*
* @param edit
*/
void init(int edit) {
//
_PageContainer pageContainer = new _PageContainer();
pagePane = new _PageCanvas();
pagePane.setLayout(null);
pageContainer.setLayout(new BorderLayout());
pageContainer.add(pagePane, BorderLayout.CENTER);
getViewport().add(pageContainer);
// hook the listeners of mouse event
ZSheetState sheetState = null;
if (edit == ZDocument.PAGE_HEADER) {
sheetState = doc.getHeaderModal();
} else if (edit == ZDocument.PAGE_FOOTER) {
sheetState = doc.getFooterModal();
}
control = new ZPrintPreviewControl(sheetState);
// pagePane.addComponentListener(control);
// register dnd support
DragSource dragSource = DragSource.getDefaultDragSource();
dragSource.createDefaultDragGestureRecognizer(pagePane, DnDConstants.ACTION_COPY_OR_MOVE, control);
new DropTarget(pagePane, DnDConstants.ACTION_MOVE, control);
// create resizeBare
// set document
doc.setControl(control);
doc.setComponent(pagePane);
this.addComponentListener(new ComponentAdapter() {
public void componentShown(ComponentEvent componentEvent) {
fireStateChanged();
}
});
this.addFocusListener(control);
}
/**
* DOCUMENT ME!
*/
void recreateMarginers() {
if (!marginersDirty) {
return;
}
float scale = doc.getScale();
marginers.clear();
Point tl = new Point((int) (scale * doc.getPageFormat().getImageableX()),
(int) (scale * doc.getPageFormat().getImageableY()));
Point br = new Point((int) (scale * (doc.getPageFormat().getImageableX() + doc.getPageFormat().getImageableWidth())),
(int) (scale * (doc.getPageFormat().getImageableY() + doc.getPageFormat().getImageableHeight())));
Point ps = new Point((int) (doc.getPageFormat().getWidth() * scale), (int) (scale * doc.getPageFormat().getHeight()));
// 0 1
// | |
// 2 -|----------|- 3
// | header |
// 8 -|----------|- 9
// | |
// | centent |
// | |
// 10-|----------|- 11
// | footer |
// 4 -|----------|- 5
// | |
// 6 7
marginers.add(new ZShape.Rectangle(tl.x - 2, 0, tl.x + 2, 6, 0)); // 0
marginers.add(new ZShape.Rectangle(br.x - 2, 0, br.x + 2, 6, 1)); // 1
marginers.add(new ZShape.Rectangle(0, tl.y - 2, 6, tl.y + 2, 2)); // 2
marginers.add(new ZShape.Rectangle(ps.x - 6, tl.y - 2, ps.x, tl.y + 2, 3)); // 3
marginers.add(new ZShape.Rectangle(0, br.y - 2, 6, br.y + 2, 4)); // 4
marginers.add(new ZShape.Rectangle(ps.x - 6, br.y - 2, ps.x, br.y + 2, 5)); // 5
marginers.add(new ZShape.Rectangle(tl.x - 2, ps.y - 6, tl.x + 2, ps.y, 6)); // 6
marginers.add(new ZShape.Rectangle(br.x - 2, ps.y - 6, br.x + 2, ps.y, 7));
int hh = (int) (doc.getHeaderHeight() * scale);
if (hh > 0) {
marginers.add(new ZShape.Rectangle(0, tl.y - 2 + hh, 6, tl.y + 2 + hh, 8)); // 2
marginers.add(new ZShape.Rectangle(ps.x - 6, tl.y - 2 + hh, ps.x, tl.y + 2 + hh, 9)); // 3
}
int fh = (int) (doc.getFooterHeight() * scale);
if (fh > 0) {
marginers.add(new ZShape.Rectangle(0, br.y - 2 - fh, 6, (br.y + 2) - fh, 10)); // 4
marginers.add(new ZShape.Rectangle(ps.x - 6, br.y - 2 - fh, ps.x, (br.y + 2) - fh, 11)); // 5
}
marginersDirty = false; // 7
//
marginLines.clear();
ZPen pen = new ZPen(1, new Color(192, 192, 192));
marginLines.add(new ZShape.SimpleHLine(tl.x, 0, tl.x, ps.y, pen)); //0-6
marginLines.add(new ZShape.SimpleHLine(br.x, 0, br.x, ps.y, pen)); //1-7
marginLines.add(new ZShape.SimpleHLine(0, tl.y, ps.x, tl.y, pen)); //2-3
marginLines.add(new ZShape.SimpleHLine(0, br.y, ps.x, br.y, pen)); //4-5
marginLines.add(new ZShape.SimpleHLine(0, tl.y + hh, ps.x, tl.y + hh, pen)); //8-9
marginLines.add(new ZShape.SimpleHLine(0, br.y - fh, ps.x, br.y - fh, pen)); //10-11
header = new ZRect(tl.x, tl.y, br.x, tl.y + hh);
body = new ZRect(tl.x, tl.y - hh, br.x, br.y - fh);
footer = new ZRect(tl.x, br.y - fh, br.x, br.y);
}
/**
*
* ZPrintPreview.ZPrintPreviewControl
*/
class ZPrintPreviewControl extends ZSheetStateControl {
final static int STS_Margin_Ready = STATE_IDLE + 1;
final static int STS_Marginning = STATE_IDLE + 2;
ZShape.SimpleHLine bar;
ZShape.Rectangle selectedMarginer;
int state = STATE_IDLE;
Cursor[] cursors;
ZPrintPreviewControl(ZSheetState sheetState) {
setModal(sheetState);
_MarginHandler mh = new _MarginHandler();
pagePane.addMouseMotionListener(mh);
pagePane.addMouseListener(mh);
editor.addListener(this);
}
/**
* DOCUMENT ME!
*/
public void setIdle() {
pagePane.setCursor(cursors[ZSheetState.DEFAULT_CURSOR]);
selectedMarginer = null;
state = STATE_IDLE;
}
/**
* DOCUMENT ME!
*
* @param modal
*/
public void setModal(ZSheetState modal) {
if (modal != null) {
doc.setEditable(modal, true);
}
if (this.modal != null) {
doc.setEditable(this.modal, false);
}
if ((modal == null) && (this.modal != null)) {
pagePane.removeMouseMotionListener(this);
pagePane.removeMouseListener(this);
pagePane.removeKeyListener(this);
} else if ((modal != null) && (this.modal == null)) {
pagePane.addMouseMotionListener(this);
pagePane.addMouseListener(this);
pagePane.addKeyListener(this);
}
super.setModal(modal);
}
/**
* DOCUMENT ME!
*
* @return
*/
public ZSheetState getModal() {
return modal;
}
/**
* DOCUMENT ME!
*
* @param e
*/
public void mouseDragged(MouseEvent e) {
if (state == STATE_IDLE) //正在调整边距,不要添乱
{
super.mouseDragged(e);
}
}
/**
* DOCUMENT ME!
*
* @param e
*/
public void mouseMoved(MouseEvent e) {
if (state == STATE_IDLE) //正在调整边距,不要添乱
{
super.mouseMoved(e);
}
}
/**
* DOCUMENT ME!
*
* @param e
*/
public void mouseReleased(MouseEvent e) {
if (state == STATE_IDLE) //正在调整边距,不要添乱
{
super.mouseReleased(e);
}
}
/**
* DOCUMENT ME!
*
* @param ui
*/
public void updateUI(ZDefaultUI ui) {
cursors = new Cursor[5];
cursors[ZSheetState.DEFAULT_CURSOR] = (Cursor) ui.get(ZDefaultUI.CURSOR_DEFAULT);
cursors[ZSheetState.RESIZE_ROW_CURSOR] = (Cursor) ui.get(ZDefaultUI.CURSOR_ROW_RESIZE);
cursors[ZSheetState.RESIZE_COL_CURSOR] = (Cursor) ui.get(ZDefaultUI.CURSOR_COL_RESIZE);
cursors[ZSheetState.MOVE_CURSOR] = (Cursor) ui.get(ZDefaultUI.CURSOR_DRAG_READY);
super.updateUI(ui);
}
/**
* DOCUMENT ME!
*
* @return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -