Howto get IPropertyChangeListener to trigger build? [message #113978] |
Thu, 21 August 2003 11:32  |
Eclipse User |
|
|
|
R2.1, WinXP
We have our own project-nature and -builder, as well as a number of
user-prefs. Some of those user-prefs set bits that the builder consults
when generating IMarkers (the bits when true mean "do generate
such-and-such a sort of marker). Of course, it'd be user-friendly to
immediately rebuild when the user changes one of those bits, and also of
course that meas there needs to be an IPropertyChangeListener attached
to ... what? I.e., where/how do I call what code to get a build to
happen?
Thanks,
Paul K
|
|
|
|
Re: Howto get IPropertyChangeListener to trigger build? [message #116903 is a reply to message #114982] |
Tue, 26 August 2003 18:00  |
Eclipse User |
|
|
|
Originally posted by: John_Arthorne.oti.com_
You do realize this code does a FULL REBUILD OF THE ENTIRE WORKSPACE
every time that property changes?? This means, for example, that all
java projects in the workspace will delete all of their .class files and
build everything from scratch. In general this is quite bad. I would
suggest inside that your property listener sets some bit (for example a
session property) on each project that has your nature. Your builder
would consult this bit when called, and then throw away state and do a
rebuild if it is set (and then clear the bit). Otherwise all other
builders will also throw away their built state. Also, if the user has
auto-build turned off, then any code that gratuitously builds is being
very rude... Other problems with the code below are left as an excercise
to the reader ;)
--
Paul T. Keyser wrote:
> Thanks to a colleague, who gave me the necessary clues ("you want to invoke
> IWorkspace.build(). You can get the current IWorkspace from
> ResourcesPlugin.getWorkspace(). ... properly encapsulate the call to build()
> in a workspace "runnable""), here is how it's done:
>
> static {
>
> OUR_PLUGIN_NAME.getDefault().getPreferenceStore().addPropert yChangeListener(new
> TriggerBuild());
> }
>
> /**
> * TriggerBuild is a IPropertyChangeListener that when attached to the
> PreferenceStore
> * will trigger a build when relevant items are changed
> *
> * @author Keyser
> */
> protected static class TriggerBuild implements IPropertyChangeListener {
>
> /**
> * @see
> org.eclipse.jface.util.IPropertyChangeListener#propertyChang e(org.eclipse.jface.util.PropertyChangeEvent)
>
> */
> public void propertyChange (final PropertyChangeEvent e) {
> if (needToBuild(e.getProperty())) {
> doBuild();
> }
> }
>
> /**
> * Perform a full build of the projects in the workspace
> */
> protected void doBuild () {
> try {
> final IWorkspace ws = ResourcesPlugin.getWorkspace();
>
> final IWorkspaceRunnable _runner = new IWorkspaceRunnable()
> {
> public void run (IProgressMonitor m) {
> try {
> ws.build(TYPE, m);
>
> } catch (CoreException e) {
> System.err.println(
> "Autobuild on change-preferences failed in "
>
> + getClass().getName());
> e.printStackTrace();
> }
> }
> };
> ws.run(_runner, null); // TODO keyser: find out how to
> create a ProgressMonitor
>
> } catch (CoreException e) {
> System.err.println(
> "Autobuild on change-preferences failed in "
> + getClass().getName());
> e.printStackTrace();
> }
> }
>
> /**
> * Is a build required?
> * @param prop the preference that has changed
> * @return boolean <code>true</code> if the changed preference
> requires a build
> */
> protected boolean needToBuild (final String prop) {
> return prop.equals(THE_ONE_TO_CHECK.TAG);
> }
>
> private static final int TYPE =
> IncrementalProjectBuilder.FULL_BUILD;
> }
>
>
|
|
|
Powered by
FUDForum. Page generated in 0.02848 seconds