Java Code Examples for org.eclipse.nebula.widgets.grid.GridItem#getBounds()

The following examples show how to use org.eclipse.nebula.widgets.grid.GridItem#getBounds() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GridCopyEnable.java    From translationstudio8 with GNU General Public License v2.0 4 votes vote down vote up
void doMouseLocationChange(int x, int y, boolean select) {
	if (gridTable.getItems().length == 0) {
		defaultCaret.setVisible(false);
		return;
	}
	Point eventP = new Point(x, y);
	GridItem _focusItem = gridTable.getItem(eventP);
	GridColumn col = gridTable.getColumn(eventP);
	if (_focusItem == null) {
		return;
	}
	GridCellRenderer gcr = col.getCellRenderer();
	int colIndex = gcr.getColumn();
	if (gcr == null || !(gcr instanceof XGridCellRenderer) || !copyAbleColumnIndexs.contains(colIndex)) {
		return;
	}
	XGridCellRenderer cellRender = (XGridCellRenderer) gcr;

	Rectangle cellBounds = _focusItem.getBounds(colIndex);
	GC gc = new GC(Display.getDefault());
	layout = cellRender.getTextLayout(gc, _focusItem, colIndex, true, false);
	if (layout == null) {
		gc.dispose();
		return;
	}
	focusContent = layout.getText();
	coordinateOffsetX = cellBounds.x + cellRender.leftMargin;
	coordinateOffsetY = cellBounds.y + cellRender.topMargin + cellRender.textTopMargin;
	if (!select) {
		focusCellRect.x = cellBounds.x;
		focusCellRect.y = cellBounds.y;
		focusCellRect.height = cellBounds.height;
		focusCellRect.width = cellBounds.width;
		focusColIndex = colIndex;
		focusItemIndex = gridTable.getIndexOfItem(_focusItem);
	}

	int[] trailing = new int[1];
	int newCaretOffset = layout.getOffset(x - coordinateOffsetX, y - coordinateOffsetY, trailing);

	int newCaretLine = layout.getLineIndex(newCaretOffset + trailing[0]);
	int lineStart = layout.getLineOffsets()[newCaretLine];
	Point point = null;
	if (newCaretOffset + trailing[0] == lineStart && trailing[0] != 0) {
		newCaretOffset += trailing[0];
		newCaretOffset = layout.getPreviousOffset(newCaretOffset, SWT.MOVEMENT_CLUSTER);
		point = layout.getLocation(newCaretOffset, true);
	} else {
		newCaretOffset += trailing[0];
		point = layout.getLocation(newCaretOffset, false);
	}

	// check area, only in cell area effective
	boolean vchange = focusCellRect.y <= y && y < focusCellRect.y + focusCellRect.height;
	boolean hchange = focusCellRect.x <= x && x < focusCellRect.x + focusCellRect.width;

	if (vchange && hchange && newCaretOffset != caretOffset) {
		focusItem = _focusItem;
		caretOffset = newCaretOffset;
		if (select) {
			doMouseSelection();
		}
		defaultCaret.setVisible(true);
		Rectangle rc = layout.getLineBounds(newCaretLine);
		defaultCaret.setBounds(point.x + coordinateOffsetX, point.y + coordinateOffsetY, 0, rc.height);
	}
	if (!select) {
		caretOffset = newCaretOffset;
		clearSelection();
	}
	layout.dispose();
	layout = null;
	gc.dispose();
}
 
Example 2
Source File: GridCopyEnable.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
void doMouseLocationChange(int x, int y, boolean select) {
	if (gridTable.getItems().length == 0) {
		defaultCaret.setVisible(false);
		return;
	}
	Point eventP = new Point(x, y);
	GridItem _focusItem = gridTable.getItem(eventP);
	GridColumn col = gridTable.getColumn(eventP);
	if (_focusItem == null) {
		return;
	}
	GridCellRenderer gcr = col.getCellRenderer();
	int colIndex = gcr.getColumn();
	if (gcr == null || !(gcr instanceof XGridCellRenderer) || !copyAbleColumnIndexs.contains(colIndex)) {
		return;
	}
	XGridCellRenderer cellRender = (XGridCellRenderer) gcr;

	Rectangle cellBounds = _focusItem.getBounds(colIndex);
	GC gc = new GC(Display.getDefault());
	layout = cellRender.getTextLayout(gc, _focusItem, colIndex, true, false);
	if (layout == null) {
		gc.dispose();
		return;
	}
	focusContent = layout.getText();
	coordinateOffsetX = cellBounds.x + cellRender.leftMargin;
	coordinateOffsetY = cellBounds.y + cellRender.topMargin + cellRender.textTopMargin;
	if (!select) {
		focusCellRect.x = cellBounds.x;
		focusCellRect.y = cellBounds.y;
		focusCellRect.height = cellBounds.height;
		focusCellRect.width = cellBounds.width;
		focusColIndex = colIndex;
		focusItemIndex = gridTable.getIndexOfItem(_focusItem);
	}

	int[] trailing = new int[1];
	int newCaretOffset = layout.getOffset(x - coordinateOffsetX, y - coordinateOffsetY, trailing);

	int newCaretLine = layout.getLineIndex(newCaretOffset + trailing[0]);
	int lineStart = layout.getLineOffsets()[newCaretLine];
	Point point = null;
	if (newCaretOffset + trailing[0] == lineStart && trailing[0] != 0) {
		newCaretOffset += trailing[0];
		newCaretOffset = layout.getPreviousOffset(newCaretOffset, SWT.MOVEMENT_CLUSTER);
		point = layout.getLocation(newCaretOffset, true);
	} else {
		newCaretOffset += trailing[0];
		point = layout.getLocation(newCaretOffset, false);
	}

	// check area, only in cell area effective
	boolean vchange = focusCellRect.y <= y && y < focusCellRect.y + focusCellRect.height;
	boolean hchange = focusCellRect.x <= x && x < focusCellRect.x + focusCellRect.width;

	if (vchange && hchange && newCaretOffset != caretOffset) {
		focusItem = _focusItem;
		caretOffset = newCaretOffset;
		if (select) {
			doMouseSelection();
		}
		defaultCaret.setVisible(true);
		Rectangle rc = layout.getLineBounds(newCaretLine);
		defaultCaret.setBounds(point.x + coordinateOffsetX, point.y + coordinateOffsetY, 0, rc.height);
	}
	if (!select) {
		caretOffset = newCaretOffset;
		clearSelection();
	}
	layout.dispose();
	layout = null;
	gc.dispose();
}