1 11 package org.eclipse.ui.internal; 12 13 import org.eclipse.jface.util.Geometry; 14 import org.eclipse.swt.SWT; 15 import org.eclipse.swt.events.PaintEvent; 16 import org.eclipse.swt.events.PaintListener; 17 import org.eclipse.swt.graphics.Color; 18 import org.eclipse.swt.graphics.GC; 19 import org.eclipse.swt.graphics.Point; 20 import org.eclipse.swt.graphics.Rectangle; 21 import org.eclipse.swt.widgets.Composite; 22 23 public class OvalComposite extends Composite implements PaintListener { 24 25 static final int[] TOP_LEFT_CORNER = new int[] { 0, 2, 1, 1, 2, 0 }; 26 27 private int orientation; 28 private Color interiorColor; 29 30 public OvalComposite(Composite parent, int orientation) { 31 super(parent, SWT.NONE); 32 33 addPaintListener(this); 34 this.orientation = orientation; 35 } 36 37 public void setOrientation(int orientation) { 38 this.orientation = orientation; 39 redraw(); 40 } 41 42 public void paintControl(PaintEvent e) { 43 GC gc = e.gc; 44 Color color = e.display.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW); 45 gc.setForeground(color); 46 if (interiorColor != null) { 47 gc.setBackground(interiorColor); 48 } 49 50 Shape shape = new Shape(TOP_LEFT_CORNER.length + 2); 51 52 IntAffineMatrix rotation = IntAffineMatrix 53 .getRotation(orientation); 54 55 rotation = rotation.multiply(IntAffineMatrix.ROT_180); 56 57 Point size = getSize(); 58 59 if (!Geometry.isHorizontal(orientation)) { 60 Geometry.flipXY(size); 61 } 62 63 shape.add(0, size.y); 64 shape.add(new Shape(TOP_LEFT_CORNER)); 65 shape.add(IntAffineMatrix.translation(size.x - 3, 0).multiply(IntAffineMatrix.FLIP_YAXIS), 66 shape.reverse()); 67 68 Point rawSize = getSize(); 69 Point adjust = new Point(0,0); 70 switch(orientation) { 71 case SWT.TOP: adjust = rawSize; break; 72 case SWT.LEFT: adjust = new Point(rawSize.x - 1, 0); break; 73 case SWT.RIGHT: adjust = new Point(0, rawSize.y - 3); break; 74 } 75 76 Shape targetShape = IntAffineMatrix.translation(adjust.x, adjust.y) 77 .multiply(rotation) 78 .transform(shape); 79 80 int[] shapeArray = targetShape.getData(); 81 if (interiorColor != null) { 82 gc.fillPolygon(shapeArray); 83 } 84 gc.drawPolyline(shapeArray); 85 } 86 87 public Rectangle getClientArea() { 88 Rectangle result = Geometry.copy(super.getClientArea()); 89 90 if (Geometry.isHorizontal(orientation)) { 91 Geometry.expand(result, -6, -6, orientation == SWT.BOTTOM ? -1 : 0, orientation == SWT.TOP ? -1 : 0); 92 } else { 93 Geometry.expand(result, orientation == SWT.RIGHT ? -1 : 0, orientation == SWT.LEFT ? -1 : 0, -6, -6); 94 } 95 96 return result; 97 } 98 99 public Rectangle computeTrim(int x, int y, int width, int height) { 100 Rectangle result = Geometry.copy(super.computeTrim(x, y, width, height)); 101 102 if (Geometry.isHorizontal(orientation)) { 103 Geometry.expand(result, 6, 6, orientation == SWT.BOTTOM ? 1 : 0, orientation == SWT.TOP ? 1 : 0); 104 } else { 105 Geometry.expand(result, orientation == SWT.RIGHT ? 1 : 0, orientation == SWT.LEFT ? 1 : 0, 6, 6); 106 } 107 108 return result; 109 } 110 111 114 public Color getInteriorColor() { 115 return interiorColor; 116 } 117 118 121 public void setInteriorColor(Color interiorColor) { 122 this.interiorColor = interiorColor; 123 } 124 } 125 | Popular Tags |