public Rectangle getRectangle(final IModelSpaceCanvas canvas) { Rectangle rect = _image.getBounds(); int w = rect.x; int h = rect.y; int numPoints = getPointCount(); if (numPoints == 1) { ImageAnchor anchor = getAnchorType(); Point2D.Double m = new Point2D.Double(getPoint(0).getX(), getPoint(0).getY()); Point2D.Double p = new Point2D.Double(0, 0); canvas.transformModelToPixel(getModelSpace(), m.getX(), m.getY(), p); if (anchor.equals(ImageAnchor.Center)) { rect = new Rectangle((int) (p.x - w / 2), (int) (p.y - h / 2), w, h); } else if (anchor.equals(ImageAnchor.UpperLeft)) { rect = new Rectangle((int) p.x, (int) p.y, w, h); } else if (anchor.equals(ImageAnchor.UpperRight)) { rect = new Rectangle((int) (p.x - w), (int) p.y, w, h); } else if (anchor.equals(ImageAnchor.LowerLeft)) { rect = new Rectangle((int) p.x, (int) (p.y - h), w, h); } else if (anchor.equals(ImageAnchor.LowerRight)) { rect = new Rectangle((int) (p.x - w), (int) (p.y - h), w, h); } else { throw new RuntimeException("Invalid image anchor: " + anchor); } } else if (numPoints >= 2) { int xmin = Integer.MAX_VALUE; int xmax = -Integer.MAX_VALUE; int ymin = Integer.MAX_VALUE; int ymax = -Integer.MAX_VALUE; for (int i = 0; i < numPoints; i++) { Point2D.Double m = new Point2D.Double(getPoint(i).getX(), getPoint(i).getY()); Point2D.Double p = new Point2D.Double(0, 0); canvas.transformModelToPixel(getModelSpace(), m.getX(), m.getY(), p); xmin = Math.min(xmin, (int) p.getX()); xmax = Math.max(xmax, (int) p.getX()); ymin = Math.min(ymin, (int) p.getY()); ymax = Math.max(ymax, (int) p.getY()); } w = xmax - xmin + 1; h = ymax - ymin + 1; rect = new Rectangle(xmin, ymin, w, h); } else { throw new RuntimeException("Invalid image definition."); } return rect; }
@Override protected void initializeViewerSpecificFeatures() { IModelSpaceCanvas canvas = _plot.getModelSpaceCanvas(); canvas.removeCursorListener(_plot); canvas.addCursorListener(this); canvas .getComposite() .addKeyListener( new KeyAdapter() { @Override public void keyPressed(final KeyEvent e) { IModelSpace activeModelSpace = _plot.getActiveModelSpace(); if (e.keyCode == SWT.ARROW_UP) { if (activeModelSpace.getAxisY().getScale() == AxisScale.LOG) { MessageDialog.openError( getShell(), "Shift Error", "Shifting of a logarithmic axis not currently supported."); return; } ModelSpaceBounds bounds = activeModelSpace.getViewableBounds(); double yStart = bounds.getStartY(); double yEnd = bounds.getEndY(); double dist = yEnd - yStart; double shift = dist / 4; double sign = -1; AxisDirection direction = activeModelSpace.getAxisY().getDirection(); if (direction.equals(AxisDirection.BOTTOM_TO_TOP)) { sign = 1; } yStart += shift * sign; yEnd += shift * sign; activeModelSpace.setViewableBounds( bounds.getStartX(), bounds.getEndX(), yStart, yEnd); } else if (e.keyCode == SWT.ARROW_DOWN) { if (activeModelSpace.getAxisY().getScale() == AxisScale.LOG) { MessageDialog.openError( getShell(), "Shift Error", "Shifting of a logarithmic axis not currently supported."); return; } ModelSpaceBounds bounds = activeModelSpace.getViewableBounds(); double yStart = bounds.getStartY(); double yEnd = bounds.getEndY(); double dist = yEnd - yStart; double shift = dist / 4; double sign = 1; AxisDirection direction = activeModelSpace.getAxisY().getDirection(); if (direction.equals(AxisDirection.BOTTOM_TO_TOP)) { sign = -1; } yStart += shift * sign; yEnd += shift * sign; activeModelSpace.setViewableBounds( bounds.getStartX(), bounds.getEndX(), yStart, yEnd); } else if (e.keyCode == SWT.ARROW_LEFT) { if (activeModelSpace.getAxisX().getScale() == AxisScale.LOG) { MessageDialog.openError( getShell(), "Shift Error", "Shifting of a logarithmic axis not currently supported."); return; } ModelSpaceBounds bounds = activeModelSpace.getViewableBounds(); double xStart = bounds.getStartX(); double xEnd = bounds.getEndX(); double dist = xEnd - xStart; double shift = dist / 4; double sign = 1; AxisDirection direction = activeModelSpace.getAxisX().getDirection(); if (direction.equals(AxisDirection.LEFT_TO_RIGHT)) { sign = -1; } xStart += shift * sign; xEnd += shift * sign; activeModelSpace.setViewableBounds( xStart, xEnd, bounds.getStartY(), bounds.getEndY()); } else if (e.keyCode == SWT.ARROW_RIGHT) { if (activeModelSpace.getAxisX().getScale() == AxisScale.LOG) { MessageDialog.openError( getShell(), "Shift Error", "Shifting of a logarithmic axis not currently supported."); return; } ModelSpaceBounds bounds = activeModelSpace.getViewableBounds(); double xStart = bounds.getStartX(); double xEnd = bounds.getEndX(); double dist = xEnd - xStart; double shift = dist / 4; double sign = -1; AxisDirection direction = activeModelSpace.getAxisX().getDirection(); if (direction.equals(AxisDirection.LEFT_TO_RIGHT)) { sign = 1; } xStart += shift * sign; xEnd += shift * sign; activeModelSpace.setViewableBounds( xStart, xEnd, bounds.getStartY(), bounds.getEndY()); } else if (e.character == ' ') { TreeSelection selection = (TreeSelection) _layerViewer.getSelection(); Iterator iterator = selection.iterator(); while (iterator.hasNext()) { Object object = iterator.next(); if (object instanceof IViewLayer) { IViewLayer layer = (IViewLayer) object; boolean visible = !layer.isVisible(); layer.setVisible(visible); _layerViewer.setChecked(layer, visible); _plot.updateAll(); } } } } }); }