FileDocCategorySizeDatePackage
VivaldiPanel.javaAPI DocAzureus 3.0.3.413409Thu Jun 28 20:37:02 BST 2007org.gudy.azureus2.ui.swt.views.stats

VivaldiPanel

public class VivaldiPanel extends Object

Fields Summary
private static final int
ALPHA_FOCUS
private static final int
ALPHA_NOFOCUS
Display
display
Composite
parent
Canvas
canvas
Scale
scale
private boolean
mouseLeftDown
private boolean
mouseRightDown
private int
xDown
private int
yDown
private boolean
antiAliasingAvailable
private List
lastContacts
private com.aelitis.azureus.core.dht.transport.DHTTransportContact
lastSelf
private org.eclipse.swt.graphics.Image
img
private int
alpha
private boolean
autoAlpha
Constructors Summary
public VivaldiPanel(Composite parent)

    this.parent = parent;
    this.display = parent.getDisplay();
    this.canvas = new Canvas(parent,SWT.NO_BACKGROUND);
    
    this.scale = new Scale();
    
  	canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				if (img != null && !img.isDisposed()) {
					Rectangle bounds = img.getBounds();
					if (bounds.width >= e.width && bounds.height >= e.height) {
						if (alpha != 255) {
							e.gc.setAlpha(alpha);
						}
						e.gc.drawImage(img, e.x, e.y, e.width, e.height, e.x, e.y,
								e.width, e.height);
					}
				} else {
					e.gc.drawText(MessageText.getString("VivaldiView.notAvailable"), 5,
							5, true);
				}
			}
		});
    
    canvas.addMouseListener(new MouseAdapter() {

      public void mouseDown(MouseEvent event) {
        if(event.button == 1) mouseLeftDown = true;
        if(event.button == 3) mouseRightDown = true;
        xDown = event.x;
        yDown = event.y;
        scale.saveMinX = scale.minX;
        scale.saveMaxX = scale.maxX;
        scale.saveMinY = scale.minY;
        scale.saveMaxY = scale.maxY;
      }
      
      public void mouseUp(MouseEvent event) {
        if(event.button == 1) mouseLeftDown = false;
        if(event.button == 3) mouseRightDown = false;
        refreshContacts(lastContacts, lastSelf);
      }                  
    });
    
    canvas.addListener(SWT.KeyDown, new Listener() {
			public void handleEvent(Event event) {
			}
		});

    canvas.addListener(SWT.MouseWheel, new Listener() {
			public void handleEvent(Event event) {
				System.out.println(event.count);
        scale.saveMinX = scale.minX;
        scale.saveMaxX = scale.maxX;
        scale.saveMinY = scale.minY;
        scale.saveMaxY = scale.maxY;
        
        int deltaY = event.count * 5;
        // scaleFactor>1 means zoom in, this happens when
        // deltaY<0 which happens when the mouse is moved up.
        float scaleFactor = 1 - (float) deltaY / 300;
        if(scaleFactor <= 0) scaleFactor = 0.01f;

        // Scalefactor of e.g. 3 makes elements 3 times larger
        float moveFactor = 1 - 1/scaleFactor;
        
        float centerX = (scale.saveMinX + scale.saveMaxX)/2;
        scale.minX = scale.saveMinX + moveFactor * (centerX - scale.saveMinX);
        scale.maxX = scale.saveMaxX - moveFactor * (scale.saveMaxX - centerX);

        float centerY = (scale.saveMinY + scale.saveMaxY)/2;
        scale.minY = scale.saveMinY + moveFactor * (centerY - scale.saveMinY);
        scale.maxY = scale.saveMaxY - moveFactor * (scale.saveMaxY - centerY);
        refreshContacts(lastContacts, lastSelf);
			}
		});

    canvas.addMouseMoveListener(new MouseMoveListener() {
      public void mouseMove(MouseEvent event) {
        if(mouseLeftDown) {
          int deltaX = event.x - xDown;
          int deltaY = event.y - yDown;
          int width = scale.width;
          int height = scale.height;
          float ratioX = (float) (scale.saveMaxX - scale.saveMinX) / (float) width;
          float ratioY = (float) (scale.saveMaxY - scale.saveMinY) / (float) height;
          float realDeltaX = deltaX * ratioX;
          float realDeltaY  = deltaY * ratioY;
          scale.minX = scale.saveMinX - realDeltaX;
          scale.maxX = scale.saveMaxX - realDeltaX;
          scale.minY = scale.saveMinY - realDeltaY;
          scale.maxY = scale.saveMaxY - realDeltaY;
          refreshContacts(lastContacts, lastSelf);
        }
        if(mouseRightDown) {
          int deltaY = event.y - yDown;
          // scaleFactor>1 means zoom in, this happens when
          // deltaY<0 which happens when the mouse is moved up.
          float scaleFactor = 1 - (float) deltaY / 300;
          if(scaleFactor <= 0) scaleFactor = 0.01f;

          // Scalefactor of e.g. 3 makes elements 3 times larger
          float moveFactor = 1 - 1/scaleFactor;
          
          float centerX = (scale.saveMinX + scale.saveMaxX)/2;
          scale.minX = scale.saveMinX + moveFactor * (centerX - scale.saveMinX);
          scale.maxX = scale.saveMaxX - moveFactor * (scale.saveMaxX - centerX);

          float centerY = (scale.saveMinY + scale.saveMaxY)/2;
          scale.minY = scale.saveMinY + moveFactor * (centerY - scale.saveMinY);
          scale.maxY = scale.saveMaxY - moveFactor * (scale.saveMaxY - centerY);
          refreshContacts(lastContacts, lastSelf);
        }
      }
    });

  	canvas.addMouseTrackListener(new MouseTrackListener() {
			public void mouseHover(MouseEvent e) {
			}
		
			public void mouseExit(MouseEvent e) {
				if (autoAlpha) {
					setAlpha(ALPHA_NOFOCUS);
				}
			}
		
			public void mouseEnter(MouseEvent e) {
				if (autoAlpha) {
					setAlpha(ALPHA_FOCUS);
				}
			}
		});
  
Methods Summary
private voiddraw(org.eclipse.swt.graphics.GC gc, float x, float y, float h)

    int x0 = scale.getX(x,y);
    int y0 = scale.getY(x,y);   
    gc.fillRectangle(x0-1,y0-1,3,3);   
    gc.drawLine(x0,y0,x0,(int)(y0-200*h/(scale.maxY-scale.minY)));
  
private voiddraw(org.eclipse.swt.graphics.GC gc, float x, float y, float h, com.aelitis.azureus.core.dht.control.DHTControlContact contact, int distance, float error)

    if(x == 0 && y == 0) return;    
    if(error > 1) error = 1;
    int errDisplay = (int) (100 * error);
    int x0 = scale.getX(x,y);
    int y0 = scale.getY(x,y);   
    gc.fillRectangle(x0-1,y0-1,3,3);   
    //int elevation =(int) ( 200*h/(scale.maxY-scale.minY));
    //gc.drawLine(x0,y0,x0,y0-elevation);
    String text = /*contact.getTransportContact().getAddress().getAddress().getHostAddress() + " (" + */distance + " ms \nerr:"+errDisplay+"%";
    int lineReturn = text.indexOf("\n");
    int xOffset = gc.getFontMetrics().getAverageCharWidth() * (lineReturn != -1 ? lineReturn:text.length()) / 2;
    gc.drawText(text,x0-xOffset,y0,true);
  
private voiddrawBorder(org.eclipse.swt.graphics.GC gc)

    
  
private voiddrawSelf(org.eclipse.swt.graphics.GC gc, float x, float y, float h, float errorEstimate)

  	int x0 = scale.getX(x, y);
		int y0 = scale.getY(x, y);
		//gc.drawOval(x0-50, y0-50, 100, 100);
		gc.drawLine(x0-15, y0, x0+15, y0); // Horizontal
		gc.drawLine(x0, y0-15, x0, y0+15); // Vertical
  
public intgetAlpha()

		return alpha;
	
public voidrefresh(java.util.List vivaldiPositions)

    if(canvas.isDisposed()) return;
    Rectangle size = canvas.getBounds();
    
    scale.width = size.width;
    scale.height = size.height;
    
    if (img != null && !img.isDisposed()) {
    	img.dispose();
    }
    
    img = new Image(display,size);
    GC gc = new GC(img);
    
    Color white = ColorCache.getColor(display,255,255,255);
    gc.setForeground(white);
    gc.setBackground(white);
    gc.fillRectangle(size);
    
    Color blue = ColorCache.getColor(display,66,87,104);
    gc.setForeground(blue);
    gc.setBackground(blue);
    
    
    
    Iterator iter = vivaldiPositions.iterator();
    while(iter.hasNext()) {
      VivaldiPosition position  = (VivaldiPosition)iter.next();
      HeightCoordinatesImpl coord = (HeightCoordinatesImpl) position.getCoordinates();
      
      float error = position.getErrorEstimate() - VivaldiPosition.ERROR_MIN;
      if(error < 0) error = 0;
      if(error > 1) error = 1;
      int blueComponent = (int) (255 - error * 255);
      int redComponent = (int) (255*error);
      // Don't use ColorCache, as our color creation is temporary and
      // varying
      Color drawColor = new Color(display,redComponent,50,blueComponent);      
      gc.setForeground(drawColor);
      draw(gc,coord.getX(),coord.getY(),coord.getH());
      drawColor.dispose();
    }
    
    gc.dispose();
    
    canvas.redraw();
  
public voidrefreshContacts(java.util.List contacts, com.aelitis.azureus.core.dht.transport.DHTTransportContact self)

  	if (contacts == null || self == null) {
  		return;
  	}
    lastContacts = contacts;
    lastSelf = self;
  	
    if(canvas.isDisposed()) return;
    Rectangle size = canvas.getBounds();
    
    if (size.isEmpty()) {
    	return;
    }
    
    scale.width = size.width;
    scale.height = size.height;
    
    Color white = ColorCache.getColor(display,255,255,255);
    Color blue = ColorCache.getColor(display,66,87,104);
    
    if (img != null && !img.isDisposed()) {
    	img.dispose();
    }
    
    img = new Image(display,size);
    
    GC gc = new GC(img);    
    
    gc.setForeground(white);
    gc.setBackground(white);
    
    gc.fillRectangle(size);
    
    if(SWT.getVersion() >= 3138 && antiAliasingAvailable) {
    	try {
    		//gc.setTextAntialias(SWT.ON);
    		//gc.setAntialias(SWT.ON);
      } catch(Exception e) {
        antiAliasingAvailable = false;
      }
    }
    
    
    gc.setForeground(blue);
    gc.setBackground(white);     
    
    DHTNetworkPosition _ownPosition = self.getNetworkPosition(DHTNetworkPosition.POSITION_TYPE_VIVALDI_V1);

    if ( _ownPosition == null ){
    	return;
    }
    
    VivaldiPosition ownPosition = (VivaldiPosition)_ownPosition;
    float ownErrorEstimate = ownPosition.getErrorEstimate();
    HeightCoordinatesImpl ownCoords =
    	(HeightCoordinatesImpl) ownPosition.getCoordinates();
    
    gc.drawText("Our error: " + ownErrorEstimate,10,10);
    
    Color black = ColorCache.getColor(display, 0, 0, 0);
    gc.setBackground(black); // Color of the squares

    // Draw all known positions of other contacts
    Iterator iter = contacts.iterator();
    while(iter.hasNext()) {
      DHTControlContact contact = (DHTControlContact) iter.next();
      DHTNetworkPosition _position = contact.getTransportContact().getNetworkPosition(DHTNetworkPosition.POSITION_TYPE_VIVALDI_V1);
      if ( _position == null ){
    	  continue;
      }
      VivaldiPosition position = (VivaldiPosition)_position;
      HeightCoordinatesImpl coord = (HeightCoordinatesImpl) position.getCoordinates();
      if(coord.isValid()) {
        draw(gc,coord.getX(),coord.getY(),coord.getH(),contact,(int)ownCoords.distance(coord),position.getErrorEstimate());
      }
    }
    
    // Mark our own position
    Color red = ColorCache.getColor(display, 255, 0, 0);
		gc.setForeground(red);
    drawSelf(gc, ownCoords.getX(), ownCoords.getY(),
						 ownCoords.getH(), ownErrorEstimate);
    
    
    gc.dispose();
    
    canvas.redraw();
  
public voidsetAlpha(int alpha)

		this.alpha = alpha;
		if (canvas != null && !canvas.isDisposed()) {
			canvas.redraw();
		}
	
public voidsetAutoAlpha(boolean autoAlpha)

		this.autoAlpha = autoAlpha;
		if (autoAlpha) {
			setAlpha(canvas.getDisplay().getCursorControl() == canvas ? ALPHA_FOCUS : ALPHA_NOFOCUS);
		}
	
public voidsetLayoutData(java.lang.Object data)

    canvas.setLayoutData(data);