standarddrawingview.java
来自「开源(Open Source)项目JHotDraw的文档和源程序」· Java 代码 · 共 690 行 · 第 1/2 页
JAVA
690 行
/*
* @(#)StandardDrawingView.java 5.2
*
*/
package CH.ifa.draw.standard;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import CH.ifa.draw.util.*;
import CH.ifa.draw.framework.*;
/**
* The standard implementation of DrawingView.
* @see DrawingView
* @see Painter
* @see Tool
*/
public class StandardDrawingView
extends JPanel
implements DrawingView,
MouseListener,
MouseMotionListener,
KeyListener {
/**
* The DrawingEditor of the view.
* @see #tool
* @see #setStatus
*/
transient private DrawingEditor fEditor;
/**
* The shown drawing.
*/
private Drawing fDrawing;
/**
* the accumulated damaged area
*/
private transient Rectangle fDamage = null;
/**
* The list of currently selected figures.
*/
transient private Vector fSelection;
/**
* The shown selection handles.
*/
transient private Vector fSelectionHandles;
/**
* The preferred size of the view
*/
private Dimension fViewSize;
/**
* The position of the last mouse click
* inside the view.
*/
private Point fLastClick;
/**
* A vector of optional backgrounds. The vector maintains
* a list a view painters that are drawn before the contents,
* that is in the background.
*/
private Vector fBackgrounds = null;
/**
* A vector of optional foregrounds. The vector maintains
* a list a view painters that are drawn after the contents,
* that is in the foreground.
*/
private Vector fForegrounds = null;
/**
* The update strategy used to repair the view.
*/
private Painter fUpdateStrategy;
/**
* The grid used to constrain points for snap to
* grid functionality.
*/
private PointConstrainer fConstrainer;
/*
* Serialization support. In JavaDraw only the Drawing is serialized.
* However, for beans support StandardDrawingView supports
* serialization
*/
private static final long serialVersionUID = -3878153366174603336L;
private int drawingViewSerializedDataVersion = 1;
/**
* Constructs the view.
*/
public StandardDrawingView(DrawingEditor editor, int width, int height) {
fEditor = editor;
fViewSize = new Dimension(width,height);
fLastClick = new Point(0, 0);
fConstrainer = null;
fSelection = new Vector();
// changed from BufferedUpdateStrategy() to SimpleUpdateStrategy() as
// JFC/Swing uses double buffering automatically as default
setDisplayUpdate(new SimpleUpdateStrategy());
setBackground(Color.lightGray);
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
}
/**
* Sets the view's editor.
*/
public void setEditor(DrawingEditor editor) {
fEditor = editor;
}
/**
* Gets the current tool.
*/
public Tool tool() {
return fEditor.tool();
}
/**
* Gets the drawing.
*/
public Drawing drawing() {
return fDrawing;
}
/**
* Sets and installs another drawing in the view.
*/
public void setDrawing(Drawing d) {
if (fDrawing != null) {
clearSelection();
fDrawing.removeDrawingChangeListener(this);
}
fDrawing = d;
if (fDrawing != null)
fDrawing.addDrawingChangeListener(this);
checkMinimumSize();
repaint();
}
/**
* Gets the editor.
*/
public DrawingEditor editor() {
return fEditor;
}
/**
* Adds a figure to the drawing.
* @return the added figure.
*/
public Figure add(Figure figure) {
return drawing().add(figure);
}
/**
* Removes a figure from the drawing.
* @return the removed figure
*/
public Figure remove(Figure figure) {
return drawing().remove(figure);
}
/**
* Adds a vector of figures to the drawing.
*/
public void addAll(Vector figures) {
FigureEnumeration k = new FigureEnumerator(figures);
while (k.hasMoreElements())
add(k.nextFigure());
}
/**
* Gets the minimum dimension of the drawing.
*/
public Dimension getMinimumSize() {
return fViewSize;
}
/**
* Gets the preferred dimension of the drawing..
*/
public Dimension getPreferredSize() {
return getMinimumSize();
}
/**
* Sets the current display update strategy.
* @see Painter
*/
public void setDisplayUpdate(Painter updateStrategy) {
fUpdateStrategy = updateStrategy;
}
/**
* Gets the currently selected figures.
* @return a vector with the selected figures. The vector
* is a copy of the current selection.
*/
public Vector selection() {
// protect the vector with the current selection
return (Vector)fSelection.clone();
}
/**
* Gets an enumeration over the currently selected figures.
*/
public FigureEnumeration selectionElements() {
return new FigureEnumerator(fSelection);
}
/**
* Gets the currently selected figures in Z order.
* @see #selection
* @return a vector with the selected figures. The vector
* is a copy of the current selection.
*/
public Vector selectionZOrdered() {
Vector result = new Vector(fSelection.size());
FigureEnumeration figures = drawing().figures();
while (figures.hasMoreElements()) {
Figure f= figures.nextFigure();
if (fSelection.contains(f)) {
result.addElement(f);
}
}
return result;
}
/**
* Gets the number of selected figures.
*/
public int selectionCount() {
return fSelection.size();
}
/**
* Adds a figure to the current selection.
*/
public void addToSelection(Figure figure) {
if (!fSelection.contains(figure)) {
fSelection.addElement(figure);
fSelectionHandles = null;
figure.invalidate();
selectionChanged();
}
}
/**
* Adds a vector of figures to the current selection.
*/
public void addToSelectionAll(Vector figures) {
FigureEnumeration k = new FigureEnumerator(figures);
while (k.hasMoreElements())
addToSelection(k.nextFigure());
}
/**
* Removes a figure from the selection.
*/
public void removeFromSelection(Figure figure) {
if (fSelection.contains(figure)) {
fSelection.removeElement(figure);
fSelectionHandles = null;
figure.invalidate();
selectionChanged();
}
}
/**
* If a figure isn't selected it is added to the selection.
* Otherwise it is removed from the selection.
*/
public void toggleSelection(Figure figure) {
if (fSelection.contains(figure))
removeFromSelection(figure);
else
addToSelection(figure);
selectionChanged();
}
/**
* Clears the current selection.
*/
public void clearSelection() {
Figure figure;
FigureEnumeration k = selectionElements();
while (k.hasMoreElements())
k.nextFigure().invalidate();
fSelection = new Vector();
fSelectionHandles = null;
selectionChanged();
}
/**
* Gets an enumeration of the currently active handles.
*/
private Enumeration selectionHandles() {
if (fSelectionHandles == null) {
fSelectionHandles = new Vector();
FigureEnumeration k = selectionElements();
while (k.hasMoreElements()) {
Figure figure = k.nextFigure();
Enumeration kk = figure.handles().elements();
while (kk.hasMoreElements())
fSelectionHandles.addElement(kk.nextElement());
}
}
return fSelectionHandles.elements();
}
/**
* Gets the current selection as a FigureSelection. A FigureSelection
* can be cut, copied, pasted.
*/
public FigureSelection getFigureSelection() {
return new FigureSelection(selectionZOrdered());
}
/**
* Finds a handle at the given coordinates.
* @return the hit handle, null if no handle is found.
*/
public Handle findHandle(int x, int y) {
Handle handle;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?