Skip to main content



      Home
Home » Modeling » GMF (Graphical Modeling Framework) » IllegalStateException when executing RecordingCommand
IllegalStateException when executing RecordingCommand [message #150125] Tue, 11 September 2007 14:01 Go to next message
Eclipse UserFriend
Originally posted by: slorenc.infogix.com

I have a node represeting a file. The node displays the name of the file in
a label. When I create this node after the node is visible on the diagram I
peform direct edit. During the edit I show a dialog from which user selects
a file. Then I want this filename to be shown in the label.

To set the name, I decided to use RecordingCommand after the dialog
prompting for filename is dismissed. However during the execution of this
command I get this exception from TransactionalEditingDomainImpl.

IllegalStateException("Cannot activate read/write transaction in read-only
transaction context"

This command is executed from NameEditPart.performDirectEditRequest

/**
* @generated NOT
*/
protected void performDirectEditRequest(Request request) {
final Request theRequest = request;
try {
getEditingDomain().runExclusive(new Runnable() {

public void run() {
....
standard generated code removed
...
//// // now make changes in the model using recording command
(records changes for undo)
//// TransactionalEditingDomain transactionalEditingDomain =
getEditingDomain();
//// RecordingCommand recordingCommand = new RecordingCommand(
//// transactionalEditingDomain) {
//// protected void doExecute() {
// sourceObject.setName(sourceName);
//// }
//// };
////
transactionalEditingDomain.getCommandStack().execute(recordi ngCommand);


The question I have is why is the transaction created as 'read-only'. The
default behavior was to show the in place editor of the label which for sure
changes the label so the transaction shouldn't be read only. The default
behavior works for some reason but.

Should I maybe perform 'open' instead of 'edit' request?

I read the documentation on EMF transactions and it talks about sharing the
transaction and acquiring it via runExclusive as shown above.

What am I doing wrong here? Should I let the first transaction commit
before starting editing again?

Any hint will be appreciated?

Swavek
Re: IllegalStateException when executing RecordingCommand [message #150482 is a reply to message #150125] Wed, 12 September 2007 14:32 Go to previous message
Eclipse UserFriend
Originally posted by: slorenc.infogix.com

I finally have a solution for this problem. The default behavior for a
figure with a label is to launch an in-place
edit box right after a figure is created on a diagram. When editing is
finished the new value from the edit box is commited to the model (e.g. name
is set to new value). The setting to the new value is performed within a
transaction which is initiated in a listener attached to the edit box. So
by the time the changes are ready to be commited the original direct edit
request is done and the read only transaction is done (rolled back). Now a
new (writeable) transaction is started for commiting of the changes.

In my case, I wanted to lauch a modal dialog instead of edit box and after
user selects an item from the dialog use this item's name to set the name
property of the underlying object. The problem was that when a command to
commit this name was executed a new nested transaction was created whose
parent was the original read only transaction. The code in the framework
checks if the transaction's parent allows writing and if not it throws the
IllegalStateException.

So how do you execute a command so that this exception is not thrown. Well,
you have to let the first transaction finish and start a new transaction
which doesn't have a parent and is not read only. But how do you do that
when you are in the
middle of processing of 'direct edit' request. In editpart for the name
(not the object whose name it is) I modified the performDirectEditRequest to
launch a AddSourceDialog as follows (see code below).

/**
* @generated NOT
*/
protected void performDirectEditRequest(Request request) {
final Request theRequest = request;
try {
getEditingDomain().runExclusive(new Runnable() {

public void run() {


Display.getCurrent ().asyncExec (new Runnable () {
public void run() {
Shell shell =
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShel l();
AddSourcesDialog dialog = new AddSourcesDialog(shell);
if (Dialog.OK == dialog.open()) {
final GraphicalEditPart editPart =
SourceNameEditPart.this;

SafeRunnable.run(new SafeRunnable() {
public void run() {

SetRequest setRequest = new SetRequest(element,
DiagramPackage.eINSTANCE.getSourceType_SourceName(), newSourceValue);
CommandStack stack =
editPart.getViewer().getEditDomain().getCommandStack();
stack.execute(getCommand(setRequest));
}
});


}
});
}
}
}
});

Notice we have 3 levels here
1) getEditingDomain().runExclusive(new Runnable() { - I left this one
because I am not sure if I will need it later
2) Display.getCurrent ().asyncExec (new Runnable () { this is the thread
which will execute my modal dialog
3) SafeRunnable.run(new SafeRunnable() { thread which executes my command
which changes the name -

After doing these changes I also added a new DirectEditPolicy
(MyDirectEditPolicy) which processes SetRequest and creates a SetCommand.
This command is what is returned from getCommand(setRequest) method. I
install this policy in place of regular DirectEditPolicy in
createDefaultEditPolicies.

So now when processing original directedit request I start this new thread
and exit performDirecEditRequest (complete processing). This commits the
first transaction. When the dialog is dismissed I run this SafeRunnable and
during execution of stack.execute(getCommand(setRequest)); a new writeable
transaction is created and the changes are commited. After that the trhead
in which the dialog ran exits.

I suspect you could also use RecordingCommand in SafeRunnable or even do
without the SafeRunnable. I just imitated the approach used by the edit box
listener which commits the name.



"Swavek Lorenc" <slorenc@infogix.com> wrote in message
news:fc6l5s$5k7$1@build.eclipse.org...
>I have a node represeting a file. The node displays the name of the file
>in a label. When I create this node after the node is visible on the
>diagram I peform direct edit. During the edit I show a dialog from which
>user selects a file. Then I want this filename to be shown in the label.
>
> To set the name, I decided to use RecordingCommand after the dialog
> prompting for filename is dismissed. However during the execution of this
> command I get this exception from TransactionalEditingDomainImpl.
>
> IllegalStateException("Cannot activate read/write transaction in read-only
> transaction context"
>
> This command is executed from NameEditPart.performDirectEditRequest
>
> /**
> * @generated NOT
> */
> protected void performDirectEditRequest(Request request) {
> final Request theRequest = request;
> try {
> getEditingDomain().runExclusive(new Runnable() {
>
> public void run() {
> ...
> standard generated code removed
> ..
> //// // now make changes in the model using recording command
> (records changes for undo)
> //// TransactionalEditingDomain transactionalEditingDomain =
> getEditingDomain();
> //// RecordingCommand recordingCommand = new RecordingCommand(
> //// transactionalEditingDomain) {
> //// protected void doExecute() {
> // sourceObject.setName(sourceName);
> //// }
> //// };
> ////
> transactionalEditingDomain.getCommandStack().execute(recordi ngCommand);
>
>
> The question I have is why is the transaction created as 'read-only'. The
> default behavior was to show the in place editor of the label which for
> sure changes the label so the transaction shouldn't be read only. The
> default behavior works for some reason but.
>
> Should I maybe perform 'open' instead of 'edit' request?
>
> I read the documentation on EMF transactions and it talks about sharing
> the transaction and acquiring it via runExclusive as shown above.
>
> What am I doing wrong here? Should I let the first transaction commit
> before starting editing again?
>
> Any hint will be appreciated?
>
> Swavek
>
>
>
>
Previous Topic:Compartment with special characteristics
Next Topic:Creating more than one gmfmodel from one ecore model.
Goto Forum:
  


Current Time: Tue Jul 22 03:09:25 EDT 2025

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

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

Back to the top