Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Reusing ValidateAction in doSave
Reusing ValidateAction in doSave [message #412013] Tue, 14 August 2007 19:29 Go to next message
Ben Dixon is currently offline Ben DixonFriend
Messages: 63
Registered: July 2009
Member
When the user saves the current model or uses Save All from the File menu, I'd like to invoke validation of the model such that it shows the dialog of problems (or ok status) just as it does when the user right-clicks on a node in the tree and selects "Validate". I found the ValidateAction but the methods I think I would use on it are protected and the code itself depends on a selection. I guess I can get around this through some hacking (making a SelectionChangedEvent or some such and calling run()) but I thought there must be a better way to go about it.

What's the best way to get the validate behavior (dialog, problems view update, etc) without hacking into ValidateAction or dismantling the code there for use in my doSave method?
Re: Reusing ValidateAction in doSave [message #412016 is a reply to message #412013] Tue, 14 August 2007 20:55 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 26226
Registered: July 2009
Senior Member
Ben,

If you create an anonymous subclass, you could set the selected objects
and the domain directly and then invoke run; you'll have access to all
protected things.


ben dixon wrote:
> When the user saves the current model or uses Save All from the File menu, I'd like to invoke validation of the model such that it shows the dialog of problems (or ok status) just as it does when the user right-clicks on a node in the tree and selects "Validate". I found the ValidateAction but the methods I think I would use on it are protected and the code itself depends on a selection. I guess I can get around this through some hacking (making a SelectionChangedEvent or some such and calling run()) but I thought there must be a better way to go about it.
>
> What's the best way to get the validate behavior (dialog, problems view update, etc) without hacking into ValidateAction or dismantling the code there for use in my doSave method?
>
Re: Reusing ValidateAction in doSave [message #412031 is a reply to message #412016] Wed, 15 August 2007 17:07 Go to previous messageGo to next message
Ben Dixon is currently offline Ben DixonFriend
Messages: 63
Registered: July 2009
Member
Works great! Thanks, Ed!
Re: Reusing ValidateAction in doSave [message #647264 is a reply to message #412031] Wed, 05 January 2011 13:12 Go to previous messageGo to next message
Esin Karabacakoglu is currently offline Esin KarabacakogluFriend
Messages: 11
Registered: May 2010
Location: Turkey
Junior Member
Hi all,

I would like to do the same in my EMF editor. I am using Eclipse Helios SR1 and EMF 2.6.0.

In doSave() method of the editor class which extends MultiPageEditorPart, I create an instance of the ValidateAction, set the requirements and call the run method. My code is below:

EObject root = resource.getContents().get(0);
ValidateAction validateAction = new ValidateAction();
validateAction.updateSelection(new StructuredSelection(root));
validateAction.setActiveWorkbenchPart(getSite().getPart());
validateAction.run();

But, get the NullPointerException from the first line of the run() method.

Root exception:
java.lang.NullPointerException
at org.eclipse.emf.edit.ui.action.ValidateAction.run(ValidateAc tion.java:170)
at psm.seagentontology.presentation.SeagentontologyEditor.valid ateEditor(SeagentontologyEditor.java:1899)
at psm.seagentontology.presentation.SeagentontologyEditor.acces s$2(SeagentontologyEditor.java:1894)
at psm.seagentontology.presentation.SeagentontologyEditor$18.ex ecute(SeagentontologyEditor.java:1524)
at org.eclipse.ui.actions.WorkspaceModifyOperation$1.run(Worksp aceModifyOperation.java:106)
at org.eclipse.core.internal.resources.Workspace.run(Workspace. java:1975)
at org.eclipse.ui.actions.WorkspaceModifyOperation.run(Workspac eModifyOperation.java:118)
at org.eclipse.jface.operation.ModalContext$ModalContextThread. run(ModalContext.java:121)

I see that PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShel l() throws the exception because the activeWorkbenchWindow is null.

Do you have any idea about the problem?

[Updated on: Wed, 05 January 2011 13:14]

Report message to a moderator

Re: Reusing ValidateAction in doSave [message #647336 is a reply to message #647264] Wed, 05 January 2011 16:48 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 26226
Registered: July 2009
Senior Member
It's not clear to me how you're invoking this action. It seems clear
there's no active workbench window, but it's not clear how you've
managed to invoke something without a window being active.

esin wrote:
> Hi all,
>
> I would like to do the same in my EMF editor. I am using Eclipse
> Helios SR1 and EMF 2.6.0.
>
> In doSave() method of the editor class which extends
> MultiPageEditorPart, I create an instance of the ValidateAction, set
> the requirements and call the run method. My code is below:
>
> private void validateEditor(final Resource resource) {
> EObject root = resource.getContents().get(0);
> ValidateAction validateAction = new ValidateAction();
> validateAction.updateSelection(new StructuredSelection(root));
> validateAction.setActiveWorkbenchPart(getSite().getPart());
> validateAction.run();
> }
>
> But, get the NullPointerException from the first line of the run()
> method.
> Root exception:
> java.lang.NullPointerException
> at org.eclipse.emf.edit.ui.action.ValidateAction.run(ValidateAc
> tion.java:170)
> at psm.seagentontology.presentation.SeagentontologyEditor.valid
> ateEditor(SeagentontologyEditor.java:1899)
> at psm.seagentontology.presentation.SeagentontologyEditor.acces
> s$2(SeagentontologyEditor.java:1894)
> at psm.seagentontology.presentation.SeagentontologyEditor$18.ex
> ecute(SeagentontologyEditor.java:1524)
> at org.eclipse.ui.actions.WorkspaceModifyOperation$1.run(Worksp
> aceModifyOperation.java:106)
> at org.eclipse.core.internal.resources.Workspace.run(Workspace.
> java:1975)
> at org.eclipse.ui.actions.WorkspaceModifyOperation.run(Workspac
> eModifyOperation.java:118)
> at org.eclipse.jface.operation.ModalContext$ModalContextThread.
> run(ModalContext.java:121)
>
> I see that
> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShel l()
> throws the exception because the activeWorkbenchWindow is null.
> Do you have any idea about the problem?
Re: Reusing ValidateAction in doSave [message #647581 is a reply to message #647336] Fri, 07 January 2011 07:45 Go to previous messageGo to next message
Esin Karabacakoglu is currently offline Esin KarabacakogluFriend
Messages: 11
Registered: May 2010
Location: Turkey
Junior Member
Hi Ed,

I created an instance of action and tried to set set the selected object using updateSelection method and tried to give a workbech part by setActiveWorkbenchPart method of ValidateAction.

I tried your suggestion in your first reply:

Quote:
If you create an anonymous subclass, you could set the selected objects and the domain directly and then invoke run; you'll have access to all protected things.


I think that I couldn't understand this suggestion about using ValidateAction in doSave. If there is no active workbench while doSave is being executed, how can we use this action by creating an anonymous subclass?

Thanks in advance.
Re: Reusing ValidateAction in doSave [message #647661 is a reply to message #647581] Fri, 07 January 2011 16:24 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 26226
Registered: July 2009
Senior Member
I was expecting you do do this in the editor, right? It's hard to
imagine how you can save the editor and not have an active workbench...

esin wrote:
> Hi Ed,
> I created an instance of action and tried to set set the selected
> object using updateSelection method and tried to give a workbech part
> by setActiveWorkbenchPart method of ValidateAction.
> I tried your suggestion in your first reply:
> Quote:
>> If you create an anonymous subclass, you could set the selected
>> objects and the domain directly and then invoke run; you'll have
>> access to all protected things.
>
>
> I think that I couldn't understand this suggestion about using
> ValidateAction in doSave. If there is no active workbench while doSave
> is being executed, how can we use this action by creating an anonymous
> subclass?
> Thanks in advance.
Re: Reusing ValidateAction in doSave [message #649660 is a reply to message #647661] Thu, 20 January 2011 07:30 Go to previous messageGo to next message
Esin Karabacakoglu is currently offline Esin KarabacakogluFriend
Messages: 11
Registered: May 2010
Location: Turkey
Junior Member
Yes, I do that in the doSave method of the editor class, and the active workbench cannot be found in the run method of the ValidateAction. You mean that it's strange, right?

Here's the code part in the doSave method:

WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
// This is the method that gets invoked when the operation runs.
//
@Override
public void execute(IProgressMonitor monitor) {
// Save the resources to the file system.
//
boolean first = true;
for (Resource resource : editingDomain.getResourceSet().getResources()) {

// validate the editor before saving...
EObject root = resource.getContents().get(0);
ValidateAction validateAction = new ValidateAction();
validateAction.updateSelection(new StructuredSelection(root));
validateAction.setActiveWorkbenchPart(getSite().getPart());
validateAction.run();

if ((first || !resource.getContents().isEmpty() || isPersisted(resource)) && !editingDomain.isReadOnly(resource)) {
try {
long timeStamp = resource.getTimeStamp();
resource.save(saveOptions);
if (resource.getTimeStamp() != timeStamp) {
savedResources.add(resource);
}
}
catch (Exception exception) {
resourceToDiagnosticMap.put(resource, analyzeResourceProblems(resource, exception));
}
first = false;
}
}
}
}
Re: Reusing ValidateAction in doSave [message #649844 is a reply to message #649660] Thu, 20 January 2011 17:50 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 26226
Registered: July 2009
Senior Member
Esin,

Comments below.

Esin Karabacakoglu wrote:
> Yes, I do that in the doSave method of the editor class, and the
> active workbench cannot be found in the run method of the
> ValidateAction. You mean that it's strange, right?
Yes, it's not clear how it's possible for there not be an active
workbench window when you must be invoking save from the active
workbench window.
>
> Here's the code part in the doSave method:
>
> WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
> // This is the method that gets invoked when the operation runs.
> //
> @Override
> public void execute(IProgressMonitor monitor) {
> // Save the resources to the file system.
> //
> boolean first = true;
> for (Resource resource :
> editingDomain.getResourceSet().getResources()) {
> // validate the editor before saving...
> EObject root = resource.getContents().get(0);
> ValidateAction validateAction = new ValidateAction();
> validateAction.updateSelection(new
> StructuredSelection(root));
> validateAction.setActiveWorkbenchPart(getSite().getPart());
> validateAction.run();
> if ((first || !resource.getContents().isEmpty()
> || isPersisted(resource)) && !editingDomain.isReadOnly(resource)) {
> try {
> long timeStamp = resource.getTimeStamp();
> resource.save(saveOptions);
> if (resource.getTimeStamp() != timeStamp) {
> savedResources.add(resource);
> }
> }
> catch (Exception exception) {
> resourceToDiagnosticMap.put(resource,
> analyzeResourceProblems(resource, exception));
> }
> first = false;
> }
> }
> }
> }
Worst comes to worst, you'll have to override the ValidateAction.run
method to get the shell in some other way...
Previous Topic:EMF CRUD Generator for a database
Next Topic:Issue of EMF generated xml file namespace
Goto Forum:
  


Current Time: Wed Nov 26 17:56:18 GMT 2014

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

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