This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 103235

Summary: Setting kit changes most active document
Product: editor Reporter: Petr Hrebejk <phrebejk>
Component: -- Other --Assignee: apireviews <apireviews>
Status: RESOLVED FIXED    
Severity: blocker CC: dbalek, err, mmetelka, sandipchitale
Priority: P2 Keywords: API, API_REVIEW_FAST
Version: 6.x   
Hardware: All   
OS: All   
Issue Type: DEFECT Exception Reporter:
Attachments: Diff of the EditorRegistry addition and its use in CompletionImpl

Description Petr Hrebejk 2007-05-03 13:24:19 UTC
API call to JEditorPane's kitt setting to Java changes the most active document.
This is bad as it causes code completion not to work when the IDE starts with a
file opened. The actual cause for it is the Declaration view window in
java/navigation which initializes its content crates a JEditoPane and sets the
mime type to text/x-java. This can be fixed so that it will do only when the
window is showing. But such fix would not be the correct as the bug may happen
again if someone else does the call. The document should be changed only on
focus change not after the API call, I guess.
Comment 1 Vitezslav Stejskal 2007-05-06 22:53:59 UTC
Ok, I think can see the problem, its solution is probably not going to be that
simple. 

First, please stop using JEP.setContentType (see
http://editor.netbeans.org/doc/editor-kit-registry.html). The right way to go
about that is basically
JEP.setEditorKit(CloneableEditorSupport.getEditorKit("text/x-java"));

The problem is that calling JEP.setEditorKit recreates and resets a document on
the JEP, which in turn means that the Registry changes the active document. I'm
not sure what the rules for code completion are, but perhaps it should stop
using Registry and track only the real editors by using
Utilities.actionsGlobalLookup().lookup(EditorCookie.class).

Regarding the Registry, its API has never been properly defined and we should
either deprecate it and stop using it, if we can cover all the existing usecases
by something else (eg. U.aGL) or design something new that would cover them.
There was a discussion about this going on on @nbdev some time ago.
Comment 2 Miloslav Metelka 2007-05-07 10:17:34 UTC
There is a usecase for word-matching to match a word in all the opened documents
in a LRU manner. I see no way how to cover this by U.aGL().

I agree that the Registry should only react to focus changes. It would IMHO be
much cleaner. I will attempt to do some rework to allow that.
Comment 3 Miloslav Metelka 2007-05-31 10:17:15 UTC
I'm already working on this. 
Comment 4 Dusan Balek 2007-06-02 00:43:41 UTC
*** Issue 104406 has been marked as a duplicate of this issue. ***
Comment 5 Miloslav Metelka 2007-06-04 09:57:48 UTC
I've created a new EditorRegistry in editor/lib2's API. I've looked at the
current usage of the old o.n.editor.Registry and it seems that it's only
necessary to
1) Get currently focused and last focused text components. E.g. for code completion.
2) Get list of all registered text components in MRU order for word-match.
3) Allow explicit registrations of text components. We do not want to have all
the JTextField etc. in the registry.

The EditorRegistry contains
JTextComponent lastFocusedComponent() - a registered component that had (or
still has) focus.
JTextComponent focusedComponent() - a lastFocusedComponent() if it still has
focus or null otherwise.
List<JTextComponent> componentList() - a list of all registered components in
MRU-focused order.

EditorRegistry fires PropertyChangeEvent for the following properties:
"focusGained" - when a registered text component gets a focus (it gets moved to
the head of the component list).
"focusLost" - when a registered text component has lost a focus. Not all clients
will react to this event.
"focusedDocument" - when "document" property of registered and focused
JTextComponent changes.

The impl is IMHO simple and straightforward with tests coverage and logging. I
had also considered updates to the old Registry but there are differences in
working and firing so I've ended up with a new EditorRegistry class.
 BTW a nice side-effect is that there should be less firing that with the old
Registry because e.g. the initial document switches in an JEditorPane:
PlainDocument -> empty "right" document (e.g. JavaDocument) -> loaded
JavaDocument is eliminated and there is just a single "focusGained" that comes
after all that docs switching.
I have rewritten a completion to use the new EditorRegistry and so the original
problem is fixed by that. I would then rewrite the rest of the occurrences.
Attaching diff.
Comment 6 Miloslav Metelka 2007-06-04 10:06:22 UTC
Created attachment 43170 [details]
Diff of the EditorRegistry addition and its use in CompletionImpl
Comment 7 Miloslav Metelka 2007-06-04 10:10:24 UTC
Asking for fasttrack review.
Comment 8 Vitezslav Stejskal 2007-06-04 22:33:13 UTC
It's looking good, certainly much better that the old one :). Just a few small
things.

VS1: Make EditorRegistry final, with private constructor.
VS2: Should not componentList return List<? extends JTextComponent> ?
VS3: Does the register method have to be public? Who is supposed to call it?
Comment 9 _ tboudreau 2007-06-05 05:42:49 UTC
Note current discussion on dev@openide about this issue - is this going to make
it for 6.0?
Comment 10 Miloslav Metelka 2007-06-05 08:55:59 UTC
ad VS1, VS2: obviously done, thanks.
ad VS3: thanks to impl dep. editor lib->editor lib2 I can make it non-public
since the only present client is BaseTextUI. We can possibly publicize later if
other clients show up.

Definitely planned for 6.0. I would like to commit sooner, possibly tomorrow.
Comment 11 Miloslav Metelka 2007-06-07 20:51:14 UTC
Fixed.
Checking in libsrc/org/netbeans/editor/BaseTextUI.java;
/cvs/editor/libsrc/org/netbeans/editor/BaseTextUI.java,v  <--  BaseTextUI.java
new revision: 1.87; previous revision: 1.86
done
RCS file:
/cvs/editor/lib2/src/org/netbeans/modules/editor/lib2/EditorApiPackageAccessor.java,v
done
Checking in lib2/src/org/netbeans/modules/editor/lib2/EditorApiPackageAccessor.java;
/cvs/editor/lib2/src/org/netbeans/modules/editor/lib2/EditorApiPackageAccessor.java,v
 <--  EditorApiPackageAccessor.java
initial revision: 1.1
done
Checking in completion/nbproject/project.xml;
/cvs/editor/completion/nbproject/project.xml,v  <--  project.xml
new revision: 1.5; previous revision: 1.4
done
RCS file: /cvs/editor/lib2/src/org/netbeans/api/editor/EditorRegistry.java,v
done
Checking in lib2/src/org/netbeans/api/editor/EditorRegistry.java;
/cvs/editor/lib2/src/org/netbeans/api/editor/EditorRegistry.java,v  <-- 
EditorRegistry.java
initial revision: 1.1
done
Checking in lib2/nbproject/project.properties;
/cvs/editor/lib2/nbproject/project.properties,v  <--  project.properties
new revision: 1.5; previous revision: 1.4
done
Checking in lib2/nbproject/project.xml;
/cvs/editor/lib2/nbproject/project.xml,v  <--  project.xml
new revision: 1.4; previous revision: 1.3
done
RCS file:
/cvs/editor/lib2/test/unit/src/org/netbeans/api/editor/EditorRegistryTest.java,v
done
Checking in lib2/test/unit/src/org/netbeans/api/editor/EditorRegistryTest.java;
/cvs/editor/lib2/test/unit/src/org/netbeans/api/editor/EditorRegistryTest.java,v
 <--  EditorRegistryTest.java
initial revision: 1.1
done
Checking in
completion/src/org/netbeans/modules/editor/completion/CompletionImpl.java;
/cvs/editor/completion/src/org/netbeans/modules/editor/completion/CompletionImpl.java,v
 <--  CompletionImpl.java
new revision: 1.71; previous revision: 1.70
done
Checking in lib2/apichanges.xml;
/cvs/editor/lib2/apichanges.xml,v  <--  apichanges.xml
new revision: 1.4; previous revision: 1.3