I have a custom Eclipse plugin I am working on. It has a CustomerExplorer View(Part) that effectively replaces the 'Project Explorer' and it has a number of MultiPageEditorParts, each has a XML editor and a Form editor, both modifying the same configuration file. The XML editor is a TextEditor. The form and the XML are linked and each updates the other whenever there is a pageChange().
My problem lies in the external modification of files opened in my Eclipse plugin. If I edit a file in an external editor and then load it (from CustomerExplorer View), upon switching to the XML tab I will recieve the message:
"Resource is out of sync with the file system: '/example/example.xml'.
Press 'F5' or select File > Refresh to refresh the file.
I am familiar with this error from using Eclipse and usually simply pushing F5, right clicking the file in question and choosing refresh or choosing File > Refresh from the menu bar usually solves it. However, in this case F5 does nothing, File > Refresh is greyed out and right clicking on the file (in my custom view), the context menu does not contain 'refresh'. I have tried opening the 'Project Explorer' view, where 'Refresh' is available in the context menu, but this does nothing.
From reading around I have been led to understand that these resources should be refreshed by eclipse but they are not. Any pointers as to why?
Hard to say exactly what is happening here, but you may need to explicitly add the action set for refreshing resources to your custom view.
Another way of solving your issue might be to track change to the file an update your view when there is one:
ResourcesPlugin.getWorkspace().addResourceChangeListener(new MyResourceTracker(), IResourceChangeEvent.POST_CHANGE);
With something like that:
public class MyResourceTrackerimplements IResourceChangeListener {
#Override
public void resourceChanged(final IResourceChangeEvent ev) {
if (ev.getDelta() != null) {
try {
ev.getDelta().accept(new IResourceDeltaVisitor() {
#Override
public boolean visit(final IResourceDelta delta)
throws CoreException {
// TODO do something
return true;
}
});
} catch (CoreException e) {
// TODO
}
}
}
}
Related
I'm writing a custom editor in Eclipse and just integrated custom error recognition. Now I'm facing a strange issue: I can add Markers to my editor that get displayed all fine, I can also delete them while the editor is running.
What doesn't work: When I close my editor I want the markers to disappear/get deleted.
What I'm doing right now, is
creating the Markers with the transient property set like this: marker.setAttribute(IMarker.TRANSIENT, true); This doesn't seem to change anything though.
trying to delete all Annotations via the source viewers annotation-model. This doesn't work, cause when I try to hook into my editors dispose() method or add a DisposeListener to my sourceviewers textwidget, the sourceviewer already has been disposed of and getSourceViewer().getAnnotationModel(); returns null.
My deleteMarkers method:
private void deleteMarkers() {
IAnnotationModel anmod = getSourceViewer().getAnnotationModel();
Iterator<Annotation> it = anmod.getAnnotationIterator();
while (it.hasNext()) {
SimpleMarkerAnnotation a = (SimpleMarkerAnnotation) it.next();
anmod.removeAnnotation(a);
try {
a.getMarker().delete();
} catch (CoreException e) {
e.printStackTrace();
}
}
}
Any help is appreciated ^^
Hook into your editor close event, get a reference to the IResource for the editor (i believe you get can that on IEditorInput) and call IResource#deleteMarkers() on the relevant resource which will delete them when you close your editor. By design eclipse does not delete markers when editors are closed.
Here is some reference:
http://help.eclipse.org/kepler/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/resources/IResource.html#deleteMarkers(java.lang.String, boolean, int)
Can we make a button using SWT in eclipse that can refresh our Project made in eclipse ? if yes then how ? thank you in advance .
You'll have to write a plugin to create your button and hook it where you need (see . then,
Button button = ...
button.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelection(SelectionEvent e) {
ResourcePlugin.getWorkspace().getRoot().getProject("myProject").refreshLocal(IResource.DEPTH_INFINITE, null);
}
}
See also https://wiki.eclipse.org/FAQ_When_should_I_use_refreshLocal%3F
However, there is already the "Refresh" context menu in Project Explorer for that. I don't really get why a new button would help. Also, if you know that you sometimes need to refresh a project, why don't you do it anyway when you detect it can be useful without requiring user to click on a button?
I have an eclipse plugin which configures a set of resources as a customized project and runs it. I have added run and debug choices to "Run As" and "Debug As" menus and also shortcuts in the tool bar. So when I put mouse over "Run As", I have two submenus:
Run on Server
My Customized project
I can click on "My customized project" and everything runs as desired.
However after I ran it once, I no longer have "My customized project" as a "Run As" submenus, it's gone. "Run on Server" is also gone and instead I have an "XSL Transformation" there.
I found a post with similar question here:
http://www.sigasi.com/content/run-menu-item-strangely-disappearing-context-menu
but his solution won't work for me. I already have the extension point defined.
Does anyone have an idea why this happens?
UPDATE:
I think I know where exactly the problem is but I still have no solution.
I have the following code when a customized project is finished running:
IJobManager manager = Job.getJobManager();
manager.addJobChangeListener(new JobChangeAdapter() {
#Override
public void done(final IJobChangeEvent event) {
if (event.getJob().getName() == "Processing with selected modules" && event.getResult() == Status.OK_STATUS) {
Display.getDefault().syncExec(new Runnable() {
#Override
public void run() {
actionExport.setEnabled(true);
try {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView("customized.output");
} catch (CoreException e) {
e.printStackTrace();
}
}
});
manager.removeJobChangeListener(this);
}
}
});
the line PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView("customized.output"); is the source of the problem. I just want to switch to another view when the project finishes to show the result. There will be no problem with disappearing "Run As" submenu if I remove this line. But I want to show the view after running a project. Anyone has any clue about it?
I want to know which file (or even project is enough) is opened in eclipse editor? I know we can do this once we get IEditorPart from doSetInput method,
IFile file = ((IFileEditorInput) iEditorPart).getFile();
But I want the name of file without using IEditorPart, how can I do the same?
Checking which is the selected file in project explorer is not of much help because, user can select multiple files at once and open all simultaneously and I did not way to distinguish which file opened at what time.
Adding more info:
I have an editor specified for a particular type of file, now every time it opens, during intializing editor I have some operation to do based on project properties.
While initializing editor, I need the file handle (of the one which user opened/double clicked) or the corresponding project handle.
I have my editor something this way:
public class MyEditor extends TextEditor{
#Override
protected void initializeEditor() {
setSourceViewerConfiguration(new MySourceViewerConfiguration(
CDTUITools.getColorManager(), store,
"MyPartitions", this));
}
//other required methods
#Override
protected void doSetInput(IEditorInput input) throws CoreException {
if(input instanceof IFileEditorInput)
{
IFile file = ((IFileEditorInput) input).getFile();
}
}
}
as I have done in the doSetInput() method , I want the file handle(even project handle is sufficient). But the problem is in initializeEditor() function there is no reference to editorInput, hence I am unable to get the file handle.
In the source viewer configuration file, I set the code scanners and this needs some project specific information that will set the corresponding rules.
You never have the guarantee (even with IFileEditorInput) to know which files are "open" with editors.
There is even no definition of an "opened file" there are for example editors that show the contents of multiple files (like the Plug-In Manifest Editor from PDE). Other editors show only the contents of a URI (which maybe local)
Could you explain which problem you are trying to solve?
I'm currently programming on an eclipse RCP application in Java for an university project.
My problem is that I want an editor loaded at application start, but I don't know which method is the right one to start with. In the perspective I can only add views and set my editor space, but I can't set any editors.
I tried overwrite the WorkbenchWindowAdvisor.postWindowOpen() method, but this only got me an exception...
You say you got an exception.. what was it? How did you overwrite postWindowOpen(), can you post your code? I could help you more if I knew these things.
Anyway, the following code opens the editor at application startup:
#Override
public void postWindowOpen() {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try {
page.openEditor(editorInput, editorId);
} catch (PartInitException e) {
// Handle the exception here
}
}
where "editorInput" is the input of your editor and "editorId" it's ID.
Also, I highly recommend reading Lars Vogel's tutorial on editors:
http://www.vogella.de/articles/EclipseEditors/article.html