use of org.eclipse.jface.text.TextSelection in project webtools.sourceediting by eclipse.
the class GoToMatchingTagAction method runWithEvent.
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.action.Action#runWithEvent(org.eclipse.swt.widgets.Event)
*/
public void runWithEvent(Event event) {
super.runWithEvent(event);
if (getTextEditor() == null)
return;
ISelection selection = getTextEditor().getSelectionProvider().getSelection();
if (!selection.isEmpty() && selection instanceof IStructuredSelection && selection instanceof ITextSelection) {
Object o = ((IStructuredSelection) selection).getFirstElement();
if (o instanceof IDOMNode) {
int offset = ((ITextSelection) selection).getOffset();
IStructuredDocumentRegion matchRegion = null;
if (((Node) o).getNodeType() == Node.ATTRIBUTE_NODE) {
o = ((Attr) o).getOwnerElement();
}
int targetOffset = -1;
if (o instanceof IDOMNode) {
IDOMNode node = (IDOMNode) o;
IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
if (startStructuredDocumentRegion != null && startStructuredDocumentRegion.containsOffset(offset)) {
matchRegion = ((IDOMNode) o).getEndStructuredDocumentRegion();
if (matchRegion != null)
targetOffset = matchRegion.getStartOffset() + 2;
} else {
IStructuredDocumentRegion endStructuredDocumentRegion = node.getEndStructuredDocumentRegion();
if (endStructuredDocumentRegion != null && endStructuredDocumentRegion.containsOffset(offset)) {
matchRegion = ((IDOMNode) o).getStartStructuredDocumentRegion();
if (matchRegion != null)
targetOffset = matchRegion.getStartOffset() + 1;
}
}
}
if (targetOffset >= 0) {
getTextEditor().getSelectionProvider().setSelection(new TextSelection(targetOffset, 0));
}
}
}
}
use of org.eclipse.jface.text.TextSelection in project webtools.sourceediting by eclipse.
the class ToggleCommentActionXMLDelegate method updateCurrentSelection.
private void updateCurrentSelection(Position selectionPosition, IDocument document, boolean updateStartOffset) {
if (fEditor instanceof ITextEditor) {
// update the selection if text selection changed
if (selectionPosition != null) {
ITextSelection selection = null;
if (updateStartOffset) {
selection = new TextSelection(document, selectionPosition.getOffset() - OPEN_COMMENT.length(), selectionPosition.getLength() + OPEN_COMMENT.length());
} else {
selection = new TextSelection(document, selectionPosition.getOffset(), selectionPosition.getLength());
}
ISelectionProvider provider = ((ITextEditor) fEditor).getSelectionProvider();
if (provider != null) {
provider.setSelection(selection);
}
document.removePosition(selectionPosition);
}
}
}
use of org.eclipse.jface.text.TextSelection in project dbeaver by serge-rider.
the class PrefPageSQLFormat method formatSQL.
private void formatSQL() {
try {
try (final InputStream sqlStream = getClass().getResourceAsStream(FORMAT_FILE_NAME)) {
final String sqlText = ContentUtils.readToString(sqlStream, StandardCharsets.UTF_8);
sqlViewer.setInput(new StringEditorInput("SQL preview", sqlText, true, GeneralUtils.getDefaultFileEncoding()));
sqlViewer.getTextViewer().setSelection(new TextSelection(0, sqlText.length()));
sqlViewer.getTextViewer().doOperation(ISourceViewer.FORMAT);
sqlViewer.getTextViewer().setSelection(new TextSelection(0, 0));
sqlViewer.reloadSyntaxRules();
}
} catch (Exception e) {
log.error(e);
}
}
use of org.eclipse.jface.text.TextSelection in project dbeaver by serge-rider.
the class OpenLinkInWindowHandler method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
SQLEditor editor = RuntimeUtils.getObjectAdapter(HandlerUtil.getActiveEditor(event), SQLEditor.class);
if (editor == null) {
DBWorkbench.getPlatformUI().showError(TITLE, "No suitable editor was found for SQL");
return null;
}
ISelection selection = editor.getSelectionProvider().getSelection();
if (isSelectedTextNullOrEmpty(selection)) {
DBWorkbench.getPlatformUI().showError(TITLE, "No text was selected");
return null;
}
TextSelection textSelection = (TextSelection) selection;
String googleLink = SEARCH_WEB_ADDRESS_PREFIX + textSelection.getText().replaceAll(" ", "%20").trim();
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
UIUtils.launchProgram(googleLink);
} else {
DBWorkbench.getPlatformUI().showError(TITLE, "Desktop is not supported.");
}
return null;
}
use of org.eclipse.jface.text.TextSelection in project dbeaver by serge-rider.
the class GenerateUUIDHandler method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
if (activePart == null) {
return null;
}
IResultSetController rsc = activePart.getAdapter(IResultSetController.class);
if (rsc != null && UIUtils.hasFocus(rsc.getControl())) {
IResultSetSelection selection = rsc.getSelection();
if (selection != null && !selection.isEmpty()) {
for (Object cell : selection.toArray()) {
DBDAttributeBinding attr = selection.getElementAttribute(cell);
ResultSetRow row = selection.getElementRow(cell);
if (row != null && attr != null) {
ResultSetValueController valueController = new ResultSetValueController(rsc, attr, row, IValueController.EditType.NONE, null);
DBDValueHandler valueHandler = valueController.getValueHandler();
String uuid = generateUUID();
valueController.updateValue(uuid, false);
}
}
rsc.redrawData(false, false);
rsc.updateEditControls();
}
} else {
ITextViewer textViewer = activePart.getAdapter(ITextViewer.class);
if (textViewer != null) {
ISelection selection = textViewer.getSelectionProvider().getSelection();
if (selection instanceof TextSelection) {
try {
int offset = ((TextSelection) selection).getOffset();
int length = ((TextSelection) selection).getLength();
String uuid = generateUUID();
textViewer.getDocument().replace(offset, length, uuid);
textViewer.getSelectionProvider().setSelection(new TextSelection(offset + uuid.length(), 0));
} catch (BadLocationException e) {
DBWorkbench.getPlatformUI().showError("Insert UUID", "Error inserting UUID in text editor", e);
}
}
}
}
return null;
}
Aggregations