예제 #1
0
 /**
  * Returns the item under the given point. The point is in display coordinates, relative to this
  * composite.
  *
  * @param p the point query.
  * @return an annotation under that point, or null if none.
  */
 public RangeAnnotation itemAt(Point p) {
   checkWidget();
   RangeAnnotation winner = null;
   // first, look for all the ones that are in the pixel range
   RangeAnnotation[] currentRanges = items.toArray(new RangeAnnotation[items.size()]);
   LinkedList<RangeAnnotation> annotationsToScore = new LinkedList<RangeAnnotation>();
   for (RangeAnnotation a : currentRanges) {
     int low = toVisualValue(a.getOffset());
     int high = toVisualValue(a.getLength() + a.getOffset());
     if (low == high) {
       high++;
     }
     if (low <= p.x && high >= p.x) {
       annotationsToScore.add(a);
     }
   }
   long score = -1;
   long rangeValue = toRangeValue(p.x);
   for (RangeAnnotation a : annotationsToScore) {
     long localScore = 0;
     long leftDiff = Math.abs(rangeValue - a.getOffset());
     long rightDiff = Math.abs((a.getOffset() + a.getLength()) - rangeValue);
     // normalize to 0 because the scaling might be huge
     localScore = rightDiff + leftDiff;
     if (score == -1 || localScore < score) {
       winner = a;
       score = localScore;
     }
   }
   return winner;
 }
예제 #2
0
 /** @param gc */
 private void paintRanges(GC gc, Rectangle bounds) {
   // draw a rectangle for each range
   gc.setAlpha(200);
   for (RangeAnnotation a : items) {
     if (a == selectedAnnotation) {
       continue;
     }
     if (a.isDisposed()) continue;
     int visualLow = toVisualValue(a.getOffset()) + bounds.x;
     int visualHigh = toVisualValue(a.getLength()) + visualLow;
     if (visualLow < bounds.x + HANDLE_SIZE) {
       visualLow = bounds.x + HANDLE_SIZE;
     }
     if (visualHigh > bounds.x + bounds.width - HANDLE_SIZE) {
       visualHigh = bounds.x + bounds.width - HANDLE_SIZE;
     }
     Color fg = a.getForeground();
     if (a == selectedAnnotation) {
       fg = gc.getDevice().getSystemColor(SWT.COLOR_WHITE);
     }
     if (fg == null) {
       fg = getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY);
     }
     Color bg = a.getBackground();
     if (bg == null) {
       bg = getDisplay().getSystemColor(SWT.COLOR_GRAY);
     }
     gc.setForeground(fg);
     gc.setBackground(bg);
     gc.fillRectangle(visualLow, bounds.y, visualHigh - visualLow, bounds.height - 1);
     gc.drawRectangle(visualLow, bounds.y, visualHigh - visualLow - 1, bounds.height - 1);
   }
   // paint the selected annotation
   if (selectedAnnotation != null) {
     RangeAnnotation a = selectedAnnotation;
     if (a.isDisposed()) return;
     int visualLow = toVisualValue(a.getOffset()) + bounds.x;
     int visualHigh = toVisualValue(a.getLength()) + visualLow;
     if (visualLow < bounds.x + HANDLE_SIZE) {
       visualLow = bounds.x + HANDLE_SIZE;
     }
     if (visualHigh > bounds.x + bounds.width - HANDLE_SIZE) {
       visualHigh = bounds.x + bounds.width - HANDLE_SIZE;
     }
     Color fg = gc.getDevice().getSystemColor(SWT.COLOR_WHITE);
     if (fg == null) {
       fg = getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY);
     }
     Color bg = a.getBackground();
     if (bg == null) {
       bg = getDisplay().getSystemColor(SWT.COLOR_GRAY);
     }
     gc.setForeground(fg);
     gc.setBackground(bg);
     gc.fillRectangle(visualLow, bounds.y, visualHigh - visualLow, bounds.height - 1);
     gc.drawRectangle(visualLow, bounds.y, visualHigh - visualLow - 1, bounds.height - 1);
   }
 }