Home » Language IDEs » Java Development Tools (JDT) » [SOLVED] How to open an IJavaElement programatically?
| | | | | | |
Re: How to open an IJavaElement programatically? [message #650945 is a reply to message #650939] |
Thu, 27 January 2011 04:20   |
Eclipse User |
|
|
|
Hi Dani,
Thank you for your question. Sorry that I wasn't clear.
What I really want to do is that to perform a Change object (created using QuickFixProcessor).
However, the problem is that, I figured out, the change is not applied if the particular file that it will modify (i.e., change.getModifiedElement()) is not open in the Eclipse's page view and selected at least one. I know that it sounds strange, however I have tried it many times with many debugging and if I don't open the file in Eclipse's workbench page, then when I invoke change.perform() nothing happens (I don't get an error, no notification). On the other hand if the file is open and selected, then when I say change.perform() the change is applied correctly.
I have to do the whole process (creating the change and applying it automatically, i.e., I cannot open the file that would change manually). Therefore, I need to one of the following before invoking change.perform()
1- Actually open the file on the workbench page (so that it is also selected) and run the change. I have tried this inside the constructor of the plugin, it works, however the actual thread that should do this is a background thread and since it is not a GUI thread, Eclipse API throws exception when I try to open an editor for the file to change (using page.openEditor()).
2- Figure out what does opening the file int eh workbench page changes internally for an IFile or CompilationUnit. I guess that if I can mimic the same thing without actually opening the file in the page, then the change will also be applied correctly.
I might be missing something or if anything does not make sense please point it, I will try to explain in detail. Again, thank you for your interest,
Regards,
|
|
|
Re: How to open an IJavaElement programatically? [message #651192 is a reply to message #650945] |
Fri, 28 January 2011 05:32   |
Eclipse User |
|
|
|
On 27.01.2011 10:20, Kivanc Muslu wrote:
> Hi Dani,
>
> Thank you for your question. Sorry that I wasn't clear.
>
> What I really want to do is that to perform a Change object (created
> using QuickFixProcessor).
This is an internal class which you should not use and for which there's
no support. You should create your own Change(Operation).
Dani
> However, the problem is that, I figured out, the change is not applied
> if the particular file that it will modify (i.e.,
> change.getModifiedElement()) is not open in the Eclipse's page view
> and selected at least one. I know that it sounds strange, however I
> have tried it many times with many debugging and if I don't open the
> file in Eclipse's workbench page, then when I invoke change.perform()
> nothing happens (I don't get an error, no notification). On the other
> hand if the file is open and selected, then when I say
> change.perform() the change is applied correctly.
> I have to do the whole process (creating the change and applying it
> automatically, i.e., I cannot open the file that would change
> manually). Therefore, I need to one of the following before invoking
> change.perform()
> 1- Actually open the file on the workbench page (so that it is also
> selected) and run the change. I have tried this inside the constructor
> of the plugin, it works, however the actual thread that should do this
> is a background thread and since it is not a GUI thread, Eclipse API
> throws exception when I try to open an editor for the file to change
> (using page.openEditor()).
> 2- Figure out what does opening the file int eh workbench page changes
> internally for an IFile or CompilationUnit. I guess that if I can
> mimic the same thing without actually opening the file in the page,
> then the change will also be applied correctly.
>
> I might be missing something or if anything does not make sense please
> point it, I will try to explain in detail. Again, thank you for your
> interest,
>
> Regards,
|
|
| | | |
Re: How to open an IJavaElement programatically? [message #651208 is a reply to message #651206] |
Fri, 28 January 2011 06:21   |
Eclipse User |
|
|
|
On 28.01.2011 12:08, Kivanc Muslu wrote:
> Excellent point.
>
> The problem is that, though I have searched many files in the
> unarchived plug-ins folder of Eclipse (i.e., I un-jared all plug-ins
> in the same folder), I couldn't find an exact location or anything
> that could bring me there.
Take a look at the apply(...) methods on the proposal.
Dani
>
> More specifically, I have searched for new QuickFixProcessor(...) and
> getCorrections(...) in the source code directory, however the results
> were not related.
>
> Most of the getCorrections(...) were for the classes that implemented
> IQuickFixProcessor (they were simply overriding it), so I couldn't
> find place to start to dig into the code.
>
> Do you by any chance know a good place so that maybe following that I
> could find the source code that does this? For example: do you know
> what is executed when the actual user clicks to a possible quick fix?
> Or what part of the GUI code is that dialog box (or gadget) is? I
> think these would be excellent starts for me, however I have no idea
> to which code they point to.
>
> Thank you,
|
|
|
Re: How to open an IJavaElement programatically? [message #651222 is a reply to message #651208] |
Fri, 28 January 2011 08:06   |
Eclipse User |
|
|
|
Hi Dani,
I had written a quite long reply normally, however for some reason the system didn't accept it and it is gone.
In summary (please ask me if something is not clear, I would explain it in more detail), I have looked at ChangeCorrectionProposal.apply(...). It seems that it is getting active page (which is got through the active window). I am not sure if this is my problem but, I am sure that when you try to get active window using a non-UI thread, then it returns null (it is even documented to return null in this case). So in my version, I definitely cannot get the active page (I get null), because I need to apply the changes using a background thread (daemon).
So, my question is: When I do change.perform(...) it doesn't change anything in the target file. Is this due to the fact that I am invoking it from a non-UI thread, and this is not permitted? If so, I might need to completely change my design, if not, I will look more into it.
I ask this, because other than that what is done in ChagneCorrectionProposal.apply(...) seems very alike to the pattern proposed in Change.java documentation to apply the changes.
* Here is a code snippet that can be used to execute a change:
* <pre>
* Change change= createChange();
* try {
* change.initializeValidationData(pm);
*
* ....
*
* if (!change.isEnabled())
* return;
* RefactoringStatus valid= change.isValid(new SubProgressMonitor(pm, 1));
* if (valid.hasFatalError())
* return;
* Change undo= change.perform(new SubProgressMonitor(pm, 1));
* if (undo != null) {
* undo.initializeValidationData(new SubProgressMonitor(pm, 1));
* // do something with the undo object
* }
* } finally {
* change.dispose();
* }
I also use a minor modified version of this above code to perform the changes. As I said however, when I invoke change.perform(...) (which returns normally), the target file does not change.
However, if I do the same using a UI thread (i.e., from plug-in's constructor for testing), then it again does not change the target file (performing the change does not change the target file).
However, if I do the same using a UI thread, and if I open the file that is supposed to change using page.openEditor(...) (since this is a UI thread, I can get the active page), now the change is applied correctly and the target file is updated accordingly.
Since I don't know the internal of these whole classes and the interaction between them, this seems pretty weird to me (i.e., it is like that I need to open the file inside an editor to apply a change object on it). Again, I might be missing something.
Thanks a lot for the help, any further guidance is greatly appreciated.
Regards,
|
|
|
Re: How to open an IJavaElement programatically? [message #651248 is a reply to message #651222] |
Fri, 28 January 2011 09:39   |
Eclipse User |
|
|
|
On 28.01.2011 14:06, Kivanc Muslu wrote:
> Hi Dani,
>
> I had written a quite long reply normally, however for some reason the
> system didn't accept it and it is gone.
>
> In summary (please ask me if something is not clear, I would explain
> it in more detail), I have looked at
> ChangeCorrectionProposal.apply(...). It seems that it is getting
> active page (which is got through the active window). I am not sure if
> this is my problem but, I am sure that when you try to get active
> window using a non-UI thread, then it returns null (it is even
> documented to return null in this case). So in my version, I
> definitely cannot get the active page (I get null), because I need to
> apply the changes using a background thread (daemon).
>
> So, my question is: When I do change.perform(...) it doesn't change
> anything in the target file. Is this due to the fact that I am
> invoking it from a non-UI thread,
Why not just post into the UI thread?
Dani
> and this is not permitted? If so, I might need to completely change my
> design, if not, I will look more into it.
>
> I ask this, because other than that what is done in
> ChagneCorrectionProposal.apply(...) seems very alike to the pattern
> proposed in Change.java documentation to apply the changes.
>
> * Here is a code snippet that can be used to execute a change:
> * <pre>
> * Change change= createChange();
> * try {
> * change.initializeValidationData(pm);
> * * ....
> *
> * if (!change.isEnabled())
> * return;
> * RefactoringStatus valid= change.isValid(new
> SubProgressMonitor(pm, 1));
> * if (valid.hasFatalError())
> * return;
> * Change undo= change.perform(new SubProgressMonitor(pm, 1));
> * if (undo != null) {
> * undo.initializeValidationData(new SubProgressMonitor(pm, 1));
> * // do something with the undo object
> * }
> * } finally {
> * change.dispose();
> * }
>
>
> I also use a minor modified version of this above code to perform the
> changes. As I said however, when I invoke change.perform(...) (which
> returns normally), the target file does not change.
>
> However, if I do the same using a UI thread (i.e., from plug-in's
> constructor for testing), then it again does not change the target
> file (performing the change does not change the target file).
>
> However, if I do the same using a UI thread, and if I open the file
> that is supposed to change using page.openEditor(...) (since this is a
> UI thread, I can get the active page), now the change is applied
> correctly and the target file is updated accordingly.
>
> Since I don't know the internal of these whole classes and the
> interaction between them, this seems pretty weird to me (i.e., it is
> like that I need to open the file inside an editor to apply a change
> object on it). Again, I might be missing something.
>
> Thanks a lot for the help, any further guidance is greatly appreciated.
>
> Regards,
|
|
| | | |
Goto Forum:
Current Time: Wed Jul 23 06:06:36 EDT 2025
Powered by FUDForum. Page generated in 0.14073 seconds
|