Set tab title for an EditorPart in Eclipse - eclipse

I currently have en Eclipse plugin that provides a multi-page editor, one page for a visual editor and one page for a source editor, similar to other editors, i.e:
This is the important part of my code:
public class DockerfileEditor extends FormEditor implements IResourceChangeListener {
....
#Override
protected void addPages() {
try {
SourceEditor sourceEditor = new SourceEditor(); // Extends from EditorPart
addPage(new DesignForm(this, "Design")); //$NON-NLS-1$
addPage(sourceEditor, sourceEditor.getEditorInput());
} catch (PartInitException e) {
e.printStackTrace();
}
}
}
In the addPages() method I'm adding my 2 pages, the first of them extends from FormPage so setting the title is really easy, but the second page extends from EditorPart (this will be my source editor), how can I set the title of this page?

addPage returns you the index of the page that was added so you can use:
int pageIndex = addPage(sourceEditor, sourceEditor.getEditorInput());
setPageText(pageIndex, "Source");

Related

Close Eclipse ViewPart tab when createPartControl function fails when launched from "Quick Access"?

The default behaviour when creating a new Eclipse ViewPart is to show the new tab regardless of what happens in the createPartControl function. For example, if didn't create anything, no widgets, nothing, a blank tab will be shown. I don't like this behaviour. I want to close that tab if initialization in createPartControl fails.
Now, I have a mouse-button-context-menu handler that can do this, e.g.
public class MyPartMB3Handler extends AbstractHandler {
#Override
public Object execute(final ExecutionEvent event)
throws ExecutionException {
// Create a view and show it.
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
IWorkbenchPage page = window.getActivePage();
try {
MyPart viewPart = (MyPart)page.showView(MyPart.ID);
if(!viewPart.isCreated()) {
page.hideView(viewPart);
}
}
catch(PartInitException e) {
e.printStackTrace();
}
return null;
}
}
The isCreated function is a little hack that lets me know if my ViewPart initialization fails, e.g.
public class MyPart extends ViewPart {
public static final String ID = "com.myplugin.MyPart";
private Composite _parent = null;
#Override
public void createPartControl(Composite parent) {
if(!MyPlugin.createPartControl(parent) { // Some common part creation code I use.
//PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(this);
return;
}
_parent = parent;
}
#Override
public void setFocus() {
}
public boolean isCreated() {
return _parent != null;
}
}
The problem arises when I launch this ViewPart from the Eclipse "Quick Access" field. I don't own the handler now. From an exception I forced, the handler might be org.eclipse.ui.internal.e4.compatibility.CompatibilityPart.createPartControl or org.eclipse.ui.internal.e4.compatibility.CompatibilityView.createPartControl or org.eclipse.ui.internal.e4.compatibility.CompatibilityPart.create.
I tried hiding the view inside the createPartControl function (see the commented line above), but Eclipse did not like that and spewed a pile of exceptions.
I thought maybe I could throw a PartInitException in createPartControl, but Eclipse tells me I'm not allowed to do that.
So, how do I get my menu handler behaviour when launching from "Quick Access"?
An underlying question might be, is there a better/proper way to achieve this behaviour?
You can close the view by running the hideView asynchronously after the createPartControl has finished - like this:
#Override
public void createPartControl(Composite parent) {
parent.getDisplay().asyncExec(new Runnable() {
#Override
public void run()
{
getSite().getPage().hideView(MyPart.this);
}
});

Eclipse plugin - is there any editable TreeSelection class

I am looking for a solution to make this tree selection editable in the package explorer view itself.
the idea
for example- if we click rename on any class in package explorer, it will prompt a new window to rename. This functionality is same for any class that implement TreeSelection Class.
But the Solution i am looking for is - when rename is invoked, the rename option is shown at the tree itself (like we have in Windows Explorer view)
any suggestion on how to attain this behavior on eclipse.
You don't need to have some special editable selection, you just want to make the tree editable. For this you use EditingSupport, like this (adapted from http://www.vogella.com/articles/EclipseJFaceTableAdvanced/article.html#jfacetable_editor):
public class NameEditingSupport extends EditingSupport {
private final TreeViewer viewer;
public FirstNameEditingSupport(TreeViewer viewer) {
super(viewer);
this.viewer = viewer;
}
#Override
protected CellEditor getCellEditor(Object element) {
return new TextCellEditor(viewer.getTree());
}
#Override
protected boolean canEdit(Object element) {
return true;
}
#Override
protected Object getValue(Object element) {
// return the name
}
#Override
protected void setValue(Object element, Object value) {
// update the name of your object
viewer.update(element, null);
}
}
// in the code creating the tree
treeViewer.setEditingSupport(new NameEditingSupport(treeViewer));

How to add an GUI component like a Button in a Editor?

I'm a beginner in RCP just started building RCP application today.I want to a GUI component like a Button ,comboBox,Checkbox in a Editor .I've managed to add a editor in Extensions and create a class for it.I have written the code to create a label in creatPartControl but it does not work..I get a black window.Should I add the editor in perspective like this
layout.addStandaloneView(Editor.id, true, IPageLayout.TOP,0.7f,
layout.getEditorArea());
layout.addStandaloneView(View.ID, true, IPageLayout.BOTTOM,0.4f,
layout.getEditorArea());
Please help me resolve this issue.If possible please give an eg on how to add a editor and create a label and a button in it.
Thank you for your help in advance
code in my Editor.java content in createPartControl()
parent.setLayout(new GridLayout());
Button b=new Button(parent,SWT.TOGGLE);
b.setText("Hello ");
Label label1 = new Label(parent, SWT.NONE);
label1.setText("First Name");
package com.hello;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
public class Editor extends EditorPart {
public static final String ID = "TestApplication.editor3";
public Editor() {
// TODO Auto-generated constructor stub
}
#Override
public void doSave(IProgressMonitor monitor) {
// TODO Auto-generated method stub
}
#Override
public void doSaveAs() {
// TODO Auto-generated method stub
}
#Override
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
// TODO Auto-generated method stub
}
#Override
public boolean isDirty() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isSaveAsAllowed() {
// TODO Auto-generated method stub
return false;
}
#Override
public void createPartControl(Composite parent) {
Label label = new Label(parent, SWT.NONE);
label.setText("sssssss");
}
#Override
public void setFocus() {
// TODO Auto-generated method stub
}
}
You are not correctly initializing the editor and it causes problems when opening the editor. Fill up your init() method like below and see if this helps:
#Override
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
setSite(site);
setInput(input);
}
It's been a few years since I worked on an Eclipse editor. Here's a screen capture of the editor so you can see I did more than add a Button.
I extended the Viewer class to create the GUI of the editor.
I extended the EditorPart class to create the functionality of the editor.
Because of the kind of editor I was building, I had to create my own version of Canvas and my own version of IDocument.

Wicket model window throw error when first open in new tab by right click and then click on model window link

When i am going to one page(A) to another page(B) using Ajax link URL show like ...?wicket:interface=:58::::#
On B page i have a link for open model window.when we direct click on link of model window its working fine but when first open link in new Tab by right click and then click on model window link its throwing an error.
I am using setResponsePage( new B(variable)) for come to another page.when i am using setResponsePage(B.class) instead of setResponsePage( new B(variable)) its working fine.
Note : I don't want to use pageparameter with bookmarkable and setResponsePage.
Error is :
org.apache.wicket.WicketRuntimeException: component listForm:group:issueList:1:editStatus not found on page com.B[id = 18], listener interface = [RequestListenerInterface name=IBehaviorListener, method=public abstract void org.apache.wicket.behavior.IBehaviorListener.onRequest()]
org.apache.wicket.protocol.http.request.InvalidUrlException: org.apache.wicket.WicketRuntimeException: component listForm:group:issueList:1:editStatus not found on page com.B[id = 18], listener interface = [RequestListenerInterface name=IBehaviorListener, method=public abstract void org.apache.wicket.behavior.IBehaviorListener.onRequest()]
at ...........................
... 27 more
"editStatus" is a link name on model window.
Code that i am using Class A
class A extends WebPage {
Link<String> escalated = new Link<String>("escalated") {
public void onClick() {
setResponsePage(new B(Variables));
} };
}
class B extends WebPage {
public B(variables..) {
}
final ModalWindow model = new ModalWindow("UpdateModel");
model.setContent(new C(model,variables,model.getContentId()));
item.add(new AjaxLink<Void>(**"editStatus"**) {
public void onClick(AjaxRequestTarget target) {
model.show(target);
}
}.add(new Image("edit_icon", "image/edit.png")));
}
}
class C extends Panel {
public C(.....) {
}
}
I have solved this issue.Error was relating with state of wicket component.
Use StatelessLink instead of Link.
correct code :
class A extends WebPage {
StatelessLink escalated = new StatelessLink("escalated") {
public void onClick() {
setResponsePage(new B(Variables));
} };
}
it would also removed "...?wicket:interface=:58::::#" from url.
when we used Link,AjaxLink it would maintain state.so when we open any link in new tab it would changed state on server side(change ids of component) but on client side it would remain same. so when we click any link on same page there are no information of updated ids and it would have thrown an error.

How to implement content assist's documentation popup in Eclipse RCP

I have implemented my own editor and added a code completion functionality to it. My content assistant is registered in source viewer configuration like this:
public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
if (assistant == null) {
assistant = new ContentAssistant();
assistant.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
assistant.setContentAssistProcessor(getMyAssistProcessor(),
MyPartitionScanner.DESIRED_PARTITION_FOR_MY_ASSISTANCE);
assistant.enableAutoActivation(true);
assistant.setAutoActivationDelay(500);
assistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
}
return assistant;
}
When I press Ctrl + SPACE inside the desired partition, the completion popup appears and works as expected.
And here's my question.. How do I implement/register a documentation popup that appears next to completion popup? (For example in java editor)
Well,
I'll answear the question myself ;-)
You have to add this line
assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
to the configuration above. Then when creating CompletionProposals, the eighth (last) parameter called additionalProposalInfo of the constructor is the text, which will be shown in the documentation popup.
new CompletionProposal(replacementString,
replacementOffset,
replacementLength,
cursorPosition,
image,
displayString,
contextInformation,
additionalProposalInfo);
More information about can be found here.
Easy, isn't it.. if you know how to do it ;)
For the styled information box (just like in JDT).
The DefaultInformationControl instance need to received a HTMLTextPresenter.
import org.eclipse.jface.internal.text.html.HTMLTextPresenter;
public class MyConfiguration extends SourceViewerConfiguration {
[...]
public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
if (assistant == null) {
[...]
assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
}
return assistant;
}
#Override
public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
return new IInformationControlCreator() {
public IInformationControl createInformationControl(Shell parent) {
return new DefaultInformationControl(parent,new HTMLTextPresenter(false));
}
};
}
}
Proposals can then use basic HTML tags in the string from method getAdditionalProposalInfo().
public class MyProposal implements ICompletionProposal {
[...]
#Override
public String getAdditionalProposalInfo() {
return "<b>Hello</b> <i>World</i>!";
}
}