use of org.eclipse.jface.text.TextSelection in project webtools.sourceediting by eclipse.
the class XSLTemplatesPage method insertTemplate.
// This code based on the JavaTemplatesPage code. Modified to work with
// SSE
@Override
protected void insertTemplate(Template template, IDocument document) {
ISourceViewer contextViewer = fTextEditor.getTextViewer();
ITextSelection textSelection = (ITextSelection) contextViewer.getSelectionProvider().getSelection();
if (!isValidTemplate(document, template, textSelection.getOffset(), textSelection.getLength()))
return;
beginCompoundChange(contextViewer);
/*
* The Editor checks whether a completion for a word exists before it
* allows for the template to be applied. We pickup the current text at
* the selection position and replace it with the first char of the
* template name for this to succeed. Another advantage by this method
* is that the template replaces the selected text provided the
* selection by itself is not used in the template pattern.
*/
String savedText;
try {
savedText = document.get(textSelection.getOffset(), textSelection.getLength());
if (savedText.length() == 0) {
String prefix = getIdentifierPart(document, template, textSelection.getOffset(), textSelection.getLength());
if (prefix.length() > 0 && !template.getName().startsWith(prefix)) {
return;
}
if (prefix.length() > 0) {
contextViewer.setSelectedRange(textSelection.getOffset() - prefix.length(), prefix.length());
textSelection = (ITextSelection) contextViewer.getSelectionProvider().getSelection();
}
}
document.replace(textSelection.getOffset(), textSelection.getLength(), template.getName().substring(0, 1));
} catch (BadLocationException e) {
endCompoundChange(contextViewer);
return;
}
Region region = new Region(textSelection.getOffset() + 1, 0);
contextViewer.getSelectionProvider().setSelection(new TextSelection(textSelection.getOffset(), 1));
DocumentTemplateContext context = getContext(document, template, textSelection.getOffset(), textSelection.getLength());
// $NON-NLS-1$
context.setVariable("selection", savedText);
if (context.getKey().length() == 0) {
try {
document.replace(textSelection.getOffset(), 1, savedText);
} catch (BadLocationException e) {
endCompoundChange(contextViewer);
return;
}
}
ITextViewer viewer = fTextEditor.getTextViewer();
int offset = viewer.getTextWidget().getCaretOffset();
int startLength = offset - region.getOffset();
// $NON-NLS-1$ //$NON-NLS-2$
String pattern = template.getPattern().replace("${cursor}", "");
CustomCompletionProposal proposal = new CustomCompletionProposal(pattern, offset, 0, startLength + pattern.length(), getImage(template), template.getName(), null, null, 0);
fTextEditor.getSite().getPage().activate(fTextEditor);
proposal.apply(fTextEditor.getTextViewer(), ' ', 0, offset);
viewer.getTextWidget().setCaretOffset(offset + pattern.length() - 1);
endCompoundChange(contextViewer);
}
use of org.eclipse.jface.text.TextSelection in project webtools.sourceediting by eclipse.
the class URLFileRegionHyperlink method open.
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.text.hyperlink.IHyperlink#open()
*/
public void open() {
if (fURL != null) {
IEditorInput input = new StorageEditorInput(new URLStorage(fURL));
IEditorDescriptor descriptor;
try {
descriptor = IDE.getEditorDescriptor(input.getName(), true);
if (descriptor != null) {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart openedEditor = IDE.openEditor(page, input, descriptor.getId(), true);
IRegion definitionRegion = findDefinitionRegion(new URLStorage(fURL));
if (definitionRegion != null) {
openedEditor.getSite().getSelectionProvider().setSelection(new TextSelection(definitionRegion.getOffset(), definitionRegion.getLength()));
}
}
} catch (PartInitException e) {
Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
}
}
}
use of org.eclipse.jface.text.TextSelection in project webtools.sourceediting by eclipse.
the class AbstractDropAction method insert.
/*
* Replaces targetEditor's current selection by "text"
*/
protected boolean insert(String text, IEditorPart targetEditor) {
if (text == null || text.length() == 0) {
return true;
}
ITextSelection textSelection = null;
IDocument doc = null;
ISelection selection = null;
ISourceEditingTextTools tools = targetEditor.getAdapter(ISourceEditingTextTools.class);
if (tools != null) {
doc = tools.getDocument();
selection = tools.getSelection();
}
ITextEditor textEditor = null;
if (targetEditor instanceof ITextEditor) {
textEditor = (ITextEditor) targetEditor;
}
if (textEditor == null) {
textEditor = ((IAdaptable) targetEditor).getAdapter(ITextEditor.class);
}
if (textEditor == null && tools != null && tools.getEditorPart() instanceof ITextEditor) {
textEditor = (ITextEditor) tools.getEditorPart();
}
if (textEditor == null && tools != null && tools.getEditorPart() != null) {
textEditor = tools.getEditorPart().getAdapter(ITextEditor.class);
}
if (selection == null && textEditor != null) {
selection = textEditor.getSelectionProvider().getSelection();
}
if (doc == null && textEditor != null) {
doc = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
}
if (selection instanceof ITextSelection) {
textSelection = (ITextSelection) selection;
try {
doc.replace(textSelection.getOffset(), textSelection.getLength(), text);
} catch (BadLocationException e) {
return false;
}
}
if (textEditor != null && textSelection != null) {
ISelectionProvider sp = textEditor.getSelectionProvider();
ITextSelection sel = new TextSelection(textSelection.getOffset(), text.length());
sp.setSelection(sel);
textEditor.selectAndReveal(sel.getOffset(), sel.getLength());
}
return true;
}
use of org.eclipse.jface.text.TextSelection in project webtools.sourceediting by eclipse.
the class TestPropertySheetConfiguration method testPropertySourceRemoval.
public void testPropertySourceRemoval() throws BadLocationException {
IDocument document = (IDocument) fEditor.getAdapter(IDocument.class);
// set up the editor document
document.replace(0, 0, "<test><myproperty props=\"yes\" /></test>");
// set current selection in editor
ISelection setSelection = new TextSelection(9, 0);
fEditor.getSelectionProvider().setSelection(setSelection);
// get current selection in editor
Object item = null;
ISelection selection = fEditor.getSelectionProvider().getSelection();
if (selection instanceof IStructuredSelection) {
item = ((IStructuredSelection) selection).getFirstElement();
IPropertySheetPage propertySheet = (IPropertySheetPage) fEditor.getAdapter(IPropertySheetPage.class);
assertTrue("No ConfigurablePropertySheetPage found", propertySheet instanceof ConfigurablePropertySheetPage);
if (propertySheet instanceof ConfigurablePropertySheetPage) {
ConfigurablePropertySheetPage cps = (ConfigurablePropertySheetPage) propertySheet;
PropertySheetConfiguration config = cps.getConfiguration();
assertNotNull("No property sheet configuration found", config);
IPropertySourceProvider provider = config.getPropertySourceProvider(cps);
assertNotNull("No property sheet provider found", provider);
IPropertySource source = provider.getPropertySource(item);
if (source instanceof IPropertySourceExtension) {
boolean canRemove = ((IPropertySourceExtension) source).isPropertyRemovable("props");
assertTrue("Current property cannot be removed", canRemove);
if (canRemove) {
((IPropertySourceExtension) source).removeProperty("props");
assertTrue("Current property cannot be removed", true);
// failing assert
return;
}
}
}
}
// if we get to here, always fail, since something went wrong.
assertTrue("testPropertySourceRemoval test did not take expected path", false);
}
use of org.eclipse.jface.text.TextSelection in project webtools.sourceediting by eclipse.
the class OccurrencesSearchViewPage method showMatch.
/**
* @see org.eclipse.search.ui.text.AbstractTextSearchViewPage#showMatch(org.eclipse.search.ui.text.Match,
* int, int)
*/
protected void showMatch(Match match, int currentOffset, int currentLength, boolean activate) throws PartInitException {
BasicSearchMatchElement element = (BasicSearchMatchElement) match.getElement();
IWorkbenchPage activePage = getActivePage();
try {
if (activePage != null) {
// open editor if needed
IDE.openEditor(getActivePage(), element.getFile());
// set the selection in the open editor
IEditorPart editor = activePage.getActiveEditor();
if (activate)
editor.getSite().getPage().activate(editor);
editor.getEditorSite().getSelectionProvider().setSelection(new TextSelection(currentOffset, currentLength));
}
} catch (PartInitException e) {
// possible exception trying to open editor
Logger.logException(e);
}
}
Aggregations