Create view only for specific editor in eclipse - java

How can I show view when specific editor is selected and hide it when another is selected? I was looking for this in extension points but I found nothing :(
Is there any way to do this?

You need to register IPartListener with IPartService and then do appropriate things in partActivated, partBroughtToTop, etc. methods.
Make sure that the part is the editor you want:
public void partActivated(IWorkbenchPart part) {
if (part instanceof MySuperEditor) {
part.getSite().getPage().showView(MySuperView.ID);
}
}
and so on.
Let me know if that helps.

Related

Vaadin unbuffered grids won't close

I am having a weird problem on my Vaadin app. I have a screen with two separate unbuffered grids.
The user is able to edit the data in those two grids and then click a "Save" button to save the changes made.
My problem is that I want to close the editors when the user clicks on "Save".
I tried the following code:
private void closeEditors() {
if (tab1.getEditor().isOpen()) {
tab1.getEditor().closeEditor();
}
if (tab2.getEditor().isOpen()) {
tab2.getEditor().closeEditor();
}
}
I don't understand why this code doesn't work, editors stay opened. I also tried calling the cancel method but in vain.
I am using Vaadin 14.
I am posting this here with not much hope of finding an answer, this problem seems really precise.
But with any luck, maybe someone has experienced a similar issue ?
Maybe there is another glitchier way of forcing my editors to close ?
Any suggestion would be of great help, thanks in advance for anything you could think of !
EDIT: a little more code
This is the grids:
private Grid<Map<String, String>> tab1;
private Grid<Map<String, List<String>>> tab2;
This is the save function
public void saveData() {
saveDataFromTab1();
saveDataFromTab2();
try {
ServicesProxyImpl.getInstance().updateInBD(someObject);
saveButton.setEnabled(false);
cancelButton.setEnabled(false);
closeEditors();
Dialog dialog = VaadinComponentUtils.generateDialog(Constantes.MSG_SAVE_OK);
dialog.open();
} catch (JAXBException e) {
e.printStackTrace();
Dialog dialog = VaadinComponentUtils.generateDialog(Constantes.MSG_SAVE_KO);
dialog.open();
}
}
And this is the save button:
public Button getSaveButton() {
Button saveButton= VaadinComponentUtils.generateButton("Save",
VaadinIcon.CHECK_CIRCLE_O, null, true);
saveButton.setEnabled(false);
saveButton.addClickListener(event -> saveData());
return saveButton;
}
EDIT 2:
I have noticed something, when I click on an element of one of my two grids, I want the editor to open for that specific element and I want to close the editor on the other grid (the one not concerned by the modification). This works ! My grids behave like I want. It seems I am only losing control over my editors after I have actually modified one of the cells and clicked on my save button.
The isOpen function returns false on both grids after I call my closeEditors function, so it seems the grid thinks its editor is closed but it is still opened on my UI.
EDIT 3: I have found a workaround
Well, I have solved my problem by adding a close event listener on both my grids and calling resetGrids when the close event is fired. This function simply removes the grids from the UI, fetches the data to be displayed and then adds the grid one again, both editors being closed. I guess it solves my problem but I would have wanted to understand what was going on...
private void closeEditors() {
tableauHoraires.getEditor().addCloseListener(e -> resetGrids());
tableauRamassagePorteAPorte.getEditor().addCloseListener(e -> resetGrids());
if (tableauRamassagePorteAPorte.getEditor().isOpen()) {
tableauRamassagePorteAPorte.getEditor().closeEditor();
}
if (tableauHoraires.getEditor().isOpen()) {
tableauHoraires.getEditor().closeEditor();
tableauHoraires.getEditor().refresh();
}
}
Make sure that the objects in your grid have proper equals and hashcode methods and that the field(s) being edited do not influence them.
I use the PK from the database.

Set Canvas to Eclipse View?

I've created an (empty until now) View in my Eclipse Plugin. I'm also using Eclipse Cloudio. This library provides the following object (description taken from the linked site):
A TagCloud is a special org.eclipse.swt.widgets.Canvas, dedicated to display a tag cloud.
Basically it's an image showing a WordCloud. Now there are snippets on the site on how to show such TagClouds in a shell / popup.
But I'd like to show them in a view (this function is supposed to be used often and I think it's bad style to spam popup-windows).
What I do not know tho is how to set this TagCloud (which is a Canvas) to the View / make the View display the Canvas. Maybe someone can help me with this?
Edit:
gregs Answer works like a charm! It just needs another setWords() function which is called from where-ever which contains .setWords to set the words when neccessary.
You just need to add the control to the view in the view createPartControl. At the simplest that would be:
#Override
public void createPartControl(final Composite parent)
{
TagCloud cloud = new TagCloud(parent, SWT.NONE);
... set up the cloud as in the example
}

Custom message when closing a part in Eclipse RCP 4

we have the following problem:
In our Eclipse RCP 4 application there are multiple parts and the parts are closable. When the user is closing a part there should be a custom pop-up (depending on some internal part state) which is asking the user if he really wants to close the part or not.
It seems to be not that easy to implement in Eclipse RCP 4 or we have just totally overseen something.
I'll just give you a short brieifing about the things we tried:
Use dirtable with a #persist method in the part. Though the problem is, we don't want this standard eclipse save dialog. So is there a way to override this?
public int promptToSaveOnClose(): This seemed to be promising but not for Eclipse 4 or is there a way to integrate it that way? Compare: http://e-rcp.blogspot.de/2007/09/prevent-that-rcp-editor-is-closed.html
Our last try was to integrate a custom part listener, simple example shown in the following:
partService.addPartListener(new IPartListener() {
public void partVisible(MPart part) {
}
public void partHidden(MPart part) {
partService.showPart(part, PartState.ACTIVATE);
}
public void partDeactivated(MPart part) {
}
public void partBroughtToTop(MPart part) {
}
public void partActivated(MPart part) {
}
});
The problem with this was we are running into a continuous loop. Something similar is posted over here in the last comment: Detect tab close in Eclipse editor
So I could write some more about this problem, but I think that's enough for the moment. If you need some more input just give me a hint.
Thanks for helping.
The save prompt is generated by the ISaveHandler registered in the context of the MWindow containing the MPart. You can write your own ISaveHandler and set it in the window context to replace the default.
You might also want to look at the IWindowCloseHandler also in the window context.
Thanks greg, this has helped and I was able to achieve changing the pop-up when the user closes a part. Here's a short description of what I've done:
Use the MDirtyable for marking the part as dirty whenever it's needed.
Create a custom save handler which implements ISaveHandler (when a part got closed the save method is called). Add the additional logic to this handler (e.g. a custom message dialog)
Register this handler at application start-up (I just chose a method which is called at the start-up):
#Inject
private MWindow window;
...
ISaveHandler saveHandler = new CustomSaveHandler(shell);
window.getContext().set(ISaveHandler.class, saveHandler);
Note that the registration via a model processor was sadly not that easy because the model processor is called too early. (Take a look at: http://www.eclipse.org/forums/index.php/t/369989/)
The IWindowCloseHandler is just needed when the complete window is closed, though this was not an requirement for us :).

Adding JFace wizard pages after addPages() was called?

i am trying to create a jface wizard.
In my wizard i have my "startpage". The options i choose in my "startpage" depending on how many pages will follow. But in my opinion its not possible to do that. Because the addPages() method getting called after the wizard was started. The addPage() method is private. But i need to add my pages there, because when i do it somewhere else, the createControl(Composite parent) don't getting called.
Is there any solution how to solve that problem?
I thought about writing a own method sth. like this:
public void addNewPage() {
Page page = new Page("pagename");
page.createControl(parent);
page.setDescription("");}
...
But it doesn't work.
Do you guys have any solution for my problem?
You could add all your pages in the wizard addPages and then override getNextPage to control which pages is displayed when Next is pressed.
If that is not enough you can always write your own implementation of the IWizard interface.
You can do so by overriding org.eclipse.jface.wizard.Wizard.getNextPage to return a new page if conditions are met (conditionForMorePages in the snippet below):
#Override
public IWizardPage getNextPage() {
IWizardPage nextPage = super.getNextPage(page);
if (nextPage == null) {
if (conditionForMorePages){
// we need an additional page.
IWizardPage nextPage = new MyAdditionalPage();
}
}
return nextPage;
}
If your wizard start with only one page, "back" and "next" buttons do not appear by default. If there is a chance you have more steps coming up dynamically, you want to display the navigation buttons. You can do so by setting the proper flag using the API
public void setForcePreviousAndNextButtons(boolean b)

Eclipse (XText) SelectionListener registration

I have implemented a view which shall register itself as listner for changes in the XText editor and related outline. To this end I am adding this line
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().addSelectionListener(this);
in the createPartControl method of the view (which implements the ISelectionListener interface). In the selectionChanged method I therefore check whether the selection is a ITextSelection, the case in which it comes from the XTextEditor, or an IStructuredSelection, the case in which it come sfrom the Outline.
The problem is that, doing so, when I start Eclipse the outline is said to be "unavailable". If I click on the outline it is refreshed, the contents are shown and the listening view correctly updated.
What am I doing wrong and what should I do to avoid the initial "unavailability" of the outline?
I have been recently faced with this exact problem and have solved it by implementing IPartListener2 in the class that extends ViewPart then adding a part listener in the createPartcontrol method like this :
getSite().getWorkbenchWindow().getPartService().addPartListener(this);
Now by using something like this in your partOpened method (that has to be implemented before you can actually use the part listener you will get the view contents to be avaiable initially :
public void partOpened(IWorkbenchPartReference partRef) {
if(partRef.getPage().getActiveEditor() instanceof XtextEditor) {
somepart=partRef.getPage().getActiveEditor();
final XtextEditor editor = (XtextEditor)somepart;
final IXtextDocument document = editor.getDocument();
document.readOnly(new IUnitOfWork.Void<XtextResource>(){
public void process (XtextResource resource) throws Exception {
IParseResult parseResult = resource.getParseResult();
if(parseResult ==null)
return;
CompositeNode rootNode=(CompositeNode) parseResult.getRootNode();
LeafNode node = (LeafNode)NodeModelUtils.findLeafNodeAtOffset(rootNode, 0);
EObject object =NodeModelUtils.findActualSemanticObjectFor(node);
view.setInput(object);
}
});
}
}
this will make the view you're implementing get it's contents when you activate the XtextEditor (that is specific to your DSL).
In order to make the view change contents in real time as you change anything in the file active in the editor you should implement an IDocumentListener and override the DocumentChanged method. If you do this you won't be dependant on the SelectionListener anymore because the view should update automatically when something changes in your document
Hope this helps!

Categories