Thursday, 13 October 2011

Morphing Conditional Activities

I’m currently working on a workflow implementation which, like many, is going to be used by users who are not necessarily programmers. So the thought of giving them conditions to write in code is the kind of thing that keeps me up at night.

I’m using Flowchart as the root node as it provides the sort of UI that we need, however there are a few issues.

First off is the display of the FlowDecision node. The standard look and feel is as follows…

image

That’s all well and good, but I’m trying to keep this usable from non-programmers and so setting the Condition property (a) from the property grid and (b) in code is a step too far.

Re-Designing FlowDecision

So, the first step is to re-design this designer. It’s a simple job as with WF4 it’s a load easier to register a new designer. Here’s what I ended up with, the XAML and code is shown later…

image

That’s looking more user friendly already. The first step was to create a new designer, the XAML of which is shown below (I’ve taken some formatting liberties with the XAML to fit on the page)…

  <sap:ActivityDesigner x:Class="CustomActivities.CustomFlowDecisionDesigner"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sap
="clr-namespace:System.Activities.Presentation;
assembly=System.Activities.Presentation"
xmlns:sapv
="clr-namespace:System.Activities.Presentation.View;
assembly=System.Activities.Presentation">

<
sap:WorkflowItemPresenter Item="{Binding ModelItem.Condition}"
HintText
="Drop a condition here"/>

</
sap:ActivityDesigner
>

All I’ve done here is derive from ActivityDesigner and then create a WorkflowItemPresenter (there’s a bit more work to do on this but I’ll save that for another blog post).


So, now I want to have a ‘condition’ activity (that’s a naming convention I’ve come up with myself). A condition activity returns a boolean, so will fit nicely into the slot provided by FlowDecision. My first one is the “IsAPastyMuncher” activity (I’m from Cornwall in the UK, pasties are part of my heritage – for some great ones you can do a lot worse than order over the phone from Malcolm Barnecutts).


Anyhow, this activity returns True or False, and the default designer looks like this…


image


Now that’s OK when it’s on its own, but as this activity is designed to be dropped within the FlowDecision it doesn’t look as good in that instance…


image


Removing the ‘Chrome’


There’s a bit too much ‘chrome’ going on there for my liking – the reason being that a standard designer derives from ActivityDesigner which adds the adornments such as the border, name and so on. What I’d really like is ‘chrome-less’ experience in the designer, and to do that all you need to do is change the base class of your designer from ActivityDesigner to WorkflowViewElement. This is as simple as editing the XAML directly and altering the root element to the following…

<sap:WorkflowViewElement x:Class="CustomActivities.IsAPastyMuncherDesigner"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sap
="clr-namespace:System.Activities.Presentation;
assembly=System.Activities.Presentation"
xmlns:sapv
="clr-namespace:System.Activities.Presentation.View;
assembly=System.Activities.Presentation">

<
CheckBox Content="Munches Pasties" IsChecked="{Binding ModelItem
.MunchesPasties}"/>

</
sap:WorkflowViewElement
>

Then you’ll have a much better experience within the designer…


image


That’s looking a load better – maybe still some room for improvement mind you (again another post I must get around to) but it’s getting there. But now to the main event…


Morphing Activities


There is however one major problem with the IsAPastyMuncher activity as now, when added to the toolbox & dropped outside of a decision I see just the following…


image


Now that’s a bit too minimal even for me! What I would ideally like to do is check if this activity has been dropped inside a FlowDecision, and if not automatically surround it with one. Your first thought might be to dive in with an activity that implements IActivityTemplateFactory, create a FlowDecision and within it create the IsAPastyMuncher activity. There’s one problem with that – FlowDecision doesn’t derive from Activity and cannot therefore be returned from the custom factory.


Never fear however, where there’s a will there’s a way, and instead I’ve overridden a method within the IsAPastyMuncher activity that is called when the activity is created (i.e. by the drag & drop operation) and in here I can do the switch. The code is shown below…

  protected override void OnModelItemChanged(object newItem)
{
ConditionHelper.MorphIntoDecision(newItem);
}

I’ve created the ConditionHelper class which actually performs the swap…

internal class ConditionHelper
{
/// <summary>
///
Method that morphs a dropped activity inside a flow step into the same
///
activity but within a flow decision
/// </summary>
/// <param name="newItem"></param>
public static void MorphIntoDecision(object newItem)
{
if (newItem is ModelItem)
{
ModelItem item = newItem as ModelItem;
ModelItem parent = item.Parent;

if (parent.ItemType == typeof(FlowStep))
{
// Morph the parent to a flow decision
EditingContext context = item.GetEditingContext();
ModelItem newModelItem = ModelFactory.CreateItem(context, new FlowDecision());

using (ModelEditingScope scope = newModelItem.BeginEdit("Convert to Decision"))
{
MorphHelper.MorphObject(parent, newModelItem);
MorphHelper.MorphProperties(parent, newModelItem);

// Now I need to set newModelItem.Condition = parent.Action,
// so the MorphProperties helper is pretty useless in this instance!
ModelProperty conditionProp = newModelItem.Properties["Condition"];
ModelProperty actionProp = parent.Properties["Action"];
conditionProp.SetValue(actionProp.Value);
actionProp.SetValue(null);

scope.Complete();
}
}
}
}
}

When an activity other than FlowDecision is added to the flow chart activity it is surrounded with a FlowStep activity. I check if this is the case and if so construct a FlowDecision activity as the parent instead and morph the existing parent into the new activity (well, MorphHelper.MorphObject does this for me). Any properties on the original parent activity which match with property names on the new parent activity are copied across using the MorphHelper.MorphProperties call, but then I need to move the actual activity (i.e. the IsAPastyMuncher) from the FlowStep to the FlowDecision. On FlowStep the property holding the activity is Action, whereas on FlowDecision this is Condition, so I use the ModelProperty class to make this change.


Now I can drop on an IsAPastyMuncher activity and have it automatically surrounded with a FlowDecision, but also allow the user to drop one inside a FlowDecision that’s already on the design surface which is the best of both worlds.


The Code


If you would like to download a sample please click here. This is built on Visual Studio 2010.


In the example there are 2 custom activities on the toolbox – the IsAPastyMuncher which morphs inside a FlowDecision when dropped, and the LovesClottedCream activity which does not.

40 comments:

Unknown said...

Hey this looks like something i could use.

Do your decision activity support a false and true branch?

Will it be possible to do that same for flowswitch?

The link to your sample seems to be broken :(

Could you mail me the sample?
- rck at mysupply dot dk

In advance thanks! :)

Unknown said...
This comment has been removed by the author.
Morgan said...

Travelling at the moment, will dig out the code when I get back home.

Biondo said...

Very interesting post! I need to do the same in my workflow application. I want to modify the designer of FlowDecision activity in order to allow users to define conditions through a user friendly GUI.
Download link seems to be broken, could you email me the source code? r.vviani AT libero DOT it.
Thanks.

Morgan said...

Emails sent!

Unknown said...

Could you mail me the sample please?
Thanks in advance!

ulker said...

Hi Morgan,

It's very useful for me too. I'm trying to make a custom decision activity in my application. I'll be grateful if you send me the sample.

Thanks in advance.

Unknown said...

HI,

Are you able to make the code available again or can you please email it to me?

Thanks

Unknown said...

Very interesting post! I need to do the same in my workflow application. Could you mail me the sample please?

Thanks!
Cuong Bui

Taylor Bara said...

I want that you review my site on the errors. have a peek here and leave your thoughts in comment section.

Steve smith said...

What a amazing post shared here. I really love this website, I would like to say thanks for sharing such great post HPE2-E69 dumps . I would like to say bundle of thanks for sharing.

smith12 said...

If you wish to become the best author of this age, you need to start a search for www.edubirdie.com reviews as soon as possible. Only this search will give you the information you need to become one of the greats.

Unknown said...

Minecraft apk Hay Day mod apk

Unknown said...

hi Morgan .

Thank you for the good content of the your blog.
Download link seems to be broken, could you email me the source code?

thanks


Inspiration said...

Thank you for the visit, thank you also have provided the information we really need, do not forget to share information and visit our page thank you. hotele , sprawdz rowniez hotele na mazurach

meldaresearchusa said...

You can leave all the stressful work to us so that we handle it for you and assure you of delivering excellent online nursing essay writers to you all the time in our best custom research paper site company.

jacklinemelda said...

Our free college research papers is also keen on ensuring that the content of the completed paper is free from grammatical errors so as to provide those seeking for online research paper writing services receive a professional legitimate custom paper .

Florahmelda said...

Our Professional Medicine Essay Writers work tirelessly to ensure that your Medicine Research Paper Writing is completed within the time frame given to avoid poor scores in your Medical Essay Assignments Writing.

Sam said...

We, at MyAssignmentHelp ensures the assignment submission before the due time, in case, amendments needed.We do not hold a customer for long, or procrastinate over the order. Every time a customer comes up with the order, the due time of delivery is decided. Once placed the assignment help online order, feel stress-free and wait for the most authentic assignment to be submitted.

noor said...

كشف تسربات المياه بالمدينة المنورة
نقل عفش بالمدينة المنورة
شركة مكافحة الحشرات بالمدينة المنورة

ارخص شركة نقل عفش بالمدينة المنورة

Unknown said...

DriverMax Pro is a free driver updater software that supports automatic installation, scheduled scans, and full device driver backups. Furthermore, it acts as a tool to discover and download the latest driver updates for your system.

Unknown said...

Autodesk AutoCAD is a computer-aided design that is created for architects, constructional professionals, and engineers that helps them in creating 2D and 3D drawings.

Unknown said...

Sandboxie provides a disconnected testing environment to the users. It enables them to run programs and files without affecting the system, application, or any platform on which they are installed.

Unknown said...

MacBooster is a software program that is related to Mac. It is multi functional. Also, it is not only a simple but effective software which is a straightforward crack.

shoaib said...


I’m impressed, I must say. Seldom do I come across a blog
that’s equally educative and interesting, and without a doubt, you’ve hit the
nail on the head. The problem is something which too few folks are speaking intelligently about.
I am very happy I found this in my search for something concerning this.
wondershare uniconverter crack
4k video downloader crack
cyberghost vpn crack
advanced systemcare pro crack serial key

Unknown said...

This is my first visit here and I feel really good reading everything in one place.
sidify music converter crack

Muzamil Ansari crack said...

easeus data recovery wizard crack
fineprint
imazing full crack
photolemur crack

Muzamil Ansari crack said...

idm crack
xrecode iii
easeus data recovery wizard crack
eset smart security crack

Muzamil Ansari crack said...

snagit crack
avast premier crack
paragon ntfs crack
reaconverter pro crack

Taha Rao said...


Hi! This is my first comment here so I just wanted to give a quick shout out and say I
really enjoy reading through your articles.
Can you suggest any other blogs/websites/forums that deal with the
same subjects? Thanks a lot!
tuxera ntfs crack

Junaid Amin said...


I just want to say that your article is just as great. The clarity of your message is simple
excellent and I can assume that you are an expert on this matter.
Well, with your permission, let me introduce you to the feed to keep you updated on future posts.
photofiltre studio x crack
smart defrag portable crack

Unknown said...

Skip to Main Content. Skip to main content. Microsoft. Software Download. Software Download. Software Download. Home. Windows. Windows 10 · Windows ...
clone fish voice changer

unknown said...

avg-antivirus-crack

gets rid of the virus from the device. it's far an antivirus that the person can deploy on their plans to get safety. also protects and gives security to the scenarios in which it's miles set up and gets rid of malware from that gadget through scanning the device. One is the protection of computers, and the opposite is the safety of the net and email. laptop level protection way that it protects the machine from spyware, rootkits, viruses, malware, trojans, ransomware, or different malware.

Softwarelife.org said...

PE-Design Crack is programming for making weaving amalgamation. This program features auto and Photo Stitch has 130 worked in literary styles, similarly as 5 new content styles for minimal substance, unique kinds of lines to make complex plans weaving, customized creation of utilization.
Pe Design Crack

Unknown said...

Your source for fun, free mobile and PC download games. Thousands of free ... Download or play free online! ... Here is the Exact Arcade Version of Dig Dug!
2007 android games

Azhar hussain said...


Oh, this is a great post. Concept In addition to wasting time and effort
I write great articles, I want to write that way. I save a lot for later and don't seem to be doing anything.


quicktime pro

Muzamil Ansari crack said...

Hello! This post couldn't have been better! Reading this post reminded me of my good old roommate! He always talked about it. I will forward this page to him. I'm pretty sure he'll read it well. Thanks for sharing!
iobit uninstaller pro crack
activepresenter pro crack
bootstrap studio crack
wondershare mobiletrans crack
smartgit

Jordan Leonard said...

This is very interesting, you are a skilled blogger. I have added your feed and hope to keep looking for more
great posts. Plus, I have shared your website on social media!
blackmagic fusion crack
cyberfox crack
xlstat crack
paperscan pro crack
smartftp enterprise crack
razer surround pro crack

cracks king 333 said...

You have Thank you for the information. This is very useful. If you know the wizard reset password, visit our Seagate Central Assistant Password Reset page.
manycam pro crack
wondershare video converter ultimate crack
manycam pro crack
magix music maker crack
iobit malware fighter free
idm crack
hma pro vpn crack
final cut pro x 10 crack

malik shoaib said...

After looking over a number of the blog posts on your
the site beutiful post this is good working of site
nitro pro keygen
cinema-4d crack
adobe acrobat reader dc crack