📄 regionglyph.java
字号:
package org.placelab.demo.mapview;import org.eclipse.swt.SWT;import org.eclipse.swt.events.MouseAdapter;import org.eclipse.swt.events.MouseEvent;import org.eclipse.swt.events.MouseListener;import org.eclipse.swt.events.MouseMoveListener;import org.eclipse.swt.events.PaintEvent;import org.eclipse.swt.graphics.Color;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.graphics.Rectangle;import org.placelab.util.swt.Glyph;import org.placelab.util.swt.GlyphGC;import org.placelab.util.swt.GlyphHolder;/** * A RegionGlyph draws a RegionBacking and allows it to be manipulated. */public class RegionGlyph extends Glyph implements MouseListener { public RegionBacking region; protected MapView map; protected IconTextInfo text; // red outline protected static final Color strokeColor = new Color(null, 255, 0, 0); protected static final int lineWidth = 2; protected static final int resizeThreshold = 6; private static final int NONE = 0; private static final int LEFT = 1; private static final int RIGHT = 2; private static final int TOP = 3; private static final int BOTTOM = 4; private static final int LOWER_LEFT = 5; private static final int LOWER_RIGHT = 6; private static final int UPPER_LEFT = 7; private static final int UPPER_RIGHT = 8; private static final int MOVE = 9; protected int modifyType = NONE; protected int lastX = 0; protected int lastY = 0; RegionGlyph(MapView _map, RegionBacking _region) { super(_map.getHolder(), SWT.NONE); region = _region; map = _map; this.setLocation(getUpperLeftPoint()); text = new IconTextInfo(map.getHolder(), region.text, getUpperLeftPoint()); text.hide(); this.enableMouseEvents(true); this.addMouseListener(this); this.getHolder().addMouseMoveListener(new MouseMoveListener() { public void mouseMove(MouseEvent e) { if(modifyType != NONE) mouseMoved(e); } }); } protected void paintImpl(PaintEvent e, GlyphGC gc) { super.paintImpl(e, gc); int w = gc.getLineWidth(); Color save_fg = gc.getForeground(); gc.setForeground(strokeColor); gc.setLineWidth(lineWidth); Rectangle bounds = this.getBoundsImpl(); //System.out.println("drawing region @ " + bounds.toString()); gc.drawRectangle(bounds.x, bounds.y, bounds.width, bounds.height); gc.setLineWidth(w); gc.setForeground(save_fg); } protected int getEdge(int x, int y) { int left = map.longitudeToPixels(region.getOriginLon()); int right = map.longitudeToPixels(region.getUpperRightLon()); int top = map.latitudeToPixels(region.getUpperRightLat()); int bottom = map.latitudeToPixels(region.getOriginLat()); left *= zoom; right *= zoom; top *= zoom; bottom *= zoom; if(Math.abs(left - x) <= resizeThreshold && Math.abs(top - y) <= resizeThreshold) { return UPPER_LEFT; } else if(Math.abs(right - x) <= resizeThreshold && Math.abs(top - y) <= resizeThreshold) { return UPPER_RIGHT; } else if(Math.abs(left - x) <= resizeThreshold && Math.abs(bottom - y) <= resizeThreshold) { return LOWER_LEFT; } else if(Math.abs(right - x) <= resizeThreshold && Math.abs(bottom - y) <= resizeThreshold) { return LOWER_RIGHT; } else if(Math.abs(left - x) <= resizeThreshold) { return LEFT; } else if(Math.abs(right - x) <= resizeThreshold) { return RIGHT; } else if(Math.abs(top - y) <= resizeThreshold) { return TOP; } else if(Math.abs(bottom - y) <= resizeThreshold) { return BOTTOM; } else { return NONE; } } /* * The upper left point is useful, because in swt graphics the upper left is * the origin. */ private Point getUpperLeftPoint() { return new Point(map.longitudeToPixels(region.getOriginLon()), map.latitudeToPixels(region.getUpperRightLat())); } protected Rectangle getBoundsImpl() { int x = getUpperLeftPoint().x; int y = getUpperLeftPoint().y; int f = lineWidth; /*System.out.println("lowerlat " + region.getOriginLat() + " upperlat " + region.getUpperRightLat() + " lower-y " + map.latitudeToPixels(region.originLat) + " upper-y " + map.latitudeToPixels(region.upperRightLat));*/ int width = map.longitudeToPixels(region.getUpperRightLon()) - x; int height = map.latitudeToPixels(region.getOriginLat()) - y; Rectangle r = new Rectangle(x - f, y - f, width + f, height + f); r.x *= zoom; r.y *= zoom; r.width *= zoom; r.height *= zoom; return r; } protected boolean pointInsideImpl(int x, int y) { return getBoundsImpl().contains(x, y); } public void mouseDoubleClick(MouseEvent arg0) { this.modifyType = NONE; this.lastX = 0; this.lastY = 0; } public void mouseDown(MouseEvent me) { if(me.button == 1 && me.stateMask == 0) { this.modifyType = this.getEdge(me.x, me.y); if(modifyType == NONE) modifyType = MOVE; lastX = me.x; lastY = me.y; } else { text.setLocation(me.x, me.y); text.show(); } } public void mouseUp(MouseEvent arg0) { mouseDoubleClick(arg0); } protected double getDeltaLat(int pixelsMoved) { return - ((double)pixelsMoved / map.getPixelsPerLat()); } protected double getDeltaLon(int pixelsMoved) { return (double)pixelsMoved / map.getPixelsPerLon(); } protected void mouseMoved(MouseEvent me) { int x = me.x; int y = me.y; text.hide(); Rectangle oldBounds = this.getBounds(); oldBounds.x -= lineWidth + 2; oldBounds.y -= lineWidth + 2; oldBounds.width += lineWidth + 20; oldBounds.height += lineWidth + 20; if(lastX == 0 || lastY == 0) { lastX = x; lastY = y; return; } int moveX = (x - lastX); int moveY = (y - lastY); if(modifyType == MOVE) { region.originLat += this.getDeltaLat(moveY); region.originLon += this.getDeltaLon(moveX); region.upperRightLat += this.getDeltaLat(moveY); region.upperRightLon += this.getDeltaLon(moveX); } else if(modifyType == LEFT) { region.originLon += this.getDeltaLon(moveX); } else if(modifyType == RIGHT) { region.upperRightLon += this.getDeltaLon(moveX); } else if(modifyType == TOP) { region.upperRightLat += this.getDeltaLat(moveY); } else if(modifyType == BOTTOM) { region.originLat += this.getDeltaLat(moveY); } else if(modifyType == UPPER_LEFT) { region.originLon += this.getDeltaLon(moveX); region.upperRightLat += this.getDeltaLat(moveY); } else if(modifyType == UPPER_RIGHT) { region.upperRightLon += this.getDeltaLon(moveX); region.upperRightLat += this.getDeltaLat(moveY); } else if(modifyType == LOWER_LEFT) { region.originLon += this.getDeltaLon(moveX); region.originLat += this.getDeltaLat(moveY); } else if(modifyType == LOWER_RIGHT) { region.upperRightLon += this.getDeltaLon(moveX); region.originLat += this.getDeltaLat(moveY); } lastX = x; lastY = y; this.setNeedsTransformRedraw(true); redraw(oldBounds); /*newBounds.x -= lineWidth + me.x; newBounds.y -= lineWidth + me.y; newBounds.width += lineWidth + me.x; newBounds.height += lineWidth + me.y;*/ //redraw(newBounds); //System.out.println("oldbounds: " + oldBounds + " new bounds " + newBounds); /*Point p = new Point(oldBounds.x, oldBounds.y); p = this.getParent().toHolder(p); oldBounds.x = p.x; oldBounds.y = p.y; this.getHolder().redraw(oldBounds.x, oldBounds.y, oldBounds.width, oldBounds.height, true); Rectangle currentBounds = this.getBounds(); p = new Point(currentBounds.x, currentBounds.y); p = this.getParent().toHolder(p); currentBounds.x = p.x; currentBounds.y = p.y; this.getHolder().redraw(currentBounds.x, currentBounds.y, currentBounds.width, currentBounds.height, true);*/ //this.getHolder().redraw(); } protected void enter(MouseEvent me) { if(modifyType == NONE) { int result = this.getEdge(me.x, me.y); switch(result) { case LEFT: case RIGHT: this.setCursor(this.getHolder().getDisplay().getSystemCursor(SWT.CURSOR_SIZEWE)); break; case UPPER_LEFT: case LOWER_RIGHT: this.setCursor(this.getHolder().getDisplay().getSystemCursor(SWT.CURSOR_SIZENWSE)); break; case UPPER_RIGHT: case LOWER_LEFT: this.setCursor(this.getHolder().getDisplay().getSystemCursor(SWT.CURSOR_SIZENESW)); break; case TOP: case BOTTOM: this.setCursor(this.getHolder().getDisplay().getSystemCursor(SWT.CURSOR_SIZENS)); break; default: this.setCursor(this.getHolder().getDisplay().getSystemCursor(SWT.CURSOR_SIZEALL)); } } // else its handled by the mouse move } protected void leave(MouseEvent me) { if(!this.getBounds().contains(me.x, me.y)) { text.hide(); } } private static final Color bgColor = new Color(null, 255, 255, 0); private class IconTextInfo extends TextInfoGlyph { private class Timeout implements Runnable { private IconTextInfo info; public Timeout(IconTextInfo i) { info = i; } public void run() { info.timeout(); } } private Timeout timeout; private final int TEXTINFO_TIMEOUT=10000; public IconTextInfo(GlyphHolder holder, String text, Point p) { super(holder, SWT.NONE); setBackground(bgColor); setText(text, true); setLocation(p); addMouseListener(new MouseAdapter() { public void mouseDown (MouseEvent e) { handleMouseDown(e); } }); timeout = null; } public void show() { setVisible(true); moveAbove(null); if (timeout != null) getHolder().getDisplay().timerExec(-1, timeout); timeout = new Timeout(this); getHolder().getDisplay().timerExec(TEXTINFO_TIMEOUT, timeout); } public void hide() { setVisible(false); if (timeout != null) { getHolder().getDisplay().timerExec(-1, timeout); timeout = null; } } void handleMouseDown(MouseEvent e) { hide(); } void timeout() { timeout = null; hide(); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -