Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Correct instantiation and configuring of JavaSourceViewer
Correct instantiation and configuring of JavaSourceViewer [message #260656] Tue, 16 June 2009 19:24 Go to next message
Alexander Kiselyov is currently offline Alexander KiselyovFriend
Messages: 3
Registered: July 2009
Junior Member
Hi, I've got a little problem. I'm trying to create view in my plug-in,
which will contain Java editor with syntax highlighting and other stuff.
For this task I decided to use
org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer.
Here is fragment of code from the
createPartControl(org.eclipse.swt.widgets.Composite) method of my view:

IPreferenceStore store = new ScopedPreferenceStore(new InstanceScope(),
Activator.getDefault().getBundle().getSymbolicName());
int styles = SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.BORDER |
SWT.FULL_SELECTION;
JavaSourceViewer viewer = new JavaSourceViewer(parent, null, null, false,
styles, store);

Activator is a main class of my plug-in (extends from AbstractUIPlugin).
This fragment works fine, except 2 things. First, I can't see text I set
such way:

viewer.getTextWidget().setText("sdlfknjhsadkjlfhn,skdf");

I've explored code of method and decided to change it to:

viewer.getTextWidget().getContents().setText("sdlfknjhsadkjlfhn,skdf ");

(text widget's setText() method have a lot of intermediate code, event
handling, etc and I think its incorrect to do it my way, in contravention
of widget's setText())

And second. As I understood, syntax highliting and other such behavior are
provided by JavaSourceViewerConfiguration class.

JavaSourceViewerConfiguration configuration =
new JavaSourceViewerConfiguration(new JavaColorManager(), store,
this, IJavaPartitions.JAVA_PARTITIONING);
viewer.configure(configuration);

But when I'm tryed to instantiate it and configure viewer with it, I've
got an NPE:

java.lang.NullPointerException
at
org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration.getSet tings(JavaSourceViewerConfiguration.java:813)
at
org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration.getCon tentAssistant(JavaSourceViewerConfiguration.java:392)
at
org.eclipse.jface.text.source.SourceViewer.configure(SourceV iewer.java:370)
at
org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer.conf igure(JavaSourceViewer.java:223)
at
com.codesrc.thinktankrcp.projects.FileContentsView.createPar tControl(FileContentsView.java:161)
...

So, I've investigated it and saw that
org.eclipse.jdt.internal.ui.JavaPlugin.getDefault() in
JavaSourceViewerConfiguration.java:813:

IDialogSettings settings=
JavaPlugin.getDefault().getDialogSettings().getSection(secti onName);

returned null. Also I tryed to experiment with
org.eclipse.jdt.internal.ui.javaeditor.JavaEditor and got a lot of similar
NPE in same situations from such classes:
org.eclipse.jdt.internal.ui.JavaPlugin,
org.eclipse.ui.internal.editors.text.EditorsPlugin,
org.eclipse.jdt.core.JavaCore. I've tried to fill it's values by
reflection :), it works, but at first it's a terrible ugly hack, and in
second - I've started to receive many other exceptions in many places, so
it is not correct way. As I understood, something in the Eclipse Platform
should correctly initialize, prepare and tune such classes. Can you help
me with it? Maybe I should differently set up my plug-in project, include
some dependencies to it or something else?
Re: Correct instantiation and configuring of JavaSourceViewer [message #260671 is a reply to message #260656] Wed, 17 June 2009 07:56 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
Alexander Kiselyov wrote:
> Hi, I've got a little problem. I'm trying to create view in my
> plug-in, which will contain Java editor with syntax highlighting and
> other stuff. For this task I decided to use
> org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer.
Note that this is an internal class which you should not use (try
SourceViewer instead). Having said that, it looks like for some reason
the org.eclipse.jdt.ui plug-in is not loaded and hence you get the NPE.
Make sure that plug-in is listed as required plug-in in your plug-in.

Dani
> Here is fragment of code from the
> createPartControl(org.eclipse.swt.widgets.Composite) method of my view:
>
> IPreferenceStore store = new ScopedPreferenceStore(new InstanceScope(),
> Activator.getDefault().getBundle().getSymbolicName());
> int styles = SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.BORDER |
> SWT.FULL_SELECTION;
> JavaSourceViewer viewer = new JavaSourceViewer(parent, null, null,
> false, styles, store);
>
> Activator is a main class of my plug-in (extends from
> AbstractUIPlugin). This fragment works fine, except 2 things. First, I
> can't see text I set such way:
> viewer.getTextWidget().setText("sdlfknjhsadkjlfhn,skdf");
>
> I've explored code of method and decided to change it to:
>
> viewer.getTextWidget().getContents().setText("sdlfknjhsadkjlfhn,skdf ");
>
> (text widget's setText() method have a lot of intermediate code, event
> handling, etc and I think its incorrect to do it my way, in
> contravention of widget's setText())
>
> And second. As I understood, syntax highliting and other such behavior
> are provided by JavaSourceViewerConfiguration class.
> JavaSourceViewerConfiguration configuration = new
> JavaSourceViewerConfiguration(new JavaColorManager(), store, this,
> IJavaPartitions.JAVA_PARTITIONING);
> viewer.configure(configuration);
>
> But when I'm tryed to instantiate it and configure viewer with it,
> I've got an NPE:
>
> java.lang.NullPointerException
> at
> org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration.getSet tings(JavaSourceViewerConfiguration.java:813)
>
> at
> org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration.getCon tentAssistant(JavaSourceViewerConfiguration.java:392)
>
> at
> org.eclipse.jface.text.source.SourceViewer.configure(SourceV iewer.java:370)
>
> at
> org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer.conf igure(JavaSourceViewer.java:223)
>
> at
> com.codesrc.thinktankrcp.projects.FileContentsView.createPar tControl(FileContentsView.java:161)
>
> ..
>
> So, I've investigated it and saw that
> org.eclipse.jdt.internal.ui.JavaPlugin.getDefault() in
> JavaSourceViewerConfiguration.java:813:
>
> IDialogSettings settings=
> JavaPlugin.getDefault().getDialogSettings().getSection(secti onName);
>
> returned null. Also I tryed to experiment with
> org.eclipse.jdt.internal.ui.javaeditor.JavaEditor and got a lot of
> similar NPE in same situations from such classes:
> org.eclipse.jdt.internal.ui.JavaPlugin,
> org.eclipse.ui.internal.editors.text.EditorsPlugin,
> org.eclipse.jdt.core.JavaCore. I've tried to fill it's values by
> reflection :), it works, but at first it's a terrible ugly hack, and
> in second - I've started to receive many other exceptions in many
> places, so it is not correct way. As I understood, something in the
> Eclipse Platform should correctly initialize, prepare and tune such
> classes. Can you help me with it? Maybe I should differently set up my
> plug-in project, include some dependencies to it or something else?
>
>
Re: Correct instantiation and configuring of JavaSourceViewer [message #260695 is a reply to message #260671] Thu, 18 June 2009 17:34 Go to previous messageGo to next message
Alexander Kiselyov is currently offline Alexander KiselyovFriend
Messages: 3
Registered: July 2009
Junior Member
Thanks for reply, Daniel. First of all, seems like I've faced some
troubles with org.eclipse.jdt.ui plug-in initialization.
I've made the following:
selected "org.eclipse.jdt.ui" in list on "Plug-ins" tab of my project
debug configuration. Also I tried to add it to "Required Plug-ins list"
but every time I've tried to add it through the GUI I received folowing
error after launching:

!ENTRY org.eclipse.osgi 2 0 2009-06-18 20:17:18.998
!MESSAGE One or more bundles are not resolved because the following root
constraints are not resolved:

...

!MESSAGE Missing required bundle org.eclipse.jdt.ui_3.4.2.

!ENTRY org.eclipse.osgi 4 0 2009-06-18 20:07:45.924
!MESSAGE Application error
!STACK 1
java.lang.RuntimeException: No application id has been found.
at
org.eclipse.equinox.internal.app.EclipseAppContainer.startDe faultApp(EclipseAppContainer.java:236)
at
org.eclipse.equinox.internal.app.MainApplicationLauncher.run (MainApplicationLauncher.java:29)
at
org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .runApplication(EclipseAppLauncher.java:110)
at
org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .start(EclipseAppLauncher.java:79)
at
org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:386)
at
org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcce ssorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMe thodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java: 549)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:504)
at org.eclipse.equinox.launcher.Main.run(Main.java:1236)
at org.eclipse.equinox.launcher.Main.main(Main.java:1212)

I've tried to wrote this entry manually to the MANIFEST.MF, but it has no
success... So I've added it to the "Automated Management of Dependencies"
list to avoid adding it to the MANIFEST.MF and it seems like bundle
doesn't starts at all... Please, can you tell me how I should add this
bundle properly? And should I add it to the "Plug-ins and Fragments" list
of .product file?

Also I've tried to open file in standard editor obtaining its id from
JavaPlugin editor registry (because of NPE I have to call constructor to
initialize fgJavaPlugin field):

JavaPlugin plugin = new JavaPlugin();
IWorkbenchPage page = getSite().getPage();
File file = new File("C:\\test.java");
IFileStore fStore = new LocalFile(file);
IEditorDescriptor[] editors =
JavaPlugin.getDefault().getWorkbench().getEditorRegistry().g etEditors(file.getName());

But there is no editors in it, returned array is empty. I think it's the
result of inproper initialization of org.eclipse.jdt.ui. Can you give me
some advice about it?
Re: Correct instantiation and configuring of JavaSourceViewer [message #260742 is a reply to message #260695] Mon, 22 June 2009 07:51 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
Alexander Kiselyov wrote:
> Thanks for reply, Daniel. First of all, seems like I've faced some
> troubles with org.eclipse.jdt.ui plug-in initialization.
> I've made the following: selected "org.eclipse.jdt.ui" in list on
> "Plug-ins" tab of my project debug configuration. Also I tried to add
> it to "Required Plug-ins list" but every time I've tried to add it
> through the GUI I received folowing error after launching:
> !ENTRY org.eclipse.osgi 2 0 2009-06-18 20:17:18.998
> !MESSAGE One or more bundles are not resolved because the following
> root constraints are not resolved:
>
> ..
>
> !MESSAGE Missing required bundle org.eclipse.jdt.ui_3.4.2.
>
> !ENTRY org.eclipse.osgi 4 0 2009-06-18 20:07:45.924
> !MESSAGE Application error
> !STACK 1
> java.lang.RuntimeException: No application id has been found.
> at
> org.eclipse.equinox.internal.app.EclipseAppContainer.startDe faultApp(EclipseAppContainer.java:236)
>
> at
> org.eclipse.equinox.internal.app.MainApplicationLauncher.run (MainApplicationLauncher.java:29)
>
> at
> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .runApplication(EclipseAppLauncher.java:110)
>
> at
> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .start(EclipseAppLauncher.java:79)
>
> at
> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:386)
>
> at
> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:179)
>
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcce ssorImpl.java:39)
>
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMe thodAccessorImpl.java:25)
>
> at java.lang.reflect.Method.invoke(Method.java:597)
> at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java: 549)
> at org.eclipse.equinox.launcher.Main.basicRun(Main.java:504)
> at org.eclipse.equinox.launcher.Main.run(Main.java:1236)
> at org.eclipse.equinox.launcher.Main.main(Main.java:1212)
>
> I've tried to wrote this entry manually to the MANIFEST.MF, but it has
> no success...
Well, it definitely needs to be part of the required bundle definition
of your MANIFEST.MF.

Dani
> So I've added it to the "Automated Management of Dependencies" list to
> avoid adding it to the MANIFEST.MF and it seems like bundle doesn't
> starts at all... Please, can you tell me how I should add this bundle
> properly? And should I add it to the "Plug-ins and Fragments" list of
> .product file?
>
> Also I've tried to open file in standard editor obtaining its id from
> JavaPlugin editor registry (because of NPE I have to call constructor
> to initialize fgJavaPlugin field):
>
> JavaPlugin plugin = new JavaPlugin();
> IWorkbenchPage page = getSite().getPage();
> File file = new File("C:\\test.java");
> IFileStore fStore = new LocalFile(file);
> IEditorDescriptor[] editors =
> JavaPlugin.getDefault().getWorkbench().getEditorRegistry().g etEditors(file.getName());
>
>
> But there is no editors in it, returned array is empty. I think it's
> the result of inproper initialization of org.eclipse.jdt.ui. Can you
> give me some advice about it?
>
Re: Correct instantiation and configuring of JavaSourceViewer [message #260777 is a reply to message #260742] Tue, 23 June 2009 16:25 Go to previous message
Alexander Kiselyov is currently offline Alexander KiselyovFriend
Messages: 3
Registered: July 2009
Junior Member
Well, I've solved the problem. After adding of all required package
bundles to the MANIFEST.MF through the "Plug-in Manifest Editor" I went to
the "Debug Configurations..." -> "Plug-ins", removed all plug-ins by
pressing "Deselect All" and then added all required by pressing "Add
Required Plug-ins". After it troubles during plug-in start in Debug mode
are gone. Now files opens correctly on active page using
CompilationUnitEditor (JavaUI.ID_CU_EDITOR). Thanks again for your replies.

Best regards,

Alexander.
Previous Topic:Need help debugging into rt.jar classes
Next Topic:Track an Object
Goto Forum:
  


Current Time: Tue Nov 12 19:52:08 GMT 2024

Powered by FUDForum. Page generated in 0.24209 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top