@Override public void activateEditor(final IWorkbenchPage page, final IStructuredSelection selection) { if (null != selection && !selection.isEmpty()) { final Object firstElement = selection.getFirstElement(); if (firstElement instanceof ResourceNode) { final File fileResource = ((ResourceNode) firstElement).getResource(); if (fileResource.exists() && fileResource.isFile()) { final URI uri = URI.createFileURI(fileResource.getAbsolutePath()); final IResource resource = externalLibraryWorkspace.getResource(uri); if (resource instanceof IFile) { final IEditorInput editorInput = EditorUtils.createEditorInput(new URIBasedStorage(uri)); final IEditorPart editor = page.findEditor(editorInput); if (null != editor) { page.bringToTop(editor); return; } } } } } super.activateEditor(page, selection); }
protected LaunchConfig getLaunchConfigFromEditor(ExecutionEvent event) { XtextEditor activeXtextEditor = EditorUtils.getActiveXtextEditor(event); if (activeXtextEditor == null) { return null; } final ITextSelection selection = (ITextSelection) activeXtextEditor.getSelectionProvider().getSelection(); return activeXtextEditor.getDocument().priorityReadOnly(new IUnitOfWork<LaunchConfig, XtextResource>() { @Override public LaunchConfig exec(XtextResource xTextResource) throws Exception { EObject lc = eObjectAtOffsetHelper.resolveContainedElementAt(xTextResource, selection.getOffset()); return findParentLaunchConfig(lc); } }); }
/** * If a platform plugin URI is given, a read-only Xtext editor is opened and returned. {@inheritDoc} * * @see {@link org.eclipse.emf.common.util.URI#isPlatformPlugin()} */ @Override public IEditorPart open(final URI uri, final EReference crossReference, final int indexInList, final boolean select) { IEditorPart result = super.open(uri, crossReference, indexInList, select); if (result == null && (uri.isPlatformPlugin() || OSGI_RESOURCE_URL_PROTOCOL.equals(uri.scheme()))) { final IModelLocation modelLocation = getModelLocation(uri.trimFragment()); if (modelLocation != null) { PlatformPluginStorage storage = new PlatformPluginStorage(modelLocation); IEditorInput editorInput = new XtextReadonlyEditorInput(storage); IWorkbenchPage activePage = workbench.getActiveWorkbenchWindow().getActivePage(); try { IEditorPart editor = IDE.openEditor(activePage, editorInput, editorID); selectAndReveal(editor, uri, crossReference, indexInList, select); return EditorUtils.getXtextEditor(editor); } catch (WrappedException e) { LOG.error("Error while opening editor part for EMF URI '" + uri + "'", e.getCause()); //$NON-NLS-1$ //$NON-NLS-2$ } catch (PartInitException partInitException) { LOG.error("Error while opening editor part for EMF URI '" + uri + "'", partInitException); //$NON-NLS-1$ //$NON-NLS-2$ } } } return result; }
public Object execute(final ExecutionEvent event) throws ExecutionException { final XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event); if (xtextEditor != null) { final IXtextDocument document = xtextEditor.getDocument(); document.readOnly(new IUnitOfWork.Void<XtextResource>() { @Override public void process(XtextResource state) throws Exception { final QuickOutlinePopup quickOutlinePopup = createPopup(xtextEditor.getEditorSite().getShell()); quickOutlinePopup.setEditor(xtextEditor); quickOutlinePopup.setInput(document); quickOutlinePopup.setEvent((Event) event.getTrigger()); quickOutlinePopup.open(); } }); } return null; }
protected void selectAndReveal(IEditorPart openEditor, final URI uri, final EReference crossReference, final int indexInList, final boolean select) { final XtextEditor xtextEditor = EditorUtils.getXtextEditor(openEditor); if (xtextEditor != null) { if (uri.fragment() != null) { xtextEditor.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>() { @Override public void process(XtextResource resource) throws Exception { if (resource != null) { EObject object = resource.getEObject(uri.fragment()); ITextRegion location = (crossReference != null) ? locationProvider.getSignificantTextRegion(object, crossReference, indexInList) : locationProvider.getSignificantTextRegion(object); if (select) { xtextEditor.selectAndReveal(location.getOffset(), location.getLength()); } else { xtextEditor.reveal(location.getOffset(), location.getLength()); } } } }); } } }
public Object execute(ExecutionEvent event) throws ExecutionException { try { XtextEditor editor = EditorUtils.getActiveXtextEditor(event); if (editor != null) { final ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection(); editor.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>() { @Override public void process(XtextResource state) throws Exception { EObject target = eObjectAtOffsetHelper.resolveElementAt(state, selection.getOffset()); findReferences(target); } }); } } catch (Exception e) { LOG.error(Messages.FindReferencesHandler_3, e); } return null; }
public IEditorPart open(URI uri, EReference crossReference, int indexInList, boolean select) { Iterator<Pair<IStorage, IProject>> storages = mapper.getStorages(uri.trimFragment()).iterator(); if (storages != null && storages.hasNext()) { try { IStorage storage = storages.next().getFirst(); // TODO we should create a JarEntryEditorInput if storage is a NonJavaResource from jdt to match the editor input used when double clicking on the same resource in a jar. IEditorInput editorInput = (storage instanceof IFile) ? new FileEditorInput((IFile) storage) : new XtextReadonlyEditorInput(storage); IWorkbenchPage activePage = workbench.getActiveWorkbenchWindow().getActivePage(); IEditorPart editor = IDE.openEditor(activePage, editorInput, editorID); selectAndReveal(editor, uri, crossReference, indexInList, select); return EditorUtils.getXtextEditor(editor); } catch (WrappedException e) { logger.error("Error while opening editor part for EMF URI '" + uri + "'", e.getCause()); } catch (PartInitException partInitException) { logger.error("Error while opening editor part for EMF URI '" + uri + "'", partInitException); } } return null; }
protected void selectAndReveal(IEditorPart openEditor, final URI uri, final EReference crossReference, final int indexInList, final boolean select) { final XtextEditor xtextEditor = EditorUtils.getXtextEditor(openEditor); if (xtextEditor != null) { xtextEditor.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>() { @Override public void process(XtextResource resource) throws Exception { if (resource != null) { EObject object = findEObjectByURI(uri, resource); if (object != null) { ITextRegion location = (crossReference != null) ? locationProvider .getSignificantTextRegion(object, crossReference, indexInList) : locationProvider.getSignificantTextRegion(object); if (select) xtextEditor.selectAndReveal(location.getOffset(), location.getLength()); } } } }); } }
public Object execute(ExecutionEvent event) throws ExecutionException { XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event); if (xtextEditor != null) { ITextSelection selection = (ITextSelection) xtextEditor.getSelectionProvider().getSelection(); IRegion region = new Region(selection.getOffset(), selection.getLength()); ISourceViewer internalSourceViewer = xtextEditor.getInternalSourceViewer(); IHyperlink[] hyperlinks = getDetector().detectHyperlinks(internalSourceViewer, region, false); if (hyperlinks != null && hyperlinks.length > 0) { IHyperlink hyperlink = hyperlinks[0]; hyperlink.open(); } } return null; }
@Override protected String getQualifiedName(ExecutionEvent event) { XtextEditor activeXtextEditor = EditorUtils.getActiveXtextEditor(event); if (activeXtextEditor == null) { return null; } final ITextSelection selection = getTextSelection(activeXtextEditor); return activeXtextEditor.getDocument().readOnly(new IUnitOfWork<String, XtextResource>() { public String exec(XtextResource xTextResource) throws Exception { EObject context = getContext(selection, xTextResource); EObject selectedElement = getSelectedName(selection, xTextResource); return getQualifiedName(selectedElement, context); } }); }
private URI getSelectionURI(ISelection currentSelection) { if (currentSelection instanceof IStructuredSelection) { IStructuredSelection iss = (IStructuredSelection) currentSelection; if (iss.size() == 1) { EObjectNode node = (EObjectNode) iss.getFirstElement(); return node.getEObjectURI(); } } else if (currentSelection instanceof TextSelection) { // Selection may be stale, get latest from editor XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(); TextSelection ts = (TextSelection) xtextEditor.getSelectionProvider().getSelection(); return xtextEditor.getDocument().readOnly(resource -> { EObject e = new EObjectAtOffsetHelper().resolveContainedElementAt(resource, ts.getOffset()); return EcoreUtil2.getURI(e); }); } return null; }
private void draw() { XtextEditor editor = EditorUtils.getActiveXtextEditor(); if (editor != null && (hoveredElement != null || !selectedElements.isEmpty())) { ISourceViewer isv = editor.getInternalSourceViewer(); styledText = isv.getTextWidget(); drawSelection(); } else { clear(); } }
/** * Constructor */ public CFGraph() { locFileProvider = new DefaultLocationInFileProvider(); editor = EditorUtils.getActiveXtextEditor(); styledText = editor.getInternalSourceViewer().getTextWidget(); layoutDone = false; }
@Override public Object execute(final ExecutionEvent event) throws ExecutionException { XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event); if (xtextEditor != null && xtextEditor.getEditorInput() instanceof XtextReadonlyEditorInput) { return null; } return super.execute(event); }
protected TextAttribute createTextAttribute(String id, TextStyle defaultTextStyle) { TextStyle textStyle = new TextStyle(); preferencesAccessor.populateTextStyle(id, textStyle, defaultTextStyle); int style = textStyle.getStyle(); Font fontFromFontData = EditorUtils.fontFromFontData(textStyle.getFontData()); return new TextAttribute( EditorUtils.colorFromRGB(textStyle.getColor()), EditorUtils.colorFromRGB(textStyle.getBackgroundColor()), style, fontFromFontData); }
@Override public Object execute(ExecutionEvent event) throws ExecutionException { try { final XtextEditor editor = EditorUtils.getActiveXtextEditor(event); Optional<EPackage> ePackage = EditorHandlingUtils.determineEPackage(editor); Optional<EObjectAtOffsetHelper> eObjectAtOffsetHelper = ePackage .flatMap(InjectingFactoryRegistry.INSTANCE::getFactory) .map(f -> f.inject(EObjectAtOffsetHelper.class)).map(EObjectAtOffsetHelper.class::cast); if (!eObjectAtOffsetHelper.isPresent()) { throw new IllegalStateException("Unable to create required helper classes."); } if (editor != null) { final ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection(); EObject selectedEObject = editor.getDocument() .priorityReadOnly(new IUnitOfWork<EObject, XtextResource>() { @Override public EObject exec(XtextResource resource) throws Exception { return eObjectAtOffsetHelper.get().resolveElementAt(resource, selection.getOffset()); } }); startRename(selectedEObject, editor, editor.getShell()); } } catch (OperationCanceledException e) { LOGGER.debug("Refactoring operation has been canceled.", e); } catch (Exception exc) { LOGGER.error("Error initializing refactoring", exc); MessageDialog.openError(Display.getCurrent().getActiveShell(), "Error initializing refactoring", exc.getMessage() + "\nSee log for details"); } return null; }
@Override protected Object executeOnEditor(ExecutionEvent event) { IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event); if (window == null) { return null; } final XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event); if (xtextEditor == null) { return null; } FileEditorInput input = (FileEditorInput) xtextEditor.getEditorInput(); final java.io.File fileOnDisk = input.getFile().getLocation().toFile(); if (xtextEditor.isDirty()) { if (MessageDialog.openConfirm(window.getShell(), "Save and Run JLustre2Excel", "The file " + input.getName() + " has unsaved changes. Save file and continue?")) { xtextEditor.doSave(null); } else { return null; } } WorkspaceJob job = new WorkspaceJob("JLustre2Excel") { @Override public IStatus runInWorkspace(final IProgressMonitor monitor) { if (hasErrors(xtextEditor)) { return errorStatus("Lustre file contains errors"); } if (hasFunctions(xtextEditor)) { return errorStatus("Functions are not support in JLustre2Excel"); } return runJob(fileOnDisk); } }; job.schedule(); return null; }
@Override protected Object executeOnEditor(ExecutionEvent event) { window = HandlerUtil.getActiveWorkbenchWindow(event); if (window == null) { return null; } final XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event); if (xtextEditor == null) { return null; } FileEditorInput input = (FileEditorInput) xtextEditor.getEditorInput(); final java.io.File fileOnDisk = input.getFile().getLocation().toFile(); if (xtextEditor.isDirty()) { if (MessageDialog.openConfirm(window.getShell(), "Save and Run JKind", "The file " + input.getName() + " has unsaved changes. Save file and continue?")) { xtextEditor.doSave(null); } else { return null; } } WorkspaceJob job = new WorkspaceJob("JKind Analysis") { @Override public IStatus runInWorkspace(final IProgressMonitor monitor) { activateTerminateHandler(monitor, this); JKindResult result = initializeJKindResult(xtextEditor); if (result == null) { return errorStatus("Lustre file contains errors"); } return runJob(fileOnDisk, result, monitor); } }; job.schedule(); return null; }
public Object executeURI(final URI uri) { final XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(); if (xtextEditor == null) { return null; } if (!saveChanges(window.getActivePage().getDirtyEditors())) { return null; } WorkspaceJob job = new WorkspaceJob(getJobName()) { @Override public IStatus runInWorkspace(final IProgressMonitor monitor) { return xtextEditor.getDocument().readOnly( new IUnitOfWork<IStatus, XtextResource>() { @Override public IStatus exec(XtextResource resource) throws Exception { EObject eobj = resource.getResourceSet().getEObject(uri, true); if (eobj instanceof Element) { return runJob((Element) eobj, monitor); } else { return Status.CANCEL_STATUS; } } }); } }; job.setRule(ResourcesPlugin.getWorkspace().getRoot()); job.schedule(); return null; }
public Object executeURI(final URI uri) { final XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(); if (xtextEditor == null) { return null; } if (!saveChanges(window.getActivePage().getDirtyEditors())) { return null; } WorkspaceJob job = new WorkspaceJob(getJobName()) { @Override public IStatus runInWorkspace(final IProgressMonitor monitor) { return xtextEditor.getDocument().readOnly(new IUnitOfWork<IStatus, XtextResource>() { @Override public IStatus exec(XtextResource resource) throws Exception { EObject eobj = resource.getResourceSet().getEObject(uri, true); if (eobj instanceof Element) { return runJob((Element) eobj, monitor); } else { return Status.CANCEL_STATUS; } } }); } }; job.setRule(ResourcesPlugin.getWorkspace().getRoot()); job.schedule(); return null; }
public Object executeURI(final URI uri) { final XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(); if (xtextEditor == null) { return null; } if (!saveChanges(window.getActivePage().getDirtyEditors())) { return null; } WorkspaceJob job = getWorkspaceJob(xtextEditor, uri); job.setRule(ResourcesPlugin.getWorkspace().getRoot()); job.schedule(); return null; }
protected void updateMelange(ExecutionEvent event, Language language, String xtextPath){ // Compute offset & new string int startOffset = -1; int length = -1; String newRegion = null; EStructuralFeature xtext = language.eClass().getEStructuralFeature("xtext"); List<INode> nodesXtext = NodeModelUtils.findNodesForFeature(language, xtext); if(!nodesXtext.isEmpty()){ INode nodeXtext = nodesXtext.get(0); startOffset = nodeXtext.getOffset(); length = nodeXtext.getLength(); newRegion = "\""+xtextPath+"\""; } else{//insert after operators EStructuralFeature operators = language.eClass().getEStructuralFeature("operators"); List<INode> nodesOp = NodeModelUtils.findNodesForFeature(language, operators); int lastOffset = -1; for(INode node : nodesOp){ if(node.getEndOffset() > lastOffset) lastOffset = node.getEndOffset(); } if(lastOffset != -1){ startOffset = lastOffset; length = 0; newRegion = "\n\n\txtext \""+xtextPath+"\""; } } // Replace in document or Melange file if(startOffset != -1 && length != -1 && newRegion != null){ int _startOffset = startOffset; int _length = length; String _newRegion = newRegion; XtextEditor editor = EditorUtils.getActiveXtextEditor(); if (editor != null && editor.getLanguageName().equals(MELANGE_EDITOR)) { //Update the editor content IXtextDocument document = editor.getDocument(); document.modify((XtextResource it) -> { document.replace(_startOffset,_length, _newRegion); return null; // no computed value }); } else{ //Update the Melange file content try { //Load Melange file String melangeWSLocation = language.eResource().getURI().toPlatformString(true); URI uri = language.eResource().getURI(); String melangeLocation = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString()+melangeWSLocation; List<String> lines = Files.readAllLines(Paths.get(melangeLocation)); StringBuffer newContent = new StringBuffer(); lines.forEach( line -> newContent.append(line+"\n") ); newContent.replace(startOffset,startOffset+length, newRegion); //Write new content Files.write(Paths.get(melangeLocation), newContent.toString().getBytes()); } catch (IOException e) { e.printStackTrace(); } } } }
protected void updateMelange(ExecutionEvent event, Language language, String siriusPath){ // Compute offset & new string int startOffset = -1; int length = -1; String newRegion = null; EStructuralFeature sirius = language.eClass().getEStructuralFeature("sirius"); List<INode> nodesSirius = NodeModelUtils.findNodesForFeature(language, sirius); if(!nodesSirius.isEmpty()){ INode nodeSirius = nodesSirius.get(0); startOffset = nodeSirius.getOffset(); length = nodeSirius.getLength(); newRegion = "\""+siriusPath+"\""; } else{//insert after operators EStructuralFeature operators = language.eClass().getEStructuralFeature("operators"); List<INode> nodesOp = NodeModelUtils.findNodesForFeature(language, operators); int lastOffset = -1; for(INode node : nodesOp){ if(node.getEndOffset() > lastOffset) lastOffset = node.getEndOffset(); } if(lastOffset != -1){ startOffset = lastOffset; length = 0; newRegion = "\n\n\tsirius \""+siriusPath+"\""; } } // Replace in document or Melange file if(startOffset != -1 && length != -1 && newRegion != null){ int _startOffset = startOffset; int _length = length; String _newRegion = newRegion; XtextEditor editor = EditorUtils.getActiveXtextEditor(); if (editor != null && editor.getLanguageName().equals(MELANGE_EDITOR)) { //Update the editor content IXtextDocument document = editor.getDocument(); document.modify((XtextResource it) -> { document.replace(_startOffset,_length, _newRegion); return null; // no computed value }); } else{ //Update the Melange file content try { //Load Melange file String melangeWSLocation = language.eResource().getURI().toPlatformString(true); URI uri = language.eResource().getURI(); String melangeLocation = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString()+melangeWSLocation; List<String> lines = Files.readAllLines(Paths.get(melangeLocation)); StringBuffer newContent = new StringBuffer(); lines.forEach( line -> newContent.append(line+"\n") ); newContent.replace(startOffset,startOffset+length, newRegion); //Write new content Files.write(Paths.get(melangeLocation), newContent.toString().getBytes()); } catch (IOException e) { e.printStackTrace(); } } } }
/** {@inheritDoc} */ @Override public XtextEditor getActiveXtextEditor() { return EditorUtils.getActiveXtextEditor(); }
/** {@inheritDoc} */ @Override public XtextEditor getXtextEditor(final IEditorPart openEditor) { return EditorUtils.getXtextEditor(openEditor); }
protected IFile getTargetFile(String[] validTargetTypes) throws ExecutionException { IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); if (window != null) { ISelection selection = (ISelection) window.getSelectionService().getSelection(); if (selection instanceof TextSelection) { XtextEditor editor = EditorUtils.getActiveXtextEditor(); if (editor != null) { IEditorInput editorInput = editor.getEditorInput(); if (editorInput instanceof IFileEditorInput) { return ((IFileEditorInput) editorInput).getFile(); } } } if (!(selection instanceof IStructuredSelection) || selection.isEmpty()) { throw new ExecutionException("Nothing is selected for action"); } Object firstElement = ((IStructuredSelection)selection).getFirstElement(); if (firstElement instanceof IAdaptable) { if (firstElement instanceof org.eclipse.core.resources.IFile) { String ext = ((org.eclipse.core.resources.IFile)firstElement).getFileExtension(); boolean validTarget = false; for (String s : validTargetTypes) { if (s.equals(ext)) { validTarget = true; break; } } if (validTarget) { return (IFile) firstElement; } else { throw new ExecutionException("Only files of type .sadl or .test can be targets for this action"); } } else { throw new ExecutionException("A valid target file must be selected"); } } } throw new ExecutionException("No project window selected"); }
@Override protected Object executeOnEditor(ExecutionEvent event) { window = HandlerUtil.getActiveWorkbenchWindow(event); if (window == null) { return null; } final XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event); if (xtextEditor == null) { return null; } FileEditorInput input = (FileEditorInput) xtextEditor.getEditorInput(); final java.io.File fileOnDisk = input.getFile().getLocation().toFile(); if (xtextEditor.isDirty()) { if (MessageDialog .openConfirm(window.getShell(), "Save and Run JRealizability", "The file " + input.getName() + " has unsaved changes. Save file and continue?")) { xtextEditor.doSave(null); } else { return null; } } WorkspaceJob job = new WorkspaceJob("JKind Analysis") { @Override public IStatus runInWorkspace(final IProgressMonitor monitor) { activateTerminateHandler(monitor, this); if (!hasQuery(xtextEditor)) { return errorStatus("Main node does not contain realizability query"); } JRealizabilityResult result = initializeJRealizabilityResult(xtextEditor); if (result == null) { return errorStatus("Lustre file contains errors"); } return runJob(fileOnDisk, result, monitor); } }; job.schedule(); return null; }
protected XtextEditor openEditor(IFile file) throws PartInitException { IWorkbenchPage page = getWorkbenchPage(); IDE.openEditor(page, file); XtextEditor activeXtextEditor = EditorUtils.getActiveXtextEditor(); return activeXtextEditor; }
public Object executeURI(final URI uri) { log = new ConsoleLogger(Logger.INFO, "AADL Validation", getWindow()); final XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(); if (xtextEditor == null) { return null; } determineNature(xtextEditor, log); if (!saveChanges(window.getActivePage().getDirtyEditors())) { return null; } // final IHandlerService handlerService = (IHandlerService) window // .getService(IHandlerService.class); WorkspaceJob job = new WorkspaceJob(getJobName()) { // private IHandlerActivation terminateActivation; @Override public IStatus runInWorkspace(final IProgressMonitor monitor) { return xtextEditor.getDocument().readOnly( new IUnitOfWork<IStatus, XtextResource>() { @Override public IStatus exec(XtextResource resource) throws Exception { OsateResourceUtil.refreshResourceSet(resource.getResourceSet()); EObject eobj = OsateResourceUtil.getResourceSet().getEObject(uri, true); if (eobj instanceof Element) { return runJob((Element) eobj, monitor); } else { return Status.CANCEL_STATUS; } } }); } }; job.setRule(ResourcesPlugin.getWorkspace().getRoot()); job.schedule(); return null; }
/** * Creates a {@link TextAttribute} from the given {@link TextStyle}. * * @param textStyle * a {@link TextStyle} * @return a {@link TextAttribute} from the given {@link TextStyle} */ public static TextAttribute createTextAttribute(final TextStyle textStyle) { int style = textStyle.getStyle(); Font fontFromFontData = EditorUtils.fontFromFontData(textStyle.getFontData()); return new TextAttribute(EditorUtils.colorFromRGB(textStyle.getColor()), EditorUtils.colorFromRGB(textStyle.getBackgroundColor()), style, fontFromFontData); }