I'm generating a plugin in Eclipse. My plugin generates source JSP code in an open Editor so I would like to format the whole code, just as if I pressed Ctrl+Shift+F.
I try to do that with Eclipse JDT:
Properties prefs = new Properties();
prefs.setProperty(JavaCore.COMPILER_SOURCE, CompilerOptions.VERSION_1_8);
prefs.setProperty(JavaCore.COMPILER_COMPLIANCE, CompilerOptions.VERSION_1_8);
prefs.setProperty(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, CompilerOptions.VERSION_1_8);
CodeFormatter formatter = ToolFactory.createCodeFormatter(prefs);
String texto2 = texto;
IDocument dc = new Document(texto2);
//ICompilationUnit cu = (ICompilationUnit)JavaCore.create(texto_editor);
TextEdit edit_formatear =
formatter.format(CodeFormatter.K_COMPILATION_UNIT,
texto2, 0, texto2.length(), 0, null);
if(edit_formatear!=null){
edit_formatear.apply(dc);
System.out.println("Edit: "+dc.get());
}
} catch (BadLocationException e) {
e.printStackTrace();
}
But I think this code only work with java code...
Any idea?
Related
I'm writing an Eclipseplugin, which has to create a new project. This works so far, but i need to copy an external file into the projectfolder. I intend to have a 'Browse' button on one of my WizardPages, which opens a filedialog, where the user can browse to the file and after closing the dialog i can use the path to this file for various actions. My problem is that the dialog window never opens. Right now i'm trying it that way (snippet from my wizardpage):
public void createControl(Composite composite) {
this.container = new Composite(composite, SWT.NONE);
GridLayout layout = new GridLayout();
this.container.setLayout(layout);
layout.numColumns = 2;
Button browseButton = new Button(this.container, SWT.PUSH);
browseButton.setText("Browse");
browseButton.addSelectionListener(new SelectionListener() {
#Override
public void widgetDefaultSelected(SelectionEvent arg0) {
FileDialog fileDialog = new FileDialog(DataPage.this.container.getShell(), SWT.OPEN);
fileDialog.setText("JZOS created File");
String path = fileDialog.open();
DataPage.this.setJzosCreatedName(path);
}
});
I tried several implementations, that i have seen in examples and tutorials but nothing did work. I'm assuming a problem with the Shell that i give to the filedialog. I tried to open a new Shell within the widgetDefaultSelected function but it didn't work either. Any Suggestions?
You should be using the widgetSelected method of SelectionListener not widgetDefaultSelected
IPath path; //has some path
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
IWorkbenchPage page; //get current active page
try {
IJavaElement sourceJavaElement = JavaCore.create(file);
ITextEditor editor = (ITextEditor)JavaUI.openInEditor(sourceJavaElement); //illegal argument exception here
} catch (PartInitException | JavaModelException e1) {
e1.printStackTrace();
}
I am using this code code to open the editor but it gives an illegal argument exception when openInEditor is called on sourceJavaElement.
I am trying to open a marker, while double-clicking on an entry from a TableViewer, inside an eclipse plug-in. I am able to get the associated resource from the marker, however nothing is happening while the openEditor method is executed.
The code is as below:
viewer.addDoubleClickListener(new IDoubleClickListener() {
public void doubleClick(DoubleClickEvent event) {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
try {
IStructuredSelection sel = (IStructuredSelection) event.getSelection();
ReviewIssue reviewIssue = (ReviewIssue) sel.getFirstElement();
if(reviewIssue != null){
MessageDialog.openError(window.getShell(), "Insta Review", reviewIssue.getMarker().getResource());
try {
IDE.openEditor(window.getActivePage(), reviewIssue.getMarker(), true);
} catch (PartInitException e) {
MessageDialog.openError(window.getShell(), "Insta Review", e.getMessage());
}
}
} catch (Exception e) {
MessageDialog.openError(window.getShell(), "Insta Review", e.getMessage());
}
}
});
Please let me know, if am missing something here. Thanks in advance.
Also ignore the message dialogs, as I plan to implement the logging functionality later.
UPDATE:
Even though I created the marker on IFile, I was getting the same behaviour. I was finally able to open the editor by using the IFile, instead of the marker.
IFile iFile = markerProject.getFile(path);
//IMarker marker = iFile.createMarker("id.myMarker");
.....
IDE.openEditor(window.getActivePage(), reviewIssue.getiFile(), true);
//IDE.openEditor(window.getActivePage(), reviewIssue.getMarker()), true);
For this to work the IMarker.getResource() method must return an IFile. The code in IDE.openEditor is:
// get the marker resource file
if (!(marker.getResource() instanceof IFile)) {
IDEWorkbenchPlugin
.log("Open editor on marker failed; marker resource not an IFile"); //$NON-NLS-1$
return null;
}
so look in the .log file in the workspace .metadata directory to see if you are getting that log message.
Normally you would create a marker for a file using the IFile.createMarker method (createMarker is actually an IResource method).
I have custom editor for eclipse. For particular reasons this editor provide two toolbar areas which are not based on standard action bars provided by eclipse for editor. This two places are dedicated for other plugins to contribute. My intention is to leverage "org.eclipse.ui.menus" extension point with custom menuContribution/locationURI so other plugins can contribute using this extension and associated toolbar:my.editor.toolbar1 and toolbar:my.editor.toolbar2 as locationURI.
My problem is how to "connect" my ToolBar with particular location. I tried following approach but result are not good. I created custom ToolbarContributionRoot event if I should not and also created CustomContributionFactory which extends ExtensionContributionFactory. It works pretty well, but problem is with pulldown commands which submenu is not resolved correctly.
toolbarManager = new ToolBarManager(SWT.FLAT);
ToolbarContributionRoot toolbarRoot = new ToolbarContributionRoot(toolbarManager);
IServiceLocator workbench = PlatformUI.getWorkbench();
IConfigurationElement[] allMenuElements
= Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.ui.menus");
for (IConfigurationElement menuContribution : allMenuElements) {
String locationURI = menuContribution.getAttribute("locationURI");
if ("toolbar:my.editor.toolbar1".equals(locationURI)) {
try {
ExtensionContributionFactory factory = CustomContributionFactory.create(menuContribution);
factory.createContributionItems(workbench, toolbarRoot);
} catch (CoreException e) {
e.printStackTrace();
}
}
}
toolbar = toolbarManager.createControl(root);
GridData gridData = new GridData(GridData.FILL, GridData.FILL, false, false, 1, 1);
toolbar.setLayoutData(gridData);
toolbar.pack();
plugin.xml of "user" looks like this:
<extension point="org.eclipse.ui.menus" id="my.helper.id">
<menuContribution locationURI="toolbar:my.editor.toolbar1">
<command commandId="my.editor.special.command1" />...
Do you have any suggestions how to blend my custom toolbars and "org.eclipse.ui.menus" extension together?
Correct way how to do it is:
toolbarManager = new ToolBarManager(SWT.FLAT);
IServiceLocator workbench = PlatformUI.getWorkbench();
IMenuService menuService = (IMenuService) workbench.getService(IMenuService.class);
menuService.populateContributionManager(toolbarManager, TOOLBAR_LOCATION);
toolbar = toolbarManager.createControl(root);
Eclipse RCP question
I open file with:
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart editorPart = IDE.openEditor(page, file);
I also get document with:
IDocument doc = ((ITextEditor)editorPart).getDocumentProvider().getDocument(editorPart.getEditorInput());
I need to get to text viewer of that document (for creating LinkedModeUI), is there any way to do this?
The following worked for me:
IEditorPart editorPart = getSite().getPage().getActiveEditor();
if (editorPart != null) {
ITextOperationTarget target =
(ITextOperationTarget)editorPart.getAdapter(ITextOperationTarget.class);
if (target instanceof ITextViewer) {
ITextViewer textViewer = (ITextViewer)target;
// ...
}
}
1) One document can be opened with more than one editor. You'll have to iterate all editors to look for your file's editors.
2) Viewer is encapsulated in editor. I think the only way is extend editor class to add getter. Or redefine it, if viewer is inaccessible from inheritors.