Databinding question regarding change highlighting - data-binding

A short discription of the application I'm developing is: It's a WinForms app, where the UI is databinded to some classes that implement INotifyPropertyChanged. That classes are updated by a background thread, and we want to allow the user to see what has been changed.
The current solution in short: every setter of a property is checking if the new values is different than the current one. In the class I'm holding a flags-enum, where every bit represents one of the properties. So if the value changes, the corresponding bit is set. Additionally there is a timer ticking every 2 secs, that takes back that bit after a while.
First thing I would like to achieve is to get ride of the mess in the property setters, and to have the logic encaplused somewhere. Another reason is that me might run out of bits within the enum, etc etc...
This solution isn't ideal, as it makes it necessary to touch every setter, and maintain an enum property, so I would like to replace this solution with a "better" one (whatever this really means).
I'm wondering if and how somebody else solved that problem already?
tia
Martin

You need to raise a PropertyChanged event when when the value is changed. See for example MSDN

Related

Adobe CQ: Make two <cq:include> with same path editable

I have two "cq:include" in same jsp with same path and I need to make both of them editable. But currently only one of them is editable.
If I change anything in one component that shows on second. But the second one itself is not editable. My requirement is to make both the components editable while keeping the include path same.
Code:
<cq:include path ="abc" resourceType="xyz"/>
<cq:include path ="abc" resourceType="xyz"/> # This one is not editable.
Having two components with the same resource type would create only a single node at the given path. Hence, any change you make in one of the component would be reflecting in both of them, as both the components would be reading from the same node.
This is also the reason for not being able to edit the second component. Try providing different paths for different components like shown below.
<cq:include path="abc" resourceType="xyz" />
<cq:include path="abc_0" resourceType="xyz" />
Similar questions have been asked here and here
The way the authoring system works it hooks per location so if you want to have it authored in two different places those two different places should NOT be on the same page. This is just good usability as it can be very confusing.
INSTEAD, I suggest the first one on the page add an attribute to the slingRequest noting that it has been placed on the page and future instances with the same path put a message on the screen saying that it is edited elsewhere. Without knowing more details it's hard to suggest the best usability for this scenario.
Update:
If you MUST do this, here is a work around.
Step 1: define a convention for naming 2 properties. e.g. realTitle (String) and realTitleLastUpdated (date/time). These will be page-level properties, making it easy to access and check them using pageProperties. Though you can do this through a subnode as well but that gets more complicated.
Step 2: For the components that must all be simulateously editable, allow them to create their own nodes. Then, on load in the EDIT environment, check the property realTitle and the lastUpdated time stored in realTitleLastUpdated
If the last edit of your component's title, e.g. jcr:lastModified is newer than realTitleLastUpdated, change the local value of title (e.g. jcr:title) to the value of the realTitle property and update the realTitleLastUpdated time to reflect the time in jcr:lastModified on your component.
If the opposite is true - realTitleLastUpdated is of a time newer than the last modification of the local component, then update the jcr:title of the component and the times to match.
Obviously if the last update times match, do nothing.
It's a bit of a run around, but this will keep everything in sync.
I realized that you may not realize that you probably need to save the state. I believe you can do this (among other ways) using resource.getResourceResolver().adaptTo(Session.class).save()
I've done this before but it's been a while. Let me know if you have issues, I'm working from memory.
Well, if you don't want author to bother about editing both the components, then you should use javascript/jquery and onChange() of one of the component, modify the value of another component as well.

Using one data source for multiple datagrids in Flex3

I want to use one data source (e.g. an Array) for multiple Datagrids that have different filterFunctions attached and show different columns.
First, I thought I use a very straight forward apporach:
create the Array
create an ArrayCollection for every DataGrid and set the "source" property to the Array
create the DataGrids and set their dataProvider property to its designated ArrayCollection
So now. every ArrayCollection can have its own filterFunction, sort state etc. but there needs to be only one Array with all the data in memory.
Now to the point that totally confused me:
As new items are added to the Array, of course no Events are dispatched and I have to call itemUpdated manually on each of the ArrayCollections. While debugging into the code in order to get a deeper understanding for Flex, I tried to figure out, what this misterious "itemUpdated" method does, especially as it notes in the adobe documentation, that, if no "property" is given (e.g. it is null), a simple "refresh()" will occur.
I did not find any calls to "refresh()" in the whole debugging (and I went down the framework whole as deep as possible (btw: lots of funny comments right in the code :-) )).
The only thing I could find was a CollectionChangeEvent getting dispatched with a PropertyChangeEvent in its "item" property. Which was of the kind "UPDATE" (and not, as I would expect "ADD"). When trying to dispatch that event manually, it never worked (e.g. the datagrid did not update).
I know I have to stick with itemUpdated for the moment, but as the dataprovider can get big (in both dimensions), performance does concern me and I wnat to understand what is going on under the hood.
And as expected, no help from adobe :-(
So a big thanks for everyone who read this whole text.
And an even bigger THANKS to anyone who answers and gives me the slightes hint in how I can get out of the confusion and understand (if thats possible at all) Fles a little better.
finest of all regards,
herbert
You have to call ArrayCollection.refresh() for each of your dataProviders to get the dataGrids to show the new changes to the source array.

Checkbox in Flex Datagrid Broken on Scrolling

I have a checkbox in a Flex DataGrid, and when I scroll, other rows are randomly checked/unchecked.
After reading over: Creating a column of RadioButtons in Adobe Flex
it's clear that the itemRenderers are getting recycled, but the problem I have with the solution presented there is it moves info about the view into the model.
Does anyone have a better way of solving it, that doesn't force me to put information for the UI into my actionscript model classes? (in my case, I am converting incoming XML data to actionscript classes, and these are getting bound to my datagrid).
Thanks for the help.
thanks everyone. great tips. unfortunately it was becoming too much overhead to keep the model pure, so i just polluted the model like the link in my original post. :( at least it works.
Chetan, neat idea.. i tried working with this for almost an entire day with no luck though.
brd6644, good thoughts on separating the two model classes.. i might go back and do this later.
You could create a subclass of DataGrid that internally stores what rows are checked/unchecked (Array/Collection of Boolean) but you would have a devil of a time keeping that in sync with the dataProvider when it is sorted or filtered. I suppose you could use a Dictionary that is keyed by the object in each index of the dataProvider and valued with a Boolean to indicate whether it's selected. That would at least isolate you from the sorting / filtering issues. This will not work if you have duplicate references in your dataProvider.
Alternatively, you could create a subclass of your ActionScript model class and add the "selected" property to it, then write some simple utility methods to "convert" between the two. That way your View deals only with the "ViewModel" class and other layers (especially the server side) deals only with the real "Model" class.
Adding to what cliff.meyers said, there is a third option of creating a custom IList class as described in this blog post by Alex Harui. It is pretty clever actually, and is cleaner as it doesn't require subclassing the component or polluting your model classes.

How to get business objects and UI controls to talk to each other

I've got a person object with a name and age property that implements INotifyPropertyChanged. I want to hook this object up to an ASP.NET form so that the 'name' and 'age' properties bind to textboxes in a way that, when changes happen in either place (in the control or in the object) the other will get updated.
Do I create an intermediary class that listens to each textbox change events and the objects change events and handle the updates between them? What's the best way to do this?
I'm unclear on how to get business objects and the UI talking to each other.
I've stressed over this exact problem a lot.
The short answer is, yes, an intermediate item.
The trick is to NOT write ANY code per control. You should be able to place a GUI control on the screen (That may or may not take code), and then bind your business logic to it through a generic binding mechanism.
I have defined the bindings through XML, through properties files, and through constant arrays--there are a million ways...
You probably have to write code per TYPE of object bound (a listbox binds differently than a text control) and you may have to write validators (but specifying the parameters to the validators and which control the validators bind to should also be done in data)
Now all that said, I'd be really surprised if some data-driven auto-binding mechanism didn't already exist, Microsoft has been into that since VB first came out (although their implementations used to be pretty inflexible, I'm sure they do a better job now).
I'm very insistent about the 0 lines of code per control because my job has typically involved configuring complex devices with dozens of pages of controls. A typical client/server system will have 7(!) lines of code PER CONTROL just to transport data from the DB, to the server, to the client, to the screen and back (this is a minimum for plain ole "dumb" code with no smart binding tricks).
0LOC/control may not be a requirement for everyone, but it's a good goal.
Comment response:
I've done most of my stuff manually in Java, so I'm not sure I can be too much help with the specifics.
Searching for C# and binding gave me this which looks promising, although it may be binding straight to a database which is too much IMO, it should bind to a business object, but the concepts should be the same.
One way to create the bindings at first is to manually instantiate binding objects... (Please excuse my Java)
TextControl textCtrl1=new TextControl("Name Goes Here");
new TextBinder(textCtrl1, personObject, nameField);
In Java, that second line gets tricky. When you are binding to a particular field, you HAVE to use reflection to find the setter and getter for that field of the personObject. In C# I think it should be easier.
Anyway, the binder should add itself as a listener to the control and the object, then forward changes back and forth.
Does that help any?
Edit2:
As you noticed, the hard part is noticing when your property is updated. Luckily, that is optional. More often than not, you don't need to update the component once the object is set (I had to deal with this a few times when I had distributed UIs that could update each other).
So, if you assume your object won't change, the "Binding" has to do the following:
get the value from the property and set it in the component.
add itself as a listener to the component.
store the property/object (if you can manipulate properties, you're set here. If not, you need to store the object and property name, and use reflection)
bail and wait for an "updated" event from your component.
When you get the update from your component:
- store the value in the property.
- You may want to set an "Updated" flag or store the original so that if you iterate through all the binding components, you can tell if any updates need to be saved/enable the "ok" button.
Your object should always be pretty much up-to-date now.
As you build a form, you may want to put all your binding controls into a collection so that you can do a few other operations...
A "Save" operation could call each binding control and tell it to copy from the control to the property, that way you don't need to use a listener.
A "Reset" operation can reset all the controls to their original value.
A "Test" operation can ask each control if it's been updated.
. etc
The neat thing about doing it this way is that every "Operation" you wish to add is pretty trivial to add, but automatically affects the entire UI.
You probably also want a little object hierarchy of controls with an abstract base "bind" class, then a specific binder for each type of control (text field, number field, date, spinner, table, pulldown)--I think that's about it.
This can be very simple, but gains complexity rapidly. Try it with a text field and see what you can do. A simple text binding object should just be like 5 lines of code if you can pass "properties" around in C#...
Okay, totally separate answer. As I told you, I'm not very up-to-date with C# technologies, but from what I've heard, LINQ may do this entire job for you.
In fact, LINQ may be made to do exactly what you are trying to do. It doesn't exist in Java, so that's why I gave you the "Manual" version in the other answer.
The comment at the bottom of this page: http://msdn.microsoft.com/en-us/library/z919e8tw.aspx alludes to a better way.

How to organize ASP.NET Wizard control with many databinds

I've got a wizard control that databound controls on each step. I can't databind them all at once because they are dependent on the previous step. So, essentially what I've got at each step is a save to the database of the previous step, and an initialization of the current step.
Are there any recommendations as to how best to organize my code? It works, but it's not very readable, and extremely brittle.
EDIT: I should add that I've seen most of the wizard control tutorials out there, but none of them seem to address what I'm trying to do. In particular, the need to save and retrieve data between steps, and how to keep it from retrieving that same data again if the step is revisited.
What you've done sounds reasonable.. Can you be more specific about the problem you are having?
One thing about the wizard control, as your workflow gets more and more complex I think the coupling between your workflow state and the wizard SelectedViewIndex becomes problematic. For this reason I eventually separate them. I will usually use a state/statemachine pattern, where the current workflow state is used to determine the appropriate wizard view index (but not vice-versa).
If you're looking for examples on how to implement a state machine, I have a test app out there that walks through dialogs like a wizard control, except using javascript. Check out http://main(dot)test.wishpot.com/WaveDataCollection.Frank/, after you get to the page CollectSamples.aspx, go ahead and view source, then start reviewing at the GotoState function.
State machines are plumbed a bit different in C#, the main difference being the state object is an abstract class with a fixed number of event handlers, which each state inheriting from that class implementing each handler (some perhaps throwing an exception). With javascript we don't need the abstract state class... Also, doing this serverside, you're going to need to be able to map from a state ID stored in your database to a state class.

Resources