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.

43 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


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...

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.

Sadia 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

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 Khan 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.

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

crackszonepc.com 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

fsgfxg 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!
cricket 07 apk download

Live Web Tutors said...

Reach us for the professional writers like Online Assignment help canada
Online Essay help canada
Online Dissertation help
Homework help Canada
Online Coursework help
Thesis help canada

Blogging Villa said...

I'm truly inspired by your writing skills and also by your blog structure. Is it a paid subject or have you changed it yourself?
No matter how good the high quality content stays, it's uncommon today to see a wonderful blog like this one.
VMware Workstation Pro Working License Key
Driver Downloader License Key Free List,
Global Mapper Crack + Keygen Free Download
Autodesk Fusion 360 Tutorial

Download Manycam Full Crack
Serial 4K Youtube to MP3
Codice Licenza Advanced System Repair Pro
Baixar Free YouTube Download Premium

Syeda Wallail Angaiz Moater said...

This is very rare and useful software. You may get many benefits free. Then why do you pay for other software for the same features? https://cracksir.com/dearmob-iphone-manager/

Sony Pitter said...

What a wonderful article and thank you so much for sharing it with us. I'm a big fan of your work, and I know it helps a lot of people
.

oliver said...


I am very happy to see this fantastic information,that is really a good for mine.https://thepcsoft.com/getdataback-crack-

Raj Sinha said...

This is a good time to make long term plans and it's timely.
Have fun. I have read this post, and if you will excuse me, I would like to advise you on interesting topics or tips.
You can write the following articles on this topic.
I want to read more topics on this topic!
english short stories with moral value english stories What is the factorial of 100

hubert said...

I appreciate your content. It is helpful for new people. this is nice and great!
Click Hear

Magical eyes said...


It's a fantastic theme that's also quite simple to use.
https://cracksync.com/red-giant-universe-crack/

Sony Pitter said...

Wow, this was an exceptionally well-written article. This was just too much information for my taste.https://xactivator.net/indiafont-download-updated/

usman said...

Usman Gull is an experienced SEO professional, having worked in the field for over 6 years. He is highly skilled in SEO analysis and optimization, keyword research, link building, content marketing, and other SEO-related tasks. Usman has worked with a variety of clients, from small businesses to large enterprises, helping them to improve their website traffic and rankings. He is also well-versed in the latest SEO tactics and trends and is always up-to-date on the latest developments in the industry. With his expertise, Usman can help you get the most out of your SEO efforts.
here is a link below
https://usmangull.com/

Angel17 said...

That's a nice project. Hope it's a successful one! basement renovation near me

dbcity said...

Thanks for this helpful blog post! It's clear and informative, offering great insights into [topic]. I appreciate the effort you put into creating such valuable content. Keep it up!
gwalior properties

dbcity said...

Wow, I must commend the author for crafting such a brilliantly articulated piece. However, I found the abundance of information a bit overwhelming for my personal preference. social media promotion agency gwalior