Reading and acting on preferences on startup / plugin loadhttps://www.eclipse.org/forums/index.php/mv/msg/214077/685160/#msg_685160
I have created a plugin which contributes a wizard and some preference pages to the Eclipse UI.
Now after the user has selected the option(s) on the preference page as he/she wished for example, they are being saved successfully.

My question is: Is there any way to execute certain code upon startup of Eclipse (at/after loading of the plugin) which makes use of the preference store without requiring the user to visit preference page? (because that it what already works, my code is executed when the pref-page is loaded)
The init methods are only being called after the user visits the preference page and therefore the plug-in preference store is accessed. How can i tell Eclipse/my plugin to access that store on load and perform an action based on a preference right away?

I already found out that an activator is executed when a plugin starts, but I can't load the preferenceStore at that early point, so activator does not seem to be an option. But where do I need to put my code to get it executed on startup then?

Thank you.
]]>Martin F.2011-06-16T23:54:36-00:00Re: Reading and acting on resukt of preferenceshttps://www.eclipse.org/forums/index.php/mv/msg/214077/685279/#msg_685279
forums-noreply@eclipse.org wrote:
> Hi,
> I have created a plugin which contributes a wizard and some preference
> pages to the Eclipse UI.
> Now after the user has selected the option(s) on the preference page
> as he/she wished for example, they are being saved successfully.
>
> My question is: Is there any way to execute certain code upon startup
> of Eclipse (at/after loading of the plugin) which makes use of the
> preference store without requiring the user to visit preference page?
> (because that it what already works, my code is executed when the
> pref-page is loaded)
> The init methods are only being called after the user visits the
> preference page and therefore the plug-in preference store is
> accessed. How can i tell Eclipse/my plugin to access that store on
> load and perform an action based on a preference right away?
>
> I already found out that an activator is executed when a plugin
> starts, but I can't load the preferenceStore at that early point, so
> activator does not seem to be an option.
Why not? We access the store in
org.osgi.framework.BundleActivator.start(BundleContext).

What exactly do you try to do?

Dani
> But where do I need to put my code to get it executed on startup then?
>
> Thank you.
>]]>Dani Megert2011-06-17T07:16:47-00:00Re: Reading and acting on resukt of preferenceshttps://www.eclipse.org/forums/index.php/mv/msg/214077/685366/#msg_685366
Dani Megert wrote on Fri, 17 June 2011 09:16

On 17.06.2011 01:54, forums-noreply@eclipse.org wrote:
>> How can i tell Eclipse/my plugin to access that store on
>> load and perform an action based on a preference right away?
>>
>> I already found out that an activator is executed when a plugin
>> starts, but I can't load the preferenceStore at that early point, so
>> activator does not seem to be an option.
>
> Why not? We access the store in
> org.osgi.framework.BundleActivator.start(BundleContext).
>
> What exactly do you try to do?

I'm trying to read a boolean pref and - depending on the value - start a service in the background:

btServer = BluetoothServer.getInstance();
if (store.getBoolean("BtServer"))
{
if (!btServer.isBtServerRunning())
{
// if server is not running, fire it up
btServer.startServer();
}
}
else // this is the case that the BT-server should be turned off
{
if (btServer.isBtServerRunning())
{
// pause it
btServer.pauseServer();
}
}

My fault was trying to do that the constructor of the Activator. So using a method public void start(BundleContext context) in the Activator allows to access the Preference store. Now I have two methods in my Activator: public void start(BundleContext context) and public void stop(BundleContext context)
But unfortunately that start()-Method is also only executed when I open my preference page and not on startup. (tried to perform a simple Logging-Output in there, to verify this) How can I achieve the desired execution onload?
Thanks.]]>Martin F.2011-06-17T11:15:57-00:00Re: Reading and acting on resukt of preferenceshttps://www.eclipse.org/forums/index.php/mv/msg/214077/685553/#msg_685553
Mtchr.schmusemail.de wrote on Fri, 17 June 2011 07:15

My fault was trying to do that the constructor of the Activator. So using a method public void start(BundleContext context) in the Activator allows to access the Preference store. Now I have two methods in my Activator: public void start(BundleContext context) and public void stop(BundleContext context)
But unfortunately that start()-Method is also only executed when I open my preference page and not on startup. (tried to perform a simple Logging-Output in there, to verify this) How can I achieve the desired execution onload?
Thanks.

In general in eclipse a plugin is only started the first time a class is accessed. ex: the user shows one of your views. That's the behaviour you are seeing.

In an RCP app, you can use your org.eclipse.ui.application.WorkbenchAdvisor.postStartup() method to check your preference and then start up the server.

PW
]]>Paul Webster2011-06-17T18:22:01-00:00Re: Reading and acting on resukt of preferenceshttps://www.eclipse.org/forums/index.php/mv/msg/214077/708736/#msg_708736
I am facing a similar kind of problem. I want to close all editors at the time of launch. Since mine is a plugin project but is not an RCP application, i am not sure how and where do i override org.eclipse.ui.application.WorkbenchAdvisor.postStartup() method. I added following piece of code in start method of activator.

PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
for (IWorkbenchWindow iWorkbenchWindow : windows) {
if (iWorkbenchWindow.getActivePage() != null) {
break;
}
iWorkbenchWindow.close();
}
but this is of no use. I added above code to one of the methods of class implementing AbstractUIPlugin. It is closing all the editors at the time of eclipse luanch but throwd a NullPoiner Exception as well. Please fond the stack trace below:

java.lang.NullPointerException
at org.eclipse.ui.internal.menus.WorkbenchMenuService.releaseContributions(WorkbenchMenuService.java:854)
at org.eclipse.ui.internal.WorkbenchWindow.hardClose(WorkbenchWindow.java:1682)
at org.eclipse.ui.internal.WorkbenchWindow.busyClose(WorkbenchWindow.java:723)
at org.eclipse.ui.internal.WorkbenchWindow.access$0(WorkbenchWindow.java:699)
at org.eclipse.ui.internal.WorkbenchWindow$5.run(WorkbenchWindow.java:815)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
at org.eclipse.ui.internal.WorkbenchWindow.close(WorkbenchWindow.java:813)
at org.eclipse.ui.internal.Workbench$61.runWithException(Workbench.java:3402)
at org.eclipse.ui.internal.StartupThreading$StartupRunnable.run(StartupThreading.java:31)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:134)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:3855)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3476)
at org.eclipse.ui.application.WorkbenchAdvisor.openWindows(WorkbenchAdvisor.java:803)
at org.eclipse.ui.internal.Workbench$28.runWithException(Workbench.java:1384)
at org.eclipse.ui.internal.StartupThreading$StartupRunnable.run(StartupThreading.java:31)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:134)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:3855)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3476)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2316)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2221)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:500)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:493)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:113)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:194)
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(EclipseStarter.java:368)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:559)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:514)
at org.eclipse.equinox.launcher.Main.run(Main.java:1311)
at org.eclipse.equinox.launcher.Main.main(Main.java:1287)