📄 glyph.java
字号:
package org.placelab.util.swt;import org.eclipse.swt.SWT;import org.eclipse.swt.events.MouseEvent;import org.eclipse.swt.events.MouseListener;import org.eclipse.swt.events.PaintEvent;import org.eclipse.swt.events.PaintListener;import org.eclipse.swt.graphics.Color;import org.eclipse.swt.graphics.Cursor;import org.eclipse.swt.graphics.GC;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.graphics.ImageData;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.graphics.Rectangle;import org.eclipse.swt.widgets.Event;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.TypedListener;import org.placelab.util.Logger;public abstract class Glyph { public static final int ANCHOR_NW=0; public static final int ANCHOR_N =1; public static final int ANCHOR_NE=2; public static final int ANCHOR_E =3; public static final int ANCHOR_SE=4; public static final int ANCHOR_S =5; public static final int ANCHOR_SW=6; public static final int ANCHOR_W =7; public static final int ANCHOR_C =8; public static final int NONE=0; public static final int DISPOSED= 1<<0; public static final int VISIBLE = 1<<1; public static final int MOUSE_EVENTS = 1<<2; public static final int IGNORE_TRANSPARENCY = 1<<3; private GlyphComposite parent; private GlyphHolder holder; private int state; private GlyphEventTable eventTable; private Cursor cursor; private int closeEnough; protected AffineTransform transform; protected boolean needsTransformRedraw = false; protected Image transformedImage = null; // between 0 and 255 protected int transparency = 255; protected double zoom; private Point originalLocation; private Point location; void init(GlyphComposite parent, int style) { if (parent==null) SWT.error(SWT.ERROR_NULL_ARGUMENT); this.state = Glyph.VISIBLE | Glyph.MOUSE_EVENTS; this.parent = parent; this.holder = parent.getHolder(); this.cursor = null; this.zoom = parent.zoom; this.parent.notifyAddChild(this); this.holder.notifyAddDescendant(this); } private Glyph() { this.closeEnough = 0; this.location = new Point(0,0); this.originalLocation = new Point(0,0); this.zoom = 1; } public Glyph(GlyphComposite parent, int style) { this(); init(parent, style); } public Glyph(GlyphHolder holder, int style) { this(); if (holder==null) SWT.error(SWT.ERROR_NULL_ARGUMENT); init(holder.getChild(), style); } /* this method is only meant for the toplevel GlyphComposite that * is the child of the GlyphHolder */ Glyph(GlyphHolder holder, int style, boolean isToplevel) { this(); if (holder==null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (! isToplevel) { init(holder.getChild(), style); return; } if (! (this instanceof GlyphComposite) ) throw new IllegalArgumentException("only GlyphComposites can have a GlyphHolder as their parent"); this.state = Glyph.VISIBLE | Glyph.MOUSE_EVENTS; this.parent = null; this.holder = holder; this.holder.notifyAddDescendant(this); } public void setFlag(int flag) { state |= flag; } public void resetFlag(int flag) { state &= ~flag; } public boolean getFlag(int flag) { return ((state & flag) != 0); } public boolean isDisposed() { return getFlag(Glyph.DISPOSED); } public GlyphHolder getHolder() { return holder; } public GlyphComposite getParent() { return parent; } public void setCloseEnough(int c) { closeEnough = c; } public int getCloseEnough() { return closeEnough; } public void setTransparency(int newTransparency) { this.transparency = newTransparency;// if(getTransform() == null) {// setTransform(new AffineTransform());// } } /** * @return a value between 0 and 255 */ public int getTransparency() { return this.transparency; } public void setTransform(AffineTransform aTransform) { this.transform = aTransform; this.setNeedsTransformRedraw(true); this.redraw(null); } public AffineTransform getTransform() { return this.transform; } public void setZoom(double z) { zoom = z; this.setLocation(originalLocation); } public double getZoom() { return zoom; } public void setLocation(Point loc) { Rectangle invalidate = this.getBounds(); originalLocation = loc; location = new Point((int)(this.originalLocation.x * zoom), (int)(this.originalLocation.y * zoom)); this.setNeedsTransformRedraw(true); this.redraw(invalidate); } /** Gets the location that the Glyph is set to for a zoom * of 1.0 * @return the unzoomed location */ public Point getOriginalLocation() { return this.originalLocation; } /** * Gets the location that the Glyph is currently at, translated * to account for zoom * @return the zoom translated location */ public Point getLocation() { return this.location; } public void dispose() { if (parent != null) parent.notifyRemoveChild(this); setFlag(Glyph.DISPOSED); notifyListeners(SWT.Dispose, null); eventTable = null; holder.notifyRemoveDescendant(this); } public void addListener(int eventType, Listener handler) { if (handler == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (eventTable == null) eventTable = new GlyphEventTable (); eventTable.hook(eventType, handler); } public void removeListener(int eventType, Listener handler) { if (handler == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (eventTable == null) return; eventTable.unhook(eventType, handler); } boolean hooks(int eventType) { if (eventTable == null) return false; return eventTable.hooks(eventType); } public void notifyListeners(int eventType, Event event) { if (eventTable == null) return; if (event == null) event = new Event(); event.type = eventType; event.display = holder.getDisplay(); event.widget = holder; if (event.time == 0) { //event.time = event.display.getLastEventTime(); } eventTable.sendEvent(event); } public void addPaintListener(PaintListener listener) { if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); TypedListener typedListener = new TypedListener(listener); addListener(SWT.Paint,typedListener); } public void addMouseListener(MouseListener listener) { if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); TypedListener typedListener = new TypedListener (listener); addListener(SWT.MouseDown,typedListener); addListener(SWT.MouseUp,typedListener); addListener(SWT.MouseDoubleClick,typedListener); } /** * If you want to do your own drawing for the AffineTransform (for instance, * your glyph is a vector graphic thats easily drawn transformed, override this * and handle for the transform yourself). */ public void paint(PaintEvent e, GlyphGC gc) { notifyListeners(SWT.Paint, GlyphEventTable.getEvent(e)); if (transform != null) { if(this.getNeedsTransformRedraw() || transformedImage == null) { if(transformedImage != null) transformedImage.dispose(); if(getBoundsImpl().width <= 0 || getBoundsImpl().height <= 0) { // can't draw something thinner than a pixel :P return; } // want the glyph to paint at 0,0, not at wherever it thinks it should // paint, also zoom doesn't stack with AffineTransform, you can use one // or the other Point oldLocation = this.originalLocation; double oldZoom = this.zoom; this.setLocation(new Point(0, 0)); this.setZoom(1.0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -