/** * 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; }
/** * Returns the index at which the given annotation exists in the range. -1 if the item is * disposed, or doesn't exist in this slider. * * @param item * @return the index at which the given annotation exists in the range. -1 if the item is * disposed, or doesn't exist in this slider. */ public int getIndex(RangeAnnotation item) { checkWidget(); if (item == null || item.isDisposed()) { return -1; } return (items.indexOf(item)); }
private void fireItemSelectionChanged(int button) { Event event = new Event(); event.button = button; event.x = 0; event.y = 0; event.width = -1; event.height = -1; event.item = selectedAnnotation; event.data = (selectedAnnotation != null) ? selectedAnnotation.getData() : null; notifyListeners(SWT.Selection, event); }
/** * Creates a new annotation for this item * * @param rangeAnnotation */ void createItem(RangeAnnotation rangeAnnotation) { items.add(rangeAnnotation); rangeAnnotation.addDisposeListener(itemDisposedListener); }
/** @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); } }