The standard behavior of the Eclipse workbench is to preserve the set of open files between invocations, and attempt to reopen those same files on restart. If any of the files is missing, then a placeholder editor appears showing an error message about the missing file. We'd like to change the behavior of our Eclipse RCP application to just silently skip any missing files instead.
We're already writing our own IApplication, WorkbenchAdvisor, etc; these classes get to inject various behaviors into the platform, but I haven't found a way to accomplish these through these classes. How could we implement the desired behavior?
The way I've handled this is actually in the editor being created: Override setInput to examine the IEditorInput being passed in for validity by calling editorInput.exists() (which, in the case of a FileEditorInput, checks to see if the file exists) or if you're using custom editor inputs, any other validation you need.
If the editorInput fails validation, then close the editor asynchronously (Eclipse doesn't like it when an editor closes before it finishes opening):
public void close () {
Display.getDefault().asyncExec(new Runnable () {
public void run () {
getSite().getPage().closeEditor(YourEditorClass.this, false);
}
});
}
An alternative way of handling this issue is to just disable reopening the editors on startup - see In Eclipse, how to close the open files(editors) when exiting without auto-load in next startup
Related
I am working on eclipse form based editor. I have given support of handling of Undo Redo and dirty flag to my editor.Both of these feature working fine for single instances of plugin. Problem is coming when i open it with 2 or more files (2 or more instances of eclipse plugin). Now, undo redo starts working weird. They work only for instances that is opened at last.
for eg: Suppose my editor supports '.xeb' file. if i open test1.xeb and test2.xeb files one by one using with my editor. then undo redo only works for instances that is opened for test2.xeb file. If i switch back to other instances, then undo redo of first instance gets appear.
i have below entries in my editor's plugin.xml:
<plugin><extension
point="org.eclipse.ui.editors">
<editor
class="Testeditor"
default="true"
extensions="xeb"
icon="icons/sample.gif"
id="testeditor"
name="editor">
</editor>
</plugin>
i debugged the code and found that this weird behavior is happening due to handling of global action in wrong way.I used below code to set global action handler:
public void setUndoRedoActionHandlers() {
final IActionBars actionBars = getEditorSite().getActionBars();
actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(),
mUndoAction);
actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(),
mRedoAction);
actionBars.updateActionBars();
}
i went through some links related to this issue. but couldn't understood the concept to implement this behavior.
http://wiki.eclipse.org/FAQ_How_do_I_find_out_what_view_or_editor_is_selected%3F
http://wiki.eclipse.org/FAQ_How_do_I_hook_into_global_actions,_such_as_Copy_and_Delete%3F
Can any one look into this issue. Thanks in advance.
only override the setFocus() method of MultiPageEditorPart in you editor class and call the appropriate method of setting the global action handler,like this way:
#Override
public void setFocus() {
switch (getActivePage()) {
case 0:
pageOne.setUndoRedoActionHandlers();
break;
case 1:
pageTwo.setUndoRedoActionHandlers();
break;
}
super.setFocus();
}
So, I’m currently developing a plugin for the eclipse IDE. In a nutshell, the plugin is a collaborative real time code editor where the editor is eclipse (which is something like Google documents but with the code and on eclipse). Meaning that when I install the plugin, I would be able to connect -using my Gmail account- eclipse to the partner’s eclipse. And when I start coding on my machine, my partner would be seeing what I write and vice versa.
The problem I’m currently facing is accessing eclipse’s editor. For example, I have to monitor all the changes in the active document so that every time a change happens, the other partner’s IDE would be notified with this change.
I found and read about the IDcoumentProvider, IDocument and IEditorInput classes and they’re somehow connected but I can’t understand this connection or how to use it. So if someone can explain this connection I would really appreciate it. Also if there is another way to achieve my goal?
You can access the IEditorPart via the IWorkbenchPage.
IEditorPart editor = ((IWorkbenchPage) PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()).getActiveEditor();
From there, you have access to various other classes, including the editor's IEditorInput, the File loaded by that editor, or the underlying GUI Control element. (Note that depending on the kind of editor (text files, diagram, etc.) you may have to cast to different classes.)
FileEditorInput input = (FileEditorInput) editor.getEditorInput();
StyledText editorControl = ((StyledText) editor.getAdapter(Control.class));
String path = input.getFile().getRawLocationURI().getRawPath();
Now, you can add a listener to the Control, e.g. a KeyAdapter for monitoring all key strokes occurring in the respective editor.
editorControl.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
System.out.println("Editing in file " + path);
}
});
Or, if monitoring all key strokes is too much, you can register an IPropertyListener to the editor. This listener will e.g. be notified whenever the editor gets 'dirty' or when it is saved. The meaning of propId can be found in IWorkbenchPartConstants.
editor.addPropertyListener(new IPropertyListener() {
#Override
public void propertyChanged(Object source, int propId) {
if (propId == IWorkbenchPartConstants.PROP_DIRTY) {
System.out.println("'Dirty' Property Changed");
}
}
});
We are using NetBeans Platform 7.0.1, and have implemented support for a new language using this (now “obsolete”) tutorial.
Since all our contents are stored in a database, and not on files, we open them like this:
FileSystem fs = FileUtil.createMemoryFileSystem();
FileObject fo = fs.getRoot().createData(fileName, fileExtension);
… write contents from database to `fo` ….
DataObject data = MyMultiDataObject.find(fo);
EditorCookie.Observable cookie = data.getCookie(EditorCookie.Observable.class);
cookie.open();
… forces undock of editor window …
And, in our layer.xml, have added a custom button to Save that sends the content back to the database.
However, when the user closes the file (by either closing the tab or the window), we haven’t figured a way of saving it.
Adding a PropertyChangeListener to the Cookie and watching for PROP_DOCUMENT (and newValue() == null) seems to do the trick for when the window is closed. But how does one get the return value from the confirmation window (I’m referring to when the file is closed after changes, the message File xxx.xxx is modified. Save it?)?
Well, it seems we've been approaching the problem in the wrong way.
Since we are opening the file in-memory, it was suggested in the netbeans-dev list that we should listen for changes in the file itself, by using
fo.addFileChangeListener(new CustomFileChangeListener());
public class CustomFileChangeListener implements FileChangeListener {
#Override
public void fileChanged(FileEvent fe) {
... file has been saved in the editor, sync with database ...
}
}
And keep it synchronized that way, taking advantage of the built-in NetBeans Platform "save" functionality.
Is there any easy to follow tutorial for debugging Java / J2EE applications in eclipse?
A step by step guide on how to check for unchecked and checked exceptions?
I have been trying to find on the internet, but to no use.
To add a Java Exception Breakpoint: Select the "Breakpoints" view in the Debug perspective and click on the view toolbar button for exception breakpoints (or choose Run->Add Java Exception Brekakpoint). In the next dialog, type the name of the exception (camel case is support, simply type NPE if you want to catch NullPointerExceptions, you'll get a list matching items), select it from the list and press OK. C'est ca.
You can activate/deactivate this special breakpoint from the Breakpoints view like normal breakpoints.
More fun: right click on a breakpoint entry in the Breakpoints view and choose "Breakpoint Properties". There you can add extra conditions, like 'only break when myCustomString.equals("WTF")' or something else. But conditional breakpoints will slow down the application significantly, only activate them if you really need them.
The debugger in Eclipse will automatically suspend execution (and let you inspect variables/step/resume etc) if an uncaught exception is thrown.
Enter for instance the following program...
public class Test {
public void methodA() {
methodB("hello");
methodB(null);
}
public void methodB(String s) {
System.out.println(s.substring(2));
}
public static void main(String[] args) {
new Test().methodA();
}
}
... and click the little "bug"-icon or choose Run -> Debug As -> Java Application
A few useful tutorials
Debugging a Java Program with Eclipse
Java Debugging with Eclipse - Tutorial
So I made a standard swing application in Netbeans 6.8 but I can't find where the resource file that defines the localized string for the Exit menu item under File.
It doesn't seem to be defined among the resource files in <project>/resources. Is this a standard string somewhere or am I missing something?
The "Exit" string doesn't get defined as a localized string like the File menu item -- it gets generated into code through the Netbeans GUI builder. That is why you don't see it in the properties file.
If you open the file {ProjectName}View.java you can see that it gets defined through the netbeans GUI builder. Click on the file menu and then the exit menu item in the GUI builder and you can look at the properties of the swing item. One of the properties is the text of the JMenuItem.
Then these properties get generated into code.
Are you just trying to shut down your application in such a way that you are able to do clean-up tasks before the system shuts down? If so then on the Java Sun site they say that you need to override the shutdown method; specifically :
#Override
protected void shutdown() {
// The default shutdown saves session window state.
super.shutdown();
// Now perform any other shutdown tasks you need.
}
This is located at this location