I have a plugin with a view that creates a tableviewer based on different files found in the selected project (the workspace has more than one project loaded). My problem is that when I try to reload the view the information remains the same as on the first run after Eclipse started.
What should I do in order to reload the content provider everytime I reload the view ?
To be told about which part is active you need to use IPartListener2. Make your ViewPart implement IPartListener2.
Set up the listener in the createPartControl:
#Override
public void createPartControl(final Composite parent)
{
....
getSite().getWorkbenchWindow().getPartService().addPartListener(this);
}
Remove the listener in dispose:
#Override
public void dispose()
{
super.dispose();
...
getSite().getWorkbenchWindow().getPartService().removePartListener(this);
}
You will have to implement the various methods of IPartListener, most of these don't need to do anything, the partVisible method is called when your view (or any other part) is shown:
#Override
public void partVisible(final IWorkbenchPartReference ref)
{
if (ref.getId().equals("your view id"))
{
// Your view has become visible ... add code here to update the table
}
}
This is how my partVisible looks:
public void partVisible(IWorkbenchPartReference partRef) {
// TODO Auto-generated method stub
if (partRef.getId().equals("view id taken from extensions"))
{
getWorkspacePath();
viewer.remove(TableContent.INSTANCE.getRow());
viewer.setInput(TableContent.INSTANCE.updateContentProvider());
viewer.refresh();
}
}
The path is updated (i've displayed the path in the view) but the content of the table isn't....updateContentProvider contains the call of the functions that need to parse some files in the selected project....
Related
I need to detect when a file (of any type) is opened in Eclipse and run some code when that happens.
I've tried with the following code but it seems to be calling the function multiple times:
Display.getDefault().asyncExec(new Runnable() {
#Override
public void run() {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().addPartListener(new IPartListener2() {
#Override
public void partOpened(IWorkbenchPartReference partRef) {
System.out.println("File opened");
}
});
}
});
Is there a way to do this in Eclipse RCP?
IPartListener2.partOpened is the correct thing to use. Make sure you only set up the listener once.
partOpened will be called for all parts, so you will need to check for the ones you are interested in.
#Override
public void partOpened(final IWorkbenchPartReference partRef)
{
// Check for editor reference and get the editor part
if (partRef instanceof IEditorReference &&
partRef.getPart(false) instanceof IEditorPart editorPart) {
// Example getting current IFile being edited:
IFile file = editorPart.getEditorInput().getAdapter(IFile.class);
}
}
Note: Example code uses Java 16 type pattern, for older releases it will need revising.
Also note Display.asyncExec is not usually needed to set this up.
This gets called when a button is clicked
#Override
public void onFavoriteIconClicked() {
viewModel.isFavoriteExist(test.getId()).observe(getViewLifecycleOwner(), new Observer<Boolean>() {
#Override
public void onChanged(Boolean aBoolean) {
viewModel.isFavoriteExist(test.getId()).removeObserver(this);
if (aBoolean) {
binding.addToFavorite.setImageResource(R.drawable.non_fav);
viewModel.delete(test);
} else if (getActivity() != null) {
Test test2 = new Test(test.getId(), test.getName());
viewModel.insert(test2);
binding.addToFavorite.setImageResource(R.drawable.fav);
}
}
});
}
If the test object exists in the Favorite database, I have to delete it. After deleting, it calls this again (since it observed a chane) and inserts it again.
It keeps looping infinitely. Is there a better way to implement this or stop this?
It seems like some business logic has entered your view (Activity) class.
Since LiveData & Room are meant to be used when receiving updates about Database changes is needed, and your use of the DB is not requiring constant updates, I would suggest going with a more direct approach.
First, Remove the use of LiveData from your Database. Use simple return values.
Your view (Activity/Fragment) can then tell the view model that a button was clicked.
#Override
public void onFavoriteIconClicked() {
viewModel.onFavoriteClicked()
}
The view will observe the view model in order to receive the correct icon to show.
Something like:
viewModel.favoriteIcon.observe(getViewLifecycleOwner(), new Observer<Integer>() {
#Override
public void onChanged(Integer iconResId) {
binding.addToFavorite.setImageResource(iconResId)
}
}
Now the viewModel can handle the logic (or better add a Repository layer - See Here)
Upon click, Check if entry exist in DB.
If exists: remove it from DB and set favoriteIcon value:
favoriteIcon.setValue(R.drawable.non_fav)
If doesn't exist: Add it to DB and set favoriteIcon value.
favoriteIcon.setValue(R.drawable.fav)
For a good tutorial about using Room & LiveData - as well as doing so using the View/ViewModel/Repository pattern, check this link
I am developing an eclipse plug in with a tree viewer. Initially I had a single treeview of which I have displayed information of some elements in the standard eclipse properties tab. That worked without problems.
I have followed an example where I implement the IPropertySource and IAdapterFactory. In the method createPartControl() of the view I call
getSite().setSelectionProvider(searchViewer);
which registers the properties.
Now I have added an swt tabfolder item to the plug in. Now In every new tabitem a treeview is displayed. That works fine, but the information in the properties tab are not shown correctly anymore. There's a strange behaviour though. On the tree elements which are of interest I have also added a doubleclick listener to do other things. After I double click an entry and right after single click on another element, the properties are shown for the doubleclicked element?!
I guess the problem is with the SelectionProvider. But I was not able to figure out how to implement it correctly now
The properties view always shows the properties for the object that the selection provider says is the current selection.
If you have multiple tree views on several tabs you will have to write a custom selection provider (ISelectionProvider) that knows which tree is currently active and provides the appropriate selection.
For example, the following is a selection provider used by the JDT code for some things which you could use as a base:
public class SimpleSelectionProvider implements ISelectionProvider {
private final ListenerList<ISelectionChangedListener> fSelectionChangedListeners;
private ISelection fSelection;
public SimpleSelectionProvider() {
fSelectionChangedListeners = new ListenerList<>();
}
#Override
public ISelection getSelection() {
return fSelection;
}
#Override
public void setSelection(ISelection selection) {
fSelection= selection;
for (ISelectionChangedListener listener : fSelectionChangedListeners) {
listener.selectionChanged(new SelectionChangedEvent(this, selection));
}
}
#Override
public void removeSelectionChangedListener(ISelectionChangedListener listener) {
fSelectionChangedListeners.remove(listener);
}
#Override
public void addSelectionChangedListener(ISelectionChangedListener listener) {
fSelectionChangedListeners.add(listener);
}
}
(org.eclipse.jdt.internal.ui.actions.SimpleSelectionProvider)
You would have to make all your trees call the setSelection method when selections change.
I have:
ListView
Button
WebMarkupContainer (Popup content container)
So, when I populate the ListView, I add an AjaxEventBehavior to the buttons. I also override getAjaxCallDecorator(), as I need to call a javascript function from each button. So, in the decorateScript function, I override the WebMarkupContainer markup id by using setMarkupId(), it works. I do the same for the Button, and it works, at least when I call getMarkupId(). But when I go to the generated HTML, it's not there! Why is this happening?
The code is the following (it is inside of the populateItem from ListView):
infoBtn.add(new AjaxEventBehavior("onclick") {
#Override
protected void onEvent(AjaxRequestTarget target) {
}
#Override
protected IAjaxCallDecorator getAjaxCallDecorator() {
return new IAjaxCallDecorator() {
#Override
public CharSequence decorateScript(Component component, CharSequence script) {
StringBuilder jsScript = new StringBuilder();
infoPopoverContent.setMarkupId(infoPopoverContent.getMarkupId(true) + String.valueOf(pos));
infoBtn.setOutputMarkupPlaceholderTag(true);
infoBtn.setMarkupId(infoBtn.getMarkupId() + String.valueOf(pos));
jsScript.append("$('#" + infoBtn.getMarkupId() + "').popover({");
jsScript.append("html:true,");
jsScript.append("placement:'bottom',");
jsScript.append("content:function() {");
jsScript.append("return $('#");
jsScript.append(infoPopoverContent.getMarkupId());
jsScript.append("').html();");
jsScript.append("}");
jsScript.append("});");
logger.debug(jsScript.toString());
pos++;
return jsScript;
}
#Override
public CharSequence decorateOnSuccessScript(Component component,
CharSequence script) {
// TODO Auto-generated method stub
return null;
}
#Override
public CharSequence decorateOnFailureScript(Component component,
CharSequence script) {
// TODO Auto-generated method stub
return null;
}
};
}
});
Make sure you're calling infoBtn.setOutputMarkupId(true), so that Wicket knows it should be outputting the id attribute.
Notice setOutputMarkupPlaceholderTag(true) is also calling setOutputMarkupId(true) behind the scenes. Without knowing much about your code, it looks like you don't really need it. setOutputMarkupPlaceholderTag() will output an emtpy container (<span id="xxx"> in case the component is not visible, just to have a reference to the place the component belgons to, and allowing Wicket for manipulation via DOM in the AJAX response (for example, to make the component visible again).
As a side note, if you don't really need the id attributes to have a specific value, you can simplify your code by not using setMarkupId() and letting Wicket generate ids for you.
Also, it might be simpler to drop the IAjaxCallDecorator and just append the script in onEvent by means of AjaxRequestTarget#appendJavascript() or AjaxRequestTarget#prependJavascript(), depending on your needs.
I am Working with GWT and eclipse. I am facing a problem . The design mode of eclipse is not working. I am using the eclipse 3.7 . Where is the problem?
One way to see design view is extending the view part. you can try this one.
public class test extends ViewPart {
public test() {
// TODO Auto-generated constructor stub
}
#Override
public void createPartControl(Composite parent) {
// TODO Auto-generated method stub
}
#Override
public void setFocus() {
}
}
You need to right click the file you want to open, then click open with --> GWT Designer.
Make sure your resource exclusion filters are not too strict.
If you are using Maven, the issue is explained here.