GWT SelectionModel in Cell widgets - setSelected by key? - gwt

Is there any way to select items by key with GWT's selection model? setSelected only seems to take an object from which it can derive a Key, but using that function means I have to construct a sort of fake object. If my KeyProvider ever changes, that part of my code could break without my knowledge, so I'd like to just construct a key directly somehow.

"I have a celltable that shows a list of entities. When the user creates a new entity, I want to refresh the list (which will have the new entity) and automatically select the new entity."
I have this behaviour in my app as well. I manipulate the ListDataProvider. I find the item or items of interest, move them to the top of the list, call the selection model to set them to selected, and refresh the attached data displays. I use the same approach for picklist tables I use when I want to pre-select the default choices for the user (usually based on the item that is spawning the picklist) and move them to the top of the list. I spent a lot of time looking through the selection model api and there is nothing for keys. I suspect that the GWT designers figured it wasn't necessary, since you have access to the ListDataProvider. Find the items of interest there and then call the selection model select method on those specific objects. I can see their point -- replicating the functionality in ListDataProvider and SelectionModel would blur the distinction between the two classes, and perhaps limit the reusability of the SelectionModel construction in other (future) data structures that don't use keys.

Related

How to do "in-place editing" of a JFace Table?

In WPF (.net), when a DataGrid is made editable and set to an observable collection, there is a placeholder row. When one clicks on it, a new element is added to the backing collection and the table is put into edit mode.
Is something like this possible with JFace Table Viewers as well?
The only solution I can think of is to create a custom observable list that delegates to the "real" list I want to observe, but always adds an additional placeholder object to the end, which gets inserted into the "real" list as soon as it is edited. But that seems a bit hackish to me...

NatTable cell selection provider

Although NatTable already has a class RowSelectionProvider, my data is provided through cells, not rows, so I cannot use this class. Is it possible to create a class CellSelectionProvider, or it would be too difficult?
What I want to do is select a cell in the NatTable, which is linked to an EObject. Then select the EObject in the editor and show its properties in the properties view. The first part I'm able to do, but not the second.
I've seen some tutorials about how connect to the properties view using JFace viewers as the selection provider, but nothing related to NatTable.
The ISelectionProvider interface specifies a getSelection() and a setSelection() method. The selection in NatTable is implemented via the SelectionLayer. While it should be quite easy to implement getSelection() based on the SelectionLayer it could become quite difficult to implement setSelection() in a general way. Since you are working with a model based approach it is maybe possible for you to get the cell coordinates for the element that is sent via an ISelection to correctly implement setSelection(), but typically this is not possible as the same value in a column can be set for multiple rows.
Maybe you also don't need setSelection() and you can implement it empty as you only want to provide a selection to the properties view. But that also depends on your use case and what you want to achieve in whole.

Best practice for MVC 4 Edit Views and "Hidden" parameters

I have recently added some fields for auditing purposes to existing models in an MVC 4/ Entity project. I don't need these fields to be displayed on the edit page. However, they are required fields on the model.
As it stands, the edit page still works, but on the controller side, the ModelState.IsValid check fails because the required fields that are actually set on the item are not output to the view and therefore not re-submitted when the edit page is submitted.
Is there an easy, built in way to rectify this, or if not, which of the following is best practice for this scenario? Are there more options?
1) Set up hidden fields on the view to hold the information (Not a fan of this option, passes data around too much)
2) in the controller, on submit, first load the model by ID, then set each individual parameter based on the fields present on the view (Seems like extra unnecessary work)
3) Create a constructor for the model that takes itself as a parameter and pulls any non-default values and returns a new object. Basically a merge. (Best I think, still a lot of extra work)
4) ???
Best practice is to not use your domain model inside the views. Create a view model class that contains only the id and the fields you want in the view. Pass this model to your view. Change the parameter type of the form submit action to match your new view model. This will then pass the model validation without using hidden fields. In your action method, you can then retrieve the object from the database using the id property of the view model class and update fields as required.
Hope that makes sense.
I prefer to do the 2nd option as long as I can get the existing object with a single query or db call. This lets me to keep my view clean(no hidden fields for all those other properties) and use the existing update method which updates the domain model.
Look into your code. If the update method is making updates in lot of other places(many other tables) which is really not needed, then you could possibly write a short version of the update method which updates only the relevant parts ( ex: UpdateContactDetails).

Core Data object graph design decision

I am designing an app which tracks data on Game objects. Each Game has a name, a date and other attributes. The problem I am having arises because I want the user to be able to add more names (for example) to pick from in the application. (in this case from a UITableView). So the user is presented with a list of names to choose from, and if the one they want is not in the list, they can add one to the list.
My solution is that I currently have a second entity called GameName so that I can show the user a list of those game names to pick from when they are adding a new Game. I just call an NSFetchRequest on all the GameName objects and display them in the UITableView. There doesn't have to be a Game object created yet to do this.
My dilemma is that I want to know if this is a good practice. It seems that if I do it this way, I will end up having a lot of entities with just one attribute, for the sake of allowing the user to pick from and add to a customizable list.
I hope this makes sense. I can clarify anything upon request.
Your approach is fine, and is commonly used in database design. The entity you want to add is called a "domain table" in databases. See this page, in particular this paragraph:
In a normalized data model, the reference domain is typically specified in a reference table. Following the previous example, a Gender reference table would have exactly two records, one per allowed value—excluding NULL. Reference tables are formally related to other tables in a database by the use of foreign keys.
Of course, you probably want to have an optional relationship between the GameName and Game entities.

How do I store arbitrary data in a GWT widget?

I'm creating a table using GWT, and in certain cells of the table, I want to store a piece of data in that cell's Widget for use in the Widget's handler callback. I can't seem to find an API method such as widget.setData(key, value);. How do I (or can I) accomplish this?
You could subclass the widget you want - and add the appropriate members and get/set methods.
The table example may be better served by storing the object inside a button instance, i.e an 'Edit' button.
Alternatively, you could maintain a map of objects that relate to each row index. This would save creating a lot of unnecessary widgets.