Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Action set, Editor input
Action set, Editor input [message #461024] Wed, 03 January 2007 18:52 Go to next message
Geri Broser is currently offline Geri BroserFriend
Messages: 20
Registered: July 2009
Junior Member
I have to create an app to open a (text, html, xml) file and to process
its contents. So far, i managed to open such files in an editor (with
syntax coloring; code folding and line numberung would be nice, but it's
not essential).

This was very helpful to achieve this:
http://www.eclipse.org/eclipse/platform-text/development/rcp /examples/index.html

However, now I'm stuck at the following issues:

1) I defined three action in an action set in plugin.xml: OpenFile,
Sign, Verify.

1.1) OpenFile, which I took from the example mentioned above, works as
intended: Selecting File --> Menu or the appropriate toolbar button
accesses the proper class OpenFileAction which loads a file into an
appropriate editor.

1.2) Sign and Verify are displayed in the appropriate menu and in the
main tool bar. But, when I select either of them an Information msgbox
appears with the following text: "This operation is not available at the
moment."

Q1: Where am I wrong?

Q2: Would it be better to add the actions via an editorContribution
(like in
http://www.eclipse.org/articles/Article-action-contribution/ index.html)?
With the advantage of disabled menu items, unless an editor window is
opened? But, as extension to WHICH editor? There are three used in the
text editor example above: AbstractTextEditor, SimpleEditor, XMLEditor.

2) When I add Sign and Verify via ApplicationActionBarAdvisor, the
proper classes are accessed, of course. The disadvantage is that this
seems only to be useful for menu items. When I add actions to the
toolbar there, labels are displayed, not icons. And the items are active
even if there's nothing to do (no editor has been opened).

Q3: How do I get the contents of the currently active editor?

Q4: How do I open an empty editor and fill it with contents (to display,
and optionally save, changed data without harming the original file)?

Geri
Re: Action set, Editor input [message #461034 is a reply to message #461024] Thu, 04 January 2007 05:04 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: krishna.kamath.gmail.com

Hi Geri,

I am answering your questions inline:

Q1: Where am I wrong?

-> To check this problem, you may want to take a look at the log file. There
might be some exception which might be logged there. This I guess may be
because the application is not able to find some classes during its runtime. I
hope seeing the log should be helpful.

Q2: Would it be better to add the actions via an editorContribution (like
in
http://www.eclipse.org/articles/Article-action-contribution/ index.html)?
With the advantage of disabled menu items, unless an editor window is
opened? But, as extension to WHICH editor? There are three used in the
text editor example above: AbstractTextEditor, SimpleEditor, XMLEditor.

-> As you said the advantage of using editorContribustion is that you will be
able to see the menu and tool items only when they are accessed. I am not sure
if you can group editors for these items, (it would be good though if it
possible :)). I am also trying for a similar feature, maybe your thread will
be helpful in this regard :).

2) When I add Sign and Verify via ApplicationActionBarAdvisor, the proper
classes are accessed, of course. The disadvantage is that this seems only
to be useful for menu items. When I add actions to the toolbar there,
labels are displayed, not icons. And the items are active even if there's
nothing to do (no editor has been opened).

-> I am not sure why this problem is arising. I have done this thing of adding
the actions in toolbar but haven't faced such problem. Have you added the icon
information for the actions in plugin.xml? Also, have you added the export
information for the icons folder in build.properties?

Q3: How do I get the contents of the currently active editor?

-> What information form the active editor do you require? If it is the
current editor input you can get it from getEditorInput().

Q4: How do I open an empty editor and fill it with contents (to display,
and optionally save, changed data without harming the original file)?

-> I don't get your question. Save the file optionally without harming the
original file?
Re: Action set, Editor input [message #461071 is a reply to message #461034] Thu, 04 January 2007 18:46 Go to previous messageGo to next message
Geri Broser is currently offline Geri BroserFriend
Messages: 20
Registered: July 2009
Junior Member
Hi Krishna,

Thanks for your quick response.

Q1: Where am I wrong?

-> To check this problem, you may want to take a look at the log file.
There might be some exception which might be logged there. This I guess
may be because the application is not able to find some classes during
its runtime. I hope seeing the log should be helpful.

>> Thanks for the hint. Yes, the log file shows:

!MESSAGE Could not create action delegate for id: myPackage.myAction
!MESSAGE The Plug-in myPackage.myPlugin could not create an instance of
class myPackage.myActionClass
java.lang.InstantiationException: myPackage.myAction

And now? Now I know what can NOT be done. Don't have a clue what CAN be
done. The class is there. In this package. Spelling is correct. It
extends Action and implements IWorkbenchWindowActionDelegate (I read in
an other thread that this is necessary, isn't it?)

(And, if one of the appropriate developers comes by eventually: A note
like "See <path>/.log for details." displayed together with the message
would be helpful.)


Q2: Would it be better to add the actions via an editorContribution
(like in
http://www.eclipse.org/articles/Article-action-contribution/ index.html)?
With the advantage of disabled menu items, unless an editor window is
opened? But, as extension to WHICH editor? There are three used in the
text editor example above: AbstractTextEditor, SimpleEditor, XMLEditor.

-> As you said the advantage of using editorContribustion is that you
will be able to see the menu and tool items only when they are accessed.
I am not sure if you can group editors for these items, (it would be
good though if it possible :)). I am also trying for a similar feature,
maybe your thread will be helpful in this regard :) .

>> I tested implementing editorContributions with the following results:

1.3) Yes. One has to define all actions for each and every editor -
SimpleEditor and XMLEditor in my case. That's odd, since in my case the
process is the same, no matter of the contents of the editor.

1.4) The menu and toolbar items are only shown, if an instance of the
appropriate editor is open. This leads to an empty menu and to an empty
toolbar unless an editor window is opened --> not very sexy. I'd rather
would like to have disabled ("grayed out") menu/toolbar items.

1.5) The result when selecting menu/toolbar item is the same: "The
chosen operation is not currently available."


2) When I add Sign and Verify via ApplicationActionBarAdvisor, the
proper classes are accessed, of course. The disadvantage is that this
seems only to be useful for menu items. When I add actions to the
toolbar there, labels are displayed, not icons. And the items are active
even if there's nothing to do (no editor has been opened).

-> I am not sure why this problem is arising. I have done this thing of
adding the actions in toolbar but haven't faced such problem. Have you
added the icon information for the actions in plugin.xml? Also, have you
added the export information for the icons folder in build.properties?

>> You don't need actions/-sets in plugin.xml if you add them in
ApplicationActionBarAdvisor, do you? Anyway, had a wrong file name at
setImageDescriptor(Activator.getImageDescriptor("/icons/sign.gif ")); in
the action class' constructor.


Q3: How do I get the contents of the currently active editor?

-> What information form the active editor do you require? If it is the
current editor input you can get it from getEditorInput().

>> Yes, i've read about getEditorInput(). But the question is how to
get the proper editor (the one which is currently active) first, to be
able to get its content via getEditorInput().


Q4: How do I open an empty editor and fill it with contents (to display,
and optionally save, changed data without harming the original file)?

-> I don't get your question. Save the file optionally without harming
the original file?

>> I would like to open a file. Process it somehow. Put the results of
this process into a new editor. Optionally save the contents of this new
editor to a new file. Thus, the contents if the original file do not change.


Geri
Re: Action set, Editor input [message #461110 is a reply to message #461071] Fri, 05 January 2007 06:54 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: krishna.kamath.gmail.com

Hi Geri,

It was getting a lot messy in the previous mails and real confusing. So
let me start of with a clean mail.

Q1:!MESSAGE Could not create action delegate for id: myPackage.myAction
!MESSAGE The Plug-in myPackage.myPlugin could not create an instance of
class myPackage.myActionClass
java.lang.InstantiationException: myPackage.myAction

You seem to have done everything right. May be you can try creating it
from extension points using action sets. It is not really necessary
though, I feel.

The following link may be useful:

http://wiki.eclipse.org/index.php/FAQ_Why_do_I_get_a_ 'plug-in_was_unable_to_load_class'_error_when_I_activate_a_m enu_or_toolbar_action%3F

Q2:You don't need actions/-sets in plugin.xml if you add them in
ApplicationActionBarAdvisor, do you? Anyway, had a wrong file name at
setImageDescriptor(Activator.getImageDescriptor("/icons/sign.gif ")); in
the action class' constructor.

Once again to the problem of having to define the actions in every editor,
the best way I feel is write all the actions in one of the packages and
use these action classes in the required editor.

As for menu and toolbar not looking 'sexy' you can may be once again use
the action classes in the ApplicationActionBarAdvisor. On opening each of
the editors get an handle to the actions and enable and disable them as
required. This would mean giving the action objects in
ApplicationActionBarAdvisor proper access (public/protected).

For "The chosen operation is not currently available." message is
concerned. This appears when the associated action class fails to get
instantiated. I feel if you are able to resolve the previous problem this
will be resolved.


Yes you don't need them in plugin.xml. But it is more clearer and less
error prone if you do it this way. Personally it is just a way I prefer
doing it. It gives a better feel :).

Q3:How do I get the contents of the currently active editor?

UIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindo w().getActivePage().getActiveEditor().getEditorInput();

Q4:
I would like to open a file. Process it somehow. Put the results of this
process into a new editor. Optionally save the contents of this new editor
to a new file. Thus, the contents if the original file do not change.

I hope the file you will be processing is from the open active editor. In
that case you can get a link to the file using the code I gave you in Q3.
From there may be you can process the info as you require and create a new
file putting the processed data into it. You can create this file on the
file system as a temp or a permanent file as you require. Then you can
open a new editor taking this new file as the input to the editor, showing
the processed data in the new editor.

I hope I have understood your problem right?

Hope these things were of some use :)

Regards,
krishan
Re: Action set, Editor input [message #461116 is a reply to message #461071] Fri, 05 January 2007 08:09 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: krishna.kamath.gmail.com

Hi Geri,

Call it my friends bad luck or your good luck(hopefully) :). He had
exactly the same problem that you were facing.

The problem turned out to be the constructor definition that he had given
was wrong. The parameters were all wrong. In his case this was the reason
why the exception was thrown. Just check if you have a similar problem.

Regards,
krishna
Re: Action set, Editor input [message #461274 is a reply to message #461110] Mon, 08 January 2007 14:27 Go to previous messageGo to next message
Geri Broser is currently offline Geri BroserFriend
Messages: 20
Registered: July 2009
Junior Member
> Q1:!MESSAGE Could not create action delegate for id: myPackage.myAction
> !MESSAGE The Plug-in myPackage.myPlugin could not create an instance
of class myPackage.myActionClass
> java.lang.InstantiationException: myPackage.myAction

As far as I can see the problem was the following:

To check which variant suits my needs best I implemented all three
variants ( mypackage.ApplicationActionBarAdvisor, plugin.xml: actionSet,
plugin.xml: editorAction) with one action class within one project. This
causes no problem generally, since thanks to OO three independent
instances of this class are created, each of which representing a
menu/coolbar entry.

BUT: I had only ONE org.eclipse.ui.commands section and ONE
org.eclipse.ui.bindings section in plugin.xml. And plugin.xml doesn't
behave OOish. Thus, the creation of the items in
ApplicationActionBarAdvisor and in plugin.xml's actionSet and
editorActions interfered with each other.

The moment I created THREE org.eclipse.ui.commands and THREE
org.eclipse.ui.bindings in plugin.xml everything worked.

If it is of interest for anybody what you have to take care of in
addition when implementing each of the different variants, I can tell
you what I found out else.


However, with the follwoing issues still not resolved:

- ActionBarAdvisor and actionSet items are active before the process
behind makes sense (when at least one editor window is open). This leads
to an NPE when selecting menu items/coolbar icons before this state is
reached. Which can be try/catched and ignored, of course, though not
very neat.

Yes, I could check for
mypackage.Activator.getDefault().getWorkbench().getActiveWor kbenchWindow().getActivePage().getActiveEditor()
!= null before, but the behaviour described below would be much nicer.

- editorAction items are only displayed when an editor is open. If it is
not they are not visible at all, rather than being displayed disabled
("grayed out").

Do you (or anybody else) know how to achieve this? It works in my app
with the built-in menu items, so it must be possible (easily?) somehow.
Please don't suggest to dig through the RCP framework's code to find out
how it's done there. I tried this a few times and lost myself every time
in the dozens of packages and hundreds of classes/interfaces. And, my
understanding of using a framework is different: I'm not supposed to
know how to build a street to drive a car ;-)


> Q3:How do I get the contents of the currently active editor?

>
UIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindo w().getActivePage().getActiveEditor().getEditorInput();

>> Man! These are the lines which make me damning OO sometimes :-)

>> Nevertheless, thanks for the info. The line I had to use in my
action class' IActionDelegate.run(IAction action) method is:

>> IEditorInput editorInput =
[myPackage.]Activator.getDefault().getWorkbench().getActiveW orkbenchWindow().getActivePage().getActiveEditor().getEditor Input();

>> Since the New --> Plug-in project --> RC App?:[x]Yes wizard calls
the controlling class "Activator" in 3.2.

>> However, now I get the editorInput, but this is not of much help
since the org.eclipse.ui.IEditorInput does not provide a method to get
the editor's content as String or char[], for instance.

>> As far as I can see, one solution would be to use the
sub-interface's IFileEditorInput.getFile() and to read the contents from
HDD myself again (not very elegant).

>> Or I use IStorageEditorInput.getStorage(), then IStorage.getStorage,
then InputStream.getContents(). (Does this read the file from HDD once
again, too?)


> Q4: I would like to open a file. Process it somehow. Put the results
of this process into a new editor. Optionally save the contents of this
new editor to a new file. Thus, the contents if the original file do not
change.
>
> I hope the file you will be processing is from the open active
editor. In that case you can get a link to the file using the code I
gave you in Q3. From there may be you can process the info as you
require and create a new file putting the processed data into it. You
can create this file on the file system as a temp or a permanent file as
you require. Then you can open a new editor taking this new file as the
input to the editor, showing the processed data in the new editor.
> I hope I have understood your problem right?
> Hope these things were of some use :)

>> Yes, the ideas are of use, though not exactly what I imagined.
Didn't plan to bother with (temp) files, before my users decide
whether to save the - changed - contents to a file or to discard it finally.

Geri
Enable/disable action [message #461527 is a reply to message #461274] Thu, 11 January 2007 11:36 Go to previous message
Geri Broser is currently offline Geri BroserFriend
Messages: 20
Registered: July 2009
Junior Member
> ... rather than being displayed disabled ("grayed out").

I did it! *slaponmyownback*

It was not quick, and maybe it's dirty, but it works.

Forget about Action Sets. Forget about editorContributions. You have to add your Actions in
ApplicationActionBarAdvisor.java.

1) Add the following to Application.java:
..
..
..
import java.util.HashMap;
import org.eclipse.jface.action.Action;
..
..
..
public class Application ...
.
.
.
// Your application's actions storage
private static HashMap actions;
.
.
.
static Action addAction( Action action )
{
actions.put( action.getId(), action );
}

public static Action getAction( String id )
{
return (Action) actions.get( id );
}

public Application()
{
actions = new HashMap();
}
.
.
.


2) Add the following to ApplicationActionBarAdvisor.java for each action you would like to have acccess to:
.
.
.
protected void makeActions( final IWorkbenchWindow window )
.
.
.
yourAction = new YourAction();
Application.addAction( yourAction );
.
.
.


3) Add the following to YourAction.java:
.
.
.
import your.package.Application;
.
.
.
private final static String ID = "your.package.actions.YourActionID";
.
.
.
public YourAction()
.
.
.
setID( ID );
setEnabled( false );
}
.
.
.

4) Add the following to the class which is ought to control action enablement ("SimpleEditor" in my case):
.
.
.
import your.package.Application;
.
.
.
private static byte instanceCount = 0;
.
.
.
public SimpleEditor()
.
.
.
Application.getAction( "your.package.actions.YourActionID" ).setEnabled( true );
instanceCount++;
}
.
.
.
public void dispose()
{
if ( --instanceCount == 0 )
Application.getAction( "your.package.actions.YourActionID" ).setEnabled( false );
super.dispose();
}

5) Be happy! :-)
Previous Topic:load workbench after login
Next Topic:Is there a standard TableViewSorter?
Goto Forum:
  


Current Time: Thu Apr 25 04:21:25 GMT 2024

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

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

Back to the top