Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 13603 - From the JavaPerspective it does open the last used editor
Summary: From the JavaPerspective it does open the last used editor
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 1.0   Edit
Hardware: PC Windows 2000
: P1 critical (vote)
Target Milestone: 2.0 F2   Edit
Assignee: Kai-Uwe Maetzel CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-04-12 06:25 EDT by Joe Winchester CLA
Modified: 2002-05-30 12:50 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joe Winchester CLA 2002-04-12 06:25:57 EDT
I have an editor in my plugin registered against .java.  JDT supplies its own 
(CompilationUnitEditor) so there are two of them.  In the resources perspective 
the last saved editor is used if the file is double clicked.  Also, in the 
resources perspective if there is no last saved editor and the file is double 
clicked the first alphabetic editor name is used.

The JavaPerspective never reads the last saved editor and always opens with the 
first editor assigned to .java.  Also, this is the first editor returned from 
the EditorRegistry's map which is not necessarily the first alphabetic label so 
the behavior is inconcsistent with the resources navigator.  The code to be 
changed is

org.eclipse.jdt.internal.ui.javaeditor.EditorUtility

This is the current code which never checks the last saved editor

public static String getEditorID(IEditorInput input, Object inputObject) {
		
	IEditorRegistry registry= PlatformUI.getWorkbench().getEditorRegistry();
	IEditorDescriptor descriptor= 
registry.getDefaultEditor(input.getName());
	if (descriptor != null)
		return descriptor.getId();
	
	return null;
}

It could be be changed to


public static String getEditorID(IEditorInput input, Object inputObject) {

	IEditorRegistry registry= PlatformUI.getWorkbench().getEditorRegistry();
	// Look to see whether the file has an editor it was last saved with
	if (input instanceof IFileEditorInput){
		String id = 
((IFileEditorInput)input).getFile().getPersistentPropert(EDITOR_KEY);
		if (id != null){
			return id;
		}
	}
	// Get the default editor for the mime type		
	IEditorDescriptor descriptor= 
registry.getDefaultEditor(input.getName());
	if (descriptor != null)
		return descriptor.getId();
	
	return null;
}

Not as important, but nevertheless inconsistent, is that if no editor has been 
used and neither claims to be the default use the first alphabetic label to be 
consistent with the resources perspective.  The code to change is

org.eclipse.ui.internal.registry.FileEditorManager

This just grabs the first editor which is in a collection and has no guarantee 
or order

public IEditorDescriptor getDefaultEditor() {
	if (editors.size() == 0) 
		return null;
	else 
		return (IEditorDescriptor)editors.get(0);
}

This needs changing to use a sorter, which is what the resources perspective 
classes such as OpenWithMenu or OpenFileAction do

public IEditorDescriptor getDefaultEditor() {
	if (editors.size() == 0) 
		return null;
	else {
		// To be consistent with the resource editor, open the first 
alphabetic one
		Object[] editorsArray = editors.toArray();
		editorsArray = sorter.sort(editorsArray);
		return editorsArray[0];
	}
}

private Sorter sorter = new Sorter() {
	private Collator collator = Collator.getInstance();
		
	public boolean compare(Object o1, Object o2) {
		String s1 = ((IEditorDescriptor)o1).getLabel();
		String s2 = ((IEditorDescriptor)o2).getLabel();
		//Return true if elementTwo is 'greater than' elementOne
		return collator.compare(s2, s1) > 0;
	}
};
Comment 1 Joe Winchester CLA 2002-04-12 08:04:13 EDT
More thoughts - the EditorUtility should definitely respect the 
persistenceProperty(EDITOR_KEY), but if there is not one how does it decide then 
?
The resource perspective OpenFileAction uses the first alphabetic label, but 
this is incorrect as it does not carry forward over different NLS.  The 
CompilationUnitEditor does not specify default=true in the extension points 
against .java, however we specify default=false.  Maybe weight it so if someone 
is default=true they win, but if not then get the list of available editors and 
put ones that have no default setting ahead of default=false.  

Alternatively make CompilationUnitEditor be default=true, which is really should 
be as it is the de-facto java source editor
Comment 2 Joe Winchester CLA 2002-04-15 04:54:03 EDT
Because of WSAD 5.0 coming up where the Java Visual Editor is included this is 
causing a lot of problems.  Users who are editing EJBs or .java files that they 
wish to open the JDT editor for are seeing the Java Visual Editor.  I have 
therefore put it up to critical and Prioriry 1 and included Paula Cox ( the WSAD 
5.0 release manager ) on the cc list.
Comment 3 Erich Gamma CLA 2002-04-17 13:16:15 EDT
We should honor the EDITOR_KEY attribute and also set the default attribute to 
true (not rely on sorting etc). 
Comment 4 Kai-Uwe Maetzel CLA 2002-05-09 06:39:42 EDT
The EDITOR_KEY property key is not public. After having determined the editor 
input, we now check whether it's a file editor input. If so, we use the 
workbench support for opening files rather then trying to mimic the workbench. 
I.e. if opening files, we are now consistent with the workbench behavior, 
independent of how this looks.

Build > 20020508.
Comment 5 Joe Winchester CLA 2002-05-22 04:41:15 EDT
Gili (gmendel@us.ibm.com) has tested this and it appears that the last used 
editor is now the one that is opened, but if there are none then the 
CompilationUnitEditorPart is not flagged as default="true" in the plugin.xml.  
Therefore our plugin that also registers a .java editor and is called 
"com.ibm..." comes alphabetically before "org.eclipse..." and we are the 
default.  This means that after doing a Create Class with the wizard, or if you 
just double click on a .java file that hasn't even been opened ( i.e. one that 
was generated by a wizard such as an EJB or one that was imported ) then the 
Java Visual Editor comes up by default.
Given Erich's comment that the editor should be the default one, is this an 
oversight, or is this working as designed that the Java Editor is not registered 
as the default one.  Thanks - JRW
Comment 6 Eduardo Pereira CLA 2002-05-28 13:58:02 EDT
1) A product can specify the default editors for each extension or file name by 
setting "org.eclipse.ui/defaultEditors" in the file plugin_customization.ini. 

e.g., org.eclipse.ui/defaultEditors= 
*.ext1:editorId1;*.ext2:editorId2;fileName:editorId3

So the product can resolve the conflicts if none or more then two editors are 
set as default.

2) Once a file is opened using another editor, it will allways use that editors.
E.g.
    a) Double click in a .txt file will open the DefaultTextEditor. (close it)
    b) Open it with Notepad (close it)
    c) Double click on it and notepad will open instead of DefaultTextEditor.
Comment 7 Erich Gamma CLA 2002-05-30 08:40:44 EDT
The JDT contributed Java editor is now tagged as the default editor for .java 
files. OK to close?
Comment 8 Gili Mendel CLA 2002-05-30 08:54:08 EDT
Yep.   tks.
Comment 9 Erich Gamma CLA 2002-05-30 12:50:19 EDT
closing